blob: 459cdaa0d1cd8ee0516bf5d264bde290461a54fe [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
Uros Bizjak44c2d662018-10-11 19:40:45 +020064 ____kvm_handle_fault_on_reboot(x, "xorl " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Sean Christopherson52017602018-09-26 09:23:57 -0700113static bool __read_mostly nested_early_check = 0;
114module_param(nested_early_check, bool, S_IRUGO);
115
Wanpeng Li20300092014-12-02 19:14:59 +0800116static u64 __read_mostly host_xss;
117
Kai Huang843e4332015-01-28 10:54:28 +0800118static bool __read_mostly enable_pml = 1;
119module_param_named(pml, enable_pml, bool, S_IRUGO);
120
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100121#define MSR_TYPE_R 1
122#define MSR_TYPE_W 2
123#define MSR_TYPE_RW 3
124
125#define MSR_BITMAP_MODE_X2APIC 1
126#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100127
Haozhong Zhang64903d62015-10-20 15:39:09 +0800128#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
129
Yunhong Jiang64672c92016-06-13 14:19:59 -0700130/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
131static int __read_mostly cpu_preemption_timer_multi;
132static bool __read_mostly enable_preemption_timer = 1;
133#ifdef CONFIG_X86_64
134module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
135#endif
136
Sean Christopherson3de63472018-07-13 08:42:30 -0700137#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800138#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
139#define KVM_VM_CR0_ALWAYS_ON \
140 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
141 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200142#define KVM_CR4_GUEST_OWNED_BITS \
143 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800144 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200145
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800146#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200147#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
148#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
149
Avi Kivity78ac8b42010-04-08 18:19:35 +0300150#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
151
Jan Kiszkaf4124502014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200176
Babu Moger7fbc85a2018-03-16 16:37:22 -0400177static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
178module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800179
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400181static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400182module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200183
184/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400185static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200187
188/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400189static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
190module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200191
Avi Kivity83287ea422012-09-16 15:10:57 +0300192extern const ulong vmx_return;
Sean Christopherson52017602018-09-26 09:23:57 -0700193extern const ulong vmx_early_consistency_check_return;
Avi Kivity83287ea422012-09-16 15:10:57 +0300194
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200195static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200196static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200197static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200199/* Storage for pre module init parameter parsing */
200static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201
202static const struct {
203 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200204 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200205} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200206 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
207 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
208 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
209 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
210 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
211 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200212};
213
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200214#define L1D_CACHE_ORDER 4
215static void *vmx_l1d_flush_pages;
216
217static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
218{
219 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200220 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200221
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200222 if (!enable_ept) {
223 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
224 return 0;
225 }
226
Yi Wangd806afa2018-08-16 13:42:39 +0800227 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
228 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200229
Yi Wangd806afa2018-08-16 13:42:39 +0800230 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
231 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
232 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
233 return 0;
234 }
235 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200236
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200237 /* If set to auto use the default l1tf mitigation method */
238 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
239 switch (l1tf_mitigation) {
240 case L1TF_MITIGATION_OFF:
241 l1tf = VMENTER_L1D_FLUSH_NEVER;
242 break;
243 case L1TF_MITIGATION_FLUSH_NOWARN:
244 case L1TF_MITIGATION_FLUSH:
245 case L1TF_MITIGATION_FLUSH_NOSMT:
246 l1tf = VMENTER_L1D_FLUSH_COND;
247 break;
248 case L1TF_MITIGATION_FULL:
249 case L1TF_MITIGATION_FULL_FORCE:
250 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
251 break;
252 }
253 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
254 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
255 }
256
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200257 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
258 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
259 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
260 if (!page)
261 return -ENOMEM;
262 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200263
264 /*
265 * Initialize each page with a different pattern in
266 * order to protect against KSM in the nested
267 * virtualization case.
268 */
269 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
270 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
271 PAGE_SIZE);
272 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200273 }
274
275 l1tf_vmx_mitigation = l1tf;
276
Thomas Gleixner895ae472018-07-13 16:23:22 +0200277 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
278 static_branch_enable(&vmx_l1d_should_flush);
279 else
280 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200281
Nicolai Stange427362a2018-07-21 22:25:00 +0200282 if (l1tf == VMENTER_L1D_FLUSH_COND)
283 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200284 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200285 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200286 return 0;
287}
288
289static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200290{
291 unsigned int i;
292
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200293 if (s) {
294 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200295 if (vmentry_l1d_param[i].for_parse &&
296 sysfs_streq(s, vmentry_l1d_param[i].option))
297 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200298 }
299 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200300 return -EINVAL;
301}
302
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
304{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200305 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200306
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200307 l1tf = vmentry_l1d_flush_parse(s);
308 if (l1tf < 0)
309 return l1tf;
310
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200311 if (!boot_cpu_has(X86_BUG_L1TF))
312 return 0;
313
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200314 /*
315 * Has vmx_init() run already? If not then this is the pre init
316 * parameter parsing. In that case just store the value and let
317 * vmx_init() do the proper setup after enable_ept has been
318 * established.
319 */
320 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
321 vmentry_l1d_flush_param = l1tf;
322 return 0;
323 }
324
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200325 mutex_lock(&vmx_l1d_flush_mutex);
326 ret = vmx_setup_l1d_flush(l1tf);
327 mutex_unlock(&vmx_l1d_flush_mutex);
328 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200329}
330
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200331static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
332{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200333 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
334 return sprintf(s, "???\n");
335
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200336 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200337}
338
339static const struct kernel_param_ops vmentry_l1d_flush_ops = {
340 .set = vmentry_l1d_flush_set,
341 .get = vmentry_l1d_flush_get,
342};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200343module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200344
Tianyu Lan877ad952018-07-19 08:40:23 +0000345enum ept_pointers_status {
346 EPT_POINTERS_CHECK = 0,
347 EPT_POINTERS_MATCH = 1,
348 EPT_POINTERS_MISMATCH = 2
349};
350
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700351struct kvm_vmx {
352 struct kvm kvm;
353
354 unsigned int tss_addr;
355 bool ept_identity_pagetable_done;
356 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000357
358 enum ept_pointers_status ept_pointers_match;
359 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700360};
361
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200362#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300363
Liran Alon392b2f22018-06-23 02:35:01 +0300364struct vmcs_hdr {
365 u32 revision_id:31;
366 u32 shadow_vmcs:1;
367};
368
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400369struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300370 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400371 u32 abort;
372 char data[0];
373};
374
Nadav Har'Eld462b812011-05-24 15:26:10 +0300375/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700376 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
377 * and whose values change infrequently, but are not constant. I.e. this is
378 * used as a write-through cache of the corresponding VMCS fields.
379 */
380struct vmcs_host_state {
381 unsigned long cr3; /* May not match real cr3 */
382 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700383 unsigned long gs_base;
384 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700385
386 u16 fs_sel, gs_sel, ldt_sel;
387#ifdef CONFIG_X86_64
388 u16 ds_sel, es_sel;
389#endif
390};
391
392/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300393 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
394 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
395 * loaded on this CPU (so we can clear them if the CPU goes down).
396 */
397struct loaded_vmcs {
398 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700399 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300400 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200401 bool launched;
402 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700403 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100404 /* Support for vnmi-less CPUs */
405 int soft_vnmi_blocked;
406 ktime_t entry_time;
407 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100408 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300409 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700410 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300411};
412
Avi Kivity26bb0982009-09-07 11:14:12 +0300413struct shared_msr_entry {
414 unsigned index;
415 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200416 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300417};
418
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300419/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300420 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
421 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
422 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
423 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
424 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
425 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600426 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300427 * underlying hardware which will be used to run L2.
428 * This structure is packed to ensure that its layout is identical across
429 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700430 *
431 * IMPORTANT: Changing the layout of existing fields in this structure
432 * will break save/restore compatibility with older kvm releases. When
433 * adding new fields, either use space in the reserved padding* arrays
434 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300435 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300436typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300437struct __packed vmcs12 {
438 /* According to the Intel spec, a VMCS region must start with the
439 * following two fields. Then follow implementation-specific data.
440 */
Liran Alon392b2f22018-06-23 02:35:01 +0300441 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300442 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300443
Nadav Har'El27d6c862011-05-25 23:06:59 +0300444 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
445 u32 padding[7]; /* room for future expansion */
446
Nadav Har'El22bd0352011-05-25 23:05:57 +0300447 u64 io_bitmap_a;
448 u64 io_bitmap_b;
449 u64 msr_bitmap;
450 u64 vm_exit_msr_store_addr;
451 u64 vm_exit_msr_load_addr;
452 u64 vm_entry_msr_load_addr;
453 u64 tsc_offset;
454 u64 virtual_apic_page_addr;
455 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800456 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300457 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800458 u64 eoi_exit_bitmap0;
459 u64 eoi_exit_bitmap1;
460 u64 eoi_exit_bitmap2;
461 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800462 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300463 u64 guest_physical_address;
464 u64 vmcs_link_pointer;
465 u64 guest_ia32_debugctl;
466 u64 guest_ia32_pat;
467 u64 guest_ia32_efer;
468 u64 guest_ia32_perf_global_ctrl;
469 u64 guest_pdptr0;
470 u64 guest_pdptr1;
471 u64 guest_pdptr2;
472 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100473 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300474 u64 host_ia32_pat;
475 u64 host_ia32_efer;
476 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700477 u64 vmread_bitmap;
478 u64 vmwrite_bitmap;
479 u64 vm_function_control;
480 u64 eptp_list_address;
481 u64 pml_address;
482 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300483 /*
484 * To allow migration of L1 (complete with its L2 guests) between
485 * machines of different natural widths (32 or 64 bit), we cannot have
486 * unsigned long fields with no explict size. We use u64 (aliased
487 * natural_width) instead. Luckily, x86 is little-endian.
488 */
489 natural_width cr0_guest_host_mask;
490 natural_width cr4_guest_host_mask;
491 natural_width cr0_read_shadow;
492 natural_width cr4_read_shadow;
493 natural_width cr3_target_value0;
494 natural_width cr3_target_value1;
495 natural_width cr3_target_value2;
496 natural_width cr3_target_value3;
497 natural_width exit_qualification;
498 natural_width guest_linear_address;
499 natural_width guest_cr0;
500 natural_width guest_cr3;
501 natural_width guest_cr4;
502 natural_width guest_es_base;
503 natural_width guest_cs_base;
504 natural_width guest_ss_base;
505 natural_width guest_ds_base;
506 natural_width guest_fs_base;
507 natural_width guest_gs_base;
508 natural_width guest_ldtr_base;
509 natural_width guest_tr_base;
510 natural_width guest_gdtr_base;
511 natural_width guest_idtr_base;
512 natural_width guest_dr7;
513 natural_width guest_rsp;
514 natural_width guest_rip;
515 natural_width guest_rflags;
516 natural_width guest_pending_dbg_exceptions;
517 natural_width guest_sysenter_esp;
518 natural_width guest_sysenter_eip;
519 natural_width host_cr0;
520 natural_width host_cr3;
521 natural_width host_cr4;
522 natural_width host_fs_base;
523 natural_width host_gs_base;
524 natural_width host_tr_base;
525 natural_width host_gdtr_base;
526 natural_width host_idtr_base;
527 natural_width host_ia32_sysenter_esp;
528 natural_width host_ia32_sysenter_eip;
529 natural_width host_rsp;
530 natural_width host_rip;
531 natural_width paddingl[8]; /* room for future expansion */
532 u32 pin_based_vm_exec_control;
533 u32 cpu_based_vm_exec_control;
534 u32 exception_bitmap;
535 u32 page_fault_error_code_mask;
536 u32 page_fault_error_code_match;
537 u32 cr3_target_count;
538 u32 vm_exit_controls;
539 u32 vm_exit_msr_store_count;
540 u32 vm_exit_msr_load_count;
541 u32 vm_entry_controls;
542 u32 vm_entry_msr_load_count;
543 u32 vm_entry_intr_info_field;
544 u32 vm_entry_exception_error_code;
545 u32 vm_entry_instruction_len;
546 u32 tpr_threshold;
547 u32 secondary_vm_exec_control;
548 u32 vm_instruction_error;
549 u32 vm_exit_reason;
550 u32 vm_exit_intr_info;
551 u32 vm_exit_intr_error_code;
552 u32 idt_vectoring_info_field;
553 u32 idt_vectoring_error_code;
554 u32 vm_exit_instruction_len;
555 u32 vmx_instruction_info;
556 u32 guest_es_limit;
557 u32 guest_cs_limit;
558 u32 guest_ss_limit;
559 u32 guest_ds_limit;
560 u32 guest_fs_limit;
561 u32 guest_gs_limit;
562 u32 guest_ldtr_limit;
563 u32 guest_tr_limit;
564 u32 guest_gdtr_limit;
565 u32 guest_idtr_limit;
566 u32 guest_es_ar_bytes;
567 u32 guest_cs_ar_bytes;
568 u32 guest_ss_ar_bytes;
569 u32 guest_ds_ar_bytes;
570 u32 guest_fs_ar_bytes;
571 u32 guest_gs_ar_bytes;
572 u32 guest_ldtr_ar_bytes;
573 u32 guest_tr_ar_bytes;
574 u32 guest_interruptibility_info;
575 u32 guest_activity_state;
576 u32 guest_sysenter_cs;
577 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100578 u32 vmx_preemption_timer_value;
579 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300580 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800581 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300582 u16 guest_es_selector;
583 u16 guest_cs_selector;
584 u16 guest_ss_selector;
585 u16 guest_ds_selector;
586 u16 guest_fs_selector;
587 u16 guest_gs_selector;
588 u16 guest_ldtr_selector;
589 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800590 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300591 u16 host_es_selector;
592 u16 host_cs_selector;
593 u16 host_ss_selector;
594 u16 host_ds_selector;
595 u16 host_fs_selector;
596 u16 host_gs_selector;
597 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700598 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300599};
600
601/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700602 * For save/restore compatibility, the vmcs12 field offsets must not change.
603 */
604#define CHECK_OFFSET(field, loc) \
605 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
606 "Offset of " #field " in struct vmcs12 has changed.")
607
608static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300609 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700610 CHECK_OFFSET(abort, 4);
611 CHECK_OFFSET(launch_state, 8);
612 CHECK_OFFSET(io_bitmap_a, 40);
613 CHECK_OFFSET(io_bitmap_b, 48);
614 CHECK_OFFSET(msr_bitmap, 56);
615 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
616 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
617 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
618 CHECK_OFFSET(tsc_offset, 88);
619 CHECK_OFFSET(virtual_apic_page_addr, 96);
620 CHECK_OFFSET(apic_access_addr, 104);
621 CHECK_OFFSET(posted_intr_desc_addr, 112);
622 CHECK_OFFSET(ept_pointer, 120);
623 CHECK_OFFSET(eoi_exit_bitmap0, 128);
624 CHECK_OFFSET(eoi_exit_bitmap1, 136);
625 CHECK_OFFSET(eoi_exit_bitmap2, 144);
626 CHECK_OFFSET(eoi_exit_bitmap3, 152);
627 CHECK_OFFSET(xss_exit_bitmap, 160);
628 CHECK_OFFSET(guest_physical_address, 168);
629 CHECK_OFFSET(vmcs_link_pointer, 176);
630 CHECK_OFFSET(guest_ia32_debugctl, 184);
631 CHECK_OFFSET(guest_ia32_pat, 192);
632 CHECK_OFFSET(guest_ia32_efer, 200);
633 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
634 CHECK_OFFSET(guest_pdptr0, 216);
635 CHECK_OFFSET(guest_pdptr1, 224);
636 CHECK_OFFSET(guest_pdptr2, 232);
637 CHECK_OFFSET(guest_pdptr3, 240);
638 CHECK_OFFSET(guest_bndcfgs, 248);
639 CHECK_OFFSET(host_ia32_pat, 256);
640 CHECK_OFFSET(host_ia32_efer, 264);
641 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
642 CHECK_OFFSET(vmread_bitmap, 280);
643 CHECK_OFFSET(vmwrite_bitmap, 288);
644 CHECK_OFFSET(vm_function_control, 296);
645 CHECK_OFFSET(eptp_list_address, 304);
646 CHECK_OFFSET(pml_address, 312);
647 CHECK_OFFSET(cr0_guest_host_mask, 344);
648 CHECK_OFFSET(cr4_guest_host_mask, 352);
649 CHECK_OFFSET(cr0_read_shadow, 360);
650 CHECK_OFFSET(cr4_read_shadow, 368);
651 CHECK_OFFSET(cr3_target_value0, 376);
652 CHECK_OFFSET(cr3_target_value1, 384);
653 CHECK_OFFSET(cr3_target_value2, 392);
654 CHECK_OFFSET(cr3_target_value3, 400);
655 CHECK_OFFSET(exit_qualification, 408);
656 CHECK_OFFSET(guest_linear_address, 416);
657 CHECK_OFFSET(guest_cr0, 424);
658 CHECK_OFFSET(guest_cr3, 432);
659 CHECK_OFFSET(guest_cr4, 440);
660 CHECK_OFFSET(guest_es_base, 448);
661 CHECK_OFFSET(guest_cs_base, 456);
662 CHECK_OFFSET(guest_ss_base, 464);
663 CHECK_OFFSET(guest_ds_base, 472);
664 CHECK_OFFSET(guest_fs_base, 480);
665 CHECK_OFFSET(guest_gs_base, 488);
666 CHECK_OFFSET(guest_ldtr_base, 496);
667 CHECK_OFFSET(guest_tr_base, 504);
668 CHECK_OFFSET(guest_gdtr_base, 512);
669 CHECK_OFFSET(guest_idtr_base, 520);
670 CHECK_OFFSET(guest_dr7, 528);
671 CHECK_OFFSET(guest_rsp, 536);
672 CHECK_OFFSET(guest_rip, 544);
673 CHECK_OFFSET(guest_rflags, 552);
674 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
675 CHECK_OFFSET(guest_sysenter_esp, 568);
676 CHECK_OFFSET(guest_sysenter_eip, 576);
677 CHECK_OFFSET(host_cr0, 584);
678 CHECK_OFFSET(host_cr3, 592);
679 CHECK_OFFSET(host_cr4, 600);
680 CHECK_OFFSET(host_fs_base, 608);
681 CHECK_OFFSET(host_gs_base, 616);
682 CHECK_OFFSET(host_tr_base, 624);
683 CHECK_OFFSET(host_gdtr_base, 632);
684 CHECK_OFFSET(host_idtr_base, 640);
685 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
686 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
687 CHECK_OFFSET(host_rsp, 664);
688 CHECK_OFFSET(host_rip, 672);
689 CHECK_OFFSET(pin_based_vm_exec_control, 744);
690 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
691 CHECK_OFFSET(exception_bitmap, 752);
692 CHECK_OFFSET(page_fault_error_code_mask, 756);
693 CHECK_OFFSET(page_fault_error_code_match, 760);
694 CHECK_OFFSET(cr3_target_count, 764);
695 CHECK_OFFSET(vm_exit_controls, 768);
696 CHECK_OFFSET(vm_exit_msr_store_count, 772);
697 CHECK_OFFSET(vm_exit_msr_load_count, 776);
698 CHECK_OFFSET(vm_entry_controls, 780);
699 CHECK_OFFSET(vm_entry_msr_load_count, 784);
700 CHECK_OFFSET(vm_entry_intr_info_field, 788);
701 CHECK_OFFSET(vm_entry_exception_error_code, 792);
702 CHECK_OFFSET(vm_entry_instruction_len, 796);
703 CHECK_OFFSET(tpr_threshold, 800);
704 CHECK_OFFSET(secondary_vm_exec_control, 804);
705 CHECK_OFFSET(vm_instruction_error, 808);
706 CHECK_OFFSET(vm_exit_reason, 812);
707 CHECK_OFFSET(vm_exit_intr_info, 816);
708 CHECK_OFFSET(vm_exit_intr_error_code, 820);
709 CHECK_OFFSET(idt_vectoring_info_field, 824);
710 CHECK_OFFSET(idt_vectoring_error_code, 828);
711 CHECK_OFFSET(vm_exit_instruction_len, 832);
712 CHECK_OFFSET(vmx_instruction_info, 836);
713 CHECK_OFFSET(guest_es_limit, 840);
714 CHECK_OFFSET(guest_cs_limit, 844);
715 CHECK_OFFSET(guest_ss_limit, 848);
716 CHECK_OFFSET(guest_ds_limit, 852);
717 CHECK_OFFSET(guest_fs_limit, 856);
718 CHECK_OFFSET(guest_gs_limit, 860);
719 CHECK_OFFSET(guest_ldtr_limit, 864);
720 CHECK_OFFSET(guest_tr_limit, 868);
721 CHECK_OFFSET(guest_gdtr_limit, 872);
722 CHECK_OFFSET(guest_idtr_limit, 876);
723 CHECK_OFFSET(guest_es_ar_bytes, 880);
724 CHECK_OFFSET(guest_cs_ar_bytes, 884);
725 CHECK_OFFSET(guest_ss_ar_bytes, 888);
726 CHECK_OFFSET(guest_ds_ar_bytes, 892);
727 CHECK_OFFSET(guest_fs_ar_bytes, 896);
728 CHECK_OFFSET(guest_gs_ar_bytes, 900);
729 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
730 CHECK_OFFSET(guest_tr_ar_bytes, 908);
731 CHECK_OFFSET(guest_interruptibility_info, 912);
732 CHECK_OFFSET(guest_activity_state, 916);
733 CHECK_OFFSET(guest_sysenter_cs, 920);
734 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
735 CHECK_OFFSET(vmx_preemption_timer_value, 928);
736 CHECK_OFFSET(virtual_processor_id, 960);
737 CHECK_OFFSET(posted_intr_nv, 962);
738 CHECK_OFFSET(guest_es_selector, 964);
739 CHECK_OFFSET(guest_cs_selector, 966);
740 CHECK_OFFSET(guest_ss_selector, 968);
741 CHECK_OFFSET(guest_ds_selector, 970);
742 CHECK_OFFSET(guest_fs_selector, 972);
743 CHECK_OFFSET(guest_gs_selector, 974);
744 CHECK_OFFSET(guest_ldtr_selector, 976);
745 CHECK_OFFSET(guest_tr_selector, 978);
746 CHECK_OFFSET(guest_intr_status, 980);
747 CHECK_OFFSET(host_es_selector, 982);
748 CHECK_OFFSET(host_cs_selector, 984);
749 CHECK_OFFSET(host_ss_selector, 986);
750 CHECK_OFFSET(host_ds_selector, 988);
751 CHECK_OFFSET(host_fs_selector, 990);
752 CHECK_OFFSET(host_gs_selector, 992);
753 CHECK_OFFSET(host_tr_selector, 994);
754 CHECK_OFFSET(guest_pml_index, 996);
755}
756
757/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300758 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
759 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
760 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700761 *
762 * IMPORTANT: Changing this value will break save/restore compatibility with
763 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300764 */
765#define VMCS12_REVISION 0x11e57ed0
766
767/*
768 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
769 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
770 * current implementation, 4K are reserved to avoid future complications.
771 */
772#define VMCS12_SIZE 0x1000
773
774/*
Jim Mattson5b157062017-12-22 12:11:12 -0800775 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
776 * supported VMCS12 field encoding.
777 */
778#define VMCS12_MAX_FIELD_INDEX 0x17
779
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100780struct nested_vmx_msrs {
781 /*
782 * We only store the "true" versions of the VMX capability MSRs. We
783 * generate the "non-true" versions by setting the must-be-1 bits
784 * according to the SDM.
785 */
786 u32 procbased_ctls_low;
787 u32 procbased_ctls_high;
788 u32 secondary_ctls_low;
789 u32 secondary_ctls_high;
790 u32 pinbased_ctls_low;
791 u32 pinbased_ctls_high;
792 u32 exit_ctls_low;
793 u32 exit_ctls_high;
794 u32 entry_ctls_low;
795 u32 entry_ctls_high;
796 u32 misc_low;
797 u32 misc_high;
798 u32 ept_caps;
799 u32 vpid_caps;
800 u64 basic;
801 u64 cr0_fixed0;
802 u64 cr0_fixed1;
803 u64 cr4_fixed0;
804 u64 cr4_fixed1;
805 u64 vmcs_enum;
806 u64 vmfunc_controls;
807};
808
Jim Mattson5b157062017-12-22 12:11:12 -0800809/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300810 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
811 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
812 */
813struct nested_vmx {
814 /* Has the level1 guest done vmxon? */
815 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400816 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400817 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300818
819 /* The guest-physical address of the current VMCS L1 keeps for L2 */
820 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700821 /*
822 * Cache of the guest's VMCS, existing outside of guest memory.
823 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700824 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700825 */
826 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300827 /*
Liran Alon61ada742018-06-23 02:35:08 +0300828 * Cache of the guest's shadow VMCS, existing outside of guest
829 * memory. Loaded from guest memory during VM entry. Flushed
830 * to guest memory during VM exit.
831 */
832 struct vmcs12 *cached_shadow_vmcs12;
833 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300834 * Indicates if the shadow vmcs must be updated with the
835 * data hold by vmcs12
836 */
837 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100838 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300839
Sean Christopherson9d6105b22018-09-26 09:23:51 -0700840 /*
841 * vmcs02 has been initialized, i.e. state that is constant for
842 * vmcs02 has been written to the backing VMCS. Initialization
843 * is delayed until L1 actually attempts to run a nested VM.
844 */
845 bool vmcs02_initialized;
846
Jim Mattson8d860bb2018-05-09 16:56:05 -0400847 bool change_vmcs01_virtual_apic_mode;
848
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +0200849 /*
850 * Enlightened VMCS has been enabled. It does not mean that L1 has to
851 * use it. However, VMX features available to L1 will be limited based
852 * on what the enlightened VMCS supports.
853 */
854 bool enlightened_vmcs_enabled;
855
Nadav Har'El644d7112011-05-25 23:12:35 +0300856 /* L2 must run next, and mustn't decide to exit to L1. */
857 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600858
859 struct loaded_vmcs vmcs02;
860
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300861 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600862 * Guest pages referred to in the vmcs02 with host-physical
863 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300864 */
865 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800866 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800867 struct page *pi_desc_page;
868 struct pi_desc *pi_desc;
869 bool pi_pending;
870 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100871
872 struct hrtimer preemption_timer;
873 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200874
875 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
876 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300877 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800878
Wanpeng Li5c614b32015-10-13 09:18:36 -0700879 u16 vpid02;
880 u16 last_vpid;
881
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100882 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200883
884 /* SMM related state */
885 struct {
886 /* in VMX operation on SMM entry? */
887 bool vmxon;
888 /* in guest mode on SMM entry? */
889 bool guest_mode;
890 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300891};
892
Yang Zhang01e439b2013-04-11 19:25:12 +0800893#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800894#define POSTED_INTR_SN 1
895
Yang Zhang01e439b2013-04-11 19:25:12 +0800896/* Posted-Interrupt Descriptor */
897struct pi_desc {
898 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800899 union {
900 struct {
901 /* bit 256 - Outstanding Notification */
902 u16 on : 1,
903 /* bit 257 - Suppress Notification */
904 sn : 1,
905 /* bit 271:258 - Reserved */
906 rsvd_1 : 14;
907 /* bit 279:272 - Notification Vector */
908 u8 nv;
909 /* bit 287:280 - Reserved */
910 u8 rsvd_2;
911 /* bit 319:288 - Notification Destination */
912 u32 ndst;
913 };
914 u64 control;
915 };
916 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800917} __aligned(64);
918
Yang Zhanga20ed542013-04-11 19:25:15 +0800919static bool pi_test_and_set_on(struct pi_desc *pi_desc)
920{
921 return test_and_set_bit(POSTED_INTR_ON,
922 (unsigned long *)&pi_desc->control);
923}
924
925static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
926{
927 return test_and_clear_bit(POSTED_INTR_ON,
928 (unsigned long *)&pi_desc->control);
929}
930
931static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
932{
933 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
934}
935
Feng Wuebbfc762015-09-18 22:29:46 +0800936static inline void pi_clear_sn(struct pi_desc *pi_desc)
937{
938 return clear_bit(POSTED_INTR_SN,
939 (unsigned long *)&pi_desc->control);
940}
941
942static inline void pi_set_sn(struct pi_desc *pi_desc)
943{
944 return set_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
Paolo Bonziniad361092016-09-20 16:15:05 +0200948static inline void pi_clear_on(struct pi_desc *pi_desc)
949{
950 clear_bit(POSTED_INTR_ON,
951 (unsigned long *)&pi_desc->control);
952}
953
Feng Wuebbfc762015-09-18 22:29:46 +0800954static inline int pi_test_on(struct pi_desc *pi_desc)
955{
956 return test_bit(POSTED_INTR_ON,
957 (unsigned long *)&pi_desc->control);
958}
959
960static inline int pi_test_sn(struct pi_desc *pi_desc)
961{
962 return test_bit(POSTED_INTR_SN,
963 (unsigned long *)&pi_desc->control);
964}
965
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400966struct vmx_msrs {
967 unsigned int nr;
968 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
969};
970
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400971struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000972 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300973 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300974 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100975 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300976 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200977 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200978 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300979 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400980 int nmsrs;
981 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800982 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400983#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300984 u64 msr_host_kernel_gs_base;
985 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100987
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100988 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100989 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100990
Gleb Natapov2961e8762013-11-25 15:37:13 +0200991 u32 vm_entry_controls_shadow;
992 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200993 u32 secondary_exec_control;
994
Nadav Har'Eld462b812011-05-24 15:26:10 +0300995 /*
996 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
997 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700998 * guest (L2), it points to a different VMCS. loaded_cpu_state points
999 * to the VMCS whose state is loaded into the CPU registers that only
1000 * need to be switched when transitioning to/from the kernel; a NULL
1001 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +03001002 */
1003 struct loaded_vmcs vmcs01;
1004 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001005 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +03001006 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001007 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001008 struct vmx_msrs guest;
1009 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001010 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001011
Avi Kivity9c8cba32007-11-22 11:42:59 +02001012 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001013 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001014 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001015 struct kvm_segment segs[8];
1016 } rmode;
1017 struct {
1018 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001019 struct kvm_save_segment {
1020 u16 selector;
1021 unsigned long base;
1022 u32 limit;
1023 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001024 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001025 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001026 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001027 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001028
Andi Kleena0861c02009-06-08 17:37:09 +08001029 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001030
Yang Zhang01e439b2013-04-11 19:25:12 +08001031 /* Posted interrupt descriptor */
1032 struct pi_desc pi_desc;
1033
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001034 /* Support for a guest hypervisor (nested VMX) */
1035 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001036
1037 /* Dynamic PLE window. */
1038 int ple_window;
1039 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001040
Sean Christophersond264ee02018-08-27 15:21:12 -07001041 bool req_immediate_exit;
1042
Kai Huang843e4332015-01-28 10:54:28 +08001043 /* Support for PML */
1044#define PML_ENTITY_NUM 512
1045 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001046
Yunhong Jiang64672c92016-06-13 14:19:59 -07001047 /* apic deadline value in host tsc */
1048 u64 hv_deadline_tsc;
1049
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001050 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001051
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001052 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001053
Wanpeng Li74c55932017-11-29 01:31:20 -08001054 unsigned long host_debugctlmsr;
1055
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001056 /*
1057 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1058 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1059 * in msr_ia32_feature_control_valid_bits.
1060 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001061 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001062 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001063 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001064};
1065
Avi Kivity2fb92db2011-04-27 19:42:18 +03001066enum segment_cache_field {
1067 SEG_FIELD_SEL = 0,
1068 SEG_FIELD_BASE = 1,
1069 SEG_FIELD_LIMIT = 2,
1070 SEG_FIELD_AR = 3,
1071
1072 SEG_FIELD_NR = 4
1073};
1074
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001075static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1076{
1077 return container_of(kvm, struct kvm_vmx, kvm);
1078}
1079
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001080static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1081{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001082 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001083}
1084
Feng Wuefc64402015-09-18 22:29:51 +08001085static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1086{
1087 return &(to_vmx(vcpu)->pi_desc);
1088}
1089
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001090#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001091#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001092#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1093#define FIELD64(number, name) \
1094 FIELD(number, name), \
1095 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001096
Abel Gordon4607c2d2013-04-18 14:35:55 +03001097
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001098static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001099#define SHADOW_FIELD_RO(x) x,
1100#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001101};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001102static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001103 ARRAY_SIZE(shadow_read_only_fields);
1104
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001105static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001106#define SHADOW_FIELD_RW(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_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001110 ARRAY_SIZE(shadow_read_write_fields);
1111
Mathias Krause772e0312012-08-30 01:30:19 +02001112static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001113 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001114 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001115 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1116 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1117 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1118 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1119 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1120 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1121 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1122 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001123 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001124 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001125 FIELD(HOST_ES_SELECTOR, host_es_selector),
1126 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1127 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1128 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1129 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1130 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1131 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1132 FIELD64(IO_BITMAP_A, io_bitmap_a),
1133 FIELD64(IO_BITMAP_B, io_bitmap_b),
1134 FIELD64(MSR_BITMAP, msr_bitmap),
1135 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1136 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1137 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001138 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001139 FIELD64(TSC_OFFSET, tsc_offset),
1140 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1141 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001142 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001143 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001144 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001145 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1146 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1147 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1148 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001149 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001150 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1151 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001152 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001153 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1154 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1155 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1156 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1157 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1158 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1159 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1160 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1161 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1162 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001163 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001164 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1165 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1166 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1167 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1168 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1169 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1170 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1171 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1172 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1173 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1174 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1175 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1176 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1177 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1178 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1179 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1180 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1181 FIELD(TPR_THRESHOLD, tpr_threshold),
1182 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1183 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1184 FIELD(VM_EXIT_REASON, vm_exit_reason),
1185 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1186 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1187 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1188 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1189 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1190 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1191 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1192 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1193 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1194 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1195 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1196 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1197 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1198 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1199 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1200 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1201 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1202 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1203 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1204 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1205 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1206 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1207 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1208 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1209 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1210 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1211 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1212 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001213 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001214 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1215 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1216 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1217 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1218 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1219 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1220 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1221 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1222 FIELD(EXIT_QUALIFICATION, exit_qualification),
1223 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1224 FIELD(GUEST_CR0, guest_cr0),
1225 FIELD(GUEST_CR3, guest_cr3),
1226 FIELD(GUEST_CR4, guest_cr4),
1227 FIELD(GUEST_ES_BASE, guest_es_base),
1228 FIELD(GUEST_CS_BASE, guest_cs_base),
1229 FIELD(GUEST_SS_BASE, guest_ss_base),
1230 FIELD(GUEST_DS_BASE, guest_ds_base),
1231 FIELD(GUEST_FS_BASE, guest_fs_base),
1232 FIELD(GUEST_GS_BASE, guest_gs_base),
1233 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1234 FIELD(GUEST_TR_BASE, guest_tr_base),
1235 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1236 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1237 FIELD(GUEST_DR7, guest_dr7),
1238 FIELD(GUEST_RSP, guest_rsp),
1239 FIELD(GUEST_RIP, guest_rip),
1240 FIELD(GUEST_RFLAGS, guest_rflags),
1241 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1242 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1243 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1244 FIELD(HOST_CR0, host_cr0),
1245 FIELD(HOST_CR3, host_cr3),
1246 FIELD(HOST_CR4, host_cr4),
1247 FIELD(HOST_FS_BASE, host_fs_base),
1248 FIELD(HOST_GS_BASE, host_gs_base),
1249 FIELD(HOST_TR_BASE, host_tr_base),
1250 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1251 FIELD(HOST_IDTR_BASE, host_idtr_base),
1252 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1253 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1254 FIELD(HOST_RSP, host_rsp),
1255 FIELD(HOST_RIP, host_rip),
1256};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001257
1258static inline short vmcs_field_to_offset(unsigned long field)
1259{
Dan Williams085331d2018-01-31 17:47:03 -08001260 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1261 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001262 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001263
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001264 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001265 return -ENOENT;
1266
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001267 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001268 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001269 return -ENOENT;
1270
Linus Torvalds15303ba2018-02-10 13:16:35 -08001271 index = array_index_nospec(index, size);
1272 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001273 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001274 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001275 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001276}
1277
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001278static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1279{
David Matlack4f2777b2016-07-13 17:16:37 -07001280 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001281}
1282
Liran Alon61ada742018-06-23 02:35:08 +03001283static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1284{
1285 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1286}
1287
Peter Feiner995f00a2017-06-30 17:26:32 -07001288static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001289static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001290static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001291static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001292static void vmx_set_segment(struct kvm_vcpu *vcpu,
1293 struct kvm_segment *var, int seg);
1294static void vmx_get_segment(struct kvm_vcpu *vcpu,
1295 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001296static bool guest_state_valid(struct kvm_vcpu *vcpu);
1297static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001298static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001299static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1300static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1301static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1302 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001303static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001304static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1305 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001306
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1308static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001309/*
1310 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1311 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1312 */
1313static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314
Feng Wubf9f6ac2015-09-18 22:29:55 +08001315/*
1316 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1317 * can find which vCPU should be waken up.
1318 */
1319static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1320static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1321
Radim Krčmář23611332016-09-29 22:41:33 +02001322enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001323 VMX_VMREAD_BITMAP,
1324 VMX_VMWRITE_BITMAP,
1325 VMX_BITMAP_NR
1326};
1327
1328static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1329
Radim Krčmář23611332016-09-29 22:41:33 +02001330#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1331#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001332
Avi Kivity110312c2010-12-21 12:54:20 +02001333static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001334static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001335
Sheng Yang2384d2b2008-01-17 15:14:33 +08001336static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1337static DEFINE_SPINLOCK(vmx_vpid_lock);
1338
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001339static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340 int size;
1341 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001342 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001344 u32 pin_based_exec_ctrl;
1345 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001346 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001347 u32 vmexit_ctrl;
1348 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001349 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001350} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351
Hannes Ederefff9e52008-11-28 17:02:06 +01001352static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001353 u32 ept;
1354 u32 vpid;
1355} vmx_capability;
1356
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357#define VMX_SEGMENT_FIELD(seg) \
1358 [VCPU_SREG_##seg] = { \
1359 .selector = GUEST_##seg##_SELECTOR, \
1360 .base = GUEST_##seg##_BASE, \
1361 .limit = GUEST_##seg##_LIMIT, \
1362 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1363 }
1364
Mathias Krause772e0312012-08-30 01:30:19 +02001365static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 unsigned selector;
1367 unsigned base;
1368 unsigned limit;
1369 unsigned ar_bytes;
1370} kvm_vmx_segment_fields[] = {
1371 VMX_SEGMENT_FIELD(CS),
1372 VMX_SEGMENT_FIELD(DS),
1373 VMX_SEGMENT_FIELD(ES),
1374 VMX_SEGMENT_FIELD(FS),
1375 VMX_SEGMENT_FIELD(GS),
1376 VMX_SEGMENT_FIELD(SS),
1377 VMX_SEGMENT_FIELD(TR),
1378 VMX_SEGMENT_FIELD(LDTR),
1379};
1380
Avi Kivity26bb0982009-09-07 11:14:12 +03001381static u64 host_efer;
1382
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001383static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1384
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001385/*
Brian Gerst8c065852010-07-17 09:03:26 -04001386 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001387 * away by decrementing the array size.
1388 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001390#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001391 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001393 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001396DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1397
1398#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1399
1400#define KVM_EVMCS_VERSION 1
1401
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001402/*
1403 * Enlightened VMCSv1 doesn't support these:
1404 *
1405 * POSTED_INTR_NV = 0x00000002,
1406 * GUEST_INTR_STATUS = 0x00000810,
1407 * APIC_ACCESS_ADDR = 0x00002014,
1408 * POSTED_INTR_DESC_ADDR = 0x00002016,
1409 * EOI_EXIT_BITMAP0 = 0x0000201c,
1410 * EOI_EXIT_BITMAP1 = 0x0000201e,
1411 * EOI_EXIT_BITMAP2 = 0x00002020,
1412 * EOI_EXIT_BITMAP3 = 0x00002022,
1413 * GUEST_PML_INDEX = 0x00000812,
1414 * PML_ADDRESS = 0x0000200e,
1415 * VM_FUNCTION_CONTROL = 0x00002018,
1416 * EPTP_LIST_ADDRESS = 0x00002024,
1417 * VMREAD_BITMAP = 0x00002026,
1418 * VMWRITE_BITMAP = 0x00002028,
1419 *
1420 * TSC_MULTIPLIER = 0x00002032,
1421 * PLE_GAP = 0x00004020,
1422 * PLE_WINDOW = 0x00004022,
1423 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1424 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1425 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1426 *
1427 * Currently unsupported in KVM:
1428 * GUEST_IA32_RTIT_CTL = 0x00002814,
1429 */
1430#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
1431 PIN_BASED_VMX_PREEMPTION_TIMER)
1432#define EVMCS1_UNSUPPORTED_2NDEXEC \
1433 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
1434 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
1435 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
1436 SECONDARY_EXEC_ENABLE_PML | \
1437 SECONDARY_EXEC_ENABLE_VMFUNC | \
1438 SECONDARY_EXEC_SHADOW_VMCS | \
1439 SECONDARY_EXEC_TSC_SCALING | \
1440 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
1441#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
1442#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
1443#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
1444
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001445#if IS_ENABLED(CONFIG_HYPERV)
1446static bool __read_mostly enlightened_vmcs = true;
1447module_param(enlightened_vmcs, bool, 0444);
1448
1449static inline void evmcs_write64(unsigned long field, u64 value)
1450{
1451 u16 clean_field;
1452 int offset = get_evmcs_offset(field, &clean_field);
1453
1454 if (offset < 0)
1455 return;
1456
1457 *(u64 *)((char *)current_evmcs + offset) = value;
1458
1459 current_evmcs->hv_clean_fields &= ~clean_field;
1460}
1461
1462static inline void evmcs_write32(unsigned long field, u32 value)
1463{
1464 u16 clean_field;
1465 int offset = get_evmcs_offset(field, &clean_field);
1466
1467 if (offset < 0)
1468 return;
1469
1470 *(u32 *)((char *)current_evmcs + offset) = value;
1471 current_evmcs->hv_clean_fields &= ~clean_field;
1472}
1473
1474static inline void evmcs_write16(unsigned long field, u16 value)
1475{
1476 u16 clean_field;
1477 int offset = get_evmcs_offset(field, &clean_field);
1478
1479 if (offset < 0)
1480 return;
1481
1482 *(u16 *)((char *)current_evmcs + offset) = value;
1483 current_evmcs->hv_clean_fields &= ~clean_field;
1484}
1485
1486static inline u64 evmcs_read64(unsigned long field)
1487{
1488 int offset = get_evmcs_offset(field, NULL);
1489
1490 if (offset < 0)
1491 return 0;
1492
1493 return *(u64 *)((char *)current_evmcs + offset);
1494}
1495
1496static inline u32 evmcs_read32(unsigned long field)
1497{
1498 int offset = get_evmcs_offset(field, NULL);
1499
1500 if (offset < 0)
1501 return 0;
1502
1503 return *(u32 *)((char *)current_evmcs + offset);
1504}
1505
1506static inline u16 evmcs_read16(unsigned long field)
1507{
1508 int offset = get_evmcs_offset(field, NULL);
1509
1510 if (offset < 0)
1511 return 0;
1512
1513 return *(u16 *)((char *)current_evmcs + offset);
1514}
1515
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001516static inline void evmcs_touch_msr_bitmap(void)
1517{
1518 if (unlikely(!current_evmcs))
1519 return;
1520
1521 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1522 current_evmcs->hv_clean_fields &=
1523 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1524}
1525
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001526static void evmcs_load(u64 phys_addr)
1527{
1528 struct hv_vp_assist_page *vp_ap =
1529 hv_get_vp_assist_page(smp_processor_id());
1530
1531 vp_ap->current_nested_vmcs = phys_addr;
1532 vp_ap->enlighten_vmentry = 1;
1533}
1534
1535static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1536{
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001537 vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1538 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001539
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001540 vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1541 vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001542
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001543}
Tianyu Lan877ad952018-07-19 08:40:23 +00001544
1545/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1546static void check_ept_pointer_match(struct kvm *kvm)
1547{
1548 struct kvm_vcpu *vcpu;
1549 u64 tmp_eptp = INVALID_PAGE;
1550 int i;
1551
1552 kvm_for_each_vcpu(i, vcpu, kvm) {
1553 if (!VALID_PAGE(tmp_eptp)) {
1554 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1555 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1556 to_kvm_vmx(kvm)->ept_pointers_match
1557 = EPT_POINTERS_MISMATCH;
1558 return;
1559 }
1560 }
1561
1562 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1563}
1564
1565static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1566{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001567 struct kvm_vcpu *vcpu;
1568 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001569
1570 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1571
1572 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1573 check_ept_pointer_match(kvm);
1574
1575 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001576 kvm_for_each_vcpu(i, vcpu, kvm)
1577 ret |= hyperv_flush_guest_mapping(
1578 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer);
1579 } else {
1580 ret = hyperv_flush_guest_mapping(
1581 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
Tianyu Lan877ad952018-07-19 08:40:23 +00001582 }
1583
Tianyu Lan877ad952018-07-19 08:40:23 +00001584 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1585 return ret;
1586}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001587#else /* !IS_ENABLED(CONFIG_HYPERV) */
1588static inline void evmcs_write64(unsigned long field, u64 value) {}
1589static inline void evmcs_write32(unsigned long field, u32 value) {}
1590static inline void evmcs_write16(unsigned long field, u16 value) {}
1591static inline u64 evmcs_read64(unsigned long field) { return 0; }
1592static inline u32 evmcs_read32(unsigned long field) { return 0; }
1593static inline u16 evmcs_read16(unsigned long field) { return 0; }
1594static inline void evmcs_load(u64 phys_addr) {}
1595static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001596static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001597#endif /* IS_ENABLED(CONFIG_HYPERV) */
1598
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02001599static int nested_enable_evmcs(struct kvm_vcpu *vcpu,
1600 uint16_t *vmcs_version)
1601{
1602 struct vcpu_vmx *vmx = to_vmx(vcpu);
1603
1604 /* We don't support disabling the feature for simplicity. */
1605 if (vmx->nested.enlightened_vmcs_enabled)
1606 return 0;
1607
1608 vmx->nested.enlightened_vmcs_enabled = true;
1609
1610 /*
1611 * vmcs_version represents the range of supported Enlightened VMCS
1612 * versions: lower 8 bits is the minimal version, higher 8 bits is the
1613 * maximum supported version. KVM supports versions from 1 to
1614 * KVM_EVMCS_VERSION.
1615 */
1616 *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1;
1617
1618 vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1619 vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
1620 vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1621 vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
1622 vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC;
1623
1624 return 0;
1625}
1626
Jan Kiszka5bb16012016-02-09 20:14:21 +01001627static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628{
1629 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1630 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001631 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1632}
1633
Jan Kiszka6f054852016-02-09 20:15:18 +01001634static inline bool is_debug(u32 intr_info)
1635{
1636 return is_exception_n(intr_info, DB_VECTOR);
1637}
1638
1639static inline bool is_breakpoint(u32 intr_info)
1640{
1641 return is_exception_n(intr_info, BP_VECTOR);
1642}
1643
Jan Kiszka5bb16012016-02-09 20:14:21 +01001644static inline bool is_page_fault(u32 intr_info)
1645{
1646 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647}
1648
Gui Jianfeng31299942010-03-15 17:29:09 +08001649static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001650{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001651 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001652}
1653
Liran Alon9e869482018-03-12 13:12:51 +02001654static inline bool is_gp_fault(u32 intr_info)
1655{
1656 return is_exception_n(intr_info, GP_VECTOR);
1657}
1658
Gui Jianfeng31299942010-03-15 17:29:09 +08001659static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001660{
1661 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1662 INTR_INFO_VALID_MASK)) ==
1663 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1664}
1665
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001666/* Undocumented: icebp/int1 */
1667static inline bool is_icebp(u32 intr_info)
1668{
1669 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1670 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1671}
1672
Gui Jianfeng31299942010-03-15 17:29:09 +08001673static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001674{
Sheng Yang04547152009-04-01 15:52:31 +08001675 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001676}
1677
Gui Jianfeng31299942010-03-15 17:29:09 +08001678static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001679{
Sheng Yang04547152009-04-01 15:52:31 +08001680 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001681}
1682
Paolo Bonzini35754c92015-07-29 12:05:37 +02001683static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001684{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001685 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001686}
1687
Gui Jianfeng31299942010-03-15 17:29:09 +08001688static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001689{
Sheng Yang04547152009-04-01 15:52:31 +08001690 return vmcs_config.cpu_based_exec_ctrl &
1691 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001692}
1693
Avi Kivity774ead32007-12-26 13:57:04 +02001694static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001695{
Sheng Yang04547152009-04-01 15:52:31 +08001696 return vmcs_config.cpu_based_2nd_exec_ctrl &
1697 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1698}
1699
Yang Zhang8d146952013-01-25 10:18:50 +08001700static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1701{
1702 return vmcs_config.cpu_based_2nd_exec_ctrl &
1703 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1704}
1705
Yang Zhang83d4c282013-01-25 10:18:49 +08001706static inline bool cpu_has_vmx_apic_register_virt(void)
1707{
1708 return vmcs_config.cpu_based_2nd_exec_ctrl &
1709 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1710}
1711
Yang Zhangc7c9c562013-01-25 10:18:51 +08001712static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1713{
1714 return vmcs_config.cpu_based_2nd_exec_ctrl &
1715 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1716}
1717
Sean Christopherson0b665d32018-08-14 09:33:34 -07001718static inline bool cpu_has_vmx_encls_vmexit(void)
1719{
1720 return vmcs_config.cpu_based_2nd_exec_ctrl &
1721 SECONDARY_EXEC_ENCLS_EXITING;
1722}
1723
Yunhong Jiang64672c92016-06-13 14:19:59 -07001724/*
1725 * Comment's format: document - errata name - stepping - processor name.
1726 * Refer from
1727 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1728 */
1729static u32 vmx_preemption_cpu_tfms[] = {
1730/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17310x000206E6,
1732/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1733/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1734/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17350x00020652,
1736/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17370x00020655,
1738/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1739/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1740/*
1741 * 320767.pdf - AAP86 - B1 -
1742 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1743 */
17440x000106E5,
1745/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17460x000106A0,
1747/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17480x000106A1,
1749/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17500x000106A4,
1751 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1752 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1753 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17540x000106A5,
1755};
1756
1757static inline bool cpu_has_broken_vmx_preemption_timer(void)
1758{
1759 u32 eax = cpuid_eax(0x00000001), i;
1760
1761 /* Clear the reserved bits */
1762 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001763 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001764 if (eax == vmx_preemption_cpu_tfms[i])
1765 return true;
1766
1767 return false;
1768}
1769
1770static inline bool cpu_has_vmx_preemption_timer(void)
1771{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001772 return vmcs_config.pin_based_exec_ctrl &
1773 PIN_BASED_VMX_PREEMPTION_TIMER;
1774}
1775
Yang Zhang01e439b2013-04-11 19:25:12 +08001776static inline bool cpu_has_vmx_posted_intr(void)
1777{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001778 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1779 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001780}
1781
1782static inline bool cpu_has_vmx_apicv(void)
1783{
1784 return cpu_has_vmx_apic_register_virt() &&
1785 cpu_has_vmx_virtual_intr_delivery() &&
1786 cpu_has_vmx_posted_intr();
1787}
1788
Sheng Yang04547152009-04-01 15:52:31 +08001789static inline bool cpu_has_vmx_flexpriority(void)
1790{
1791 return cpu_has_vmx_tpr_shadow() &&
1792 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001793}
1794
Marcelo Tosattie7997942009-06-11 12:07:40 -03001795static inline bool cpu_has_vmx_ept_execute_only(void)
1796{
Gui Jianfeng31299942010-03-15 17:29:09 +08001797 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001798}
1799
Marcelo Tosattie7997942009-06-11 12:07:40 -03001800static inline bool cpu_has_vmx_ept_2m_page(void)
1801{
Gui Jianfeng31299942010-03-15 17:29:09 +08001802 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001803}
1804
Sheng Yang878403b2010-01-05 19:02:29 +08001805static inline bool cpu_has_vmx_ept_1g_page(void)
1806{
Gui Jianfeng31299942010-03-15 17:29:09 +08001807 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001808}
1809
Sheng Yang4bc9b982010-06-02 14:05:24 +08001810static inline bool cpu_has_vmx_ept_4levels(void)
1811{
1812 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1813}
1814
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001815static inline bool cpu_has_vmx_ept_mt_wb(void)
1816{
1817 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1818}
1819
Yu Zhang855feb62017-08-24 20:27:55 +08001820static inline bool cpu_has_vmx_ept_5levels(void)
1821{
1822 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1823}
1824
Xudong Hao83c3a332012-05-28 19:33:35 +08001825static inline bool cpu_has_vmx_ept_ad_bits(void)
1826{
1827 return vmx_capability.ept & VMX_EPT_AD_BIT;
1828}
1829
Gui Jianfeng31299942010-03-15 17:29:09 +08001830static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001831{
Gui Jianfeng31299942010-03-15 17:29:09 +08001832 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001833}
1834
Gui Jianfeng31299942010-03-15 17:29:09 +08001835static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001836{
Gui Jianfeng31299942010-03-15 17:29:09 +08001837 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001838}
1839
Liran Aloncd9a4912018-05-22 17:16:15 +03001840static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1841{
1842 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1843}
1844
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001845static inline bool cpu_has_vmx_invvpid_single(void)
1846{
1847 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1848}
1849
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001850static inline bool cpu_has_vmx_invvpid_global(void)
1851{
1852 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1853}
1854
Wanpeng Li08d839c2017-03-23 05:30:08 -07001855static inline bool cpu_has_vmx_invvpid(void)
1856{
1857 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1858}
1859
Gui Jianfeng31299942010-03-15 17:29:09 +08001860static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001861{
Sheng Yang04547152009-04-01 15:52:31 +08001862 return vmcs_config.cpu_based_2nd_exec_ctrl &
1863 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001864}
1865
Gui Jianfeng31299942010-03-15 17:29:09 +08001866static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001867{
1868 return vmcs_config.cpu_based_2nd_exec_ctrl &
1869 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001873{
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1876}
1877
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001878static inline bool cpu_has_vmx_basic_inout(void)
1879{
1880 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1881}
1882
Paolo Bonzini35754c92015-07-29 12:05:37 +02001883static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001884{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001885 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001886}
1887
Gui Jianfeng31299942010-03-15 17:29:09 +08001888static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001889{
Sheng Yang04547152009-04-01 15:52:31 +08001890 return vmcs_config.cpu_based_2nd_exec_ctrl &
1891 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001892}
1893
Gui Jianfeng31299942010-03-15 17:29:09 +08001894static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001895{
1896 return vmcs_config.cpu_based_2nd_exec_ctrl &
1897 SECONDARY_EXEC_RDTSCP;
1898}
1899
Mao, Junjiead756a12012-07-02 01:18:48 +00001900static inline bool cpu_has_vmx_invpcid(void)
1901{
1902 return vmcs_config.cpu_based_2nd_exec_ctrl &
1903 SECONDARY_EXEC_ENABLE_INVPCID;
1904}
1905
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001906static inline bool cpu_has_virtual_nmis(void)
1907{
1908 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1909}
1910
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001911static inline bool cpu_has_vmx_wbinvd_exit(void)
1912{
1913 return vmcs_config.cpu_based_2nd_exec_ctrl &
1914 SECONDARY_EXEC_WBINVD_EXITING;
1915}
1916
Abel Gordonabc4fc52013-04-18 14:35:25 +03001917static inline bool cpu_has_vmx_shadow_vmcs(void)
1918{
1919 u64 vmx_msr;
1920 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1921 /* check if the cpu supports writing r/o exit information fields */
1922 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1923 return false;
1924
1925 return vmcs_config.cpu_based_2nd_exec_ctrl &
1926 SECONDARY_EXEC_SHADOW_VMCS;
1927}
1928
Kai Huang843e4332015-01-28 10:54:28 +08001929static inline bool cpu_has_vmx_pml(void)
1930{
1931 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1932}
1933
Haozhong Zhang64903d62015-10-20 15:39:09 +08001934static inline bool cpu_has_vmx_tsc_scaling(void)
1935{
1936 return vmcs_config.cpu_based_2nd_exec_ctrl &
1937 SECONDARY_EXEC_TSC_SCALING;
1938}
1939
Bandan Das2a499e42017-08-03 15:54:41 -04001940static inline bool cpu_has_vmx_vmfunc(void)
1941{
1942 return vmcs_config.cpu_based_2nd_exec_ctrl &
1943 SECONDARY_EXEC_ENABLE_VMFUNC;
1944}
1945
Sean Christopherson64f7a112018-04-30 10:01:06 -07001946static bool vmx_umip_emulated(void)
1947{
1948 return vmcs_config.cpu_based_2nd_exec_ctrl &
1949 SECONDARY_EXEC_DESC;
1950}
1951
Sheng Yang04547152009-04-01 15:52:31 +08001952static inline bool report_flexpriority(void)
1953{
1954 return flexpriority_enabled;
1955}
1956
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001957static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1958{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001959 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001960}
1961
Jim Mattsonf4160e42018-05-29 09:11:33 -07001962/*
1963 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1964 * to modify any valid field of the VMCS, or are the VM-exit
1965 * information fields read-only?
1966 */
1967static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1968{
1969 return to_vmx(vcpu)->nested.msrs.misc_low &
1970 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1971}
1972
Marc Orr04473782018-06-20 17:21:29 -07001973static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1974{
1975 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1976}
1977
1978static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1979{
1980 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1981 CPU_BASED_MONITOR_TRAP_FLAG;
1982}
1983
Liran Alonfa97d7d2018-07-18 14:07:59 +02001984static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1985{
1986 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1987 SECONDARY_EXEC_SHADOW_VMCS;
1988}
1989
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001990static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1991{
1992 return vmcs12->cpu_based_vm_exec_control & bit;
1993}
1994
1995static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1996{
1997 return (vmcs12->cpu_based_vm_exec_control &
1998 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1999 (vmcs12->secondary_vm_exec_control & bit);
2000}
2001
Jan Kiszkaf4124502014-03-07 20:03:13 +01002002static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
2003{
2004 return vmcs12->pin_based_vm_exec_control &
2005 PIN_BASED_VMX_PREEMPTION_TIMER;
2006}
2007
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05002008static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
2009{
2010 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
2011}
2012
2013static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
2014{
2015 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
2016}
2017
Nadav Har'El155a97a2013-08-05 11:07:16 +03002018static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
2019{
2020 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
2021}
2022
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002023static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2024{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002025 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002026}
2027
Bandan Dasc5f983f2017-05-05 15:25:14 -04002028static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2029{
2030 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2031}
2032
Wincy Vanf2b93282015-02-03 23:56:03 +08002033static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2034{
2035 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2036}
2037
Wanpeng Li5c614b32015-10-13 09:18:36 -07002038static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2039{
2040 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2041}
2042
Wincy Van82f0dd42015-02-03 23:57:18 +08002043static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2044{
2045 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2046}
2047
Wincy Van608406e2015-02-03 23:57:51 +08002048static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2049{
2050 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2051}
2052
Wincy Van705699a2015-02-03 23:58:17 +08002053static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2054{
2055 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2056}
2057
Bandan Das27c42a12017-08-03 15:54:42 -04002058static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2059{
2060 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2061}
2062
Bandan Das41ab9372017-08-03 15:54:43 -04002063static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2064{
2065 return nested_cpu_has_vmfunc(vmcs12) &&
2066 (vmcs12->vm_function_control &
2067 VMX_VMFUNC_EPTP_SWITCHING);
2068}
2069
Liran Alonf792d272018-06-23 02:35:05 +03002070static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2071{
2072 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2073}
2074
Jim Mattsonef85b672016-12-12 11:01:37 -08002075static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002076{
2077 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002078 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002079}
2080
Jan Kiszka533558b2014-01-04 18:47:20 +01002081static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2082 u32 exit_intr_info,
2083 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002084
Rusty Russell8b9cf982007-07-30 16:31:43 +10002085static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002086{
2087 int i;
2088
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002089 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002090 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002091 return i;
2092 return -1;
2093}
2094
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002095static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002096{
2097 struct {
2098 u64 vpid : 16;
2099 u64 rsvd : 48;
2100 u64 gva;
2101 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002102 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002103
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002104 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2105 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002106 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002107}
2108
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002109static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002110{
2111 struct {
2112 u64 eptp, gpa;
2113 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002114 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002115
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002116 asm volatile (__ex("invept %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 Yang14394422008-04-28 12:24:45 +08002119}
2120
Avi Kivity26bb0982009-09-07 11:14:12 +03002121static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002122{
2123 int i;
2124
Rusty Russell8b9cf982007-07-30 16:31:43 +10002125 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002126 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002127 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002128 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002129}
2130
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131static void vmcs_clear(struct vmcs *vmcs)
2132{
2133 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002134 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002136 asm volatile (__ex("vmclear %1") CC_SET(na)
2137 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002138 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2140 vmcs, phys_addr);
2141}
2142
Nadav Har'Eld462b812011-05-24 15:26:10 +03002143static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2144{
2145 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002146 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2147 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002148 loaded_vmcs->cpu = -1;
2149 loaded_vmcs->launched = 0;
2150}
2151
Dongxiao Xu7725b892010-05-11 18:29:38 +08002152static void vmcs_load(struct vmcs *vmcs)
2153{
2154 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002155 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002156
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002157 if (static_branch_unlikely(&enable_evmcs))
2158 return evmcs_load(phys_addr);
2159
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002160 asm volatile (__ex("vmptrld %1") CC_SET(na)
2161 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002162 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002163 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002164 vmcs, phys_addr);
2165}
2166
Dave Young2965faa2015-09-09 15:38:55 -07002167#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002168/*
2169 * This bitmap is used to indicate whether the vmclear
2170 * operation is enabled on all cpus. All disabled by
2171 * default.
2172 */
2173static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2174
2175static inline void crash_enable_local_vmclear(int cpu)
2176{
2177 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2178}
2179
2180static inline void crash_disable_local_vmclear(int cpu)
2181{
2182 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2183}
2184
2185static inline int crash_local_vmclear_enabled(int cpu)
2186{
2187 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2188}
2189
2190static void crash_vmclear_local_loaded_vmcss(void)
2191{
2192 int cpu = raw_smp_processor_id();
2193 struct loaded_vmcs *v;
2194
2195 if (!crash_local_vmclear_enabled(cpu))
2196 return;
2197
2198 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2199 loaded_vmcss_on_cpu_link)
2200 vmcs_clear(v->vmcs);
2201}
2202#else
2203static inline void crash_enable_local_vmclear(int cpu) { }
2204static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002205#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002206
Nadav Har'Eld462b812011-05-24 15:26:10 +03002207static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002209 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002210 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211
Nadav Har'Eld462b812011-05-24 15:26:10 +03002212 if (loaded_vmcs->cpu != cpu)
2213 return; /* vcpu migration can race with cpu offline */
2214 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002216 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002217 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002218
2219 /*
2220 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2221 * is before setting loaded_vmcs->vcpu to -1 which is done in
2222 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2223 * then adds the vmcs into percpu list before it is deleted.
2224 */
2225 smp_wmb();
2226
Nadav Har'Eld462b812011-05-24 15:26:10 +03002227 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002228 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229}
2230
Nadav Har'Eld462b812011-05-24 15:26:10 +03002231static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002232{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002233 int cpu = loaded_vmcs->cpu;
2234
2235 if (cpu != -1)
2236 smp_call_function_single(cpu,
2237 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002238}
2239
Junaid Shahidfaff8752018-06-29 13:10:05 -07002240static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2241{
2242 if (vpid == 0)
2243 return true;
2244
2245 if (cpu_has_vmx_invvpid_individual_addr()) {
2246 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2247 return true;
2248 }
2249
2250 return false;
2251}
2252
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002254{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002255 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002256 return;
2257
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002258 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002259 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002260}
2261
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002262static inline void vpid_sync_vcpu_global(void)
2263{
2264 if (cpu_has_vmx_invvpid_global())
2265 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2266}
2267
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002268static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002269{
2270 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002271 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002272 else
2273 vpid_sync_vcpu_global();
2274}
2275
Sheng Yang14394422008-04-28 12:24:45 +08002276static inline void ept_sync_global(void)
2277{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002278 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002279}
2280
2281static inline void ept_sync_context(u64 eptp)
2282{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002283 if (cpu_has_vmx_invept_context())
2284 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2285 else
2286 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002287}
2288
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002289static __always_inline void vmcs_check16(unsigned long field)
2290{
2291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2292 "16-bit accessor invalid for 64-bit field");
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2294 "16-bit accessor invalid for 64-bit high field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2296 "16-bit accessor invalid for 32-bit high field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2298 "16-bit accessor invalid for natural width field");
2299}
2300
2301static __always_inline void vmcs_check32(unsigned long field)
2302{
2303 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2304 "32-bit accessor invalid for 16-bit field");
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2306 "32-bit accessor invalid for natural width field");
2307}
2308
2309static __always_inline void vmcs_check64(unsigned long field)
2310{
2311 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2312 "64-bit accessor invalid for 16-bit field");
2313 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2314 "64-bit accessor invalid for 64-bit high field");
2315 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2316 "64-bit accessor invalid for 32-bit field");
2317 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2318 "64-bit accessor invalid for natural width field");
2319}
2320
2321static __always_inline void vmcs_checkl(unsigned long field)
2322{
2323 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2324 "Natural width accessor invalid for 16-bit field");
2325 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2326 "Natural width accessor invalid for 64-bit field");
2327 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2328 "Natural width accessor invalid for 64-bit high field");
2329 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2330 "Natural width accessor invalid for 32-bit field");
2331}
2332
2333static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334{
Avi Kivity5e520e62011-05-15 10:13:12 -04002335 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Uros Bizjak44c2d662018-10-11 19:40:45 +02002337 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002338 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339 return value;
2340}
2341
Avi Kivity96304212011-05-15 10:13:13 -04002342static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002344 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002345 if (static_branch_unlikely(&enable_evmcs))
2346 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002347 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348}
2349
Avi Kivity96304212011-05-15 10:13:13 -04002350static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002352 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002353 if (static_branch_unlikely(&enable_evmcs))
2354 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356}
2357
Avi Kivity96304212011-05-15 10:13:13 -04002358static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002360 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002361 if (static_branch_unlikely(&enable_evmcs))
2362 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002363#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002364 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002366 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367#endif
2368}
2369
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002370static __always_inline unsigned long vmcs_readl(unsigned long field)
2371{
2372 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002373 if (static_branch_unlikely(&enable_evmcs))
2374 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375 return __vmcs_readl(field);
2376}
2377
Avi Kivitye52de1b2007-01-05 16:36:56 -08002378static noinline void vmwrite_error(unsigned long field, unsigned long value)
2379{
2380 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2381 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2382 dump_stack();
2383}
2384
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002385static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002387 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002389 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2390 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002391 if (unlikely(error))
2392 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393}
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002398 if (static_branch_unlikely(&enable_evmcs))
2399 return evmcs_write16(field, value);
2400
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002401 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402}
2403
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002407 if (static_branch_unlikely(&enable_evmcs))
2408 return evmcs_write32(field, value);
2409
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002410 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002411}
2412
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002415 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002416 if (static_branch_unlikely(&enable_evmcs))
2417 return evmcs_write64(field, value);
2418
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002419 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002420#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002423#endif
2424}
2425
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002426static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002427{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002428 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002429 if (static_branch_unlikely(&enable_evmcs))
2430 return evmcs_write64(field, value);
2431
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002432 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002433}
2434
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002435static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002436{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002437 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2438 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002439 if (static_branch_unlikely(&enable_evmcs))
2440 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2441
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002442 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2443}
2444
2445static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2446{
2447 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2448 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002449 if (static_branch_unlikely(&enable_evmcs))
2450 return evmcs_write32(field, evmcs_read32(field) | mask);
2451
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002452 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002453}
2454
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002455static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2456{
2457 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2458}
2459
Gleb Natapov2961e8762013-11-25 15:37:13 +02002460static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2461{
2462 vmcs_write32(VM_ENTRY_CONTROLS, val);
2463 vmx->vm_entry_controls_shadow = val;
2464}
2465
2466static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2467{
2468 if (vmx->vm_entry_controls_shadow != val)
2469 vm_entry_controls_init(vmx, val);
2470}
2471
2472static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2473{
2474 return vmx->vm_entry_controls_shadow;
2475}
2476
2477
2478static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2479{
2480 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2481}
2482
2483static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2484{
2485 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2486}
2487
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002488static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2489{
2490 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2491}
2492
Gleb Natapov2961e8762013-11-25 15:37:13 +02002493static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2494{
2495 vmcs_write32(VM_EXIT_CONTROLS, val);
2496 vmx->vm_exit_controls_shadow = val;
2497}
2498
2499static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2500{
2501 if (vmx->vm_exit_controls_shadow != val)
2502 vm_exit_controls_init(vmx, val);
2503}
2504
2505static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2506{
2507 return vmx->vm_exit_controls_shadow;
2508}
2509
2510
2511static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2512{
2513 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2514}
2515
2516static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2517{
2518 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2519}
2520
Avi Kivity2fb92db2011-04-27 19:42:18 +03002521static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2522{
2523 vmx->segment_cache.bitmask = 0;
2524}
2525
2526static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2527 unsigned field)
2528{
2529 bool ret;
2530 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2531
2532 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2533 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2534 vmx->segment_cache.bitmask = 0;
2535 }
2536 ret = vmx->segment_cache.bitmask & mask;
2537 vmx->segment_cache.bitmask |= mask;
2538 return ret;
2539}
2540
2541static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2542{
2543 u16 *p = &vmx->segment_cache.seg[seg].selector;
2544
2545 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2546 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2547 return *p;
2548}
2549
2550static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2551{
2552 ulong *p = &vmx->segment_cache.seg[seg].base;
2553
2554 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2555 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2556 return *p;
2557}
2558
2559static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2560{
2561 u32 *p = &vmx->segment_cache.seg[seg].limit;
2562
2563 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2564 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2565 return *p;
2566}
2567
2568static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2569{
2570 u32 *p = &vmx->segment_cache.seg[seg].ar;
2571
2572 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2573 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2574 return *p;
2575}
2576
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002577static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2578{
2579 u32 eb;
2580
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002581 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002582 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002583 /*
2584 * Guest access to VMware backdoor ports could legitimately
2585 * trigger #GP because of TSS I/O permission bitmap.
2586 * We intercept those #GP and allow access to them anyway
2587 * as VMware does.
2588 */
2589 if (enable_vmware_backdoor)
2590 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002591 if ((vcpu->guest_debug &
2592 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2593 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2594 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002595 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002596 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002597 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002598 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002599
2600 /* When we are running a nested L2 guest and L1 specified for it a
2601 * certain exception bitmap, we must trap the same exceptions and pass
2602 * them to L1. When running L2, we will only handle the exceptions
2603 * specified above if L1 did not want them.
2604 */
2605 if (is_guest_mode(vcpu))
2606 eb |= get_vmcs12(vcpu)->exception_bitmap;
2607
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002608 vmcs_write32(EXCEPTION_BITMAP, eb);
2609}
2610
Ashok Raj15d45072018-02-01 22:59:43 +01002611/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002612 * Check if MSR is intercepted for currently loaded MSR bitmap.
2613 */
2614static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2615{
2616 unsigned long *msr_bitmap;
2617 int f = sizeof(unsigned long);
2618
2619 if (!cpu_has_vmx_msr_bitmap())
2620 return true;
2621
2622 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2623
2624 if (msr <= 0x1fff) {
2625 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2626 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2627 msr &= 0x1fff;
2628 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2629 }
2630
2631 return true;
2632}
2633
2634/*
Ashok Raj15d45072018-02-01 22:59:43 +01002635 * Check if MSR is intercepted for L01 MSR bitmap.
2636 */
2637static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2638{
2639 unsigned long *msr_bitmap;
2640 int f = sizeof(unsigned long);
2641
2642 if (!cpu_has_vmx_msr_bitmap())
2643 return true;
2644
2645 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2646
2647 if (msr <= 0x1fff) {
2648 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2649 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2650 msr &= 0x1fff;
2651 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2652 }
2653
2654 return true;
2655}
2656
Gleb Natapov2961e8762013-11-25 15:37:13 +02002657static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2658 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002659{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002660 vm_entry_controls_clearbit(vmx, entry);
2661 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002662}
2663
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002664static int find_msr(struct vmx_msrs *m, unsigned int msr)
2665{
2666 unsigned int i;
2667
2668 for (i = 0; i < m->nr; ++i) {
2669 if (m->val[i].index == msr)
2670 return i;
2671 }
2672 return -ENOENT;
2673}
2674
Avi Kivity61d2ef22010-04-28 16:40:38 +03002675static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2676{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002677 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002678 struct msr_autoload *m = &vmx->msr_autoload;
2679
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002680 switch (msr) {
2681 case MSR_EFER:
2682 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002683 clear_atomic_switch_msr_special(vmx,
2684 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002685 VM_EXIT_LOAD_IA32_EFER);
2686 return;
2687 }
2688 break;
2689 case MSR_CORE_PERF_GLOBAL_CTRL:
2690 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002691 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002692 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2693 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2694 return;
2695 }
2696 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002697 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002698 i = find_msr(&m->guest, msr);
2699 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002700 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002701 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002702 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002703 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002704
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002705skip_guest:
2706 i = find_msr(&m->host, msr);
2707 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002708 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002709
2710 --m->host.nr;
2711 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002712 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002713}
2714
Gleb Natapov2961e8762013-11-25 15:37:13 +02002715static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2716 unsigned long entry, unsigned long exit,
2717 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2718 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002719{
2720 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002721 if (host_val_vmcs != HOST_IA32_EFER)
2722 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002723 vm_entry_controls_setbit(vmx, entry);
2724 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002725}
2726
Avi Kivity61d2ef22010-04-28 16:40:38 +03002727static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002728 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002729{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002730 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002731 struct msr_autoload *m = &vmx->msr_autoload;
2732
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002733 switch (msr) {
2734 case MSR_EFER:
2735 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002736 add_atomic_switch_msr_special(vmx,
2737 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002738 VM_EXIT_LOAD_IA32_EFER,
2739 GUEST_IA32_EFER,
2740 HOST_IA32_EFER,
2741 guest_val, host_val);
2742 return;
2743 }
2744 break;
2745 case MSR_CORE_PERF_GLOBAL_CTRL:
2746 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002747 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002748 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2749 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2750 GUEST_IA32_PERF_GLOBAL_CTRL,
2751 HOST_IA32_PERF_GLOBAL_CTRL,
2752 guest_val, host_val);
2753 return;
2754 }
2755 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002756 case MSR_IA32_PEBS_ENABLE:
2757 /* PEBS needs a quiescent period after being disabled (to write
2758 * a record). Disabling PEBS through VMX MSR swapping doesn't
2759 * provide that period, so a CPU could write host's record into
2760 * guest's memory.
2761 */
2762 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002763 }
2764
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002765 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002766 if (!entry_only)
2767 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002768
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002769 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002770 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002771 "Can't add msr %x\n", msr);
2772 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002773 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002774 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002775 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002776 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002777 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002778 m->guest.val[i].index = msr;
2779 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002780
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002781 if (entry_only)
2782 return;
2783
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002784 if (j < 0) {
2785 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002786 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002787 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002788 m->host.val[j].index = msr;
2789 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002790}
2791
Avi Kivity92c0d902009-10-29 11:00:16 +02002792static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002793{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002794 u64 guest_efer = vmx->vcpu.arch.efer;
2795 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002796
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002797 if (!enable_ept) {
2798 /*
2799 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2800 * host CPUID is more efficient than testing guest CPUID
2801 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2802 */
2803 if (boot_cpu_has(X86_FEATURE_SMEP))
2804 guest_efer |= EFER_NX;
2805 else if (!(guest_efer & EFER_NX))
2806 ignore_bits |= EFER_NX;
2807 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002808
Avi Kivity51c6cf62007-08-29 03:48:05 +03002809 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002810 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002811 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002812 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002813#ifdef CONFIG_X86_64
2814 ignore_bits |= EFER_LMA | EFER_LME;
2815 /* SCE is meaningful only in long mode on Intel */
2816 if (guest_efer & EFER_LMA)
2817 ignore_bits &= ~(u64)EFER_SCE;
2818#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002819
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002820 /*
2821 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2822 * On CPUs that support "load IA32_EFER", always switch EFER
2823 * atomically, since it's faster than switching it manually.
2824 */
2825 if (cpu_has_load_ia32_efer ||
2826 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002827 if (!(guest_efer & EFER_LMA))
2828 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002829 if (guest_efer != host_efer)
2830 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002831 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002832 else
2833 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002834 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002835 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002836 clear_atomic_switch_msr(vmx, MSR_EFER);
2837
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002838 guest_efer &= ~ignore_bits;
2839 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002840
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002841 vmx->guest_msrs[efer_offset].data = guest_efer;
2842 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2843
2844 return true;
2845 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002846}
2847
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002848#ifdef CONFIG_X86_32
2849/*
2850 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2851 * VMCS rather than the segment table. KVM uses this helper to figure
2852 * out the current bases to poke them into the VMCS before entry.
2853 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002854static unsigned long segment_base(u16 selector)
2855{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002856 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002857 unsigned long v;
2858
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002859 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002860 return 0;
2861
Thomas Garnier45fc8752017-03-14 10:05:08 -07002862 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002863
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002864 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002865 u16 ldt_selector = kvm_read_ldt();
2866
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002867 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002868 return 0;
2869
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002870 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002871 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002872 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002873 return v;
2874}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002875#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002876
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002877static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002878{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002879 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002880 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002881#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002882 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002883#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002884 unsigned long fs_base, gs_base;
2885 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002886 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002887
Sean Christophersond264ee02018-08-27 15:21:12 -07002888 vmx->req_immediate_exit = false;
2889
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002890 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002891 return;
2892
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002893 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002894 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002895
Avi Kivity33ed6322007-05-02 16:54:03 +03002896 /*
2897 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2898 * allow segment selectors with cpl > 0 or ti == 1.
2899 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002900 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002901
2902#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002903 savesegment(ds, host_state->ds_sel);
2904 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002905
2906 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002907 if (likely(is_64bit_mm(current->mm))) {
2908 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002909 fs_sel = current->thread.fsindex;
2910 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002911 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002912 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002913 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002914 savesegment(fs, fs_sel);
2915 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002916 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002917 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002918 }
2919
Paolo Bonzini4679b612018-09-24 17:23:01 +02002920 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002921#else
Sean Christophersone368b872018-07-23 12:32:41 -07002922 savesegment(fs, fs_sel);
2923 savesegment(gs, gs_sel);
2924 fs_base = segment_base(fs_sel);
2925 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002926#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002927
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002928 if (unlikely(fs_sel != host_state->fs_sel)) {
2929 if (!(fs_sel & 7))
2930 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2931 else
2932 vmcs_write16(HOST_FS_SELECTOR, 0);
2933 host_state->fs_sel = fs_sel;
2934 }
2935 if (unlikely(gs_sel != host_state->gs_sel)) {
2936 if (!(gs_sel & 7))
2937 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2938 else
2939 vmcs_write16(HOST_GS_SELECTOR, 0);
2940 host_state->gs_sel = gs_sel;
2941 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002942 if (unlikely(fs_base != host_state->fs_base)) {
2943 vmcs_writel(HOST_FS_BASE, fs_base);
2944 host_state->fs_base = fs_base;
2945 }
2946 if (unlikely(gs_base != host_state->gs_base)) {
2947 vmcs_writel(HOST_GS_BASE, gs_base);
2948 host_state->gs_base = gs_base;
2949 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002950
Avi Kivity26bb0982009-09-07 11:14:12 +03002951 for (i = 0; i < vmx->save_nmsrs; ++i)
2952 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002953 vmx->guest_msrs[i].data,
2954 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002955}
2956
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002957static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002958{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002959 struct vmcs_host_state *host_state;
2960
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002961 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002962 return;
2963
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002964 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002965 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002966
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002967 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002968 vmx->loaded_cpu_state = NULL;
2969
Avi Kivityc8770e72010-11-11 12:37:26 +02002970#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002971 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002972#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002973 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2974 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002975#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002976 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002977#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002978 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002979#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002980 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002981 if (host_state->fs_sel & 7)
2982 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002983#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002984 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2985 loadsegment(ds, host_state->ds_sel);
2986 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002987 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002988#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002989 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002990#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002991 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002992#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002993 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002994}
2995
Sean Christopherson678e3152018-07-23 12:32:43 -07002996#ifdef CONFIG_X86_64
2997static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002998{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002999 preempt_disable();
3000 if (vmx->loaded_cpu_state)
3001 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
3002 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003003 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03003004}
3005
Sean Christopherson678e3152018-07-23 12:32:43 -07003006static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
3007{
Paolo Bonzini4679b612018-09-24 17:23:01 +02003008 preempt_disable();
3009 if (vmx->loaded_cpu_state)
3010 wrmsrl(MSR_KERNEL_GS_BASE, data);
3011 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07003012 vmx->msr_guest_kernel_gs_base = data;
3013}
3014#endif
3015
Feng Wu28b835d2015-09-18 22:29:54 +08003016static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3017{
3018 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3019 struct pi_desc old, new;
3020 unsigned int dest;
3021
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003022 /*
3023 * In case of hot-plug or hot-unplug, we may have to undo
3024 * vmx_vcpu_pi_put even if there is no assigned device. And we
3025 * always keep PI.NDST up to date for simplicity: it makes the
3026 * code easier, and CPU migration is not a fast path.
3027 */
3028 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003029 return;
3030
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003031 /*
3032 * First handle the simple case where no cmpxchg is necessary; just
3033 * allow posting non-urgent interrupts.
3034 *
3035 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3036 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3037 * expects the VCPU to be on the blocked_vcpu_list that matches
3038 * PI.NDST.
3039 */
3040 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3041 vcpu->cpu == cpu) {
3042 pi_clear_sn(pi_desc);
3043 return;
3044 }
3045
3046 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003047 do {
3048 old.control = new.control = pi_desc->control;
3049
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003050 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003051
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003052 if (x2apic_enabled())
3053 new.ndst = dest;
3054 else
3055 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003056
Feng Wu28b835d2015-09-18 22:29:54 +08003057 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003058 } while (cmpxchg64(&pi_desc->control, old.control,
3059 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003060}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003061
Peter Feinerc95ba922016-08-17 09:36:47 -07003062static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3063{
3064 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3065 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3066}
3067
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068/*
3069 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3070 * vcpu mutex is already taken.
3071 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003072static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003073{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003074 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003075 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003077 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003078 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003079 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003080 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003081
3082 /*
3083 * Read loaded_vmcs->cpu should be before fetching
3084 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3085 * See the comments in __loaded_vmcs_clear().
3086 */
3087 smp_rmb();
3088
Nadav Har'Eld462b812011-05-24 15:26:10 +03003089 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3090 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003091 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003092 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003093 }
3094
3095 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3096 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3097 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003098 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003099 }
3100
3101 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003102 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003103 unsigned long sysenter_esp;
3104
3105 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003106
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 /*
3108 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003109 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003110 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003111 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003112 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003113 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003115 /*
3116 * VM exits change the host TR limit to 0x67 after a VM
3117 * exit. This is okay, since 0x67 covers everything except
3118 * the IO bitmap and have have code to handle the IO bitmap
3119 * being lost after a VM exit.
3120 */
3121 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3122
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3124 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003125
Nadav Har'Eld462b812011-05-24 15:26:10 +03003126 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127 }
Feng Wu28b835d2015-09-18 22:29:54 +08003128
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003129 /* Setup TSC multiplier */
3130 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003131 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3132 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003133
Feng Wu28b835d2015-09-18 22:29:54 +08003134 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003135 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003136 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003137}
3138
3139static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3140{
3141 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3142
3143 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003144 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3145 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003146 return;
3147
3148 /* Set SN when the vCPU is preempted */
3149 if (vcpu->preempted)
3150 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151}
3152
3153static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3154{
Feng Wu28b835d2015-09-18 22:29:54 +08003155 vmx_vcpu_pi_put(vcpu);
3156
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003157 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158}
3159
Wanpeng Lif244dee2017-07-20 01:11:54 -07003160static bool emulation_required(struct kvm_vcpu *vcpu)
3161{
3162 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3163}
3164
Avi Kivityedcafe32009-12-30 18:07:40 +02003165static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3166
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003167/*
3168 * Return the cr0 value that a nested guest would read. This is a combination
3169 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3170 * its hypervisor (cr0_read_shadow).
3171 */
3172static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3173{
3174 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3175 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3176}
3177static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3178{
3179 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3180 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3181}
3182
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3184{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003185 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003186
Avi Kivity6de12732011-03-07 12:51:22 +02003187 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3188 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3189 rflags = vmcs_readl(GUEST_RFLAGS);
3190 if (to_vmx(vcpu)->rmode.vm86_active) {
3191 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3192 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3193 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3194 }
3195 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003196 }
Avi Kivity6de12732011-03-07 12:51:22 +02003197 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198}
3199
3200static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3201{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003202 unsigned long old_rflags = vmx_get_rflags(vcpu);
3203
Avi Kivity6de12732011-03-07 12:51:22 +02003204 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3205 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003206 if (to_vmx(vcpu)->rmode.vm86_active) {
3207 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003208 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003209 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003211
3212 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3213 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214}
3215
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003216static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003217{
3218 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3219 int ret = 0;
3220
3221 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003222 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003223 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003224 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003225
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003226 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003227}
3228
3229static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3230{
3231 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3232 u32 interruptibility = interruptibility_old;
3233
3234 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3235
Jan Kiszka48005f62010-02-19 19:38:07 +01003236 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003237 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003238 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003239 interruptibility |= GUEST_INTR_STATE_STI;
3240
3241 if ((interruptibility != interruptibility_old))
3242 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3243}
3244
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3246{
3247 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003249 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003251 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252
Glauber Costa2809f5d2009-05-12 16:21:05 -04003253 /* skipping an emulated instruction also counts */
3254 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255}
3256
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003257static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3258 unsigned long exit_qual)
3259{
3260 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3261 unsigned int nr = vcpu->arch.exception.nr;
3262 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3263
3264 if (vcpu->arch.exception.has_error_code) {
3265 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3266 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3267 }
3268
3269 if (kvm_exception_is_soft(nr))
3270 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3271 else
3272 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3273
3274 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3275 vmx_get_nmi_mask(vcpu))
3276 intr_info |= INTR_INFO_UNBLOCK_NMI;
3277
3278 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3279}
3280
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003281/*
3282 * KVM wants to inject page-faults which it got to the guest. This function
3283 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003284 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003285static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003286{
3287 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003288 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003289
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003290 if (nr == PF_VECTOR) {
3291 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003292 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003293 return 1;
3294 }
3295 /*
3296 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3297 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3298 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3299 * can be written only when inject_pending_event runs. This should be
3300 * conditional on a new capability---if the capability is disabled,
3301 * kvm_multiple_exception would write the ancillary information to
3302 * CR2 or DR6, for backwards ABI-compatibility.
3303 */
3304 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3305 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003306 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003307 return 1;
3308 }
3309 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003310 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003311 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003312 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003313 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3314 *exit_qual ^= DR6_RTM;
3315 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003316 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003317 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003318 return 1;
3319 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003320 }
3321
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003322 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003323}
3324
Wanpeng Licaa057a2018-03-12 04:53:03 -07003325static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3326{
3327 /*
3328 * Ensure that we clear the HLT state in the VMCS. We don't need to
3329 * explicitly skip the instruction because if the HLT state is set,
3330 * then the instruction is already executing and RIP has already been
3331 * advanced.
3332 */
3333 if (kvm_hlt_in_guest(vcpu->kvm) &&
3334 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3335 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3336}
3337
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003338static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003339{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003340 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003341 unsigned nr = vcpu->arch.exception.nr;
3342 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003343 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003344 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003345
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003346 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003347 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003348 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3349 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003350
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003351 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003352 int inc_eip = 0;
3353 if (kvm_exception_is_soft(nr))
3354 inc_eip = vcpu->arch.event_exit_inst_len;
3355 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003356 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003357 return;
3358 }
3359
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003360 WARN_ON_ONCE(vmx->emulation_required);
3361
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003362 if (kvm_exception_is_soft(nr)) {
3363 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3364 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003365 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3366 } else
3367 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3368
3369 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003370
3371 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003372}
3373
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003374static bool vmx_rdtscp_supported(void)
3375{
3376 return cpu_has_vmx_rdtscp();
3377}
3378
Mao, Junjiead756a12012-07-02 01:18:48 +00003379static bool vmx_invpcid_supported(void)
3380{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003381 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003382}
3383
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384/*
Eddie Donga75beee2007-05-17 18:55:15 +03003385 * Swap MSR entry in host/guest MSR entry array.
3386 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003388{
Avi Kivity26bb0982009-09-07 11:14:12 +03003389 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003390
3391 tmp = vmx->guest_msrs[to];
3392 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3393 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003394}
3395
3396/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003397 * Set up the vmcs to automatically save and restore system
3398 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3399 * mode, as fiddling with msrs is very expensive.
3400 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003401static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003402{
Avi Kivity26bb0982009-09-07 11:14:12 +03003403 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003404
Eddie Donga75beee2007-05-17 18:55:15 +03003405 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003406#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003407 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003408 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003409 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003410 move_msr_up(vmx, index, save_nmsrs++);
3411 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003412 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003413 move_msr_up(vmx, index, save_nmsrs++);
3414 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003415 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003416 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003417 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003418 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003419 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003420 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003421 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003422 * if efer.sce is enabled.
3423 */
Brian Gerst8c065852010-07-17 09:03:26 -04003424 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003425 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003426 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003427 }
Eddie Donga75beee2007-05-17 18:55:15 +03003428#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003429 index = __find_msr_index(vmx, MSR_EFER);
3430 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003431 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003432
Avi Kivity26bb0982009-09-07 11:14:12 +03003433 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003434
Yang Zhang8d146952013-01-25 10:18:50 +08003435 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003436 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003437}
3438
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003439static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003441 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003443 if (is_guest_mode(vcpu) &&
3444 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3445 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3446
3447 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448}
3449
3450/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003451 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003453static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003455 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003456 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003457 * We're here if L1 chose not to trap WRMSR to TSC. According
3458 * to the spec, this should set L1's TSC; The offset that L1
3459 * set for L2 remains unchanged, and still needs to be added
3460 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003461 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003462 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003463 /* recalculate vmcs02.TSC_OFFSET: */
3464 vmcs12 = get_vmcs12(vcpu);
3465 vmcs_write64(TSC_OFFSET, offset +
3466 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3467 vmcs12->tsc_offset : 0));
3468 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003469 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3470 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003471 vmcs_write64(TSC_OFFSET, offset);
3472 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473}
3474
Nadav Har'El801d3422011-05-25 23:02:23 +03003475/*
3476 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3477 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3478 * all guests if the "nested" module option is off, and can also be disabled
3479 * for a single guest by disabling its VMX cpuid bit.
3480 */
3481static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3482{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003483 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003484}
3485
Avi Kivity6aa8b732006-12-10 02:21:36 -08003486/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003487 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3488 * returned for the various VMX controls MSRs when nested VMX is enabled.
3489 * The same values should also be used to verify that vmcs12 control fields are
3490 * valid during nested entry from L1 to L2.
3491 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3492 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3493 * bit in the high half is on if the corresponding bit in the control field
3494 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003495 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003496static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003497{
Paolo Bonzini13893092018-02-26 13:40:09 +01003498 if (!nested) {
3499 memset(msrs, 0, sizeof(*msrs));
3500 return;
3501 }
3502
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003503 /*
3504 * Note that as a general rule, the high half of the MSRs (bits in
3505 * the control fields which may be 1) should be initialized by the
3506 * intersection of the underlying hardware's MSR (i.e., features which
3507 * can be supported) and the list of features we want to expose -
3508 * because they are known to be properly supported in our code.
3509 * Also, usually, the low half of the MSRs (bits which must be 1) can
3510 * be set to 0, meaning that L1 may turn off any of these bits. The
3511 * reason is that if one of these bits is necessary, it will appear
3512 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3513 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003514 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003515 * These rules have exceptions below.
3516 */
3517
3518 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003519 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 msrs->pinbased_ctls_low,
3521 msrs->pinbased_ctls_high);
3522 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003523 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003525 PIN_BASED_EXT_INTR_MASK |
3526 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003527 PIN_BASED_VIRTUAL_NMIS |
3528 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003530 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003531 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003532
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003533 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003534 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 msrs->exit_ctls_low,
3536 msrs->exit_ctls_high);
3537 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003538 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003539
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003540 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003541#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003542 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003543#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003544 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003546 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003547 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003548 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3549
Jan Kiszka2996fca2014-06-16 13:59:43 +02003550 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003552
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003553 /* entry controls */
3554 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003555 msrs->entry_ctls_low,
3556 msrs->entry_ctls_high);
3557 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003558 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003559 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003560#ifdef CONFIG_X86_64
3561 VM_ENTRY_IA32E_MODE |
3562#endif
3563 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003564 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003565 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003566
Jan Kiszka2996fca2014-06-16 13:59:43 +02003567 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003568 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003569
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003570 /* cpu-based controls */
3571 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003572 msrs->procbased_ctls_low,
3573 msrs->procbased_ctls_high);
3574 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003575 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003577 CPU_BASED_VIRTUAL_INTR_PENDING |
3578 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003579 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3580 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3581 CPU_BASED_CR3_STORE_EXITING |
3582#ifdef CONFIG_X86_64
3583 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3584#endif
3585 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003586 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3587 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3588 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3589 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003590 /*
3591 * We can allow some features even when not supported by the
3592 * hardware. For example, L1 can specify an MSR bitmap - and we
3593 * can use it to avoid exits to L1 - even when L0 runs L2
3594 * without MSR bitmaps.
3595 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003596 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003597 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003598 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003599
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003600 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003601 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003602 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3603
Paolo Bonzini80154d72017-08-24 13:55:35 +02003604 /*
3605 * secondary cpu-based controls. Do not include those that
3606 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3607 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003608 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003609 msrs->secondary_ctls_low,
3610 msrs->secondary_ctls_high);
3611 msrs->secondary_ctls_low = 0;
3612 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003613 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003614 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003615 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003616 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003617 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003618
Liran Alon32c7acf2018-06-23 02:35:11 +03003619 /*
3620 * We can emulate "VMCS shadowing," even if the hardware
3621 * doesn't support it.
3622 */
3623 msrs->secondary_ctls_high |=
3624 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003625
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003626 if (enable_ept) {
3627 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003629 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003630 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003631 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003632 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003634 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003635 msrs->ept_caps &= vmx_capability.ept;
3636 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003637 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3638 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003639 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003640 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003641 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003642 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003643 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003644 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003645
Bandan Das27c42a12017-08-03 15:54:42 -04003646 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003648 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003649 /*
3650 * Advertise EPTP switching unconditionally
3651 * since we emulate it
3652 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003653 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003655 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003656 }
3657
Paolo Bonzinief697a72016-03-18 16:58:38 +01003658 /*
3659 * Old versions of KVM use the single-context version without
3660 * checking for support, so declare that it is supported even
3661 * though it is treated as global context. The alternative is
3662 * not failing the single-context invvpid, and it is worse.
3663 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003664 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003665 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003666 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003667 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003668 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003669 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003670
Radim Krčmář0790ec12015-03-17 14:02:32 +01003671 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003672 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003673 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3674
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003675 if (flexpriority_enabled)
3676 msrs->secondary_ctls_high |=
3677 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3678
Jan Kiszkac18911a2013-03-13 16:06:41 +01003679 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003680 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003681 msrs->misc_low,
3682 msrs->misc_high);
3683 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3684 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003685 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003686 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003687 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003689
3690 /*
3691 * This MSR reports some information about VMX support. We
3692 * should return information about the VMX we emulate for the
3693 * guest, and the VMCS structure we give it - not about the
3694 * VMX support of the underlying hardware.
3695 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003696 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003697 VMCS12_REVISION |
3698 VMX_BASIC_TRUE_CTLS |
3699 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3700 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3701
3702 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003703 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003704
3705 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003706 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003707 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3708 * We picked the standard core2 setting.
3709 */
3710#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3711#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003712 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3713 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003714
3715 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003716 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3717 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003718
3719 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003720 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003721}
3722
David Matlack38991522016-11-29 18:14:08 -08003723/*
3724 * if fixed0[i] == 1: val[i] must be 1
3725 * if fixed1[i] == 0: val[i] must be 0
3726 */
3727static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3728{
3729 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003730}
3731
3732static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3733{
David Matlack38991522016-11-29 18:14:08 -08003734 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003735}
3736
3737static inline u64 vmx_control_msr(u32 low, u32 high)
3738{
3739 return low | ((u64)high << 32);
3740}
3741
David Matlack62cc6b9d2016-11-29 18:14:07 -08003742static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3743{
3744 superset &= mask;
3745 subset &= mask;
3746
3747 return (superset | subset) == superset;
3748}
3749
3750static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3751{
3752 const u64 feature_and_reserved =
3753 /* feature (except bit 48; see below) */
3754 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3755 /* reserved */
3756 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003757 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003758
3759 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3760 return -EINVAL;
3761
3762 /*
3763 * KVM does not emulate a version of VMX that constrains physical
3764 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3765 */
3766 if (data & BIT_ULL(48))
3767 return -EINVAL;
3768
3769 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3770 vmx_basic_vmcs_revision_id(data))
3771 return -EINVAL;
3772
3773 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3774 return -EINVAL;
3775
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003776 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777 return 0;
3778}
3779
3780static int
3781vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3782{
3783 u64 supported;
3784 u32 *lowp, *highp;
3785
3786 switch (msr_index) {
3787 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003788 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3789 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003790 break;
3791 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003792 lowp = &vmx->nested.msrs.procbased_ctls_low;
3793 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003794 break;
3795 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003796 lowp = &vmx->nested.msrs.exit_ctls_low;
3797 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003798 break;
3799 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003800 lowp = &vmx->nested.msrs.entry_ctls_low;
3801 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003802 break;
3803 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003804 lowp = &vmx->nested.msrs.secondary_ctls_low;
3805 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003806 break;
3807 default:
3808 BUG();
3809 }
3810
3811 supported = vmx_control_msr(*lowp, *highp);
3812
3813 /* Check must-be-1 bits are still 1. */
3814 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3815 return -EINVAL;
3816
3817 /* Check must-be-0 bits are still 0. */
3818 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3819 return -EINVAL;
3820
3821 *lowp = data;
3822 *highp = data >> 32;
3823 return 0;
3824}
3825
3826static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3827{
3828 const u64 feature_and_reserved_bits =
3829 /* feature */
3830 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3831 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3832 /* reserved */
3833 GENMASK_ULL(13, 9) | BIT_ULL(31);
3834 u64 vmx_misc;
3835
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003836 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3837 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003838
3839 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3840 return -EINVAL;
3841
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003842 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003843 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3844 vmx_misc_preemption_timer_rate(data) !=
3845 vmx_misc_preemption_timer_rate(vmx_misc))
3846 return -EINVAL;
3847
3848 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3849 return -EINVAL;
3850
3851 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3852 return -EINVAL;
3853
3854 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3855 return -EINVAL;
3856
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 vmx->nested.msrs.misc_low = data;
3858 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003859
3860 /*
3861 * If L1 has read-only VM-exit information fields, use the
3862 * less permissive vmx_vmwrite_bitmap to specify write
3863 * permissions for the shadow VMCS.
3864 */
3865 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3866 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3867
David Matlack62cc6b9d2016-11-29 18:14:07 -08003868 return 0;
3869}
3870
3871static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3872{
3873 u64 vmx_ept_vpid_cap;
3874
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3876 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003877
3878 /* Every bit is either reserved or a feature bit. */
3879 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3880 return -EINVAL;
3881
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003882 vmx->nested.msrs.ept_caps = data;
3883 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003884 return 0;
3885}
3886
3887static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3888{
3889 u64 *msr;
3890
3891 switch (msr_index) {
3892 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003893 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003894 break;
3895 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003896 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003897 break;
3898 default:
3899 BUG();
3900 }
3901
3902 /*
3903 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3904 * must be 1 in the restored value.
3905 */
3906 if (!is_bitwise_subset(data, *msr, -1ULL))
3907 return -EINVAL;
3908
3909 *msr = data;
3910 return 0;
3911}
3912
3913/*
3914 * Called when userspace is restoring VMX MSRs.
3915 *
3916 * Returns 0 on success, non-0 otherwise.
3917 */
3918static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3919{
3920 struct vcpu_vmx *vmx = to_vmx(vcpu);
3921
Jim Mattsona943ac52018-05-29 09:11:32 -07003922 /*
3923 * Don't allow changes to the VMX capability MSRs while the vCPU
3924 * is in VMX operation.
3925 */
3926 if (vmx->nested.vmxon)
3927 return -EBUSY;
3928
David Matlack62cc6b9d2016-11-29 18:14:07 -08003929 switch (msr_index) {
3930 case MSR_IA32_VMX_BASIC:
3931 return vmx_restore_vmx_basic(vmx, data);
3932 case MSR_IA32_VMX_PINBASED_CTLS:
3933 case MSR_IA32_VMX_PROCBASED_CTLS:
3934 case MSR_IA32_VMX_EXIT_CTLS:
3935 case MSR_IA32_VMX_ENTRY_CTLS:
3936 /*
3937 * The "non-true" VMX capability MSRs are generated from the
3938 * "true" MSRs, so we do not support restoring them directly.
3939 *
3940 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3941 * should restore the "true" MSRs with the must-be-1 bits
3942 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3943 * DEFAULT SETTINGS".
3944 */
3945 return -EINVAL;
3946 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3947 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3948 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3949 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3950 case MSR_IA32_VMX_PROCBASED_CTLS2:
3951 return vmx_restore_control_msr(vmx, msr_index, data);
3952 case MSR_IA32_VMX_MISC:
3953 return vmx_restore_vmx_misc(vmx, data);
3954 case MSR_IA32_VMX_CR0_FIXED0:
3955 case MSR_IA32_VMX_CR4_FIXED0:
3956 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3957 case MSR_IA32_VMX_CR0_FIXED1:
3958 case MSR_IA32_VMX_CR4_FIXED1:
3959 /*
3960 * These MSRs are generated based on the vCPU's CPUID, so we
3961 * do not support restoring them directly.
3962 */
3963 return -EINVAL;
3964 case MSR_IA32_VMX_EPT_VPID_CAP:
3965 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3966 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003967 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003968 return 0;
3969 default:
3970 /*
3971 * The rest of the VMX capability MSRs do not support restore.
3972 */
3973 return -EINVAL;
3974 }
3975}
3976
Jan Kiszkacae50132014-01-04 18:47:22 +01003977/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003978static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003979{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003980 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003981 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003982 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003983 break;
3984 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3985 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003986 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003987 msrs->pinbased_ctls_low,
3988 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003989 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3990 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 break;
3992 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3993 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003994 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003995 msrs->procbased_ctls_low,
3996 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003997 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3998 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
4000 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
4001 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004002 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004003 msrs->exit_ctls_low,
4004 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004005 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
4006 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004007 break;
4008 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
4009 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004010 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004011 msrs->entry_ctls_low,
4012 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004013 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4014 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004015 break;
4016 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004017 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004018 msrs->misc_low,
4019 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004020 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004021 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004022 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004023 break;
4024 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004025 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
4027 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004028 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004029 break;
4030 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004031 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004032 break;
4033 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004034 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004035 break;
4036 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004037 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004038 msrs->secondary_ctls_low,
4039 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004040 break;
4041 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004042 *pdata = msrs->ept_caps |
4043 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004044 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004045 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004046 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004047 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004048 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004049 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004050 }
4051
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004052 return 0;
4053}
4054
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004055static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4056 uint64_t val)
4057{
4058 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4059
4060 return !(val & ~valid_bits);
4061}
4062
Tom Lendacky801e4592018-02-21 13:39:51 -06004063static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4064{
Paolo Bonzini13893092018-02-26 13:40:09 +01004065 switch (msr->index) {
4066 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4067 if (!nested)
4068 return 1;
4069 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4070 default:
4071 return 1;
4072 }
4073
4074 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004075}
4076
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004077/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 * Reads an msr value (of 'msr_index') into 'pdata'.
4079 * Returns 0 on success, non-0 otherwise.
4080 * Assumes vcpu_load() was already called.
4081 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004084 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004085 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004087 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004088#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004090 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004091 break;
4092 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004093 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004094 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004095 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004096 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004097 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004098#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004099 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004101 case MSR_IA32_SPEC_CTRL:
4102 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004103 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4104 return 1;
4105
4106 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4107 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004108 case MSR_IA32_ARCH_CAPABILITIES:
4109 if (!msr_info->host_initiated &&
4110 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4111 return 1;
4112 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4113 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004115 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004116 break;
4117 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004118 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004119 break;
4120 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004121 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004122 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004123 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004124 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004125 (!msr_info->host_initiated &&
4126 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004127 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004128 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004129 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004130 case MSR_IA32_MCG_EXT_CTL:
4131 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004132 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004133 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004134 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004135 msr_info->data = vcpu->arch.mcg_ext_ctl;
4136 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004137 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004138 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004139 break;
4140 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4141 if (!nested_vmx_allowed(vcpu))
4142 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004143 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4144 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004145 case MSR_IA32_XSS:
4146 if (!vmx_xsaves_supported())
4147 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004148 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004149 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004150 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004151 if (!msr_info->host_initiated &&
4152 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004153 return 1;
4154 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004156 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004157 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004158 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004159 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004161 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162 }
4163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 return 0;
4165}
4166
Jan Kiszkacae50132014-01-04 18:47:22 +01004167static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4168
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169/*
4170 * Writes msr value into into the appropriate "register".
4171 * Returns 0 on success, non-0 otherwise.
4172 * Assumes vcpu_load() was already called.
4173 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004174static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004176 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004177 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004178 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004179 u32 msr_index = msr_info->index;
4180 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004181
Avi Kivity6aa8b732006-12-10 02:21:36 -08004182 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004183 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004184 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004185 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004186#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004188 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 vmcs_writel(GUEST_FS_BASE, data);
4190 break;
4191 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004192 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193 vmcs_writel(GUEST_GS_BASE, data);
4194 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004195 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004196 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004197 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198#endif
4199 case MSR_IA32_SYSENTER_CS:
4200 vmcs_write32(GUEST_SYSENTER_CS, data);
4201 break;
4202 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004203 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204 break;
4205 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004206 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004208 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004209 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004210 (!msr_info->host_initiated &&
4211 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004212 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004213 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004214 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004216 vmcs_write64(GUEST_BNDCFGS, data);
4217 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004218 case MSR_IA32_SPEC_CTRL:
4219 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004220 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4221 return 1;
4222
4223 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004224 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004225 return 1;
4226
4227 vmx->spec_ctrl = data;
4228
4229 if (!data)
4230 break;
4231
4232 /*
4233 * For non-nested:
4234 * When it's written (to non-zero) for the first time, pass
4235 * it through.
4236 *
4237 * For nested:
4238 * The handling of the MSR bitmap for L2 guests is done in
4239 * nested_vmx_merge_msr_bitmap. We should not touch the
4240 * vmcs02.msr_bitmap here since it gets completely overwritten
4241 * in the merging. We update the vmcs01 here for L1 as well
4242 * since it will end up touching the MSR anyway now.
4243 */
4244 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4245 MSR_IA32_SPEC_CTRL,
4246 MSR_TYPE_RW);
4247 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004248 case MSR_IA32_PRED_CMD:
4249 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004250 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4251 return 1;
4252
4253 if (data & ~PRED_CMD_IBPB)
4254 return 1;
4255
4256 if (!data)
4257 break;
4258
4259 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4260
4261 /*
4262 * For non-nested:
4263 * When it's written (to non-zero) for the first time, pass
4264 * it through.
4265 *
4266 * For nested:
4267 * The handling of the MSR bitmap for L2 guests is done in
4268 * nested_vmx_merge_msr_bitmap. We should not touch the
4269 * vmcs02.msr_bitmap here since it gets completely overwritten
4270 * in the merging.
4271 */
4272 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4273 MSR_TYPE_W);
4274 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004275 case MSR_IA32_ARCH_CAPABILITIES:
4276 if (!msr_info->host_initiated)
4277 return 1;
4278 vmx->arch_capabilities = data;
4279 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004280 case MSR_IA32_CR_PAT:
4281 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004282 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4283 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004284 vmcs_write64(GUEST_IA32_PAT, data);
4285 vcpu->arch.pat = data;
4286 break;
4287 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004288 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004289 break;
Will Auldba904632012-11-29 12:42:50 -08004290 case MSR_IA32_TSC_ADJUST:
4291 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004292 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004293 case MSR_IA32_MCG_EXT_CTL:
4294 if ((!msr_info->host_initiated &&
4295 !(to_vmx(vcpu)->msr_ia32_feature_control &
4296 FEATURE_CONTROL_LMCE)) ||
4297 (data & ~MCG_EXT_CTL_LMCE_EN))
4298 return 1;
4299 vcpu->arch.mcg_ext_ctl = data;
4300 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004301 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004302 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004303 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004304 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4305 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004306 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004307 if (msr_info->host_initiated && data == 0)
4308 vmx_leave_nested(vcpu);
4309 break;
4310 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004311 if (!msr_info->host_initiated)
4312 return 1; /* they are read-only */
4313 if (!nested_vmx_allowed(vcpu))
4314 return 1;
4315 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004316 case MSR_IA32_XSS:
4317 if (!vmx_xsaves_supported())
4318 return 1;
4319 /*
4320 * The only supported bit as of Skylake is bit 8, but
4321 * it is not supported on KVM.
4322 */
4323 if (data != 0)
4324 return 1;
4325 vcpu->arch.ia32_xss = data;
4326 if (vcpu->arch.ia32_xss != host_xss)
4327 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004328 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004329 else
4330 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4331 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004332 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004333 if (!msr_info->host_initiated &&
4334 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004335 return 1;
4336 /* Check reserved bit, higher 32 bits should be zero */
4337 if ((data >> 32) != 0)
4338 return 1;
4339 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004340 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004341 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004342 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004343 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004344 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004345 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4346 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004347 ret = kvm_set_shared_msr(msr->index, msr->data,
4348 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004349 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004350 if (ret)
4351 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004352 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004353 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004355 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356 }
4357
Eddie Dong2cc51562007-05-21 07:28:09 +03004358 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359}
4360
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004361static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004363 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4364 switch (reg) {
4365 case VCPU_REGS_RSP:
4366 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4367 break;
4368 case VCPU_REGS_RIP:
4369 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4370 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004371 case VCPU_EXREG_PDPTR:
4372 if (enable_ept)
4373 ept_save_pdptrs(vcpu);
4374 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004375 default:
4376 break;
4377 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378}
4379
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380static __init int cpu_has_kvm_support(void)
4381{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004382 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383}
4384
4385static __init int vmx_disabled_by_bios(void)
4386{
4387 u64 msr;
4388
4389 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004390 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004391 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004392 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4393 && tboot_enabled())
4394 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004395 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004396 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004397 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004398 && !tboot_enabled()) {
4399 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004400 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004401 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004402 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004403 /* launched w/o TXT and VMX disabled */
4404 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4405 && !tboot_enabled())
4406 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004407 }
4408
4409 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410}
4411
Dongxiao Xu7725b892010-05-11 18:29:38 +08004412static void kvm_cpu_vmxon(u64 addr)
4413{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004414 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004415 intel_pt_handle_vmx(1);
4416
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004417 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004418}
4419
Radim Krčmář13a34e02014-08-28 15:13:03 +02004420static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004421{
4422 int cpu = raw_smp_processor_id();
4423 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004424 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004426 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004427 return -EBUSY;
4428
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004429 /*
4430 * This can happen if we hot-added a CPU but failed to allocate
4431 * VP assist page for it.
4432 */
4433 if (static_branch_unlikely(&enable_evmcs) &&
4434 !hv_get_vp_assist_page(cpu))
4435 return -EFAULT;
4436
Nadav Har'Eld462b812011-05-24 15:26:10 +03004437 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004438 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4439 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004440
4441 /*
4442 * Now we can enable the vmclear operation in kdump
4443 * since the loaded_vmcss_on_cpu list on this cpu
4444 * has been initialized.
4445 *
4446 * Though the cpu is not in VMX operation now, there
4447 * is no problem to enable the vmclear operation
4448 * for the loaded_vmcss_on_cpu list is empty!
4449 */
4450 crash_enable_local_vmclear(cpu);
4451
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004453
4454 test_bits = FEATURE_CONTROL_LOCKED;
4455 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4456 if (tboot_enabled())
4457 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4458
4459 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004460 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004461 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4462 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004463 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004464 if (enable_ept)
4465 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004466
4467 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468}
4469
Nadav Har'Eld462b812011-05-24 15:26:10 +03004470static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004471{
4472 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004473 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004474
Nadav Har'Eld462b812011-05-24 15:26:10 +03004475 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4476 loaded_vmcss_on_cpu_link)
4477 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004478}
4479
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004480
4481/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4482 * tricks.
4483 */
4484static void kvm_cpu_vmxoff(void)
4485{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004486 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004487
4488 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004489 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004490}
4491
Radim Krčmář13a34e02014-08-28 15:13:03 +02004492static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004493{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004494 vmclear_local_loaded_vmcss();
4495 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496}
4497
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004498static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004499 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500{
4501 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004502 u32 ctl = ctl_min | ctl_opt;
4503
4504 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4505
4506 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4507 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4508
4509 /* Ensure minimum (required) set of control bits are supported. */
4510 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004511 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004512
4513 *result = ctl;
4514 return 0;
4515}
4516
Avi Kivity110312c2010-12-21 12:54:20 +02004517static __init bool allow_1_setting(u32 msr, u32 ctl)
4518{
4519 u32 vmx_msr_low, vmx_msr_high;
4520
4521 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4522 return vmx_msr_high & ctl;
4523}
4524
Yang, Sheng002c7f72007-07-31 14:23:01 +03004525static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526{
4527 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004528 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004529 u32 _pin_based_exec_control = 0;
4530 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004531 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004532 u32 _vmexit_control = 0;
4533 u32 _vmentry_control = 0;
4534
Paolo Bonzini13893092018-02-26 13:40:09 +01004535 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304536 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004537#ifdef CONFIG_X86_64
4538 CPU_BASED_CR8_LOAD_EXITING |
4539 CPU_BASED_CR8_STORE_EXITING |
4540#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004541 CPU_BASED_CR3_LOAD_EXITING |
4542 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004543 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004544 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004545 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004546 CPU_BASED_MWAIT_EXITING |
4547 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004548 CPU_BASED_INVLPG_EXITING |
4549 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004550
Sheng Yangf78e0e22007-10-29 09:40:42 +08004551 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004552 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004553 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004554 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4555 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004556 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004557#ifdef CONFIG_X86_64
4558 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4559 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4560 ~CPU_BASED_CR8_STORE_EXITING;
4561#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004562 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004563 min2 = 0;
4564 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004565 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004566 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004567 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004568 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004569 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004570 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004571 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004572 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004573 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004574 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004575 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004576 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004577 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004578 SECONDARY_EXEC_RDSEED_EXITING |
4579 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004580 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004581 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004582 SECONDARY_EXEC_ENABLE_VMFUNC |
4583 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004584 if (adjust_vmx_controls(min2, opt2,
4585 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004586 &_cpu_based_2nd_exec_control) < 0)
4587 return -EIO;
4588 }
4589#ifndef CONFIG_X86_64
4590 if (!(_cpu_based_2nd_exec_control &
4591 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4592 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4593#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004594
4595 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4596 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004597 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004598 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4599 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004600
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004601 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4602 &vmx_capability.ept, &vmx_capability.vpid);
4603
Sheng Yangd56f5462008-04-25 10:13:16 +08004604 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004605 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4606 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004607 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4608 CPU_BASED_CR3_STORE_EXITING |
4609 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004610 } else if (vmx_capability.ept) {
4611 vmx_capability.ept = 0;
4612 pr_warn_once("EPT CAP should not exist if not support "
4613 "1-setting enable EPT VM-execution control\n");
4614 }
4615 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4616 vmx_capability.vpid) {
4617 vmx_capability.vpid = 0;
4618 pr_warn_once("VPID CAP should not exist if not support "
4619 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004620 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004621
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004622 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004623#ifdef CONFIG_X86_64
4624 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4625#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004626 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004627 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004628 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4629 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004630 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004632 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4633 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4634 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004635 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4636 &_pin_based_exec_control) < 0)
4637 return -EIO;
4638
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004639 if (cpu_has_broken_vmx_preemption_timer())
4640 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004641 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004642 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004643 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4644
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004645 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004646 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004647 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4648 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004650
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004651 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004652
4653 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4654 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004655 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004656
4657#ifdef CONFIG_X86_64
4658 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4659 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004660 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004661#endif
4662
4663 /* Require Write-Back (WB) memory type for VMCS accesses. */
4664 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004665 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004666
Yang, Sheng002c7f72007-07-31 14:23:01 +03004667 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004668 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004669 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004670
Liran Alon2307af12018-06-29 22:59:04 +03004671 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004672
Yang, Sheng002c7f72007-07-31 14:23:01 +03004673 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4674 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004675 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004676 vmcs_conf->vmexit_ctrl = _vmexit_control;
4677 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004678
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004679 if (static_branch_unlikely(&enable_evmcs))
4680 evmcs_sanitize_exec_ctrls(vmcs_conf);
4681
Avi Kivity110312c2010-12-21 12:54:20 +02004682 cpu_has_load_ia32_efer =
4683 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4684 VM_ENTRY_LOAD_IA32_EFER)
4685 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4686 VM_EXIT_LOAD_IA32_EFER);
4687
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004688 cpu_has_load_perf_global_ctrl =
4689 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4690 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4691 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4692 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4693
4694 /*
4695 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004696 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004697 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4698 *
4699 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4700 *
4701 * AAK155 (model 26)
4702 * AAP115 (model 30)
4703 * AAT100 (model 37)
4704 * BC86,AAY89,BD102 (model 44)
4705 * BA97 (model 46)
4706 *
4707 */
4708 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4709 switch (boot_cpu_data.x86_model) {
4710 case 26:
4711 case 30:
4712 case 37:
4713 case 44:
4714 case 46:
4715 cpu_has_load_perf_global_ctrl = false;
4716 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4717 "does not work properly. Using workaround\n");
4718 break;
4719 default:
4720 break;
4721 }
4722 }
4723
Borislav Petkov782511b2016-04-04 22:25:03 +02004724 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004725 rdmsrl(MSR_IA32_XSS, host_xss);
4726
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004727 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004728}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729
Liran Alon491a6032018-06-23 02:35:12 +03004730static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731{
4732 int node = cpu_to_node(cpu);
4733 struct page *pages;
4734 struct vmcs *vmcs;
4735
Vlastimil Babka96db8002015-09-08 15:03:50 -07004736 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737 if (!pages)
4738 return NULL;
4739 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004740 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004741
4742 /* KVM supports Enlightened VMCS v1 only */
4743 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004744 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004745 else
Liran Alon392b2f22018-06-23 02:35:01 +03004746 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004747
Liran Alon491a6032018-06-23 02:35:12 +03004748 if (shadow)
4749 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004750 return vmcs;
4751}
4752
Avi Kivity6aa8b732006-12-10 02:21:36 -08004753static void free_vmcs(struct vmcs *vmcs)
4754{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004755 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004756}
4757
Nadav Har'Eld462b812011-05-24 15:26:10 +03004758/*
4759 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4760 */
4761static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4762{
4763 if (!loaded_vmcs->vmcs)
4764 return;
4765 loaded_vmcs_clear(loaded_vmcs);
4766 free_vmcs(loaded_vmcs->vmcs);
4767 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004768 if (loaded_vmcs->msr_bitmap)
4769 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004770 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004771}
4772
Liran Alon491a6032018-06-23 02:35:12 +03004773static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004774{
Liran Alon491a6032018-06-23 02:35:12 +03004775 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004776}
4777
4778static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4779{
Liran Alon491a6032018-06-23 02:35:12 +03004780 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004781 if (!loaded_vmcs->vmcs)
4782 return -ENOMEM;
4783
4784 loaded_vmcs->shadow_vmcs = NULL;
4785 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004786
4787 if (cpu_has_vmx_msr_bitmap()) {
4788 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4789 if (!loaded_vmcs->msr_bitmap)
4790 goto out_vmcs;
4791 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004792
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004793 if (IS_ENABLED(CONFIG_HYPERV) &&
4794 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004795 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4796 struct hv_enlightened_vmcs *evmcs =
4797 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4798
4799 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4800 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004801 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004802
4803 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4804
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004805 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004806
4807out_vmcs:
4808 free_loaded_vmcs(loaded_vmcs);
4809 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004810}
4811
Sam Ravnborg39959582007-06-01 00:47:13 -07004812static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004813{
4814 int cpu;
4815
Zachary Amsden3230bb42009-09-29 11:38:37 -10004816 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004818 per_cpu(vmxarea, cpu) = NULL;
4819 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004820}
4821
Jim Mattsond37f4262017-12-22 12:12:16 -08004822enum vmcs_field_width {
4823 VMCS_FIELD_WIDTH_U16 = 0,
4824 VMCS_FIELD_WIDTH_U64 = 1,
4825 VMCS_FIELD_WIDTH_U32 = 2,
4826 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004827};
4828
Jim Mattsond37f4262017-12-22 12:12:16 -08004829static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004830{
4831 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004832 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004833 return (field >> 13) & 0x3 ;
4834}
4835
4836static inline int vmcs_field_readonly(unsigned long field)
4837{
4838 return (((field >> 10) & 0x3) == 1);
4839}
4840
Bandan Dasfe2b2012014-04-21 15:20:14 -04004841static void init_vmcs_shadow_fields(void)
4842{
4843 int i, j;
4844
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004845 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4846 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004847 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004848 (i + 1 == max_shadow_read_only_fields ||
4849 shadow_read_only_fields[i + 1] != field + 1))
4850 pr_err("Missing field from shadow_read_only_field %x\n",
4851 field + 1);
4852
4853 clear_bit(field, vmx_vmread_bitmap);
4854#ifdef CONFIG_X86_64
4855 if (field & 1)
4856 continue;
4857#endif
4858 if (j < i)
4859 shadow_read_only_fields[j] = field;
4860 j++;
4861 }
4862 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004863
4864 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004865 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004866 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004867 (i + 1 == max_shadow_read_write_fields ||
4868 shadow_read_write_fields[i + 1] != field + 1))
4869 pr_err("Missing field from shadow_read_write_field %x\n",
4870 field + 1);
4871
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004872 /*
4873 * PML and the preemption timer can be emulated, but the
4874 * processor cannot vmwrite to fields that don't exist
4875 * on bare metal.
4876 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004877 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004878 case GUEST_PML_INDEX:
4879 if (!cpu_has_vmx_pml())
4880 continue;
4881 break;
4882 case VMX_PREEMPTION_TIMER_VALUE:
4883 if (!cpu_has_vmx_preemption_timer())
4884 continue;
4885 break;
4886 case GUEST_INTR_STATUS:
4887 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004888 continue;
4889 break;
4890 default:
4891 break;
4892 }
4893
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004894 clear_bit(field, vmx_vmwrite_bitmap);
4895 clear_bit(field, vmx_vmread_bitmap);
4896#ifdef CONFIG_X86_64
4897 if (field & 1)
4898 continue;
4899#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004900 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004901 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004902 j++;
4903 }
4904 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004905}
4906
Avi Kivity6aa8b732006-12-10 02:21:36 -08004907static __init int alloc_kvm_area(void)
4908{
4909 int cpu;
4910
Zachary Amsden3230bb42009-09-29 11:38:37 -10004911 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 struct vmcs *vmcs;
4913
Liran Alon491a6032018-06-23 02:35:12 +03004914 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915 if (!vmcs) {
4916 free_kvm_area();
4917 return -ENOMEM;
4918 }
4919
Liran Alon2307af12018-06-29 22:59:04 +03004920 /*
4921 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4922 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4923 * revision_id reported by MSR_IA32_VMX_BASIC.
4924 *
4925 * However, even though not explictly documented by
4926 * TLFS, VMXArea passed as VMXON argument should
4927 * still be marked with revision_id reported by
4928 * physical CPU.
4929 */
4930 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004931 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004932
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933 per_cpu(vmxarea, cpu) = vmcs;
4934 }
4935 return 0;
4936}
4937
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004938static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004939 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004940{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004941 if (!emulate_invalid_guest_state) {
4942 /*
4943 * CS and SS RPL should be equal during guest entry according
4944 * to VMX spec, but in reality it is not always so. Since vcpu
4945 * is in the middle of the transition from real mode to
4946 * protected mode it is safe to assume that RPL 0 is a good
4947 * default value.
4948 */
4949 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004950 save->selector &= ~SEGMENT_RPL_MASK;
4951 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004952 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004954 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955}
4956
4957static void enter_pmode(struct kvm_vcpu *vcpu)
4958{
4959 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004960 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961
Gleb Natapovd99e4152012-12-20 16:57:45 +02004962 /*
4963 * Update real mode segment cache. It may be not up-to-date if sement
4964 * register was written while vcpu was in a guest mode.
4965 */
4966 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4967 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4968 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4969 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4970 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4971 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4972
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004973 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974
Avi Kivity2fb92db2011-04-27 19:42:18 +03004975 vmx_segment_cache_clear(vmx);
4976
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004977 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978
4979 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004980 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4981 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982 vmcs_writel(GUEST_RFLAGS, flags);
4983
Rusty Russell66aee912007-07-17 23:34:16 +10004984 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4985 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986
4987 update_exception_bitmap(vcpu);
4988
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004989 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4990 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4991 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4992 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4993 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4994 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004995}
4996
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004997static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998{
Mathias Krause772e0312012-08-30 01:30:19 +02004999 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02005000 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001
Gleb Natapovd99e4152012-12-20 16:57:45 +02005002 var.dpl = 0x3;
5003 if (seg == VCPU_SREG_CS)
5004 var.type = 0x3;
5005
5006 if (!emulate_invalid_guest_state) {
5007 var.selector = var.base >> 4;
5008 var.base = var.base & 0xffff0;
5009 var.limit = 0xffff;
5010 var.g = 0;
5011 var.db = 0;
5012 var.present = 1;
5013 var.s = 1;
5014 var.l = 0;
5015 var.unusable = 0;
5016 var.type = 0x3;
5017 var.avl = 0;
5018 if (save->base & 0xf)
5019 printk_once(KERN_WARNING "kvm: segment base is not "
5020 "paragraph aligned when entering "
5021 "protected mode (seg=%d)", seg);
5022 }
5023
5024 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005025 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005026 vmcs_write32(sf->limit, var.limit);
5027 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028}
5029
5030static void enter_rmode(struct kvm_vcpu *vcpu)
5031{
5032 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005033 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005034 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005036 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5037 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5038 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5039 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5040 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005041 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5042 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005043
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005044 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045
Gleb Natapov776e58e2011-03-13 12:34:27 +02005046 /*
5047 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005048 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005049 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005050 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005051 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5052 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005053
Avi Kivity2fb92db2011-04-27 19:42:18 +03005054 vmx_segment_cache_clear(vmx);
5055
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005056 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005057 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005058 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5059
5060 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005061 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005062
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005063 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005064
5065 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005066 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005067 update_exception_bitmap(vcpu);
5068
Gleb Natapovd99e4152012-12-20 16:57:45 +02005069 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5070 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5071 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5072 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5073 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5074 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005075
Eddie Dong8668a3c2007-10-10 14:26:45 +08005076 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005077}
5078
Amit Shah401d10d2009-02-20 22:53:37 +05305079static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5080{
5081 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005082 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5083
5084 if (!msr)
5085 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305086
Avi Kivityf6801df2010-01-21 15:31:50 +02005087 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305088 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005089 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305090 msr->data = efer;
5091 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005092 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305093
5094 msr->data = efer & ~EFER_LME;
5095 }
5096 setup_msrs(vmx);
5097}
5098
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005099#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100
5101static void enter_lmode(struct kvm_vcpu *vcpu)
5102{
5103 u32 guest_tr_ar;
5104
Avi Kivity2fb92db2011-04-27 19:42:18 +03005105 vmx_segment_cache_clear(to_vmx(vcpu));
5106
Avi Kivity6aa8b732006-12-10 02:21:36 -08005107 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005108 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005109 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5110 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005111 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005112 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5113 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114 }
Avi Kivityda38f432010-07-06 11:30:49 +03005115 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005116}
5117
5118static void exit_lmode(struct kvm_vcpu *vcpu)
5119{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005120 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005121 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122}
5123
5124#endif
5125
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005126static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5127 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005128{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005129 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005130 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005131 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005132 ept_sync_context(construct_eptp(vcpu,
5133 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005134 } else {
5135 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005136 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005137}
5138
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005139static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005140{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005141 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005142}
5143
Junaid Shahidfaff8752018-06-29 13:10:05 -07005144static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5145{
5146 int vpid = to_vmx(vcpu)->vpid;
5147
5148 if (!vpid_sync_vcpu_addr(vpid, addr))
5149 vpid_sync_context(vpid);
5150
5151 /*
5152 * If VPIDs are not supported or enabled, then the above is a no-op.
5153 * But we don't really need a TLB flush in that case anyway, because
5154 * each VM entry/exit includes an implicit flush when VPID is 0.
5155 */
5156}
5157
Avi Kivitye8467fd2009-12-29 18:43:06 +02005158static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5159{
5160 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5161
5162 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5163 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5164}
5165
Avi Kivityaff48ba2010-12-05 18:56:11 +02005166static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5167{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005168 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005169 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5170 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5171}
5172
Anthony Liguori25c4c272007-04-27 09:29:21 +03005173static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005174{
Avi Kivityfc78f512009-12-07 12:16:48 +02005175 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5176
5177 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5178 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005179}
5180
Sheng Yang14394422008-04-28 12:24:45 +08005181static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5182{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005183 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5184
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005185 if (!test_bit(VCPU_EXREG_PDPTR,
5186 (unsigned long *)&vcpu->arch.regs_dirty))
5187 return;
5188
Sheng Yang14394422008-04-28 12:24:45 +08005189 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005190 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5191 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5192 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5193 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005194 }
5195}
5196
Avi Kivity8f5d5492009-05-31 18:41:29 +03005197static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5198{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005199 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5200
Avi Kivity8f5d5492009-05-31 18:41:29 +03005201 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005202 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5203 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5204 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5205 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005206 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005207
5208 __set_bit(VCPU_EXREG_PDPTR,
5209 (unsigned long *)&vcpu->arch.regs_avail);
5210 __set_bit(VCPU_EXREG_PDPTR,
5211 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005212}
5213
David Matlack38991522016-11-29 18:14:08 -08005214static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5215{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005216 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5217 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005218 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5219
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005220 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005221 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5222 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5223 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5224
5225 return fixed_bits_valid(val, fixed0, fixed1);
5226}
5227
5228static bool nested_host_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
5233 return fixed_bits_valid(val, fixed0, fixed1);
5234}
5235
5236static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5237{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005238 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5239 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005240
5241 return fixed_bits_valid(val, fixed0, fixed1);
5242}
5243
5244/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5245#define nested_guest_cr4_valid nested_cr4_valid
5246#define nested_host_cr4_valid nested_cr4_valid
5247
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005248static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005249
5250static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5251 unsigned long cr0,
5252 struct kvm_vcpu *vcpu)
5253{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005254 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5255 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005256 if (!(cr0 & X86_CR0_PG)) {
5257 /* From paging/starting to nonpaging */
5258 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005259 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005260 (CPU_BASED_CR3_LOAD_EXITING |
5261 CPU_BASED_CR3_STORE_EXITING));
5262 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005263 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005264 } else if (!is_paging(vcpu)) {
5265 /* From nonpaging to paging */
5266 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005267 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005268 ~(CPU_BASED_CR3_LOAD_EXITING |
5269 CPU_BASED_CR3_STORE_EXITING));
5270 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005271 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005272 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005273
5274 if (!(cr0 & X86_CR0_WP))
5275 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005276}
5277
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5279{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005280 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005281 unsigned long hw_cr0;
5282
Sean Christopherson3de63472018-07-13 08:42:30 -07005283 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005284 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005285 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005286 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005287 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005288
Gleb Natapov218e7632013-01-21 15:36:45 +02005289 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5290 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291
Gleb Natapov218e7632013-01-21 15:36:45 +02005292 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5293 enter_rmode(vcpu);
5294 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005295
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005296#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005297 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005298 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005299 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005300 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301 exit_lmode(vcpu);
5302 }
5303#endif
5304
Sean Christophersonb4d18512018-03-05 12:04:40 -08005305 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005306 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5307
Avi Kivity6aa8b732006-12-10 02:21:36 -08005308 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005309 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005310 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005311
5312 /* depends on vcpu->arch.cr0 to be set to a new value */
5313 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314}
5315
Yu Zhang855feb62017-08-24 20:27:55 +08005316static int get_ept_level(struct kvm_vcpu *vcpu)
5317{
5318 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5319 return 5;
5320 return 4;
5321}
5322
Peter Feiner995f00a2017-06-30 17:26:32 -07005323static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005324{
Yu Zhang855feb62017-08-24 20:27:55 +08005325 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005326
Yu Zhang855feb62017-08-24 20:27:55 +08005327 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005328
Peter Feiner995f00a2017-06-30 17:26:32 -07005329 if (enable_ept_ad_bits &&
5330 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005331 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005332 eptp |= (root_hpa & PAGE_MASK);
5333
5334 return eptp;
5335}
5336
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5338{
Tianyu Lan877ad952018-07-19 08:40:23 +00005339 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005340 unsigned long guest_cr3;
5341 u64 eptp;
5342
5343 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005344 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005345 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005346 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005347
5348 if (kvm_x86_ops->tlb_remote_flush) {
5349 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5350 to_vmx(vcpu)->ept_pointer = eptp;
5351 to_kvm_vmx(kvm)->ept_pointers_match
5352 = EPT_POINTERS_CHECK;
5353 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5354 }
5355
Sean Christophersone90008d2018-03-05 12:04:37 -08005356 if (enable_unrestricted_guest || is_paging(vcpu) ||
5357 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005358 guest_cr3 = kvm_read_cr3(vcpu);
5359 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005360 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005361 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005362 }
5363
Sheng Yang14394422008-04-28 12:24:45 +08005364 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005365}
5366
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005367static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005368{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005369 /*
5370 * Pass through host's Machine Check Enable value to hw_cr4, which
5371 * is in force while we are in guest mode. Do not let guests control
5372 * this bit, even if host CR4.MCE == 0.
5373 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005374 unsigned long hw_cr4;
5375
5376 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5377 if (enable_unrestricted_guest)
5378 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5379 else if (to_vmx(vcpu)->rmode.vm86_active)
5380 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5381 else
5382 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005383
Sean Christopherson64f7a112018-04-30 10:01:06 -07005384 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5385 if (cr4 & X86_CR4_UMIP) {
5386 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005387 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005388 hw_cr4 &= ~X86_CR4_UMIP;
5389 } else if (!is_guest_mode(vcpu) ||
5390 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5391 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5392 SECONDARY_EXEC_DESC);
5393 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005394
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005395 if (cr4 & X86_CR4_VMXE) {
5396 /*
5397 * To use VMXON (and later other VMX instructions), a guest
5398 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5399 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005400 * is here. We operate under the default treatment of SMM,
5401 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005402 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005403 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005404 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005405 }
David Matlack38991522016-11-29 18:14:08 -08005406
5407 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005408 return 1;
5409
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005410 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005411
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005412 if (!enable_unrestricted_guest) {
5413 if (enable_ept) {
5414 if (!is_paging(vcpu)) {
5415 hw_cr4 &= ~X86_CR4_PAE;
5416 hw_cr4 |= X86_CR4_PSE;
5417 } else if (!(cr4 & X86_CR4_PAE)) {
5418 hw_cr4 &= ~X86_CR4_PAE;
5419 }
5420 }
5421
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005422 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005423 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5424 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5425 * to be manually disabled when guest switches to non-paging
5426 * mode.
5427 *
5428 * If !enable_unrestricted_guest, the CPU is always running
5429 * with CR0.PG=1 and CR4 needs to be modified.
5430 * If enable_unrestricted_guest, the CPU automatically
5431 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005432 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005433 if (!is_paging(vcpu))
5434 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5435 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005436
Sheng Yang14394422008-04-28 12:24:45 +08005437 vmcs_writel(CR4_READ_SHADOW, cr4);
5438 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005439 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005440}
5441
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442static void vmx_get_segment(struct kvm_vcpu *vcpu,
5443 struct kvm_segment *var, int seg)
5444{
Avi Kivitya9179492011-01-03 14:28:52 +02005445 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446 u32 ar;
5447
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005448 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005449 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005450 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005451 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005452 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005453 var->base = vmx_read_guest_seg_base(vmx, seg);
5454 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5455 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005456 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005457 var->base = vmx_read_guest_seg_base(vmx, seg);
5458 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5459 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5460 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005461 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462 var->type = ar & 15;
5463 var->s = (ar >> 4) & 1;
5464 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005465 /*
5466 * Some userspaces do not preserve unusable property. Since usable
5467 * segment has to be present according to VMX spec we can use present
5468 * property to amend userspace bug by making unusable segment always
5469 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5470 * segment as unusable.
5471 */
5472 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005473 var->avl = (ar >> 12) & 1;
5474 var->l = (ar >> 13) & 1;
5475 var->db = (ar >> 14) & 1;
5476 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477}
5478
Avi Kivitya9179492011-01-03 14:28:52 +02005479static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5480{
Avi Kivitya9179492011-01-03 14:28:52 +02005481 struct kvm_segment s;
5482
5483 if (to_vmx(vcpu)->rmode.vm86_active) {
5484 vmx_get_segment(vcpu, &s, seg);
5485 return s.base;
5486 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005487 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005488}
5489
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005490static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005491{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005492 struct vcpu_vmx *vmx = to_vmx(vcpu);
5493
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005494 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005495 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005496 else {
5497 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005498 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005499 }
Avi Kivity69c73022011-03-07 15:26:44 +02005500}
5501
Avi Kivity653e3102007-05-07 10:55:37 +03005502static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005503{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005504 u32 ar;
5505
Avi Kivityf0495f92012-06-07 17:06:10 +03005506 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005507 ar = 1 << 16;
5508 else {
5509 ar = var->type & 15;
5510 ar |= (var->s & 1) << 4;
5511 ar |= (var->dpl & 3) << 5;
5512 ar |= (var->present & 1) << 7;
5513 ar |= (var->avl & 1) << 12;
5514 ar |= (var->l & 1) << 13;
5515 ar |= (var->db & 1) << 14;
5516 ar |= (var->g & 1) << 15;
5517 }
Avi Kivity653e3102007-05-07 10:55:37 +03005518
5519 return ar;
5520}
5521
5522static void vmx_set_segment(struct kvm_vcpu *vcpu,
5523 struct kvm_segment *var, int seg)
5524{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005525 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005526 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005527
Avi Kivity2fb92db2011-04-27 19:42:18 +03005528 vmx_segment_cache_clear(vmx);
5529
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005530 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5531 vmx->rmode.segs[seg] = *var;
5532 if (seg == VCPU_SREG_TR)
5533 vmcs_write16(sf->selector, var->selector);
5534 else if (var->s)
5535 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005536 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005537 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005538
Avi Kivity653e3102007-05-07 10:55:37 +03005539 vmcs_writel(sf->base, var->base);
5540 vmcs_write32(sf->limit, var->limit);
5541 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005542
5543 /*
5544 * Fix the "Accessed" bit in AR field of segment registers for older
5545 * qemu binaries.
5546 * IA32 arch specifies that at the time of processor reset the
5547 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005548 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005549 * state vmexit when "unrestricted guest" mode is turned on.
5550 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5551 * tree. Newer qemu binaries with that qemu fix would not need this
5552 * kvm hack.
5553 */
5554 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005555 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005556
Gleb Natapovf924d662012-12-12 19:10:55 +02005557 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005558
5559out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005560 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561}
5562
Avi Kivity6aa8b732006-12-10 02:21:36 -08005563static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5564{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005565 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566
5567 *db = (ar >> 14) & 1;
5568 *l = (ar >> 13) & 1;
5569}
5570
Gleb Natapov89a27f42010-02-16 10:51:48 +02005571static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005573 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5574 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575}
5576
Gleb Natapov89a27f42010-02-16 10:51:48 +02005577static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005578{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005579 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5580 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005581}
5582
Gleb Natapov89a27f42010-02-16 10:51:48 +02005583static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005584{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005585 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5586 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005587}
5588
Gleb Natapov89a27f42010-02-16 10:51:48 +02005589static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005590{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005591 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5592 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005593}
5594
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005595static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5596{
5597 struct kvm_segment var;
5598 u32 ar;
5599
5600 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005601 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005602 if (seg == VCPU_SREG_CS)
5603 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005604 ar = vmx_segment_access_rights(&var);
5605
5606 if (var.base != (var.selector << 4))
5607 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005608 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005609 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005610 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005611 return false;
5612
5613 return true;
5614}
5615
5616static bool code_segment_valid(struct kvm_vcpu *vcpu)
5617{
5618 struct kvm_segment cs;
5619 unsigned int cs_rpl;
5620
5621 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005622 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005623
Avi Kivity1872a3f2009-01-04 23:26:52 +02005624 if (cs.unusable)
5625 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005626 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005627 return false;
5628 if (!cs.s)
5629 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005630 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005631 if (cs.dpl > cs_rpl)
5632 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005633 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005634 if (cs.dpl != cs_rpl)
5635 return false;
5636 }
5637 if (!cs.present)
5638 return false;
5639
5640 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5641 return true;
5642}
5643
5644static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5645{
5646 struct kvm_segment ss;
5647 unsigned int ss_rpl;
5648
5649 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005650 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005651
Avi Kivity1872a3f2009-01-04 23:26:52 +02005652 if (ss.unusable)
5653 return true;
5654 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005655 return false;
5656 if (!ss.s)
5657 return false;
5658 if (ss.dpl != ss_rpl) /* DPL != RPL */
5659 return false;
5660 if (!ss.present)
5661 return false;
5662
5663 return true;
5664}
5665
5666static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5667{
5668 struct kvm_segment var;
5669 unsigned int rpl;
5670
5671 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005672 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005673
Avi Kivity1872a3f2009-01-04 23:26:52 +02005674 if (var.unusable)
5675 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005676 if (!var.s)
5677 return false;
5678 if (!var.present)
5679 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005680 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005681 if (var.dpl < rpl) /* DPL < RPL */
5682 return false;
5683 }
5684
5685 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5686 * rights flags
5687 */
5688 return true;
5689}
5690
5691static bool tr_valid(struct kvm_vcpu *vcpu)
5692{
5693 struct kvm_segment tr;
5694
5695 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5696
Avi Kivity1872a3f2009-01-04 23:26:52 +02005697 if (tr.unusable)
5698 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005699 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005700 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005701 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005702 return false;
5703 if (!tr.present)
5704 return false;
5705
5706 return true;
5707}
5708
5709static bool ldtr_valid(struct kvm_vcpu *vcpu)
5710{
5711 struct kvm_segment ldtr;
5712
5713 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5714
Avi Kivity1872a3f2009-01-04 23:26:52 +02005715 if (ldtr.unusable)
5716 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005717 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005718 return false;
5719 if (ldtr.type != 2)
5720 return false;
5721 if (!ldtr.present)
5722 return false;
5723
5724 return true;
5725}
5726
5727static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5728{
5729 struct kvm_segment cs, ss;
5730
5731 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5732 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5733
Nadav Amitb32a9912015-03-29 16:33:04 +03005734 return ((cs.selector & SEGMENT_RPL_MASK) ==
5735 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005736}
5737
5738/*
5739 * Check if guest state is valid. Returns true if valid, false if
5740 * not.
5741 * We assume that registers are always usable
5742 */
5743static bool guest_state_valid(struct kvm_vcpu *vcpu)
5744{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005745 if (enable_unrestricted_guest)
5746 return true;
5747
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005748 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005749 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005750 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5751 return false;
5752 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5753 return false;
5754 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5755 return false;
5756 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5757 return false;
5758 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5759 return false;
5760 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5761 return false;
5762 } else {
5763 /* protected mode guest state checks */
5764 if (!cs_ss_rpl_check(vcpu))
5765 return false;
5766 if (!code_segment_valid(vcpu))
5767 return false;
5768 if (!stack_segment_valid(vcpu))
5769 return false;
5770 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5771 return false;
5772 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5773 return false;
5774 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5775 return false;
5776 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5777 return false;
5778 if (!tr_valid(vcpu))
5779 return false;
5780 if (!ldtr_valid(vcpu))
5781 return false;
5782 }
5783 /* TODO:
5784 * - Add checks on RIP
5785 * - Add checks on RFLAGS
5786 */
5787
5788 return true;
5789}
5790
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005791static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5792{
5793 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5794}
5795
Mike Dayd77c26f2007-10-08 09:02:08 -04005796static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005797{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005798 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005799 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005800 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005801
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005802 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005803 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005804 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5805 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005806 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005807 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005808 r = kvm_write_guest_page(kvm, fn++, &data,
5809 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005810 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005811 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005812 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5813 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005814 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005815 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5816 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005817 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005818 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005819 r = kvm_write_guest_page(kvm, fn, &data,
5820 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5821 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005822out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005823 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005824 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005825}
5826
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005827static int init_rmode_identity_map(struct kvm *kvm)
5828{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005829 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005830 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005831 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005832 u32 tmp;
5833
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005834 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005835 mutex_lock(&kvm->slots_lock);
5836
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005837 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005838 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005839
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005840 if (!kvm_vmx->ept_identity_map_addr)
5841 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5842 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005843
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005844 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005845 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005846 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005847 goto out2;
5848
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005849 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005850 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5851 if (r < 0)
5852 goto out;
5853 /* Set up identity-mapping pagetable for EPT in real mode */
5854 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5855 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5856 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5857 r = kvm_write_guest_page(kvm, identity_map_pfn,
5858 &tmp, i * sizeof(tmp), sizeof(tmp));
5859 if (r < 0)
5860 goto out;
5861 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005862 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005863
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005864out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005865 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005866
5867out2:
5868 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005869 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005870}
5871
Avi Kivity6aa8b732006-12-10 02:21:36 -08005872static void seg_setup(int seg)
5873{
Mathias Krause772e0312012-08-30 01:30:19 +02005874 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005875 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005876
5877 vmcs_write16(sf->selector, 0);
5878 vmcs_writel(sf->base, 0);
5879 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005880 ar = 0x93;
5881 if (seg == VCPU_SREG_CS)
5882 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005883
5884 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005885}
5886
Sheng Yangf78e0e22007-10-29 09:40:42 +08005887static int alloc_apic_access_page(struct kvm *kvm)
5888{
Xiao Guangrong44841412012-09-07 14:14:20 +08005889 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005890 int r = 0;
5891
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005892 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005893 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005894 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005895 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5896 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005897 if (r)
5898 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005899
Tang Chen73a6d942014-09-11 13:38:00 +08005900 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005901 if (is_error_page(page)) {
5902 r = -EFAULT;
5903 goto out;
5904 }
5905
Tang Chenc24ae0d2014-09-24 15:57:58 +08005906 /*
5907 * Do not pin the page in memory, so that memory hot-unplug
5908 * is able to migrate it.
5909 */
5910 put_page(page);
5911 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005912out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005913 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005914 return r;
5915}
5916
Wanpeng Li991e7a02015-09-16 17:30:05 +08005917static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005918{
5919 int vpid;
5920
Avi Kivity919818a2009-03-23 18:01:29 +02005921 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005922 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005923 spin_lock(&vmx_vpid_lock);
5924 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005925 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005926 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005927 else
5928 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005929 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005930 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005931}
5932
Wanpeng Li991e7a02015-09-16 17:30:05 +08005933static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005934{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005935 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005936 return;
5937 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005938 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005939 spin_unlock(&vmx_vpid_lock);
5940}
5941
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005942static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5943 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005944{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005945 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005946
5947 if (!cpu_has_vmx_msr_bitmap())
5948 return;
5949
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005950 if (static_branch_unlikely(&enable_evmcs))
5951 evmcs_touch_msr_bitmap();
5952
Sheng Yang25c5f222008-03-28 13:18:56 +08005953 /*
5954 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5955 * have the write-low and read-high bitmap offsets the wrong way round.
5956 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5957 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005958 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005959 if (type & MSR_TYPE_R)
5960 /* read-low */
5961 __clear_bit(msr, msr_bitmap + 0x000 / f);
5962
5963 if (type & MSR_TYPE_W)
5964 /* write-low */
5965 __clear_bit(msr, msr_bitmap + 0x800 / f);
5966
Sheng Yang25c5f222008-03-28 13:18:56 +08005967 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5968 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005969 if (type & MSR_TYPE_R)
5970 /* read-high */
5971 __clear_bit(msr, msr_bitmap + 0x400 / f);
5972
5973 if (type & MSR_TYPE_W)
5974 /* write-high */
5975 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5976
5977 }
5978}
5979
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005980static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5981 u32 msr, int type)
5982{
5983 int f = sizeof(unsigned long);
5984
5985 if (!cpu_has_vmx_msr_bitmap())
5986 return;
5987
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005988 if (static_branch_unlikely(&enable_evmcs))
5989 evmcs_touch_msr_bitmap();
5990
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005991 /*
5992 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5993 * have the write-low and read-high bitmap offsets the wrong way round.
5994 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5995 */
5996 if (msr <= 0x1fff) {
5997 if (type & MSR_TYPE_R)
5998 /* read-low */
5999 __set_bit(msr, msr_bitmap + 0x000 / f);
6000
6001 if (type & MSR_TYPE_W)
6002 /* write-low */
6003 __set_bit(msr, msr_bitmap + 0x800 / f);
6004
6005 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6006 msr &= 0x1fff;
6007 if (type & MSR_TYPE_R)
6008 /* read-high */
6009 __set_bit(msr, msr_bitmap + 0x400 / f);
6010
6011 if (type & MSR_TYPE_W)
6012 /* write-high */
6013 __set_bit(msr, msr_bitmap + 0xc00 / f);
6014
6015 }
6016}
6017
6018static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6019 u32 msr, int type, bool value)
6020{
6021 if (value)
6022 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6023 else
6024 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6025}
6026
Wincy Vanf2b93282015-02-03 23:56:03 +08006027/*
6028 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6029 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6030 */
6031static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6032 unsigned long *msr_bitmap_nested,
6033 u32 msr, int type)
6034{
6035 int f = sizeof(unsigned long);
6036
Wincy Vanf2b93282015-02-03 23:56:03 +08006037 /*
6038 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6039 * have the write-low and read-high bitmap offsets the wrong way round.
6040 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6041 */
6042 if (msr <= 0x1fff) {
6043 if (type & MSR_TYPE_R &&
6044 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6045 /* read-low */
6046 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6047
6048 if (type & MSR_TYPE_W &&
6049 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6050 /* write-low */
6051 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6052
6053 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6054 msr &= 0x1fff;
6055 if (type & MSR_TYPE_R &&
6056 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6057 /* read-high */
6058 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6059
6060 if (type & MSR_TYPE_W &&
6061 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6062 /* write-high */
6063 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6064
6065 }
6066}
6067
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006068static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006069{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006070 u8 mode = 0;
6071
6072 if (cpu_has_secondary_exec_ctrls() &&
6073 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6074 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6075 mode |= MSR_BITMAP_MODE_X2APIC;
6076 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6077 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6078 }
6079
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006080 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006081}
6082
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006083#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6084
6085static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6086 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006087{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006088 int msr;
6089
6090 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6091 unsigned word = msr / BITS_PER_LONG;
6092 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6093 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006094 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006095
6096 if (mode & MSR_BITMAP_MODE_X2APIC) {
6097 /*
6098 * TPR reads and writes can be virtualized even if virtual interrupt
6099 * delivery is not in use.
6100 */
6101 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6102 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6103 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6104 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6105 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6106 }
6107 }
6108}
6109
6110static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6111{
6112 struct vcpu_vmx *vmx = to_vmx(vcpu);
6113 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6114 u8 mode = vmx_msr_bitmap_mode(vcpu);
6115 u8 changed = mode ^ vmx->msr_bitmap_mode;
6116
6117 if (!changed)
6118 return;
6119
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006120 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6121 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6122
6123 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006124}
6125
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006126static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006127{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006128 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006129}
6130
David Matlackc9f04402017-08-01 14:00:40 -07006131static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6132{
6133 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6134 gfn_t gfn;
6135
6136 /*
6137 * Don't need to mark the APIC access page dirty; it is never
6138 * written to by the CPU during APIC virtualization.
6139 */
6140
6141 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6142 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6143 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6144 }
6145
6146 if (nested_cpu_has_posted_intr(vmcs12)) {
6147 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6148 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6149 }
6150}
6151
6152
David Hildenbrand6342c502017-01-25 11:58:58 +01006153static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006154{
6155 struct vcpu_vmx *vmx = to_vmx(vcpu);
6156 int max_irr;
6157 void *vapic_page;
6158 u16 status;
6159
David Matlackc9f04402017-08-01 14:00:40 -07006160 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6161 return;
Wincy Van705699a2015-02-03 23:58:17 +08006162
David Matlackc9f04402017-08-01 14:00:40 -07006163 vmx->nested.pi_pending = false;
6164 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6165 return;
Wincy Van705699a2015-02-03 23:58:17 +08006166
David Matlackc9f04402017-08-01 14:00:40 -07006167 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6168 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006169 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006170 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6171 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006172 kunmap(vmx->nested.virtual_apic_page);
6173
6174 status = vmcs_read16(GUEST_INTR_STATUS);
6175 if ((u8)max_irr > ((u8)status & 0xff)) {
6176 status &= ~0xff;
6177 status |= (u8)max_irr;
6178 vmcs_write16(GUEST_INTR_STATUS, status);
6179 }
6180 }
David Matlackc9f04402017-08-01 14:00:40 -07006181
6182 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006183}
6184
Paolo Bonzini7e712682018-10-03 13:44:26 +02006185static u8 vmx_get_rvi(void)
6186{
6187 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6188}
6189
Liran Alone6c67d82018-09-04 10:56:52 +03006190static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6191{
6192 struct vcpu_vmx *vmx = to_vmx(vcpu);
6193 void *vapic_page;
6194 u32 vppr;
6195 int rvi;
6196
6197 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6198 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6199 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6200 return false;
6201
Paolo Bonzini7e712682018-10-03 13:44:26 +02006202 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006203
6204 vapic_page = kmap(vmx->nested.virtual_apic_page);
6205 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6206 kunmap(vmx->nested.virtual_apic_page);
6207
6208 return ((rvi & 0xf0) > (vppr & 0xf0));
6209}
6210
Wincy Van06a55242017-04-28 13:13:59 +08006211static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6212 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006213{
6214#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006215 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6216
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006217 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006218 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006219 * The vector of interrupt to be delivered to vcpu had
6220 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006221 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006222 * Following cases will be reached in this block, and
6223 * we always send a notification event in all cases as
6224 * explained below.
6225 *
6226 * Case 1: vcpu keeps in non-root mode. Sending a
6227 * notification event posts the interrupt to vcpu.
6228 *
6229 * Case 2: vcpu exits to root mode and is still
6230 * runnable. PIR will be synced to vIRR before the
6231 * next vcpu entry. Sending a notification event in
6232 * this case has no effect, as vcpu is not in root
6233 * mode.
6234 *
6235 * Case 3: vcpu exits to root mode and is blocked.
6236 * vcpu_block() has already synced PIR to vIRR and
6237 * never blocks vcpu if vIRR is not cleared. Therefore,
6238 * a blocked vcpu here does not wait for any requested
6239 * interrupts in PIR, and sending a notification event
6240 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006241 */
Feng Wu28b835d2015-09-18 22:29:54 +08006242
Wincy Van06a55242017-04-28 13:13:59 +08006243 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006244 return true;
6245 }
6246#endif
6247 return false;
6248}
6249
Wincy Van705699a2015-02-03 23:58:17 +08006250static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6251 int vector)
6252{
6253 struct vcpu_vmx *vmx = to_vmx(vcpu);
6254
6255 if (is_guest_mode(vcpu) &&
6256 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006257 /*
6258 * If a posted intr is not recognized by hardware,
6259 * we will accomplish it in the next vmentry.
6260 */
6261 vmx->nested.pi_pending = true;
6262 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006263 /* the PIR and ON have been set by L1. */
6264 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6265 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006266 return 0;
6267 }
6268 return -1;
6269}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006270/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006271 * Send interrupt to vcpu via posted interrupt way.
6272 * 1. If target vcpu is running(non-root mode), send posted interrupt
6273 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6274 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6275 * interrupt from PIR in next vmentry.
6276 */
6277static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6278{
6279 struct vcpu_vmx *vmx = to_vmx(vcpu);
6280 int r;
6281
Wincy Van705699a2015-02-03 23:58:17 +08006282 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6283 if (!r)
6284 return;
6285
Yang Zhanga20ed542013-04-11 19:25:15 +08006286 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6287 return;
6288
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006289 /* If a previous notification has sent the IPI, nothing to do. */
6290 if (pi_test_and_set_on(&vmx->pi_desc))
6291 return;
6292
Wincy Van06a55242017-04-28 13:13:59 +08006293 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006294 kvm_vcpu_kick(vcpu);
6295}
6296
Avi Kivity6aa8b732006-12-10 02:21:36 -08006297/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006298 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6299 * will not change in the lifetime of the guest.
6300 * Note that host-state that does change is set elsewhere. E.g., host-state
6301 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6302 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006303static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006304{
6305 u32 low32, high32;
6306 unsigned long tmpl;
6307 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006308 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006309
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006310 cr0 = read_cr0();
6311 WARN_ON(cr0 & X86_CR0_TS);
6312 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006313
6314 /*
6315 * Save the most likely value for this task's CR3 in the VMCS.
6316 * We can't use __get_current_cr3_fast() because we're not atomic.
6317 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006318 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006319 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006320 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006321
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006322 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006323 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006324 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006325 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006326
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006327 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006328#ifdef CONFIG_X86_64
6329 /*
6330 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006331 * vmx_prepare_switch_to_host(), in case userspace uses
6332 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006333 */
6334 vmcs_write16(HOST_DS_SELECTOR, 0);
6335 vmcs_write16(HOST_ES_SELECTOR, 0);
6336#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006337 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6338 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006339#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006340 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6341 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6342
Juergen Gross87930012017-09-04 12:25:27 +02006343 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006344 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006345 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006346
Avi Kivity83287ea422012-09-16 15:10:57 +03006347 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006348
6349 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6350 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6351 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6352 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6353
6354 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6355 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6356 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6357 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006358
6359 if (cpu_has_load_ia32_efer)
6360 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006361}
6362
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006363static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6364{
6365 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6366 if (enable_ept)
6367 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006368 if (is_guest_mode(&vmx->vcpu))
6369 vmx->vcpu.arch.cr4_guest_owned_bits &=
6370 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006371 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6372}
6373
Yang Zhang01e439b2013-04-11 19:25:12 +08006374static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6375{
6376 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6377
Andrey Smetanind62caab2015-11-10 15:36:33 +03006378 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006379 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006380
6381 if (!enable_vnmi)
6382 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6383
Yunhong Jiang64672c92016-06-13 14:19:59 -07006384 /* Enable the preemption timer dynamically */
6385 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006386 return pin_based_exec_ctrl;
6387}
6388
Andrey Smetanind62caab2015-11-10 15:36:33 +03006389static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6390{
6391 struct vcpu_vmx *vmx = to_vmx(vcpu);
6392
6393 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006394 if (cpu_has_secondary_exec_ctrls()) {
6395 if (kvm_vcpu_apicv_active(vcpu))
6396 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6397 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6398 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6399 else
6400 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6401 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6402 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6403 }
6404
6405 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006406 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006407}
6408
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006409static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6410{
6411 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006412
6413 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6414 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6415
Paolo Bonzini35754c92015-07-29 12:05:37 +02006416 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006417 exec_control &= ~CPU_BASED_TPR_SHADOW;
6418#ifdef CONFIG_X86_64
6419 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6420 CPU_BASED_CR8_LOAD_EXITING;
6421#endif
6422 }
6423 if (!enable_ept)
6424 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6425 CPU_BASED_CR3_LOAD_EXITING |
6426 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006427 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6428 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6429 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006430 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6431 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006432 return exec_control;
6433}
6434
Jim Mattson45ec3682017-08-23 16:32:04 -07006435static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006436{
Jim Mattson45ec3682017-08-23 16:32:04 -07006437 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006438 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006439}
6440
Jim Mattson75f4fc82017-08-23 16:32:03 -07006441static bool vmx_rdseed_supported(void)
6442{
6443 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006444 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006445}
6446
Paolo Bonzini80154d72017-08-24 13:55:35 +02006447static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006448{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006449 struct kvm_vcpu *vcpu = &vmx->vcpu;
6450
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006451 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006452
Paolo Bonzini80154d72017-08-24 13:55:35 +02006453 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006454 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6455 if (vmx->vpid == 0)
6456 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6457 if (!enable_ept) {
6458 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6459 enable_unrestricted_guest = 0;
6460 }
6461 if (!enable_unrestricted_guest)
6462 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006463 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006464 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006465 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006466 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6467 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006468 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006469
6470 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6471 * in vmx_set_cr4. */
6472 exec_control &= ~SECONDARY_EXEC_DESC;
6473
Abel Gordonabc4fc52013-04-18 14:35:25 +03006474 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6475 (handle_vmptrld).
6476 We can NOT enable shadow_vmcs here because we don't have yet
6477 a current VMCS12
6478 */
6479 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006480
6481 if (!enable_pml)
6482 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006483
Paolo Bonzini3db13482017-08-24 14:48:03 +02006484 if (vmx_xsaves_supported()) {
6485 /* Exposing XSAVES only when XSAVE is exposed */
6486 bool xsaves_enabled =
6487 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6488 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6489
6490 if (!xsaves_enabled)
6491 exec_control &= ~SECONDARY_EXEC_XSAVES;
6492
6493 if (nested) {
6494 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006495 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006496 SECONDARY_EXEC_XSAVES;
6497 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006498 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006499 ~SECONDARY_EXEC_XSAVES;
6500 }
6501 }
6502
Paolo Bonzini80154d72017-08-24 13:55:35 +02006503 if (vmx_rdtscp_supported()) {
6504 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6505 if (!rdtscp_enabled)
6506 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6507
6508 if (nested) {
6509 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006510 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006511 SECONDARY_EXEC_RDTSCP;
6512 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006513 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006514 ~SECONDARY_EXEC_RDTSCP;
6515 }
6516 }
6517
6518 if (vmx_invpcid_supported()) {
6519 /* Exposing INVPCID only when PCID is exposed */
6520 bool invpcid_enabled =
6521 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6522 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6523
6524 if (!invpcid_enabled) {
6525 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6526 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6527 }
6528
6529 if (nested) {
6530 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006531 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006532 SECONDARY_EXEC_ENABLE_INVPCID;
6533 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006534 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006535 ~SECONDARY_EXEC_ENABLE_INVPCID;
6536 }
6537 }
6538
Jim Mattson45ec3682017-08-23 16:32:04 -07006539 if (vmx_rdrand_supported()) {
6540 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6541 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006542 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006543
6544 if (nested) {
6545 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006546 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006547 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006548 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006549 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006550 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006551 }
6552 }
6553
Jim Mattson75f4fc82017-08-23 16:32:03 -07006554 if (vmx_rdseed_supported()) {
6555 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6556 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006557 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006558
6559 if (nested) {
6560 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006561 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006562 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006563 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006564 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006565 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006566 }
6567 }
6568
Paolo Bonzini80154d72017-08-24 13:55:35 +02006569 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006570}
6571
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006572static void ept_set_mmio_spte_mask(void)
6573{
6574 /*
6575 * EPT Misconfigurations can be generated if the value of bits 2:0
6576 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006577 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006578 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6579 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006580}
6581
Wanpeng Lif53cd632014-12-02 19:14:58 +08006582#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006583/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006584 * Sets up the vmcs for emulated real mode.
6585 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006586static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006587{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006588 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006589
Abel Gordon4607c2d2013-04-18 14:35:55 +03006590 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006591 /*
6592 * At vCPU creation, "VMWRITE to any supported field
6593 * in the VMCS" is supported, so use the more
6594 * permissive vmx_vmread_bitmap to specify both read
6595 * and write permissions for the shadow VMCS.
6596 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006597 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006598 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006599 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006600 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006601 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006602
Avi Kivity6aa8b732006-12-10 02:21:36 -08006603 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6604
Avi Kivity6aa8b732006-12-10 02:21:36 -08006605 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006606 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006607 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006608
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006609 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006610
Dan Williamsdfa169b2016-06-02 11:17:24 -07006611 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006612 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006613 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006614 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006615 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006616
Andrey Smetanind62caab2015-11-10 15:36:33 +03006617 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006618 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6619 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6620 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6621 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6622
6623 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006624
Li RongQing0bcf2612015-12-03 13:29:34 +08006625 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006626 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006627 }
6628
Wanpeng Lib31c1142018-03-12 04:53:04 -07006629 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006630 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006631 vmx->ple_window = ple_window;
6632 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006633 }
6634
Xiao Guangrongc3707952011-07-12 03:28:04 +08006635 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6636 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6638
Avi Kivity9581d442010-10-19 16:46:55 +02006639 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6640 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006641 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6643 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006644
Bandan Das2a499e42017-08-03 15:54:41 -04006645 if (cpu_has_vmx_vmfunc())
6646 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6647
Eddie Dong2cc51562007-05-21 07:28:09 +03006648 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6649 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006650 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006651 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006652 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006653
Radim Krčmář74545702015-04-27 15:11:25 +02006654 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6655 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006656
Paolo Bonzini03916db2014-07-24 14:21:57 +02006657 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006658 u32 index = vmx_msr_index[i];
6659 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006660 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006661
6662 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6663 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006664 if (wrmsr_safe(index, data_low, data_high) < 0)
6665 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006666 vmx->guest_msrs[j].index = i;
6667 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006668 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006669 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006670 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006671
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006672 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006673
6674 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006675
6676 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006677 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006678
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006679 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6680 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6681
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006682 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006683
Wanpeng Lif53cd632014-12-02 19:14:58 +08006684 if (vmx_xsaves_supported())
6685 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6686
Peter Feiner4e595162016-07-07 14:49:58 -07006687 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006688 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6689 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6690 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006691
6692 if (cpu_has_vmx_encls_vmexit())
6693 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006694}
6695
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006696static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006697{
6698 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006699 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006700 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006701
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006702 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006703 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006704
Wanpeng Li518e7b92018-02-28 14:03:31 +08006705 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006706 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006707 kvm_set_cr8(vcpu, 0);
6708
6709 if (!init_event) {
6710 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6711 MSR_IA32_APICBASE_ENABLE;
6712 if (kvm_vcpu_is_reset_bsp(vcpu))
6713 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6714 apic_base_msr.host_initiated = true;
6715 kvm_set_apic_base(vcpu, &apic_base_msr);
6716 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006717
Avi Kivity2fb92db2011-04-27 19:42:18 +03006718 vmx_segment_cache_clear(vmx);
6719
Avi Kivity5706be02008-08-20 15:07:31 +03006720 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006721 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006722 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006723
6724 seg_setup(VCPU_SREG_DS);
6725 seg_setup(VCPU_SREG_ES);
6726 seg_setup(VCPU_SREG_FS);
6727 seg_setup(VCPU_SREG_GS);
6728 seg_setup(VCPU_SREG_SS);
6729
6730 vmcs_write16(GUEST_TR_SELECTOR, 0);
6731 vmcs_writel(GUEST_TR_BASE, 0);
6732 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6733 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6734
6735 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6736 vmcs_writel(GUEST_LDTR_BASE, 0);
6737 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6738 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6739
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006740 if (!init_event) {
6741 vmcs_write32(GUEST_SYSENTER_CS, 0);
6742 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6743 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6744 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6745 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006746
Wanpeng Lic37c2872017-11-20 14:52:21 -08006747 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006748 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006749
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006750 vmcs_writel(GUEST_GDTR_BASE, 0);
6751 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6752
6753 vmcs_writel(GUEST_IDTR_BASE, 0);
6754 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6755
Anthony Liguori443381a2010-12-06 10:53:38 -06006756 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006757 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006758 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006759 if (kvm_mpx_supported())
6760 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006761
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006762 setup_msrs(vmx);
6763
Avi Kivity6aa8b732006-12-10 02:21:36 -08006764 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6765
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006766 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006767 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006768 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006769 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006770 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006771 vmcs_write32(TPR_THRESHOLD, 0);
6772 }
6773
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006774 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006775
Sheng Yang2384d2b2008-01-17 15:14:33 +08006776 if (vmx->vpid != 0)
6777 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6778
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006779 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006780 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006781 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006782 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006783 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006784
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006785 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006787 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006788 if (init_event)
6789 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006790}
6791
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006792/*
6793 * In nested virtualization, check if L1 asked to exit on external interrupts.
6794 * For most existing hypervisors, this will always return true.
6795 */
6796static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6797{
6798 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6799 PIN_BASED_EXT_INTR_MASK;
6800}
6801
Bandan Das77b0f5d2014-04-19 18:17:45 -04006802/*
6803 * In nested virtualization, check if L1 has set
6804 * VM_EXIT_ACK_INTR_ON_EXIT
6805 */
6806static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6807{
6808 return get_vmcs12(vcpu)->vm_exit_controls &
6809 VM_EXIT_ACK_INTR_ON_EXIT;
6810}
6811
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006812static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6813{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006814 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006815}
6816
Jan Kiszkac9a79532014-03-07 20:03:15 +01006817static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006818{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006819 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6820 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006821}
6822
Jan Kiszkac9a79532014-03-07 20:03:15 +01006823static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006824{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006825 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006826 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006827 enable_irq_window(vcpu);
6828 return;
6829 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006830
Paolo Bonzini47c01522016-12-19 11:44:07 +01006831 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6832 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006833}
6834
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006835static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006836{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006838 uint32_t intr;
6839 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006840
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006841 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006842
Avi Kivityfa89a812008-09-01 15:57:51 +03006843 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006844 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006845 int inc_eip = 0;
6846 if (vcpu->arch.interrupt.soft)
6847 inc_eip = vcpu->arch.event_exit_inst_len;
6848 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006849 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006850 return;
6851 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006852 intr = irq | INTR_INFO_VALID_MASK;
6853 if (vcpu->arch.interrupt.soft) {
6854 intr |= INTR_TYPE_SOFT_INTR;
6855 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6856 vmx->vcpu.arch.event_exit_inst_len);
6857 } else
6858 intr |= INTR_TYPE_EXT_INTR;
6859 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006860
6861 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006862}
6863
Sheng Yangf08864b2008-05-15 18:23:25 +08006864static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6865{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006866 struct vcpu_vmx *vmx = to_vmx(vcpu);
6867
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006868 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006869 /*
6870 * Tracking the NMI-blocked state in software is built upon
6871 * finding the next open IRQ window. This, in turn, depends on
6872 * well-behaving guests: They have to keep IRQs disabled at
6873 * least as long as the NMI handler runs. Otherwise we may
6874 * cause NMI nesting, maybe breaking the guest. But as this is
6875 * highly unlikely, we can live with the residual risk.
6876 */
6877 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6878 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6879 }
6880
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006881 ++vcpu->stat.nmi_injections;
6882 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006883
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006884 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006885 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006886 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006887 return;
6888 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006889
Sheng Yangf08864b2008-05-15 18:23:25 +08006890 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6891 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006892
6893 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006894}
6895
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006896static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6897{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006898 struct vcpu_vmx *vmx = to_vmx(vcpu);
6899 bool masked;
6900
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006901 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006902 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006903 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006904 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006905 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6906 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6907 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006908}
6909
6910static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6911{
6912 struct vcpu_vmx *vmx = to_vmx(vcpu);
6913
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006914 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006915 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6916 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6917 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6918 }
6919 } else {
6920 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6921 if (masked)
6922 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6923 GUEST_INTR_STATE_NMI);
6924 else
6925 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6926 GUEST_INTR_STATE_NMI);
6927 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006928}
6929
Jan Kiszka2505dc92013-04-14 12:12:47 +02006930static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6931{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006932 if (to_vmx(vcpu)->nested.nested_run_pending)
6933 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006934
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006935 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006936 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6937 return 0;
6938
Jan Kiszka2505dc92013-04-14 12:12:47 +02006939 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6940 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6941 | GUEST_INTR_STATE_NMI));
6942}
6943
Gleb Natapov78646122009-03-23 12:12:11 +02006944static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6945{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006946 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6947 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006948 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6949 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006950}
6951
Izik Eiduscbc94022007-10-25 00:29:55 +02006952static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6953{
6954 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006955
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006956 if (enable_unrestricted_guest)
6957 return 0;
6958
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006959 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6960 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006961 if (ret)
6962 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006963 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006964 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006965}
6966
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006967static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6968{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006969 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006970 return 0;
6971}
6972
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006973static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006974{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006975 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006976 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006977 /*
6978 * Update instruction length as we may reinject the exception
6979 * from user space while in guest debugging mode.
6980 */
6981 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6982 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006983 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006984 return false;
6985 /* fall through */
6986 case DB_VECTOR:
6987 if (vcpu->guest_debug &
6988 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6989 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006990 /* fall through */
6991 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006992 case OF_VECTOR:
6993 case BR_VECTOR:
6994 case UD_VECTOR:
6995 case DF_VECTOR:
6996 case SS_VECTOR:
6997 case GP_VECTOR:
6998 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006999 return true;
7000 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007001 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007002 return false;
7003}
7004
7005static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7006 int vec, u32 err_code)
7007{
7008 /*
7009 * Instruction with address size override prefix opcode 0x67
7010 * Cause the #SS fault with 0 error code in VM86 mode.
7011 */
7012 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007013 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007014 if (vcpu->arch.halt_request) {
7015 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007016 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007017 }
7018 return 1;
7019 }
7020 return 0;
7021 }
7022
7023 /*
7024 * Forward all other exceptions that are valid in real mode.
7025 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7026 * the required debugging infrastructure rework.
7027 */
7028 kvm_queue_exception(vcpu, vec);
7029 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030}
7031
Andi Kleena0861c02009-06-08 17:37:09 +08007032/*
7033 * Trigger machine check on the host. We assume all the MSRs are already set up
7034 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7035 * We pass a fake environment to the machine check handler because we want
7036 * the guest to be always treated like user space, no matter what context
7037 * it used internally.
7038 */
7039static void kvm_machine_check(void)
7040{
7041#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7042 struct pt_regs regs = {
7043 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7044 .flags = X86_EFLAGS_IF,
7045 };
7046
7047 do_machine_check(&regs, 0);
7048#endif
7049}
7050
Avi Kivity851ba692009-08-24 11:10:17 +03007051static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007052{
7053 /* already handled by vcpu_run */
7054 return 1;
7055}
7056
Avi Kivity851ba692009-08-24 11:10:17 +03007057static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058{
Avi Kivity1155f762007-11-22 11:30:47 +02007059 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007060 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007061 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007062 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007063 u32 vect_info;
7064 enum emulation_result er;
7065
Avi Kivity1155f762007-11-22 11:30:47 +02007066 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007067 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007068
Andi Kleena0861c02009-06-08 17:37:09 +08007069 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007070 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007071
Jim Mattsonef85b672016-12-12 11:01:37 -08007072 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007073 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007074
Wanpeng Li082d06e2018-04-03 16:28:48 -07007075 if (is_invalid_opcode(intr_info))
7076 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007077
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007079 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007081
Liran Alon9e869482018-03-12 13:12:51 +02007082 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7083 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007084 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007085 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7086 if (er == EMULATE_USER_EXIT)
7087 return 0;
7088 else if (er != EMULATE_DONE)
7089 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7090 return 1;
7091 }
7092
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007093 /*
7094 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7095 * MMIO, it is better to report an internal error.
7096 * See the comments in vmx_handle_exit.
7097 */
7098 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7099 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7100 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7101 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007102 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007103 vcpu->run->internal.data[0] = vect_info;
7104 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007105 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007106 return 0;
7107 }
7108
Avi Kivity6aa8b732006-12-10 02:21:36 -08007109 if (is_page_fault(intr_info)) {
7110 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007111 /* EPT won't cause page fault directly */
7112 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007113 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007114 }
7115
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007116 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007117
7118 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7119 return handle_rmode_exception(vcpu, ex_no, error_code);
7120
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007121 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007122 case AC_VECTOR:
7123 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7124 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 case DB_VECTOR:
7126 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7127 if (!(vcpu->guest_debug &
7128 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007129 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007130 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007131 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007132 skip_emulated_instruction(vcpu);
7133
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007134 kvm_queue_exception(vcpu, DB_VECTOR);
7135 return 1;
7136 }
7137 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7138 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7139 /* fall through */
7140 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007141 /*
7142 * Update instruction length as we may reinject #BP from
7143 * user space while in guest debugging mode. Reading it for
7144 * #DB as well causes no harm, it is not used in that case.
7145 */
7146 vmx->vcpu.arch.event_exit_inst_len =
7147 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007149 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007150 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7151 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007152 break;
7153 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007154 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7155 kvm_run->ex.exception = ex_no;
7156 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007157 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007158 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159 return 0;
7160}
7161
Avi Kivity851ba692009-08-24 11:10:17 +03007162static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007163{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007164 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007165 return 1;
7166}
7167
Avi Kivity851ba692009-08-24 11:10:17 +03007168static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007169{
Avi Kivity851ba692009-08-24 11:10:17 +03007170 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007171 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007172 return 0;
7173}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007174
Avi Kivity851ba692009-08-24 11:10:17 +03007175static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007176{
He, Qingbfdaab02007-09-12 14:18:28 +08007177 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007178 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007179 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180
He, Qingbfdaab02007-09-12 14:18:28 +08007181 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007182 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007183
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007184 ++vcpu->stat.io_exits;
7185
Sean Christopherson432baf62018-03-08 08:57:26 -08007186 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007187 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007188
7189 port = exit_qualification >> 16;
7190 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007191 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007192
Sean Christophersondca7f122018-03-08 08:57:27 -08007193 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007194}
7195
Ingo Molnar102d8322007-02-19 14:37:47 +02007196static void
7197vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7198{
7199 /*
7200 * Patch in the VMCALL instruction:
7201 */
7202 hypercall[0] = 0x0f;
7203 hypercall[1] = 0x01;
7204 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007205}
7206
Guo Chao0fa06072012-06-28 15:16:19 +08007207/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007208static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7209{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7212 unsigned long orig_val = val;
7213
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007214 /*
7215 * We get here when L2 changed cr0 in a way that did not change
7216 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 * but did change L0 shadowed bits. So we first calculate the
7218 * effective cr0 value that L1 would like to write into the
7219 * hardware. It consists of the L2-owned bits from the new
7220 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007222 val = (val & ~vmcs12->cr0_guest_host_mask) |
7223 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7224
David Matlack38991522016-11-29 18:14:08 -08007225 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007226 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007227
7228 if (kvm_set_cr0(vcpu, val))
7229 return 1;
7230 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007231 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007232 } else {
7233 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007234 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007235 return 1;
David Matlack38991522016-11-29 18:14:08 -08007236
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007237 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007238 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007239}
7240
7241static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7242{
7243 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007244 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7245 unsigned long orig_val = val;
7246
7247 /* analogously to handle_set_cr0 */
7248 val = (val & ~vmcs12->cr4_guest_host_mask) |
7249 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7250 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007251 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007252 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253 return 0;
7254 } else
7255 return kvm_set_cr4(vcpu, val);
7256}
7257
Paolo Bonzini0367f202016-07-12 10:44:55 +02007258static int handle_desc(struct kvm_vcpu *vcpu)
7259{
7260 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007261 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007262}
7263
Avi Kivity851ba692009-08-24 11:10:17 +03007264static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007265{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007266 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007267 int cr;
7268 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007269 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271
He, Qingbfdaab02007-09-12 14:18:28 +08007272 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 cr = exit_qualification & 15;
7274 reg = (exit_qualification >> 8) & 15;
7275 switch ((exit_qualification >> 4) & 3) {
7276 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007277 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007278 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279 switch (cr) {
7280 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007281 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007282 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007283 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007284 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007285 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007286 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007288 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007289 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007290 case 8: {
7291 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007292 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007293 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007294 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007295 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007296 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007297 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007298 return ret;
7299 /*
7300 * TODO: we might be squashing a
7301 * KVM_GUESTDBG_SINGLESTEP-triggered
7302 * KVM_EXIT_DEBUG here.
7303 */
Avi Kivity851ba692009-08-24 11:10:17 +03007304 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007305 return 0;
7306 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007307 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007309 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007310 WARN_ONCE(1, "Guest should always own CR0.TS");
7311 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007312 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007313 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007314 case 1: /*mov from cr*/
7315 switch (cr) {
7316 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007317 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007318 val = kvm_read_cr3(vcpu);
7319 kvm_register_write(vcpu, reg, val);
7320 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007321 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007323 val = kvm_get_cr8(vcpu);
7324 kvm_register_write(vcpu, reg, val);
7325 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007326 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007327 }
7328 break;
7329 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007330 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007331 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007332 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007333
Kyle Huey6affcbe2016-11-29 12:40:40 -08007334 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007335 default:
7336 break;
7337 }
Avi Kivity851ba692009-08-24 11:10:17 +03007338 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007339 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007340 (int)(exit_qualification >> 4) & 3, cr);
7341 return 0;
7342}
7343
Avi Kivity851ba692009-08-24 11:10:17 +03007344static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007345{
He, Qingbfdaab02007-09-12 14:18:28 +08007346 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007347 int dr, dr7, reg;
7348
7349 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7350 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7351
7352 /* First, if DR does not exist, trigger UD */
7353 if (!kvm_require_dr(vcpu, dr))
7354 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007355
Jan Kiszkaf2483412010-01-20 18:20:20 +01007356 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007357 if (!kvm_require_cpl(vcpu, 0))
7358 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007359 dr7 = vmcs_readl(GUEST_DR7);
7360 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007361 /*
7362 * As the vm-exit takes precedence over the debug trap, we
7363 * need to emulate the latter, either for the host or the
7364 * guest debugging itself.
7365 */
7366 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007367 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007368 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007369 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007370 vcpu->run->debug.arch.exception = DB_VECTOR;
7371 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007372 return 0;
7373 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007374 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007375 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007376 kvm_queue_exception(vcpu, DB_VECTOR);
7377 return 1;
7378 }
7379 }
7380
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007381 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007382 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7383 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007384
7385 /*
7386 * No more DR vmexits; force a reload of the debug registers
7387 * and reenter on this instruction. The next vmexit will
7388 * retrieve the full state of the debug registers.
7389 */
7390 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7391 return 1;
7392 }
7393
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007394 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7395 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007396 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007397
7398 if (kvm_get_dr(vcpu, dr, &val))
7399 return 1;
7400 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007401 } else
Nadav Amit57773922014-06-18 17:19:23 +03007402 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007403 return 1;
7404
Kyle Huey6affcbe2016-11-29 12:40:40 -08007405 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406}
7407
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007408static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7409{
7410 return vcpu->arch.dr6;
7411}
7412
7413static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7414{
7415}
7416
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007417static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7418{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007419 get_debugreg(vcpu->arch.db[0], 0);
7420 get_debugreg(vcpu->arch.db[1], 1);
7421 get_debugreg(vcpu->arch.db[2], 2);
7422 get_debugreg(vcpu->arch.db[3], 3);
7423 get_debugreg(vcpu->arch.dr6, 6);
7424 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7425
7426 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007427 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007428}
7429
Gleb Natapov020df072010-04-13 10:05:23 +03007430static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7431{
7432 vmcs_writel(GUEST_DR7, val);
7433}
7434
Avi Kivity851ba692009-08-24 11:10:17 +03007435static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007436{
Kyle Huey6a908b62016-11-29 12:40:37 -08007437 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007438}
7439
Avi Kivity851ba692009-08-24 11:10:17 +03007440static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007441{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007442 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007443 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007444
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007445 msr_info.index = ecx;
7446 msr_info.host_initiated = false;
7447 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007448 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007449 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450 return 1;
7451 }
7452
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007453 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007454
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007456 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7457 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007458 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007459}
7460
Avi Kivity851ba692009-08-24 11:10:17 +03007461static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007462{
Will Auld8fe8ab42012-11-29 12:42:12 -08007463 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007464 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7465 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7466 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467
Will Auld8fe8ab42012-11-29 12:42:12 -08007468 msr.data = data;
7469 msr.index = ecx;
7470 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007471 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007472 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007473 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007474 return 1;
7475 }
7476
Avi Kivity59200272010-01-25 19:47:02 +02007477 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007478 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007482{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007483 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007484 return 1;
7485}
7486
Avi Kivity851ba692009-08-24 11:10:17 +03007487static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007488{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007489 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7490 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007491
Avi Kivity3842d132010-07-27 12:30:24 +03007492 kvm_make_request(KVM_REQ_EVENT, vcpu);
7493
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007494 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007495 return 1;
7496}
7497
Avi Kivity851ba692009-08-24 11:10:17 +03007498static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007499{
Avi Kivityd3bef152007-06-05 15:53:05 +03007500 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007501}
7502
Avi Kivity851ba692009-08-24 11:10:17 +03007503static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007504{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007505 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007506}
7507
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007508static int handle_invd(struct kvm_vcpu *vcpu)
7509{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007510 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007511}
7512
Avi Kivity851ba692009-08-24 11:10:17 +03007513static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007514{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007515 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007516
7517 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007518 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007519}
7520
Avi Kivityfee84b02011-11-10 14:57:25 +02007521static int handle_rdpmc(struct kvm_vcpu *vcpu)
7522{
7523 int err;
7524
7525 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007526 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007527}
7528
Avi Kivity851ba692009-08-24 11:10:17 +03007529static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007530{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007531 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007532}
7533
Dexuan Cui2acf9232010-06-10 11:27:12 +08007534static int handle_xsetbv(struct kvm_vcpu *vcpu)
7535{
7536 u64 new_bv = kvm_read_edx_eax(vcpu);
7537 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7538
7539 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007540 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007541 return 1;
7542}
7543
Wanpeng Lif53cd632014-12-02 19:14:58 +08007544static int handle_xsaves(struct kvm_vcpu *vcpu)
7545{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007546 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007547 WARN(1, "this should never happen\n");
7548 return 1;
7549}
7550
7551static int handle_xrstors(struct kvm_vcpu *vcpu)
7552{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007553 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007554 WARN(1, "this should never happen\n");
7555 return 1;
7556}
7557
Avi Kivity851ba692009-08-24 11:10:17 +03007558static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007559{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007560 if (likely(fasteoi)) {
7561 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7562 int access_type, offset;
7563
7564 access_type = exit_qualification & APIC_ACCESS_TYPE;
7565 offset = exit_qualification & APIC_ACCESS_OFFSET;
7566 /*
7567 * Sane guest uses MOV to write EOI, with written value
7568 * not cared. So make a short-circuit here by avoiding
7569 * heavy instruction emulation.
7570 */
7571 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7572 (offset == APIC_EOI)) {
7573 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007574 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007575 }
7576 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007577 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007578}
7579
Yang Zhangc7c9c562013-01-25 10:18:51 +08007580static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7581{
7582 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7583 int vector = exit_qualification & 0xff;
7584
7585 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7586 kvm_apic_set_eoi_accelerated(vcpu, vector);
7587 return 1;
7588}
7589
Yang Zhang83d4c282013-01-25 10:18:49 +08007590static int handle_apic_write(struct kvm_vcpu *vcpu)
7591{
7592 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7593 u32 offset = exit_qualification & 0xfff;
7594
7595 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7596 kvm_apic_write_nodecode(vcpu, offset);
7597 return 1;
7598}
7599
Avi Kivity851ba692009-08-24 11:10:17 +03007600static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007601{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007602 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007603 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007604 bool has_error_code = false;
7605 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007606 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007607 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007608
7609 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007610 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007611 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007612
7613 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7614
7615 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007616 if (reason == TASK_SWITCH_GATE && idt_v) {
7617 switch (type) {
7618 case INTR_TYPE_NMI_INTR:
7619 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007620 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007621 break;
7622 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007623 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007624 kvm_clear_interrupt_queue(vcpu);
7625 break;
7626 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007627 if (vmx->idt_vectoring_info &
7628 VECTORING_INFO_DELIVER_CODE_MASK) {
7629 has_error_code = true;
7630 error_code =
7631 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7632 }
7633 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007634 case INTR_TYPE_SOFT_EXCEPTION:
7635 kvm_clear_exception_queue(vcpu);
7636 break;
7637 default:
7638 break;
7639 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007640 }
Izik Eidus37817f22008-03-24 23:14:53 +02007641 tss_selector = exit_qualification;
7642
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007643 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7644 type != INTR_TYPE_EXT_INTR &&
7645 type != INTR_TYPE_NMI_INTR))
7646 skip_emulated_instruction(vcpu);
7647
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007648 if (kvm_task_switch(vcpu, tss_selector,
7649 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7650 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007651 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7652 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7653 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007654 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007655 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007656
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007657 /*
7658 * TODO: What about debug traps on tss switch?
7659 * Are we supposed to inject them and update dr6?
7660 */
7661
7662 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007663}
7664
Avi Kivity851ba692009-08-24 11:10:17 +03007665static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007666{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007667 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007668 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007669 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007670
Sheng Yangf9c617f2009-03-25 10:08:52 +08007671 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007672
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007673 /*
7674 * EPT violation happened while executing iret from NMI,
7675 * "blocked by NMI" bit has to be set before next VM entry.
7676 * There are errata that may cause this bit to not be set:
7677 * AAK134, BY25.
7678 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007679 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007680 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007681 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007682 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7683
Sheng Yang14394422008-04-28 12:24:45 +08007684 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007685 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007686
Junaid Shahid27959a42016-12-06 16:46:10 -08007687 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007688 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007689 ? PFERR_USER_MASK : 0;
7690 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007691 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007692 ? PFERR_WRITE_MASK : 0;
7693 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007694 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007695 ? PFERR_FETCH_MASK : 0;
7696 /* ept page table entry is present? */
7697 error_code |= (exit_qualification &
7698 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7699 EPT_VIOLATION_EXECUTABLE))
7700 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007701
Paolo Bonzinieebed242016-11-28 14:39:58 +01007702 error_code |= (exit_qualification & 0x100) != 0 ?
7703 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007704
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007705 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007706 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007707}
7708
Avi Kivity851ba692009-08-24 11:10:17 +03007709static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007710{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007711 gpa_t gpa;
7712
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007713 /*
7714 * A nested guest cannot optimize MMIO vmexits, because we have an
7715 * nGPA here instead of the required GPA.
7716 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007717 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007718 if (!is_guest_mode(vcpu) &&
7719 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007720 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007721 /*
7722 * Doing kvm_skip_emulated_instruction() depends on undefined
7723 * behavior: Intel's manual doesn't mandate
7724 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7725 * occurs and while on real hardware it was observed to be set,
7726 * other hypervisors (namely Hyper-V) don't set it, we end up
7727 * advancing IP with some random value. Disable fast mmio when
7728 * running nested and keep it for real hardware in hope that
7729 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7730 */
7731 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7732 return kvm_skip_emulated_instruction(vcpu);
7733 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007734 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007735 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007736 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007737
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007738 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007739}
7740
Avi Kivity851ba692009-08-24 11:10:17 +03007741static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007742{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007743 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007744 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7745 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007746 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007747 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007748
7749 return 1;
7750}
7751
Mohammed Gamal80ced182009-09-01 12:48:18 +02007752static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007753{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007754 struct vcpu_vmx *vmx = to_vmx(vcpu);
7755 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007756 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007757 u32 cpu_exec_ctrl;
7758 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007759 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007760
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007761 /*
7762 * We should never reach the point where we are emulating L2
7763 * due to invalid guest state as that means we incorrectly
7764 * allowed a nested VMEntry with an invalid vmcs12.
7765 */
7766 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7767
Avi Kivity49e9d552010-09-19 14:34:08 +02007768 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7769 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007770
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007771 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007772 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007773 return handle_interrupt_window(&vmx->vcpu);
7774
Radim Krčmář72875d82017-04-26 22:32:19 +02007775 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007776 return 1;
7777
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007778 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007779
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007780 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007781 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007782 ret = 0;
7783 goto out;
7784 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007785
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007786 if (err != EMULATE_DONE)
7787 goto emulation_error;
7788
7789 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7790 vcpu->arch.exception.pending)
7791 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007792
Gleb Natapov8d76c492013-05-08 18:38:44 +03007793 if (vcpu->arch.halt_request) {
7794 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007795 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007796 goto out;
7797 }
7798
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007799 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007800 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007801 if (need_resched())
7802 schedule();
7803 }
7804
Mohammed Gamal80ced182009-09-01 12:48:18 +02007805out:
7806 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007807
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007808emulation_error:
7809 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7810 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7811 vcpu->run->internal.ndata = 0;
7812 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007813}
7814
7815static void grow_ple_window(struct kvm_vcpu *vcpu)
7816{
7817 struct vcpu_vmx *vmx = to_vmx(vcpu);
7818 int old = vmx->ple_window;
7819
Babu Mogerc8e88712018-03-16 16:37:24 -04007820 vmx->ple_window = __grow_ple_window(old, ple_window,
7821 ple_window_grow,
7822 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007823
7824 if (vmx->ple_window != old)
7825 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007826
7827 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007828}
7829
7830static void shrink_ple_window(struct kvm_vcpu *vcpu)
7831{
7832 struct vcpu_vmx *vmx = to_vmx(vcpu);
7833 int old = vmx->ple_window;
7834
Babu Mogerc8e88712018-03-16 16:37:24 -04007835 vmx->ple_window = __shrink_ple_window(old, ple_window,
7836 ple_window_shrink,
7837 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007838
7839 if (vmx->ple_window != old)
7840 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007841
7842 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007843}
7844
7845/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007846 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7847 */
7848static void wakeup_handler(void)
7849{
7850 struct kvm_vcpu *vcpu;
7851 int cpu = smp_processor_id();
7852
7853 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7854 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7855 blocked_vcpu_list) {
7856 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7857
7858 if (pi_test_on(pi_desc) == 1)
7859 kvm_vcpu_kick(vcpu);
7860 }
7861 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7862}
7863
Peng Haoe01bca22018-04-07 05:47:32 +08007864static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007865{
7866 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7867 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7868 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7869 0ull, VMX_EPT_EXECUTABLE_MASK,
7870 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007871 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007872
7873 ept_set_mmio_spte_mask();
7874 kvm_enable_tdp();
7875}
7876
Tiejun Chenf2c76482014-10-28 10:14:47 +08007877static __init int hardware_setup(void)
7878{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007879 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007880 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007881
7882 rdmsrl_safe(MSR_EFER, &host_efer);
7883
7884 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7885 kvm_define_shared_msr(i, vmx_msr_index[i]);
7886
Radim Krčmář23611332016-09-29 22:41:33 +02007887 for (i = 0; i < VMX_BITMAP_NR; i++) {
7888 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7889 if (!vmx_bitmap[i])
7890 goto out;
7891 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007892
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007893 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7894 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7895
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007896 if (setup_vmcs_config(&vmcs_config) < 0) {
7897 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007898 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007899 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007900
7901 if (boot_cpu_has(X86_FEATURE_NX))
7902 kvm_enable_efer_bits(EFER_NX);
7903
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007904 if (boot_cpu_has(X86_FEATURE_MPX)) {
7905 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7906 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7907 }
7908
Wanpeng Li08d839c2017-03-23 05:30:08 -07007909 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7910 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007911 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007912
Tiejun Chenf2c76482014-10-28 10:14:47 +08007913 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007914 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007915 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007916 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007918
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007919 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007920 enable_ept_ad_bits = 0;
7921
Wanpeng Li8ad81822017-10-09 15:51:53 -07007922 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007923 enable_unrestricted_guest = 0;
7924
Paolo Bonziniad15a292015-01-30 16:18:49 +01007925 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007926 flexpriority_enabled = 0;
7927
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007928 if (!cpu_has_virtual_nmis())
7929 enable_vnmi = 0;
7930
Paolo Bonziniad15a292015-01-30 16:18:49 +01007931 /*
7932 * set_apic_access_page_addr() is used to reload apic access
7933 * page upon invalidation. No need to do anything if not
7934 * using the APIC_ACCESS_ADDR VMCS field.
7935 */
7936 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007937 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007938
7939 if (!cpu_has_vmx_tpr_shadow())
7940 kvm_x86_ops->update_cr8_intercept = NULL;
7941
7942 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7943 kvm_disable_largepages();
7944
Tianyu Lan877ad952018-07-19 08:40:23 +00007945#if IS_ENABLED(CONFIG_HYPERV)
7946 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7947 && enable_ept)
7948 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7949#endif
7950
Wanpeng Li0f107682017-09-28 18:06:24 -07007951 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007952 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007953 ple_window = 0;
7954 ple_window_grow = 0;
7955 ple_window_max = 0;
7956 ple_window_shrink = 0;
7957 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007958
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007959 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007960 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007961 kvm_x86_ops->sync_pir_to_irr = NULL;
7962 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007963
Haozhong Zhang64903d62015-10-20 15:39:09 +08007964 if (cpu_has_vmx_tsc_scaling()) {
7965 kvm_has_tsc_control = true;
7966 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7967 kvm_tsc_scaling_ratio_frac_bits = 48;
7968 }
7969
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007970 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7971
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007972 if (enable_ept)
7973 vmx_enable_tdp();
7974 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007975 kvm_disable_tdp();
7976
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007977 if (!nested) {
7978 kvm_x86_ops->get_nested_state = NULL;
7979 kvm_x86_ops->set_nested_state = NULL;
7980 }
7981
Kai Huang843e4332015-01-28 10:54:28 +08007982 /*
7983 * Only enable PML when hardware supports PML feature, and both EPT
7984 * and EPT A/D bit features are enabled -- PML depends on them to work.
7985 */
7986 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7987 enable_pml = 0;
7988
7989 if (!enable_pml) {
7990 kvm_x86_ops->slot_enable_log_dirty = NULL;
7991 kvm_x86_ops->slot_disable_log_dirty = NULL;
7992 kvm_x86_ops->flush_log_dirty = NULL;
7993 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7994 }
7995
Sean Christophersond264ee02018-08-27 15:21:12 -07007996 if (!cpu_has_vmx_preemption_timer())
7997 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7998
Yunhong Jiang64672c92016-06-13 14:19:59 -07007999 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8000 u64 vmx_msr;
8001
8002 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8003 cpu_preemption_timer_multi =
8004 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8005 } else {
8006 kvm_x86_ops->set_hv_timer = NULL;
8007 kvm_x86_ops->cancel_hv_timer = NULL;
8008 }
8009
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008010 if (!cpu_has_vmx_shadow_vmcs())
8011 enable_shadow_vmcs = 0;
8012 if (enable_shadow_vmcs)
8013 init_vmcs_shadow_fields();
8014
Feng Wubf9f6ac2015-09-18 22:29:55 +08008015 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008016 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008017
Ashok Rajc45dcc72016-06-22 14:59:56 +08008018 kvm_mce_cap_supported |= MCG_LMCE_P;
8019
Tiejun Chenf2c76482014-10-28 10:14:47 +08008020 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008021
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008022out:
Radim Krčmář23611332016-09-29 22:41:33 +02008023 for (i = 0; i < VMX_BITMAP_NR; i++)
8024 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008025
8026 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008027}
8028
8029static __exit void hardware_unsetup(void)
8030{
Radim Krčmář23611332016-09-29 22:41:33 +02008031 int i;
8032
8033 for (i = 0; i < VMX_BITMAP_NR; i++)
8034 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008035
Tiejun Chenf2c76482014-10-28 10:14:47 +08008036 free_kvm_area();
8037}
8038
Avi Kivity6aa8b732006-12-10 02:21:36 -08008039/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008040 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8041 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8042 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008043static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008044{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008045 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008046 grow_ple_window(vcpu);
8047
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008048 /*
8049 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8050 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8051 * never set PAUSE_EXITING and just set PLE if supported,
8052 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8053 */
8054 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008055 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008056}
8057
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008058static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008059{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008060 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008061}
8062
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008063static int handle_mwait(struct kvm_vcpu *vcpu)
8064{
8065 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8066 return handle_nop(vcpu);
8067}
8068
Jim Mattson45ec3682017-08-23 16:32:04 -07008069static int handle_invalid_op(struct kvm_vcpu *vcpu)
8070{
8071 kvm_queue_exception(vcpu, UD_VECTOR);
8072 return 1;
8073}
8074
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008075static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8076{
8077 return 1;
8078}
8079
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008080static int handle_monitor(struct kvm_vcpu *vcpu)
8081{
8082 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8083 return handle_nop(vcpu);
8084}
8085
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008086/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008087 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008088 * set the success or error code of an emulated VMX instruction (as specified
8089 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8090 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008091 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008092static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008093{
8094 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8095 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8096 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008097 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008098}
8099
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008100static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008101{
8102 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8103 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8104 X86_EFLAGS_SF | X86_EFLAGS_OF))
8105 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008106 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008107}
8108
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008109static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8110 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008111{
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008112 /*
8113 * failValid writes the error number to the current VMCS, which
8114 * can't be done if there isn't a current VMCS.
8115 */
8116 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8117 return nested_vmx_failInvalid(vcpu);
8118
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008119 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8120 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8121 X86_EFLAGS_SF | X86_EFLAGS_OF))
8122 | X86_EFLAGS_ZF);
8123 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8124 /*
8125 * We don't need to force a shadow sync because
8126 * VM_INSTRUCTION_ERROR is not shadowed
8127 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008128 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008129}
Abel Gordon145c28d2013-04-18 14:36:55 +03008130
Wincy Vanff651cb2014-12-11 08:52:58 +03008131static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8132{
8133 /* TODO: not to reset guest simply here. */
8134 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008135 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008136}
8137
Jan Kiszkaf4124502014-03-07 20:03:13 +01008138static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8139{
8140 struct vcpu_vmx *vmx =
8141 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8142
8143 vmx->nested.preemption_timer_expired = true;
8144 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8145 kvm_vcpu_kick(&vmx->vcpu);
8146
8147 return HRTIMER_NORESTART;
8148}
8149
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008150/*
Bandan Das19677e32014-05-06 02:19:15 -04008151 * Decode the memory-address operand of a vmx instruction, as recorded on an
8152 * exit caused by such an instruction (run by a guest hypervisor).
8153 * On success, returns 0. When the operand is invalid, returns 1 and throws
8154 * #UD or #GP.
8155 */
8156static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8157 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008158 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008159{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008160 gva_t off;
8161 bool exn;
8162 struct kvm_segment s;
8163
Bandan Das19677e32014-05-06 02:19:15 -04008164 /*
8165 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8166 * Execution", on an exit, vmx_instruction_info holds most of the
8167 * addressing components of the operand. Only the displacement part
8168 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8169 * For how an actual address is calculated from all these components,
8170 * refer to Vol. 1, "Operand Addressing".
8171 */
8172 int scaling = vmx_instruction_info & 3;
8173 int addr_size = (vmx_instruction_info >> 7) & 7;
8174 bool is_reg = vmx_instruction_info & (1u << 10);
8175 int seg_reg = (vmx_instruction_info >> 15) & 7;
8176 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8177 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8178 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8179 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8180
8181 if (is_reg) {
8182 kvm_queue_exception(vcpu, UD_VECTOR);
8183 return 1;
8184 }
8185
8186 /* Addr = segment_base + offset */
8187 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008188 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008189 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008190 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008191 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008192 off += kvm_register_read(vcpu, index_reg)<<scaling;
8193 vmx_get_segment(vcpu, &s, seg_reg);
8194 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008195
8196 if (addr_size == 1) /* 32 bit */
8197 *ret &= 0xffffffff;
8198
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008199 /* Checks for #GP/#SS exceptions. */
8200 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008201 if (is_long_mode(vcpu)) {
8202 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8203 * non-canonical form. This is the only check on the memory
8204 * destination for long mode!
8205 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008206 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008207 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008208 /* Protected mode: apply checks for segment validity in the
8209 * following order:
8210 * - segment type check (#GP(0) may be thrown)
8211 * - usability check (#GP(0)/#SS(0))
8212 * - limit check (#GP(0)/#SS(0))
8213 */
8214 if (wr)
8215 /* #GP(0) if the destination operand is located in a
8216 * read-only data segment or any code segment.
8217 */
8218 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8219 else
8220 /* #GP(0) if the source operand is located in an
8221 * execute-only code segment
8222 */
8223 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008224 if (exn) {
8225 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8226 return 1;
8227 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008228 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8229 */
8230 exn = (s.unusable != 0);
8231 /* Protected mode: #GP(0)/#SS(0) if the memory
8232 * operand is outside the segment limit.
8233 */
8234 exn = exn || (off + sizeof(u64) > s.limit);
8235 }
8236 if (exn) {
8237 kvm_queue_exception_e(vcpu,
8238 seg_reg == VCPU_SREG_SS ?
8239 SS_VECTOR : GP_VECTOR,
8240 0);
8241 return 1;
8242 }
8243
Bandan Das19677e32014-05-06 02:19:15 -04008244 return 0;
8245}
8246
Radim Krčmářcbf71272017-05-19 15:48:51 +02008247static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008248{
8249 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008250 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008251
8252 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008253 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008254 return 1;
8255
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008256 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008257 kvm_inject_page_fault(vcpu, &e);
8258 return 1;
8259 }
8260
Bandan Das3573e222014-05-06 02:19:16 -04008261 return 0;
8262}
8263
Liran Alonabfc52c2018-06-23 02:35:13 +03008264/*
8265 * Allocate a shadow VMCS and associate it with the currently loaded
8266 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8267 * VMCS is also VMCLEARed, so that it is ready for use.
8268 */
8269static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8270{
8271 struct vcpu_vmx *vmx = to_vmx(vcpu);
8272 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8273
8274 /*
8275 * We should allocate a shadow vmcs for vmcs01 only when L1
8276 * executes VMXON and free it when L1 executes VMXOFF.
8277 * As it is invalid to execute VMXON twice, we shouldn't reach
8278 * here when vmcs01 already have an allocated shadow vmcs.
8279 */
8280 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8281
8282 if (!loaded_vmcs->shadow_vmcs) {
8283 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8284 if (loaded_vmcs->shadow_vmcs)
8285 vmcs_clear(loaded_vmcs->shadow_vmcs);
8286 }
8287 return loaded_vmcs->shadow_vmcs;
8288}
8289
Jim Mattsone29acc52016-11-30 12:03:43 -08008290static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8291{
8292 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008293 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008294
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008295 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8296 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008297 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008298
8299 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8300 if (!vmx->nested.cached_vmcs12)
8301 goto out_cached_vmcs12;
8302
Liran Alon61ada742018-06-23 02:35:08 +03008303 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8304 if (!vmx->nested.cached_shadow_vmcs12)
8305 goto out_cached_shadow_vmcs12;
8306
Liran Alonabfc52c2018-06-23 02:35:13 +03008307 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8308 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008309
Jim Mattsone29acc52016-11-30 12:03:43 -08008310 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8311 HRTIMER_MODE_REL_PINNED);
8312 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8313
Roman Kagan63aff652018-07-19 21:59:07 +03008314 vmx->nested.vpid02 = allocate_vpid();
8315
Sean Christopherson9d6105b22018-09-26 09:23:51 -07008316 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008317 vmx->nested.vmxon = true;
8318 return 0;
8319
8320out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008321 kfree(vmx->nested.cached_shadow_vmcs12);
8322
8323out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008324 kfree(vmx->nested.cached_vmcs12);
8325
8326out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008327 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008328
Jim Mattsonde3a0022017-11-27 17:22:25 -06008329out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008330 return -ENOMEM;
8331}
8332
Bandan Das3573e222014-05-06 02:19:16 -04008333/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008334 * Emulate the VMXON instruction.
8335 * Currently, we just remember that VMX is active, and do not save or even
8336 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8337 * do not currently need to store anything in that guest-allocated memory
8338 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8339 * argument is different from the VMXON pointer (which the spec says they do).
8340 */
8341static int handle_vmon(struct kvm_vcpu *vcpu)
8342{
Jim Mattsone29acc52016-11-30 12:03:43 -08008343 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008344 gpa_t vmptr;
8345 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008346 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008347 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8348 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008349
Jim Mattson70f3aac2017-04-26 08:53:46 -07008350 /*
8351 * The Intel VMX Instruction Reference lists a bunch of bits that are
8352 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8353 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8354 * Otherwise, we should fail with #UD. But most faulting conditions
8355 * have already been checked by hardware, prior to the VM-exit for
8356 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8357 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008358 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008359 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008360 kvm_queue_exception(vcpu, UD_VECTOR);
8361 return 1;
8362 }
8363
Felix Wilhelm727ba742018-06-11 09:43:44 +02008364 /* CPL=0 must be checked manually. */
8365 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008366 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008367 return 1;
8368 }
8369
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008370 if (vmx->nested.vmxon)
8371 return nested_vmx_failValid(vcpu,
8372 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008373
Haozhong Zhang3b840802016-06-22 14:59:54 +08008374 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008375 != VMXON_NEEDED_FEATURES) {
8376 kvm_inject_gp(vcpu, 0);
8377 return 1;
8378 }
8379
Radim Krčmářcbf71272017-05-19 15:48:51 +02008380 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008381 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008382
8383 /*
8384 * SDM 3: 24.11.5
8385 * The first 4 bytes of VMXON region contain the supported
8386 * VMCS revision identifier
8387 *
8388 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8389 * which replaces physical address width with 32
8390 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008391 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8392 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008393
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008394 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008395 if (is_error_page(page))
8396 return nested_vmx_failInvalid(vcpu);
8397
Radim Krčmářcbf71272017-05-19 15:48:51 +02008398 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8399 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008400 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008401 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008402 }
8403 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008404 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008405
8406 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008407 ret = enter_vmx_operation(vcpu);
8408 if (ret)
8409 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008410
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008411 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008412}
8413
8414/*
8415 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8416 * for running VMX instructions (except VMXON, whose prerequisites are
8417 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008418 * Note that many of these exceptions have priority over VM exits, so they
8419 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008420 */
8421static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8422{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008423 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008424 kvm_queue_exception(vcpu, UD_VECTOR);
8425 return 0;
8426 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008427
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008428 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008429 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008430 return 0;
8431 }
8432
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008433 return 1;
8434}
8435
David Matlack8ca44e82017-08-01 14:00:39 -07008436static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8437{
8438 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8439 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8440}
8441
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008442static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008443{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008444 struct vcpu_vmx *vmx = to_vmx(vcpu);
8445
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008446 if (vmx->nested.current_vmptr == -1ull)
8447 return;
8448
Abel Gordon012f83c2013-04-18 14:39:25 +03008449 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008450 /* copy to memory all shadowed fields in case
8451 they were modified */
8452 copy_shadow_to_vmcs12(vmx);
8453 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008454 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008455 }
Wincy Van705699a2015-02-03 23:58:17 +08008456 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008457
8458 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008459 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008460 vmx->nested.current_vmptr >> PAGE_SHIFT,
8461 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008462
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008463 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8464
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008465 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008466}
8467
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008468/*
8469 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8470 * just stops using VMX.
8471 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008472static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008473{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008474 struct vcpu_vmx *vmx = to_vmx(vcpu);
8475
Wanpeng Lib7455822017-11-22 14:04:00 -08008476 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008477 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008478
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008479 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008480 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008481 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008482 vmx->nested.posted_intr_nv = -1;
8483 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008484 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008485 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008486 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8487 free_vmcs(vmx->vmcs01.shadow_vmcs);
8488 vmx->vmcs01.shadow_vmcs = NULL;
8489 }
David Matlack4f2777b2016-07-13 17:16:37 -07008490 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008491 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008492 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008493 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008494 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008495 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008496 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008497 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008498 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008499 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008500 }
Wincy Van705699a2015-02-03 23:58:17 +08008501 if (vmx->nested.pi_desc_page) {
8502 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008503 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008504 vmx->nested.pi_desc_page = NULL;
8505 vmx->nested.pi_desc = NULL;
8506 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008507
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008508 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8509
Jim Mattsonde3a0022017-11-27 17:22:25 -06008510 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008511}
8512
8513/* Emulate the VMXOFF instruction */
8514static int handle_vmoff(struct kvm_vcpu *vcpu)
8515{
8516 if (!nested_vmx_check_permission(vcpu))
8517 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008518 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008519 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008520}
8521
Nadav Har'El27d6c862011-05-25 23:06:59 +03008522/* Emulate the VMCLEAR instruction */
8523static int handle_vmclear(struct kvm_vcpu *vcpu)
8524{
8525 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008526 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008527 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008528
8529 if (!nested_vmx_check_permission(vcpu))
8530 return 1;
8531
Radim Krčmářcbf71272017-05-19 15:48:51 +02008532 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008533 return 1;
8534
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008535 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8536 return nested_vmx_failValid(vcpu,
8537 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008538
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008539 if (vmptr == vmx->nested.vmxon_ptr)
8540 return nested_vmx_failValid(vcpu,
8541 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008542
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008543 if (vmptr == vmx->nested.current_vmptr)
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008544 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008545
Jim Mattson587d7e722017-03-02 12:41:48 -08008546 kvm_vcpu_write_guest(vcpu,
8547 vmptr + offsetof(struct vmcs12, launch_state),
8548 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008549
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008550 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008551}
8552
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008553static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8554
8555/* Emulate the VMLAUNCH instruction */
8556static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8557{
8558 return nested_vmx_run(vcpu, true);
8559}
8560
8561/* Emulate the VMRESUME instruction */
8562static int handle_vmresume(struct kvm_vcpu *vcpu)
8563{
8564
8565 return nested_vmx_run(vcpu, false);
8566}
8567
Nadav Har'El49f705c2011-05-25 23:08:30 +03008568/*
8569 * Read a vmcs12 field. Since these can have varying lengths and we return
8570 * one type, we chose the biggest type (u64) and zero-extend the return value
8571 * to that size. Note that the caller, handle_vmread, might need to use only
8572 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8573 * 64-bit fields are to be returned).
8574 */
Liran Alone2536742018-06-23 02:35:02 +03008575static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008576 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008577{
8578 short offset = vmcs_field_to_offset(field);
8579 char *p;
8580
8581 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008582 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008583
Liran Alone2536742018-06-23 02:35:02 +03008584 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008585
Jim Mattsond37f4262017-12-22 12:12:16 -08008586 switch (vmcs_field_width(field)) {
8587 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008588 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008589 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008590 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008591 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008592 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008593 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008594 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008596 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008597 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008598 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008599 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008600 WARN_ON(1);
8601 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008602 }
8603}
8604
Abel Gordon20b97fe2013-04-18 14:36:25 +03008605
Liran Alone2536742018-06-23 02:35:02 +03008606static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008607 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008608 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008609 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008610 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008611 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008612
Jim Mattsond37f4262017-12-22 12:12:16 -08008613 switch (vmcs_field_width(field)) {
8614 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008615 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008616 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008617 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008618 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008619 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008620 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008621 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008622 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008623 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008624 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008625 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008626 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008627 WARN_ON(1);
8628 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008629 }
8630
8631}
8632
Jim Mattsonf4160e42018-05-29 09:11:33 -07008633/*
8634 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8635 * they have been modified by the L1 guest. Note that the "read-only"
8636 * VM-exit information fields are actually writable if the vCPU is
8637 * configured to support "VMWRITE to any supported field in the VMCS."
8638 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008639static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8640{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008641 const u16 *fields[] = {
8642 shadow_read_write_fields,
8643 shadow_read_only_fields
8644 };
8645 const int max_fields[] = {
8646 max_shadow_read_write_fields,
8647 max_shadow_read_only_fields
8648 };
8649 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008650 unsigned long field;
8651 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008652 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008653
Jan Kiszka282da872014-10-08 18:05:39 +02008654 preempt_disable();
8655
Abel Gordon16f5b902013-04-18 14:38:25 +03008656 vmcs_load(shadow_vmcs);
8657
Jim Mattsonf4160e42018-05-29 09:11:33 -07008658 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8659 for (i = 0; i < max_fields[q]; i++) {
8660 field = fields[q][i];
8661 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008662 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008663 }
8664 /*
8665 * Skip the VM-exit information fields if they are read-only.
8666 */
8667 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8668 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008669 }
8670
8671 vmcs_clear(shadow_vmcs);
8672 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008673
8674 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008675}
8676
Abel Gordonc3114422013-04-18 14:38:55 +03008677static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8678{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008679 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008680 shadow_read_write_fields,
8681 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008682 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008683 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008684 max_shadow_read_write_fields,
8685 max_shadow_read_only_fields
8686 };
8687 int i, q;
8688 unsigned long field;
8689 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008690 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008691
8692 vmcs_load(shadow_vmcs);
8693
Mathias Krausec2bae892013-06-26 20:36:21 +02008694 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008695 for (i = 0; i < max_fields[q]; i++) {
8696 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008697 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008698 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008699 }
8700 }
8701
8702 vmcs_clear(shadow_vmcs);
8703 vmcs_load(vmx->loaded_vmcs->vmcs);
8704}
8705
Nadav Har'El49f705c2011-05-25 23:08:30 +03008706static int handle_vmread(struct kvm_vcpu *vcpu)
8707{
8708 unsigned long field;
8709 u64 field_value;
8710 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8711 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8712 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008713 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008714
Kyle Hueyeb277562016-11-29 12:40:39 -08008715 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008716 return 1;
8717
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008718 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8719 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008720
Liran Alon6d894f42018-06-23 02:35:09 +03008721 if (!is_guest_mode(vcpu))
8722 vmcs12 = get_vmcs12(vcpu);
8723 else {
8724 /*
8725 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8726 * to shadowed-field sets the ALU flags for VMfailInvalid.
8727 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008728 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8729 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008730 vmcs12 = get_shadow_vmcs12(vcpu);
8731 }
8732
Nadav Har'El49f705c2011-05-25 23:08:30 +03008733 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008734 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008735 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008736 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
8737 return nested_vmx_failValid(vcpu,
8738 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8739
Nadav Har'El49f705c2011-05-25 23:08:30 +03008740 /*
8741 * Now copy part of this value to register or memory, as requested.
8742 * Note that the number of bits actually copied is 32 or 64 depending
8743 * on the guest's mode (32 or 64 bit), not on the given field's length.
8744 */
8745 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008746 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008747 field_value);
8748 } else {
8749 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008750 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008751 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008752 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008753 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8754 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008755 }
8756
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008757 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008758}
8759
8760
8761static int handle_vmwrite(struct kvm_vcpu *vcpu)
8762{
8763 unsigned long field;
8764 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008765 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008766 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8767 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008768
Nadav Har'El49f705c2011-05-25 23:08:30 +03008769 /* The value to write might be 32 or 64 bits, depending on L1's long
8770 * mode, and eventually we need to write that into a field of several
8771 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008772 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773 * bits into the vmcs12 field.
8774 */
8775 u64 field_value = 0;
8776 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008777 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008778
Kyle Hueyeb277562016-11-29 12:40:39 -08008779 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008780 return 1;
8781
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008782 if (vmx->nested.current_vmptr == -1ull)
8783 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008784
Nadav Har'El49f705c2011-05-25 23:08:30 +03008785 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008786 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008787 (((vmx_instruction_info) >> 3) & 0xf));
8788 else {
8789 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008790 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008791 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008792 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8793 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008794 kvm_inject_page_fault(vcpu, &e);
8795 return 1;
8796 }
8797 }
8798
8799
Nadav Amit27e6fb52014-06-18 17:19:26 +03008800 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008801 /*
8802 * If the vCPU supports "VMWRITE to any supported field in the
8803 * VMCS," then the "read-only" fields are actually read/write.
8804 */
8805 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008806 !nested_cpu_has_vmwrite_any_field(vcpu))
8807 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008808 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008809
Liran Alon6d894f42018-06-23 02:35:09 +03008810 if (!is_guest_mode(vcpu))
8811 vmcs12 = get_vmcs12(vcpu);
8812 else {
8813 /*
8814 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8815 * to shadowed-field sets the ALU flags for VMfailInvalid.
8816 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008817 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8818 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008819 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008820 }
8821
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008822 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
8823 return nested_vmx_failValid(vcpu,
8824 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008825
Liran Alon6d894f42018-06-23 02:35:09 +03008826 /*
8827 * Do not track vmcs12 dirty-state if in guest-mode
8828 * as we actually dirty shadow vmcs12 instead of vmcs12.
8829 */
8830 if (!is_guest_mode(vcpu)) {
8831 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008832#define SHADOW_FIELD_RW(x) case x:
8833#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008834 /*
8835 * The fields that can be updated by L1 without a vmexit are
8836 * always updated in the vmcs02, the others go down the slow
8837 * path of prepare_vmcs02.
8838 */
8839 break;
8840 default:
8841 vmx->nested.dirty_vmcs12 = true;
8842 break;
8843 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008844 }
8845
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008846 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008847}
8848
Jim Mattsona8bc2842016-11-30 12:03:44 -08008849static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8850{
8851 vmx->nested.current_vmptr = vmptr;
8852 if (enable_shadow_vmcs) {
8853 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8854 SECONDARY_EXEC_SHADOW_VMCS);
8855 vmcs_write64(VMCS_LINK_POINTER,
8856 __pa(vmx->vmcs01.shadow_vmcs));
8857 vmx->nested.sync_shadow_vmcs = true;
8858 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008859 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008860}
8861
Nadav Har'El63846662011-05-25 23:07:29 +03008862/* Emulate the VMPTRLD instruction */
8863static int handle_vmptrld(struct kvm_vcpu *vcpu)
8864{
8865 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008866 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008867
8868 if (!nested_vmx_check_permission(vcpu))
8869 return 1;
8870
Radim Krčmářcbf71272017-05-19 15:48:51 +02008871 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008872 return 1;
8873
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008874 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8875 return nested_vmx_failValid(vcpu,
8876 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008877
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008878 if (vmptr == vmx->nested.vmxon_ptr)
8879 return nested_vmx_failValid(vcpu,
8880 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008881
Nadav Har'El63846662011-05-25 23:07:29 +03008882 if (vmx->nested.current_vmptr != vmptr) {
8883 struct vmcs12 *new_vmcs12;
8884 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008885 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008886 if (is_error_page(page))
8887 return nested_vmx_failInvalid(vcpu);
8888
Nadav Har'El63846662011-05-25 23:07:29 +03008889 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008890 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008891 (new_vmcs12->hdr.shadow_vmcs &&
8892 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008893 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008894 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008895 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03008896 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03008897 }
Nadav Har'El63846662011-05-25 23:07:29 +03008898
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008899 nested_release_vmcs12(vcpu);
8900
David Matlack4f2777b2016-07-13 17:16:37 -07008901 /*
8902 * Load VMCS12 from guest memory since it is not already
8903 * cached.
8904 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008905 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8906 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008907 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008908
Jim Mattsona8bc2842016-11-30 12:03:44 -08008909 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008910 }
8911
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008912 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008913}
8914
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008915/* Emulate the VMPTRST instruction */
8916static int handle_vmptrst(struct kvm_vcpu *vcpu)
8917{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008918 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8919 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8920 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008921 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008922 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008923
8924 if (!nested_vmx_check_permission(vcpu))
8925 return 1;
8926
Sean Christopherson0a06d422018-07-19 10:31:00 -07008927 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008928 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008929 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008930 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8931 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008932 kvm_inject_page_fault(vcpu, &e);
8933 return 1;
8934 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008935 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008936}
8937
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008938/* Emulate the INVEPT instruction */
8939static int handle_invept(struct kvm_vcpu *vcpu)
8940{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008942 u32 vmx_instruction_info, types;
8943 unsigned long type;
8944 gva_t gva;
8945 struct x86_exception e;
8946 struct {
8947 u64 eptp, gpa;
8948 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008949
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008950 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008951 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008952 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008953 kvm_queue_exception(vcpu, UD_VECTOR);
8954 return 1;
8955 }
8956
8957 if (!nested_vmx_check_permission(vcpu))
8958 return 1;
8959
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008960 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008961 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008962
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008963 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008965 if (type >= 32 || !(types & (1 << type)))
8966 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008967 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008968
8969 /* According to the Intel VMX instruction reference, the memory
8970 * operand is read even if it isn't needed (e.g., for type==global)
8971 */
8972 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008973 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008974 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008975 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008976 kvm_inject_page_fault(vcpu, &e);
8977 return 1;
8978 }
8979
8980 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008981 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008982 /*
8983 * TODO: track mappings and invalidate
8984 * single context requests appropriately
8985 */
8986 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008987 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008988 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008989 break;
8990 default:
8991 BUG_ON(1);
8992 break;
8993 }
8994
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008995 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008996}
8997
Liran Alon3d5bdae2018-10-08 23:42:18 +03008998static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8999{
9000 struct vcpu_vmx *vmx = to_vmx(vcpu);
9001
9002 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9003}
9004
Petr Matouseka642fc32014-09-23 20:22:30 +02009005static int handle_invvpid(struct kvm_vcpu *vcpu)
9006{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009007 struct vcpu_vmx *vmx = to_vmx(vcpu);
9008 u32 vmx_instruction_info;
9009 unsigned long type, types;
9010 gva_t gva;
9011 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009012 struct {
9013 u64 vpid;
9014 u64 gla;
9015 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009016 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009017
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009018 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009019 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009020 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009021 kvm_queue_exception(vcpu, UD_VECTOR);
9022 return 1;
9023 }
9024
9025 if (!nested_vmx_check_permission(vcpu))
9026 return 1;
9027
9028 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9029 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9030
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009031 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009032 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009033
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009034 if (type >= 32 || !(types & (1 << type)))
9035 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009036 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009037
9038 /* according to the intel vmx instruction reference, the memory
9039 * operand is read even if it isn't needed (e.g., for type==global)
9040 */
9041 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9042 vmx_instruction_info, false, &gva))
9043 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009044 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009045 kvm_inject_page_fault(vcpu, &e);
9046 return 1;
9047 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009048 if (operand.vpid >> 16)
9049 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009050 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009051
Liran Alon3d5bdae2018-10-08 23:42:18 +03009052 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009053 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009054 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009055 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009056 is_noncanonical_address(operand.gla, vcpu))
9057 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009058 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009059 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009060 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009061 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009062 } else
Liran Alon327c0722018-10-08 23:42:19 +03009063 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009064 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009065 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009066 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009067 if (!operand.vpid)
9068 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009069 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009070 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009071 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009072 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009073 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009074 break;
9075 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009076 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009077 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009078 }
9079
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009080 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009081}
9082
Junaid Shahideb4b2482018-06-27 14:59:14 -07009083static int handle_invpcid(struct kvm_vcpu *vcpu)
9084{
9085 u32 vmx_instruction_info;
9086 unsigned long type;
9087 bool pcid_enabled;
9088 gva_t gva;
9089 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009090 unsigned i;
9091 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009092 struct {
9093 u64 pcid;
9094 u64 gla;
9095 } operand;
9096
9097 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9098 kvm_queue_exception(vcpu, UD_VECTOR);
9099 return 1;
9100 }
9101
9102 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9103 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9104
9105 if (type > 3) {
9106 kvm_inject_gp(vcpu, 0);
9107 return 1;
9108 }
9109
9110 /* According to the Intel instruction reference, the memory operand
9111 * is read even if it isn't needed (e.g., for type==all)
9112 */
9113 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9114 vmx_instruction_info, false, &gva))
9115 return 1;
9116
9117 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9118 kvm_inject_page_fault(vcpu, &e);
9119 return 1;
9120 }
9121
9122 if (operand.pcid >> 12 != 0) {
9123 kvm_inject_gp(vcpu, 0);
9124 return 1;
9125 }
9126
9127 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9128
9129 switch (type) {
9130 case INVPCID_TYPE_INDIV_ADDR:
9131 if ((!pcid_enabled && (operand.pcid != 0)) ||
9132 is_noncanonical_address(operand.gla, vcpu)) {
9133 kvm_inject_gp(vcpu, 0);
9134 return 1;
9135 }
9136 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9137 return kvm_skip_emulated_instruction(vcpu);
9138
9139 case INVPCID_TYPE_SINGLE_CTXT:
9140 if (!pcid_enabled && (operand.pcid != 0)) {
9141 kvm_inject_gp(vcpu, 0);
9142 return 1;
9143 }
9144
9145 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9146 kvm_mmu_sync_roots(vcpu);
9147 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9148 }
9149
Junaid Shahidb94742c2018-06-27 14:59:20 -07009150 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009151 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009152 == operand.pcid)
9153 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009154
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009155 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009156 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009157 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009158 * given PCID, then nothing needs to be done here because a
9159 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009160 */
9161
9162 return kvm_skip_emulated_instruction(vcpu);
9163
9164 case INVPCID_TYPE_ALL_NON_GLOBAL:
9165 /*
9166 * Currently, KVM doesn't mark global entries in the shadow
9167 * page tables, so a non-global flush just degenerates to a
9168 * global flush. If needed, we could optimize this later by
9169 * keeping track of global entries in shadow page tables.
9170 */
9171
9172 /* fall-through */
9173 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9174 kvm_mmu_unload(vcpu);
9175 return kvm_skip_emulated_instruction(vcpu);
9176
9177 default:
9178 BUG(); /* We have already checked above that type <= 3 */
9179 }
9180}
9181
Kai Huang843e4332015-01-28 10:54:28 +08009182static int handle_pml_full(struct kvm_vcpu *vcpu)
9183{
9184 unsigned long exit_qualification;
9185
9186 trace_kvm_pml_full(vcpu->vcpu_id);
9187
9188 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9189
9190 /*
9191 * PML buffer FULL happened while executing iret from NMI,
9192 * "blocked by NMI" bit has to be set before next VM entry.
9193 */
9194 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009195 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009196 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9197 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9198 GUEST_INTR_STATE_NMI);
9199
9200 /*
9201 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9202 * here.., and there's no userspace involvement needed for PML.
9203 */
9204 return 1;
9205}
9206
Yunhong Jiang64672c92016-06-13 14:19:59 -07009207static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9208{
Sean Christophersond264ee02018-08-27 15:21:12 -07009209 if (!to_vmx(vcpu)->req_immediate_exit)
9210 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009211 return 1;
9212}
9213
Bandan Das41ab9372017-08-03 15:54:43 -04009214static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9215{
9216 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009217 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9218
9219 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009220 switch (address & VMX_EPTP_MT_MASK) {
9221 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009222 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009223 return false;
9224 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009225 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009226 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009227 return false;
9228 break;
9229 default:
9230 return false;
9231 }
9232
David Hildenbrandbb97a012017-08-10 23:15:28 +02009233 /* only 4 levels page-walk length are valid */
9234 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009235 return false;
9236
9237 /* Reserved bits should not be set */
9238 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9239 return false;
9240
9241 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009242 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009243 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009244 return false;
9245 }
9246
9247 return true;
9248}
9249
9250static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9251 struct vmcs12 *vmcs12)
9252{
9253 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9254 u64 address;
9255 bool accessed_dirty;
9256 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9257
9258 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9259 !nested_cpu_has_ept(vmcs12))
9260 return 1;
9261
9262 if (index >= VMFUNC_EPTP_ENTRIES)
9263 return 1;
9264
9265
9266 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9267 &address, index * 8, 8))
9268 return 1;
9269
David Hildenbrandbb97a012017-08-10 23:15:28 +02009270 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009271
9272 /*
9273 * If the (L2) guest does a vmfunc to the currently
9274 * active ept pointer, we don't have to do anything else
9275 */
9276 if (vmcs12->ept_pointer != address) {
9277 if (!valid_ept_address(vcpu, address))
9278 return 1;
9279
9280 kvm_mmu_unload(vcpu);
9281 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d95942018-10-08 21:28:10 +02009282 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009283 vmcs12->ept_pointer = address;
9284 /*
9285 * TODO: Check what's the correct approach in case
9286 * mmu reload fails. Currently, we just let the next
9287 * reload potentially fail
9288 */
9289 kvm_mmu_reload(vcpu);
9290 }
9291
9292 return 0;
9293}
9294
Bandan Das2a499e42017-08-03 15:54:41 -04009295static int handle_vmfunc(struct kvm_vcpu *vcpu)
9296{
Bandan Das27c42a12017-08-03 15:54:42 -04009297 struct vcpu_vmx *vmx = to_vmx(vcpu);
9298 struct vmcs12 *vmcs12;
9299 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9300
9301 /*
9302 * VMFUNC is only supported for nested guests, but we always enable the
9303 * secondary control for simplicity; for non-nested mode, fake that we
9304 * didn't by injecting #UD.
9305 */
9306 if (!is_guest_mode(vcpu)) {
9307 kvm_queue_exception(vcpu, UD_VECTOR);
9308 return 1;
9309 }
9310
9311 vmcs12 = get_vmcs12(vcpu);
9312 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9313 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009314
9315 switch (function) {
9316 case 0:
9317 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9318 goto fail;
9319 break;
9320 default:
9321 goto fail;
9322 }
9323 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009324
9325fail:
9326 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9327 vmcs_read32(VM_EXIT_INTR_INFO),
9328 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009329 return 1;
9330}
9331
Sean Christopherson0b665d32018-08-14 09:33:34 -07009332static int handle_encls(struct kvm_vcpu *vcpu)
9333{
9334 /*
9335 * SGX virtualization is not yet supported. There is no software
9336 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9337 * to prevent the guest from executing ENCLS.
9338 */
9339 kvm_queue_exception(vcpu, UD_VECTOR);
9340 return 1;
9341}
9342
Nadav Har'El0140cae2011-05-25 23:06:28 +03009343/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009344 * The exit handlers return 1 if the exit was handled fully and guest execution
9345 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9346 * to be done to userspace and return 0.
9347 */
Mathias Krause772e0312012-08-30 01:30:19 +02009348static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009349 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9350 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009351 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009352 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009353 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009354 [EXIT_REASON_CR_ACCESS] = handle_cr,
9355 [EXIT_REASON_DR_ACCESS] = handle_dr,
9356 [EXIT_REASON_CPUID] = handle_cpuid,
9357 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9358 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9359 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9360 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009361 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009362 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009363 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009364 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009365 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009366 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009367 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009368 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009369 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009370 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009371 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009372 [EXIT_REASON_VMOFF] = handle_vmoff,
9373 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009374 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9375 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009376 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009377 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009378 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009379 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009380 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009381 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009382 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9383 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009384 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9385 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009386 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009387 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009388 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009389 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009390 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009391 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009392 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009393 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009394 [EXIT_REASON_XSAVES] = handle_xsaves,
9395 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009396 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009397 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009398 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009399 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009400 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009401};
9402
9403static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009404 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009405
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009406static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9407 struct vmcs12 *vmcs12)
9408{
9409 unsigned long exit_qualification;
9410 gpa_t bitmap, last_bitmap;
9411 unsigned int port;
9412 int size;
9413 u8 b;
9414
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009415 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009416 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009417
9418 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9419
9420 port = exit_qualification >> 16;
9421 size = (exit_qualification & 7) + 1;
9422
9423 last_bitmap = (gpa_t)-1;
9424 b = -1;
9425
9426 while (size > 0) {
9427 if (port < 0x8000)
9428 bitmap = vmcs12->io_bitmap_a;
9429 else if (port < 0x10000)
9430 bitmap = vmcs12->io_bitmap_b;
9431 else
Joe Perches1d804d02015-03-30 16:46:09 -07009432 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009433 bitmap += (port & 0x7fff) / 8;
9434
9435 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009436 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009437 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009438 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009439 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009440
9441 port++;
9442 size--;
9443 last_bitmap = bitmap;
9444 }
9445
Joe Perches1d804d02015-03-30 16:46:09 -07009446 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009447}
9448
Nadav Har'El644d7112011-05-25 23:12:35 +03009449/*
9450 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9451 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9452 * disinterest in the current event (read or write a specific MSR) by using an
9453 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9454 */
9455static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9456 struct vmcs12 *vmcs12, u32 exit_reason)
9457{
9458 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9459 gpa_t bitmap;
9460
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009461 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009462 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009463
9464 /*
9465 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9466 * for the four combinations of read/write and low/high MSR numbers.
9467 * First we need to figure out which of the four to use:
9468 */
9469 bitmap = vmcs12->msr_bitmap;
9470 if (exit_reason == EXIT_REASON_MSR_WRITE)
9471 bitmap += 2048;
9472 if (msr_index >= 0xc0000000) {
9473 msr_index -= 0xc0000000;
9474 bitmap += 1024;
9475 }
9476
9477 /* Then read the msr_index'th bit from this bitmap: */
9478 if (msr_index < 1024*8) {
9479 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009480 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009481 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009482 return 1 & (b >> (msr_index & 7));
9483 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009484 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009485}
9486
9487/*
9488 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9489 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9490 * intercept (via guest_host_mask etc.) the current event.
9491 */
9492static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9493 struct vmcs12 *vmcs12)
9494{
9495 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9496 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009497 int reg;
9498 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009499
9500 switch ((exit_qualification >> 4) & 3) {
9501 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009502 reg = (exit_qualification >> 8) & 15;
9503 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009504 switch (cr) {
9505 case 0:
9506 if (vmcs12->cr0_guest_host_mask &
9507 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009508 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009509 break;
9510 case 3:
9511 if ((vmcs12->cr3_target_count >= 1 &&
9512 vmcs12->cr3_target_value0 == val) ||
9513 (vmcs12->cr3_target_count >= 2 &&
9514 vmcs12->cr3_target_value1 == val) ||
9515 (vmcs12->cr3_target_count >= 3 &&
9516 vmcs12->cr3_target_value2 == val) ||
9517 (vmcs12->cr3_target_count >= 4 &&
9518 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009519 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009520 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009521 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009522 break;
9523 case 4:
9524 if (vmcs12->cr4_guest_host_mask &
9525 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009526 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009527 break;
9528 case 8:
9529 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009530 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009531 break;
9532 }
9533 break;
9534 case 2: /* clts */
9535 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9536 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009537 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009538 break;
9539 case 1: /* mov from cr */
9540 switch (cr) {
9541 case 3:
9542 if (vmcs12->cpu_based_vm_exec_control &
9543 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 break;
9546 case 8:
9547 if (vmcs12->cpu_based_vm_exec_control &
9548 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009549 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009550 break;
9551 }
9552 break;
9553 case 3: /* lmsw */
9554 /*
9555 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9556 * cr0. Other attempted changes are ignored, with no exit.
9557 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009558 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009559 if (vmcs12->cr0_guest_host_mask & 0xe &
9560 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009561 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009562 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9563 !(vmcs12->cr0_read_shadow & 0x1) &&
9564 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009565 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009566 break;
9567 }
Joe Perches1d804d02015-03-30 16:46:09 -07009568 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009569}
9570
Liran Alona7cde482018-06-23 02:35:10 +03009571static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9572 struct vmcs12 *vmcs12, gpa_t bitmap)
9573{
9574 u32 vmx_instruction_info;
9575 unsigned long field;
9576 u8 b;
9577
9578 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9579 return true;
9580
9581 /* Decode instruction info and find the field to access */
9582 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9583 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9584
9585 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9586 if (field >> 15)
9587 return true;
9588
9589 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9590 return true;
9591
9592 return 1 & (b >> (field & 7));
9593}
9594
Nadav Har'El644d7112011-05-25 23:12:35 +03009595/*
9596 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9597 * should handle it ourselves in L0 (and then continue L2). Only call this
9598 * when in is_guest_mode (L2).
9599 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009600static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009601{
Nadav Har'El644d7112011-05-25 23:12:35 +03009602 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9603 struct vcpu_vmx *vmx = to_vmx(vcpu);
9604 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9605
Jim Mattson4f350c62017-09-14 16:31:44 -07009606 if (vmx->nested.nested_run_pending)
9607 return false;
9608
9609 if (unlikely(vmx->fail)) {
9610 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9611 vmcs_read32(VM_INSTRUCTION_ERROR));
9612 return true;
9613 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009614
David Matlackc9f04402017-08-01 14:00:40 -07009615 /*
9616 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009617 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9618 * Page). The CPU may write to these pages via their host
9619 * physical address while L2 is running, bypassing any
9620 * address-translation-based dirty tracking (e.g. EPT write
9621 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009622 *
9623 * Mark them dirty on every exit from L2 to prevent them from
9624 * getting out of sync with dirty tracking.
9625 */
9626 nested_mark_vmcs12_pages_dirty(vcpu);
9627
Jim Mattson4f350c62017-09-14 16:31:44 -07009628 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9629 vmcs_readl(EXIT_QUALIFICATION),
9630 vmx->idt_vectoring_info,
9631 intr_info,
9632 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9633 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009634
9635 switch (exit_reason) {
9636 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009637 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009638 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009639 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009640 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009641 else if (is_debug(intr_info) &&
9642 vcpu->guest_debug &
9643 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9644 return false;
9645 else if (is_breakpoint(intr_info) &&
9646 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9647 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009648 return vmcs12->exception_bitmap &
9649 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9650 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009651 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009652 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009653 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009654 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009655 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009656 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009657 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009658 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009659 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009661 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009662 case EXIT_REASON_HLT:
9663 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9664 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009665 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 case EXIT_REASON_INVLPG:
9667 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9668 case EXIT_REASON_RDPMC:
9669 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009670 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009671 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009672 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009673 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009674 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009675 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009676 case EXIT_REASON_VMREAD:
9677 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9678 vmcs12->vmread_bitmap);
9679 case EXIT_REASON_VMWRITE:
9680 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9681 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009682 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9683 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009684 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009685 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009686 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009687 /*
9688 * VMX instructions trap unconditionally. This allows L1 to
9689 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9690 */
Joe Perches1d804d02015-03-30 16:46:09 -07009691 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009692 case EXIT_REASON_CR_ACCESS:
9693 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9694 case EXIT_REASON_DR_ACCESS:
9695 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9696 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009697 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009698 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9699 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009700 case EXIT_REASON_MSR_READ:
9701 case EXIT_REASON_MSR_WRITE:
9702 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9703 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009704 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009705 case EXIT_REASON_MWAIT_INSTRUCTION:
9706 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009707 case EXIT_REASON_MONITOR_TRAP_FLAG:
9708 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009709 case EXIT_REASON_MONITOR_INSTRUCTION:
9710 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9711 case EXIT_REASON_PAUSE_INSTRUCTION:
9712 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9713 nested_cpu_has2(vmcs12,
9714 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9715 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009716 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009717 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009718 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009719 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009720 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009721 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009722 /*
9723 * The controls for "virtualize APIC accesses," "APIC-
9724 * register virtualization," and "virtual-interrupt
9725 * delivery" only come from vmcs12.
9726 */
Joe Perches1d804d02015-03-30 16:46:09 -07009727 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009728 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009729 /*
9730 * L0 always deals with the EPT violation. If nested EPT is
9731 * used, and the nested mmu code discovers that the address is
9732 * missing in the guest EPT table (EPT12), the EPT violation
9733 * will be injected with nested_ept_inject_page_fault()
9734 */
Joe Perches1d804d02015-03-30 16:46:09 -07009735 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009736 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009737 /*
9738 * L2 never uses directly L1's EPT, but rather L0's own EPT
9739 * table (shadow on EPT) or a merged EPT table that L0 built
9740 * (EPT on EPT). So any problems with the structure of the
9741 * table is L0's fault.
9742 */
Joe Perches1d804d02015-03-30 16:46:09 -07009743 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009744 case EXIT_REASON_INVPCID:
9745 return
9746 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9747 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009748 case EXIT_REASON_WBINVD:
9749 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9750 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009752 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9753 /*
9754 * This should never happen, since it is not possible to
9755 * set XSS to a non-zero value---neither in L1 nor in L2.
9756 * If if it were, XSS would have to be checked against
9757 * the XSS exit bitmap in vmcs12.
9758 */
9759 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009760 case EXIT_REASON_PREEMPTION_TIMER:
9761 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009762 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009763 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009764 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009765 case EXIT_REASON_VMFUNC:
9766 /* VM functions are emulated through L2->L0 vmexits. */
9767 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009768 case EXIT_REASON_ENCLS:
9769 /* SGX is never exposed to L1 */
9770 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009771 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009772 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009773 }
9774}
9775
Paolo Bonzini7313c692017-07-27 10:31:25 +02009776static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9777{
9778 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9779
9780 /*
9781 * At this point, the exit interruption info in exit_intr_info
9782 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9783 * we need to query the in-kernel LAPIC.
9784 */
9785 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9786 if ((exit_intr_info &
9787 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9788 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9789 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9790 vmcs12->vm_exit_intr_error_code =
9791 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9792 }
9793
9794 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9795 vmcs_readl(EXIT_QUALIFICATION));
9796 return 1;
9797}
9798
Avi Kivity586f9602010-11-18 13:09:54 +02009799static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9800{
9801 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9802 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9803}
9804
Kai Huanga3eaa862015-11-04 13:46:05 +08009805static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009806{
Kai Huanga3eaa862015-11-04 13:46:05 +08009807 if (vmx->pml_pg) {
9808 __free_page(vmx->pml_pg);
9809 vmx->pml_pg = NULL;
9810 }
Kai Huang843e4332015-01-28 10:54:28 +08009811}
9812
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009813static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009814{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009815 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009816 u64 *pml_buf;
9817 u16 pml_idx;
9818
9819 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9820
9821 /* Do nothing if PML buffer is empty */
9822 if (pml_idx == (PML_ENTITY_NUM - 1))
9823 return;
9824
9825 /* PML index always points to next available PML buffer entity */
9826 if (pml_idx >= PML_ENTITY_NUM)
9827 pml_idx = 0;
9828 else
9829 pml_idx++;
9830
9831 pml_buf = page_address(vmx->pml_pg);
9832 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9833 u64 gpa;
9834
9835 gpa = pml_buf[pml_idx];
9836 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009837 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009838 }
9839
9840 /* reset PML index */
9841 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9842}
9843
9844/*
9845 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9846 * Called before reporting dirty_bitmap to userspace.
9847 */
9848static void kvm_flush_pml_buffers(struct kvm *kvm)
9849{
9850 int i;
9851 struct kvm_vcpu *vcpu;
9852 /*
9853 * We only need to kick vcpu out of guest mode here, as PML buffer
9854 * is flushed at beginning of all VMEXITs, and it's obvious that only
9855 * vcpus running in guest are possible to have unflushed GPAs in PML
9856 * buffer.
9857 */
9858 kvm_for_each_vcpu(i, vcpu, kvm)
9859 kvm_vcpu_kick(vcpu);
9860}
9861
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009862static void vmx_dump_sel(char *name, uint32_t sel)
9863{
9864 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009865 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009866 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9867 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9868 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9869}
9870
9871static void vmx_dump_dtsel(char *name, uint32_t limit)
9872{
9873 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9874 name, vmcs_read32(limit),
9875 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9876}
9877
9878static void dump_vmcs(void)
9879{
9880 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9881 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9882 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9883 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9884 u32 secondary_exec_control = 0;
9885 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009886 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009887 int i, n;
9888
9889 if (cpu_has_secondary_exec_ctrls())
9890 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9891
9892 pr_err("*** Guest State ***\n");
9893 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9894 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9895 vmcs_readl(CR0_GUEST_HOST_MASK));
9896 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9897 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9898 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9899 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9900 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9901 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009902 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9903 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9904 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9905 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009906 }
9907 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9908 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9909 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9910 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9911 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9912 vmcs_readl(GUEST_SYSENTER_ESP),
9913 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9914 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9915 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9916 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9917 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9918 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9919 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9920 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9921 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9922 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9923 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9924 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9925 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009926 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9927 efer, vmcs_read64(GUEST_IA32_PAT));
9928 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9929 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009930 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009931 if (cpu_has_load_perf_global_ctrl &&
9932 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009933 pr_err("PerfGlobCtl = 0x%016llx\n",
9934 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009935 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009936 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009937 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9938 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9939 vmcs_read32(GUEST_ACTIVITY_STATE));
9940 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9941 pr_err("InterruptStatus = %04x\n",
9942 vmcs_read16(GUEST_INTR_STATUS));
9943
9944 pr_err("*** Host State ***\n");
9945 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9946 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9947 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9948 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9949 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9950 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9951 vmcs_read16(HOST_TR_SELECTOR));
9952 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9953 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9954 vmcs_readl(HOST_TR_BASE));
9955 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9956 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9957 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9958 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9959 vmcs_readl(HOST_CR4));
9960 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9961 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9962 vmcs_read32(HOST_IA32_SYSENTER_CS),
9963 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9964 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009965 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9966 vmcs_read64(HOST_IA32_EFER),
9967 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009968 if (cpu_has_load_perf_global_ctrl &&
9969 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009970 pr_err("PerfGlobCtl = 0x%016llx\n",
9971 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009972
9973 pr_err("*** Control State ***\n");
9974 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9975 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9976 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9977 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9978 vmcs_read32(EXCEPTION_BITMAP),
9979 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9980 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9981 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9982 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9983 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9984 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9985 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9986 vmcs_read32(VM_EXIT_INTR_INFO),
9987 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9988 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9989 pr_err(" reason=%08x qualification=%016lx\n",
9990 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9991 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9992 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9993 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009994 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009995 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009996 pr_err("TSC Multiplier = 0x%016llx\n",
9997 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009998 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9999 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10000 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10001 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10002 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010003 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010004 n = vmcs_read32(CR3_TARGET_COUNT);
10005 for (i = 0; i + 1 < n; i += 4)
10006 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10007 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10008 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10009 if (i < n)
10010 pr_err("CR3 target%u=%016lx\n",
10011 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10012 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10013 pr_err("PLE Gap=%08x Window=%08x\n",
10014 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10015 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10016 pr_err("Virtual processor ID = 0x%04x\n",
10017 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10018}
10019
Avi Kivity6aa8b732006-12-10 02:21:36 -080010020/*
10021 * The guest has exited. See if we can fix it or if we need userspace
10022 * assistance.
10023 */
Avi Kivity851ba692009-08-24 11:10:17 +030010024static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010025{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010026 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010027 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010028 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010029
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010030 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10031
Kai Huang843e4332015-01-28 10:54:28 +080010032 /*
10033 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10034 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10035 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10036 * mode as if vcpus is in root mode, the PML buffer must has been
10037 * flushed already.
10038 */
10039 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010040 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010041
Mohammed Gamal80ced182009-09-01 12:48:18 +020010042 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010043 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010044 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010045
Paolo Bonzini7313c692017-07-27 10:31:25 +020010046 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10047 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010048
Mohammed Gamal51207022010-05-31 22:40:54 +030010049 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010050 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010051 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10052 vcpu->run->fail_entry.hardware_entry_failure_reason
10053 = exit_reason;
10054 return 0;
10055 }
10056
Avi Kivity29bd8a72007-09-10 17:27:03 +030010057 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010058 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10059 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010060 = vmcs_read32(VM_INSTRUCTION_ERROR);
10061 return 0;
10062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010063
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010064 /*
10065 * Note:
10066 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10067 * delivery event since it indicates guest is accessing MMIO.
10068 * The vm-exit can be triggered again after return to guest that
10069 * will cause infinite loop.
10070 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010071 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010072 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010073 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010074 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010075 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10076 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10077 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010078 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010079 vcpu->run->internal.data[0] = vectoring_info;
10080 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010081 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10082 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10083 vcpu->run->internal.ndata++;
10084 vcpu->run->internal.data[3] =
10085 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10086 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010087 return 0;
10088 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010089
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010090 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010091 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10092 if (vmx_interrupt_allowed(vcpu)) {
10093 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10094 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10095 vcpu->arch.nmi_pending) {
10096 /*
10097 * This CPU don't support us in finding the end of an
10098 * NMI-blocked window if the guest runs with IRQs
10099 * disabled. So we pull the trigger after 1 s of
10100 * futile waiting, but inform the user about this.
10101 */
10102 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10103 "state on VCPU %d after 1 s timeout\n",
10104 __func__, vcpu->vcpu_id);
10105 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10106 }
10107 }
10108
Avi Kivity6aa8b732006-12-10 02:21:36 -080010109 if (exit_reason < kvm_vmx_max_exit_handlers
10110 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010111 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010112 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010113 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10114 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010115 kvm_queue_exception(vcpu, UD_VECTOR);
10116 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010117 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010118}
10119
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010120/*
10121 * Software based L1D cache flush which is used when microcode providing
10122 * the cache control MSR is not loaded.
10123 *
10124 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10125 * flush it is required to read in 64 KiB because the replacement algorithm
10126 * is not exactly LRU. This could be sized at runtime via topology
10127 * information but as all relevant affected CPUs have 32KiB L1D cache size
10128 * there is no point in doing so.
10129 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010130static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010131{
10132 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010133
10134 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010135 * This code is only executed when the the flush mode is 'cond' or
10136 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010137 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010138 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010139 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010140
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010141 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010142 * Clear the per-vcpu flush bit, it gets set again
10143 * either from vcpu_run() or from one of the unsafe
10144 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010145 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010146 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010147 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010148
10149 /*
10150 * Clear the per-cpu flush bit, it gets set again from
10151 * the interrupt handlers.
10152 */
10153 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10154 kvm_clear_cpu_l1tf_flush_l1d();
10155
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010156 if (!flush_l1d)
10157 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010158 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010159
10160 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010161
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010162 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10163 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10164 return;
10165 }
10166
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010167 asm volatile(
10168 /* First ensure the pages are in the TLB */
10169 "xorl %%eax, %%eax\n"
10170 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010171 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010172 "addl $4096, %%eax\n\t"
10173 "cmpl %%eax, %[size]\n\t"
10174 "jne .Lpopulate_tlb\n\t"
10175 "xorl %%eax, %%eax\n\t"
10176 "cpuid\n\t"
10177 /* Now fill the cache */
10178 "xorl %%eax, %%eax\n"
10179 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010180 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010181 "addl $64, %%eax\n\t"
10182 "cmpl %%eax, %[size]\n\t"
10183 "jne .Lfill_cache\n\t"
10184 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010185 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010186 [size] "r" (size)
10187 : "eax", "ebx", "ecx", "edx");
10188}
10189
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010190static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010191{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010192 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10193
10194 if (is_guest_mode(vcpu) &&
10195 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10196 return;
10197
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010198 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010199 vmcs_write32(TPR_THRESHOLD, 0);
10200 return;
10201 }
10202
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010203 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010204}
10205
Jim Mattson8d860bb2018-05-09 16:56:05 -040010206static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010207{
10208 u32 sec_exec_control;
10209
Jim Mattson8d860bb2018-05-09 16:56:05 -040010210 if (!lapic_in_kernel(vcpu))
10211 return;
10212
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010213 if (!flexpriority_enabled &&
10214 !cpu_has_vmx_virtualize_x2apic_mode())
10215 return;
10216
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010217 /* Postpone execution until vmcs01 is the current VMCS. */
10218 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010219 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010220 return;
10221 }
10222
Yang Zhang8d146952013-01-25 10:18:50 +080010223 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010224 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10225 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010226
Jim Mattson8d860bb2018-05-09 16:56:05 -040010227 switch (kvm_get_apic_mode(vcpu)) {
10228 case LAPIC_MODE_INVALID:
10229 WARN_ONCE(true, "Invalid local APIC state");
10230 case LAPIC_MODE_DISABLED:
10231 break;
10232 case LAPIC_MODE_XAPIC:
10233 if (flexpriority_enabled) {
10234 sec_exec_control |=
10235 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10236 vmx_flush_tlb(vcpu, true);
10237 }
10238 break;
10239 case LAPIC_MODE_X2APIC:
10240 if (cpu_has_vmx_virtualize_x2apic_mode())
10241 sec_exec_control |=
10242 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10243 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010244 }
10245 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10246
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010247 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010248}
10249
Tang Chen38b99172014-09-24 15:57:54 +080010250static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10251{
Jim Mattsonab5df312018-05-09 17:02:03 -040010252 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010253 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010254 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010255 }
Tang Chen38b99172014-09-24 15:57:54 +080010256}
10257
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010258static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010259{
10260 u16 status;
10261 u8 old;
10262
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010263 if (max_isr == -1)
10264 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010265
10266 status = vmcs_read16(GUEST_INTR_STATUS);
10267 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010268 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010269 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010270 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010271 vmcs_write16(GUEST_INTR_STATUS, status);
10272 }
10273}
10274
10275static void vmx_set_rvi(int vector)
10276{
10277 u16 status;
10278 u8 old;
10279
Wei Wang4114c272014-11-05 10:53:43 +080010280 if (vector == -1)
10281 vector = 0;
10282
Yang Zhangc7c9c562013-01-25 10:18:51 +080010283 status = vmcs_read16(GUEST_INTR_STATUS);
10284 old = (u8)status & 0xff;
10285 if ((u8)vector != old) {
10286 status &= ~0xff;
10287 status |= (u8)vector;
10288 vmcs_write16(GUEST_INTR_STATUS, status);
10289 }
10290}
10291
10292static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10293{
Liran Alon851c1a182017-12-24 18:12:56 +020010294 /*
10295 * When running L2, updating RVI is only relevant when
10296 * vmcs12 virtual-interrupt-delivery enabled.
10297 * However, it can be enabled only when L1 also
10298 * intercepts external-interrupts and in that case
10299 * we should not update vmcs02 RVI but instead intercept
10300 * interrupt. Therefore, do nothing when running L2.
10301 */
10302 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010303 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010304}
10305
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010306static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010307{
10308 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010309 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010310 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010311
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010312 WARN_ON(!vcpu->arch.apicv_active);
10313 if (pi_test_on(&vmx->pi_desc)) {
10314 pi_clear_on(&vmx->pi_desc);
10315 /*
10316 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10317 * But on x86 this is just a compiler barrier anyway.
10318 */
10319 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010320 max_irr_updated =
10321 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10322
10323 /*
10324 * If we are running L2 and L1 has a new pending interrupt
10325 * which can be injected, we should re-evaluate
10326 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010327 * If L1 intercepts external-interrupts, we should
10328 * exit from L2 to L1. Otherwise, interrupt should be
10329 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010330 */
Liran Alon851c1a182017-12-24 18:12:56 +020010331 if (is_guest_mode(vcpu) && max_irr_updated) {
10332 if (nested_exit_on_intr(vcpu))
10333 kvm_vcpu_exiting_guest_mode(vcpu);
10334 else
10335 kvm_make_request(KVM_REQ_EVENT, vcpu);
10336 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010337 } else {
10338 max_irr = kvm_lapic_find_highest_irr(vcpu);
10339 }
10340 vmx_hwapic_irr_update(vcpu, max_irr);
10341 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010342}
10343
Paolo Bonzini7e712682018-10-03 13:44:26 +020010344static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10345{
10346 u8 rvi = vmx_get_rvi();
10347 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10348
10349 return ((rvi & 0xf0) > (vppr & 0xf0));
10350}
10351
Andrey Smetanin63086302015-11-10 15:36:32 +030010352static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010353{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010354 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010355 return;
10356
Yang Zhangc7c9c562013-01-25 10:18:51 +080010357 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10358 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10359 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10360 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10361}
10362
Paolo Bonzini967235d2016-12-19 14:03:45 +010010363static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10364{
10365 struct vcpu_vmx *vmx = to_vmx(vcpu);
10366
10367 pi_clear_on(&vmx->pi_desc);
10368 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10369}
10370
Avi Kivity51aa01d2010-07-20 14:31:20 +030010371static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010372{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010373 u32 exit_intr_info = 0;
10374 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010375
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010376 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10377 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010378 return;
10379
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010380 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10381 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10382 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010383
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010384 /* if exit due to PF check for async PF */
10385 if (is_page_fault(exit_intr_info))
10386 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10387
Andi Kleena0861c02009-06-08 17:37:09 +080010388 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010389 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10390 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010391 kvm_machine_check();
10392
Gleb Natapov20f65982009-05-11 13:35:55 +030010393 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010394 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010395 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010396 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010397 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010398 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010399}
Gleb Natapov20f65982009-05-11 13:35:55 +030010400
Yang Zhanga547c6d2013-04-11 19:25:10 +080010401static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10402{
10403 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10404
Yang Zhanga547c6d2013-04-11 19:25:10 +080010405 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10406 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10407 unsigned int vector;
10408 unsigned long entry;
10409 gate_desc *desc;
10410 struct vcpu_vmx *vmx = to_vmx(vcpu);
10411#ifdef CONFIG_X86_64
10412 unsigned long tmp;
10413#endif
10414
10415 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10416 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010417 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010418 asm volatile(
10419#ifdef CONFIG_X86_64
10420 "mov %%" _ASM_SP ", %[sp]\n\t"
10421 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10422 "push $%c[ss]\n\t"
10423 "push %[sp]\n\t"
10424#endif
10425 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010426 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010427 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010428 :
10429#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010430 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010431#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010432 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010433 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010434 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010435 [ss]"i"(__KERNEL_DS),
10436 [cs]"i"(__KERNEL_CS)
10437 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010438 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010439}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010440STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010441
Tom Lendackybc226f02018-05-10 22:06:39 +020010442static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010443{
Tom Lendackybc226f02018-05-10 22:06:39 +020010444 switch (index) {
10445 case MSR_IA32_SMBASE:
10446 /*
10447 * We cannot do SMM unless we can run the guest in big
10448 * real mode.
10449 */
10450 return enable_unrestricted_guest || emulate_invalid_guest_state;
10451 case MSR_AMD64_VIRT_SPEC_CTRL:
10452 /* This is AMD only. */
10453 return false;
10454 default:
10455 return true;
10456 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010457}
10458
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010459static bool vmx_mpx_supported(void)
10460{
10461 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10462 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10463}
10464
Wanpeng Li55412b22014-12-02 19:21:30 +080010465static bool vmx_xsaves_supported(void)
10466{
10467 return vmcs_config.cpu_based_2nd_exec_ctrl &
10468 SECONDARY_EXEC_XSAVES;
10469}
10470
Avi Kivity51aa01d2010-07-20 14:31:20 +030010471static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10472{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010473 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010474 bool unblock_nmi;
10475 u8 vector;
10476 bool idtv_info_valid;
10477
10478 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010479
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010480 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010481 if (vmx->loaded_vmcs->nmi_known_unmasked)
10482 return;
10483 /*
10484 * Can't use vmx->exit_intr_info since we're not sure what
10485 * the exit reason is.
10486 */
10487 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10488 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10489 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10490 /*
10491 * SDM 3: 27.7.1.2 (September 2008)
10492 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10493 * a guest IRET fault.
10494 * SDM 3: 23.2.2 (September 2008)
10495 * Bit 12 is undefined in any of the following cases:
10496 * If the VM exit sets the valid bit in the IDT-vectoring
10497 * information field.
10498 * If the VM exit is due to a double fault.
10499 */
10500 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10501 vector != DF_VECTOR && !idtv_info_valid)
10502 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10503 GUEST_INTR_STATE_NMI);
10504 else
10505 vmx->loaded_vmcs->nmi_known_unmasked =
10506 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10507 & GUEST_INTR_STATE_NMI);
10508 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10509 vmx->loaded_vmcs->vnmi_blocked_time +=
10510 ktime_to_ns(ktime_sub(ktime_get(),
10511 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010512}
10513
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010514static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010515 u32 idt_vectoring_info,
10516 int instr_len_field,
10517 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010518{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010519 u8 vector;
10520 int type;
10521 bool idtv_info_valid;
10522
10523 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010524
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010525 vcpu->arch.nmi_injected = false;
10526 kvm_clear_exception_queue(vcpu);
10527 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010528
10529 if (!idtv_info_valid)
10530 return;
10531
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010532 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010533
Avi Kivity668f6122008-07-02 09:28:55 +030010534 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10535 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010536
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010537 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010538 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010539 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010540 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010541 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010542 * Clear bit "block by NMI" before VM entry if a NMI
10543 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010544 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010545 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010546 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010547 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010548 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010549 /* fall through */
10550 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010551 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010552 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010553 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010554 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010555 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010556 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010557 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010558 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010559 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010560 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010561 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010562 break;
10563 default:
10564 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010565 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010566}
10567
Avi Kivity83422e12010-07-20 14:43:23 +030010568static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10569{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010570 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010571 VM_EXIT_INSTRUCTION_LEN,
10572 IDT_VECTORING_ERROR_CODE);
10573}
10574
Avi Kivityb463a6f2010-07-20 15:06:17 +030010575static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10576{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010577 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010578 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10579 VM_ENTRY_INSTRUCTION_LEN,
10580 VM_ENTRY_EXCEPTION_ERROR_CODE);
10581
10582 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10583}
10584
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010585static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10586{
10587 int i, nr_msrs;
10588 struct perf_guest_switch_msr *msrs;
10589
10590 msrs = perf_guest_get_msrs(&nr_msrs);
10591
10592 if (!msrs)
10593 return;
10594
10595 for (i = 0; i < nr_msrs; i++)
10596 if (msrs[i].host == msrs[i].guest)
10597 clear_atomic_switch_msr(vmx, msrs[i].msr);
10598 else
10599 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010600 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010601}
10602
Sean Christophersonf459a702018-08-27 15:21:11 -070010603static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10604{
10605 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10606 if (!vmx->loaded_vmcs->hv_timer_armed)
10607 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10608 PIN_BASED_VMX_PREEMPTION_TIMER);
10609 vmx->loaded_vmcs->hv_timer_armed = true;
10610}
10611
10612static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010613{
10614 struct vcpu_vmx *vmx = to_vmx(vcpu);
10615 u64 tscl;
10616 u32 delta_tsc;
10617
Sean Christophersond264ee02018-08-27 15:21:12 -070010618 if (vmx->req_immediate_exit) {
10619 vmx_arm_hv_timer(vmx, 0);
10620 return;
10621 }
10622
Sean Christophersonf459a702018-08-27 15:21:11 -070010623 if (vmx->hv_deadline_tsc != -1) {
10624 tscl = rdtsc();
10625 if (vmx->hv_deadline_tsc > tscl)
10626 /* set_hv_timer ensures the delta fits in 32-bits */
10627 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10628 cpu_preemption_timer_multi);
10629 else
10630 delta_tsc = 0;
10631
10632 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010633 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010634 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010635
Sean Christophersonf459a702018-08-27 15:21:11 -070010636 if (vmx->loaded_vmcs->hv_timer_armed)
10637 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10638 PIN_BASED_VMX_PREEMPTION_TIMER);
10639 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010640}
10641
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010642static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010643{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010644 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010645 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010646
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010647 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010648 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010649 vmx->loaded_vmcs->soft_vnmi_blocked))
10650 vmx->loaded_vmcs->entry_time = ktime_get();
10651
Avi Kivity104f2262010-11-18 13:12:52 +020010652 /* Don't enter VMX if guest state is invalid, let the exit handler
10653 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010654 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010655 return;
10656
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010657 if (vmx->ple_window_dirty) {
10658 vmx->ple_window_dirty = false;
10659 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10660 }
10661
Abel Gordon012f83c2013-04-18 14:39:25 +030010662 if (vmx->nested.sync_shadow_vmcs) {
10663 copy_vmcs12_to_shadow(vmx);
10664 vmx->nested.sync_shadow_vmcs = false;
10665 }
10666
Avi Kivity104f2262010-11-18 13:12:52 +020010667 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10668 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10669 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10670 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10671
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010672 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010673 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010674 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010675 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010676 }
10677
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010678 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010679 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010680 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010681 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010682 }
10683
Avi Kivity104f2262010-11-18 13:12:52 +020010684 /* When single-stepping over STI and MOV SS, we must clear the
10685 * corresponding interruptibility bits in the guest state. Otherwise
10686 * vmentry fails as it then expects bit 14 (BS) in pending debug
10687 * exceptions being set, but that's not correct for the guest debugging
10688 * case. */
10689 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10690 vmx_set_interrupt_shadow(vcpu, 0);
10691
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010692 if (static_cpu_has(X86_FEATURE_PKU) &&
10693 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10694 vcpu->arch.pkru != vmx->host_pkru)
10695 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010696
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010697 atomic_switch_perf_msrs(vmx);
10698
Sean Christophersonf459a702018-08-27 15:21:11 -070010699 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010700
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010701 /*
10702 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10703 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10704 * is no need to worry about the conditional branch over the wrmsr
10705 * being speculatively taken.
10706 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010707 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010708
Nadav Har'Eld462b812011-05-24 15:26:10 +030010709 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010710
10711 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10712 (unsigned long)&current_evmcs->host_rsp : 0;
10713
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010714 if (static_branch_unlikely(&vmx_l1d_should_flush))
10715 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010716
Avi Kivity104f2262010-11-18 13:12:52 +020010717 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010718 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010719 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10720 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10721 "push %%" _ASM_CX " \n\t"
10722 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010723 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010724 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010725 /* Avoid VMWRITE when Enlightened VMCS is in use */
10726 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10727 "jz 2f \n\t"
10728 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10729 "jmp 1f \n\t"
10730 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010731 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010732 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010733 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010734 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10735 "mov %%cr2, %%" _ASM_DX " \n\t"
10736 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010737 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010738 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010739 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010740 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010741 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010742 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010743 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10744 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10745 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10746 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10747 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10748 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010749#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010750 "mov %c[r8](%0), %%r8 \n\t"
10751 "mov %c[r9](%0), %%r9 \n\t"
10752 "mov %c[r10](%0), %%r10 \n\t"
10753 "mov %c[r11](%0), %%r11 \n\t"
10754 "mov %c[r12](%0), %%r12 \n\t"
10755 "mov %c[r13](%0), %%r13 \n\t"
10756 "mov %c[r14](%0), %%r14 \n\t"
10757 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010758#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010759 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010760
Avi Kivity6aa8b732006-12-10 02:21:36 -080010761 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010762 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010763 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010764 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010765 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010766 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010767 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010768 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010769 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010770 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010771 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10772 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10773 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10774 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10775 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10776 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10777 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010778#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010779 "mov %%r8, %c[r8](%0) \n\t"
10780 "mov %%r9, %c[r9](%0) \n\t"
10781 "mov %%r10, %c[r10](%0) \n\t"
10782 "mov %%r11, %c[r11](%0) \n\t"
10783 "mov %%r12, %c[r12](%0) \n\t"
10784 "mov %%r13, %c[r13](%0) \n\t"
10785 "mov %%r14, %c[r14](%0) \n\t"
10786 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010787 "xor %%r8d, %%r8d \n\t"
10788 "xor %%r9d, %%r9d \n\t"
10789 "xor %%r10d, %%r10d \n\t"
10790 "xor %%r11d, %%r11d \n\t"
10791 "xor %%r12d, %%r12d \n\t"
10792 "xor %%r13d, %%r13d \n\t"
10793 "xor %%r14d, %%r14d \n\t"
10794 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010795#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010796 "mov %%cr2, %%" _ASM_AX " \n\t"
10797 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010798
Jim Mattson0cb5b302018-01-03 14:31:38 -080010799 "xor %%eax, %%eax \n\t"
10800 "xor %%ebx, %%ebx \n\t"
10801 "xor %%esi, %%esi \n\t"
10802 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010803 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010804 ".pushsection .rodata \n\t"
10805 ".global vmx_return \n\t"
10806 "vmx_return: " _ASM_PTR " 2b \n\t"
10807 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010808 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010809 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010810 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010811 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010812 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10813 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10814 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10815 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10816 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10817 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10818 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010819#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010820 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10821 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10822 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10823 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10824 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10825 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10826 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10827 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010828#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010829 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10830 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010831 : "cc", "memory"
10832#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010833 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010834 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010835#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010836 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010837#endif
10838 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010839
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010840 /*
10841 * We do not use IBRS in the kernel. If this vCPU has used the
10842 * SPEC_CTRL MSR it may have left it on; save the value and
10843 * turn it off. This is much more efficient than blindly adding
10844 * it to the atomic save/restore list. Especially as the former
10845 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10846 *
10847 * For non-nested case:
10848 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10849 * save it.
10850 *
10851 * For nested case:
10852 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10853 * save it.
10854 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010855 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010856 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010857
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010858 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010859
David Woodhouse117cc7a2018-01-12 11:11:27 +000010860 /* Eliminate branch target predictions from guest mode */
10861 vmexit_fill_RSB();
10862
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010863 /* All fields are clean at this point */
10864 if (static_branch_unlikely(&enable_evmcs))
10865 current_evmcs->hv_clean_fields |=
10866 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10867
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010868 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010869 if (vmx->host_debugctlmsr)
10870 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010871
Avi Kivityaa67f602012-08-01 16:48:03 +030010872#ifndef CONFIG_X86_64
10873 /*
10874 * The sysexit path does not restore ds/es, so we must set them to
10875 * a reasonable value ourselves.
10876 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010877 * We can't defer this to vmx_prepare_switch_to_host() since that
10878 * function may be executed in interrupt context, which saves and
10879 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010880 */
10881 loadsegment(ds, __USER_DS);
10882 loadsegment(es, __USER_DS);
10883#endif
10884
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010885 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010886 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010887 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010888 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010889 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010890 vcpu->arch.regs_dirty = 0;
10891
Gleb Natapove0b890d2013-09-25 12:51:33 +030010892 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010893 * eager fpu is enabled if PKEY is supported and CR4 is switched
10894 * back on host, so it is safe to read guest PKRU from current
10895 * XSAVE.
10896 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010897 if (static_cpu_has(X86_FEATURE_PKU) &&
10898 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10899 vcpu->arch.pkru = __read_pkru();
10900 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010901 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010902 }
10903
Gleb Natapove0b890d2013-09-25 12:51:33 +030010904 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010905 vmx->idt_vectoring_info = 0;
10906
10907 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10908 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10909 return;
10910
10911 vmx->loaded_vmcs->launched = 1;
10912 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010913
Avi Kivity51aa01d2010-07-20 14:31:20 +030010914 vmx_complete_atomic_exit(vmx);
10915 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010916 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010917}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010918STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010919
Sean Christopherson434a1e92018-03-20 12:17:18 -070010920static struct kvm *vmx_vm_alloc(void)
10921{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010922 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010923 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010924}
10925
10926static void vmx_vm_free(struct kvm *kvm)
10927{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010928 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010929}
10930
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010931static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010932{
10933 struct vcpu_vmx *vmx = to_vmx(vcpu);
10934 int cpu;
10935
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010936 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010937 return;
10938
10939 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010940 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010941 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010942 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010943 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010944
10945 vm_entry_controls_reset_shadow(vmx);
10946 vm_exit_controls_reset_shadow(vmx);
10947 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010948}
10949
Jim Mattson2f1fe812016-07-08 15:36:06 -070010950/*
10951 * Ensure that the current vmcs of the logical processor is the
10952 * vmcs01 of the vcpu before calling free_nested().
10953 */
10954static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10955{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020010956 vcpu_load(vcpu);
10957 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
10958 free_nested(vcpu);
10959 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010960}
10961
Avi Kivity6aa8b732006-12-10 02:21:36 -080010962static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10963{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010964 struct vcpu_vmx *vmx = to_vmx(vcpu);
10965
Kai Huang843e4332015-01-28 10:54:28 +080010966 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010967 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010968 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010969 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010970 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010971 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010972 kfree(vmx->guest_msrs);
10973 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010974 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010975}
10976
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010977static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010978{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010979 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010980 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010981 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010982 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010983
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010984 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010985 return ERR_PTR(-ENOMEM);
10986
Wanpeng Li991e7a02015-09-16 17:30:05 +080010987 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010988
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010989 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10990 if (err)
10991 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010992
Peter Feiner4e595162016-07-07 14:49:58 -070010993 err = -ENOMEM;
10994
10995 /*
10996 * If PML is turned on, failure on enabling PML just results in failure
10997 * of creating the vcpu, therefore we can simplify PML logic (by
10998 * avoiding dealing with cases, such as enabling PML partially on vcpus
10999 * for the guest, etc.
11000 */
11001 if (enable_pml) {
11002 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11003 if (!vmx->pml_pg)
11004 goto uninit_vcpu;
11005 }
11006
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011007 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011008 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11009 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011010
Peter Feiner4e595162016-07-07 14:49:58 -070011011 if (!vmx->guest_msrs)
11012 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011013
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011014 err = alloc_loaded_vmcs(&vmx->vmcs01);
11015 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011016 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011017
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011018 msr_bitmap = vmx->vmcs01.msr_bitmap;
11019 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11020 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11021 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11022 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11023 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11024 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11025 vmx->msr_bitmap_mode = 0;
11026
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011027 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011028 cpu = get_cpu();
11029 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011030 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011031 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011032 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011033 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011034 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011035 err = alloc_apic_access_page(kvm);
11036 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011037 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011038 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011039
Sean Christophersone90008d2018-03-05 12:04:37 -080011040 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011041 err = init_rmode_identity_map(kvm);
11042 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011043 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011044 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011045
Roman Kagan63aff652018-07-19 21:59:07 +030011046 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011047 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11048 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011049
Wincy Van705699a2015-02-03 23:58:17 +080011050 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011051 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011052
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011053 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11054
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011055 /*
11056 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11057 * or POSTED_INTR_WAKEUP_VECTOR.
11058 */
11059 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11060 vmx->pi_desc.sn = 1;
11061
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011062 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011063
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011064free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011065 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011066free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011067 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011068free_pml:
11069 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011070uninit_vcpu:
11071 kvm_vcpu_uninit(&vmx->vcpu);
11072free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011073 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011074 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011075 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011076}
11077
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011078#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"
11079#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 -040011080
Wanpeng Lib31c1142018-03-12 04:53:04 -070011081static int vmx_vm_init(struct kvm *kvm)
11082{
Tianyu Lan877ad952018-07-19 08:40:23 +000011083 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11084
Wanpeng Lib31c1142018-03-12 04:53:04 -070011085 if (!ple_gap)
11086 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011087
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011088 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11089 switch (l1tf_mitigation) {
11090 case L1TF_MITIGATION_OFF:
11091 case L1TF_MITIGATION_FLUSH_NOWARN:
11092 /* 'I explicitly don't care' is set */
11093 break;
11094 case L1TF_MITIGATION_FLUSH:
11095 case L1TF_MITIGATION_FLUSH_NOSMT:
11096 case L1TF_MITIGATION_FULL:
11097 /*
11098 * Warn upon starting the first VM in a potentially
11099 * insecure environment.
11100 */
11101 if (cpu_smt_control == CPU_SMT_ENABLED)
11102 pr_warn_once(L1TF_MSG_SMT);
11103 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11104 pr_warn_once(L1TF_MSG_L1D);
11105 break;
11106 case L1TF_MITIGATION_FULL_FORCE:
11107 /* Flush is enforced */
11108 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011109 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011110 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011111 return 0;
11112}
11113
Yang, Sheng002c7f72007-07-31 14:23:01 +030011114static void __init vmx_check_processor_compat(void *rtn)
11115{
11116 struct vmcs_config vmcs_conf;
11117
11118 *(int *)rtn = 0;
11119 if (setup_vmcs_config(&vmcs_conf) < 0)
11120 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011121 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011122 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11123 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11124 smp_processor_id());
11125 *(int *)rtn = -EIO;
11126 }
11127}
11128
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011129static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011130{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011131 u8 cache;
11132 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011133
Sheng Yang522c68c2009-04-27 20:35:43 +080011134 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011135 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011136 * 2. EPT with VT-d:
11137 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011138 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011139 * b. VT-d with snooping control feature: snooping control feature of
11140 * VT-d engine can guarantee the cache correctness. Just set it
11141 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011142 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011143 * consistent with host MTRR
11144 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011145 if (is_mmio) {
11146 cache = MTRR_TYPE_UNCACHABLE;
11147 goto exit;
11148 }
11149
11150 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011151 ipat = VMX_EPT_IPAT_BIT;
11152 cache = MTRR_TYPE_WRBACK;
11153 goto exit;
11154 }
11155
11156 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11157 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011158 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011159 cache = MTRR_TYPE_WRBACK;
11160 else
11161 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011162 goto exit;
11163 }
11164
Xiao Guangrongff536042015-06-15 16:55:22 +080011165 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011166
11167exit:
11168 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011169}
11170
Sheng Yang17cc3932010-01-05 19:02:27 +080011171static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011172{
Sheng Yang878403b2010-01-05 19:02:29 +080011173 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11174 return PT_DIRECTORY_LEVEL;
11175 else
11176 /* For shadow and EPT supported 1GB page */
11177 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011178}
11179
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011180static void vmcs_set_secondary_exec_control(u32 new_ctl)
11181{
11182 /*
11183 * These bits in the secondary execution controls field
11184 * are dynamic, the others are mostly based on the hypervisor
11185 * architecture and the guest's CPUID. Do not touch the
11186 * dynamic bits.
11187 */
11188 u32 mask =
11189 SECONDARY_EXEC_SHADOW_VMCS |
11190 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011191 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11192 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011193
11194 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11195
11196 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11197 (new_ctl & ~mask) | (cur_ctl & mask));
11198}
11199
David Matlack8322ebb2016-11-29 18:14:09 -080011200/*
11201 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11202 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11203 */
11204static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11205{
11206 struct vcpu_vmx *vmx = to_vmx(vcpu);
11207 struct kvm_cpuid_entry2 *entry;
11208
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011209 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11210 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011211
11212#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11213 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011214 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011215} while (0)
11216
11217 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11218 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11219 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11220 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11221 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11222 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11223 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11224 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11225 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11226 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11227 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11228 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11229 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11230 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11231 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11232
11233 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11234 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11235 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11236 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11237 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011238 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011239
11240#undef cr4_fixed1_update
11241}
11242
Liran Alon5f76f6f2018-09-14 03:25:52 +030011243static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11244{
11245 struct vcpu_vmx *vmx = to_vmx(vcpu);
11246
11247 if (kvm_mpx_supported()) {
11248 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11249
11250 if (mpx_enabled) {
11251 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11252 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11253 } else {
11254 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11255 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11256 }
11257 }
11258}
11259
Sheng Yang0e851882009-12-18 16:48:46 +080011260static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11261{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011262 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011263
Paolo Bonzini80154d72017-08-24 13:55:35 +020011264 if (cpu_has_secondary_exec_ctrls()) {
11265 vmx_compute_secondary_exec_control(vmx);
11266 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011267 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011268
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011269 if (nested_vmx_allowed(vcpu))
11270 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11271 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11272 else
11273 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11274 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011275
Liran Alon5f76f6f2018-09-14 03:25:52 +030011276 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011277 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011278 nested_vmx_entry_exit_ctls_update(vcpu);
11279 }
Sheng Yang0e851882009-12-18 16:48:46 +080011280}
11281
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011282static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11283{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011284 if (func == 1 && nested)
11285 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011286}
11287
Yang Zhang25d92082013-08-06 12:00:32 +030011288static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11289 struct x86_exception *fault)
11290{
Jan Kiszka533558b2014-01-04 18:47:20 +010011291 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011292 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011293 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011294 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011295
Bandan Dasc5f983f2017-05-05 15:25:14 -040011296 if (vmx->nested.pml_full) {
11297 exit_reason = EXIT_REASON_PML_FULL;
11298 vmx->nested.pml_full = false;
11299 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11300 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011301 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011302 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011303 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011304
11305 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011306 vmcs12->guest_physical_address = fault->address;
11307}
11308
Peter Feiner995f00a2017-06-30 17:26:32 -070011309static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11310{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011311 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011312}
11313
Nadav Har'El155a97a2013-08-05 11:07:16 +030011314/* Callbacks for nested_ept_init_mmu_context: */
11315
11316static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11317{
11318 /* return the page table to be shadowed - in our case, EPT12 */
11319 return get_vmcs12(vcpu)->ept_pointer;
11320}
11321
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011322static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011323{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011324 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011325
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011326 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011327 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011328 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011329 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011330 nested_ept_ad_enabled(vcpu),
11331 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011332 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11333 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11334 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011335 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011336
11337 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011338}
11339
11340static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11341{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011342 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011343 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011344}
11345
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011346static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11347 u16 error_code)
11348{
11349 bool inequality, bit;
11350
11351 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11352 inequality =
11353 (error_code & vmcs12->page_fault_error_code_mask) !=
11354 vmcs12->page_fault_error_code_match;
11355 return inequality ^ bit;
11356}
11357
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011358static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11359 struct x86_exception *fault)
11360{
11361 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11362
11363 WARN_ON(!is_guest_mode(vcpu));
11364
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011365 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11366 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011367 vmcs12->vm_exit_intr_error_code = fault->error_code;
11368 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11369 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11370 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11371 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011372 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011373 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011374 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011375}
11376
Paolo Bonzinic9923842017-12-13 14:16:30 +010011377static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11378 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011379
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011380static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011381{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011382 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011383 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011384 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011385 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011386
11387 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011388 /*
11389 * Translate L1 physical address to host physical
11390 * address for vmcs02. Keep the page pinned, so this
11391 * physical address remains valid. We keep a reference
11392 * to it so we can release it later.
11393 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011394 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011395 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011396 vmx->nested.apic_access_page = NULL;
11397 }
11398 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011399 /*
11400 * If translation failed, no matter: This feature asks
11401 * to exit when accessing the given address, and if it
11402 * can never be accessed, this feature won't do
11403 * anything anyway.
11404 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011405 if (!is_error_page(page)) {
11406 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011407 hpa = page_to_phys(vmx->nested.apic_access_page);
11408 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11409 } else {
11410 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11411 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11412 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011413 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011414
11415 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011416 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011417 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011418 vmx->nested.virtual_apic_page = NULL;
11419 }
11420 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011421
11422 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011423 * If translation failed, VM entry will fail because
11424 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11425 * Failing the vm entry is _not_ what the processor
11426 * does but it's basically the only possibility we
11427 * have. We could still enter the guest if CR8 load
11428 * exits are enabled, CR8 store exits are enabled, and
11429 * virtualize APIC access is disabled; in this case
11430 * the processor would never use the TPR shadow and we
11431 * could simply clear the bit from the execution
11432 * control. But such a configuration is useless, so
11433 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011434 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011435 if (!is_error_page(page)) {
11436 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011437 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11438 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11439 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011440 }
11441
Wincy Van705699a2015-02-03 23:58:17 +080011442 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011443 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11444 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011445 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011446 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011447 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011448 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11449 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011450 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011451 vmx->nested.pi_desc_page = page;
11452 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011453 vmx->nested.pi_desc =
11454 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11455 (unsigned long)(vmcs12->posted_intr_desc_addr &
11456 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011457 vmcs_write64(POSTED_INTR_DESC_ADDR,
11458 page_to_phys(vmx->nested.pi_desc_page) +
11459 (unsigned long)(vmcs12->posted_intr_desc_addr &
11460 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011461 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011462 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011463 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11464 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011465 else
11466 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11467 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011468}
11469
Jan Kiszkaf4124502014-03-07 20:03:13 +010011470static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11471{
11472 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11473 struct vcpu_vmx *vmx = to_vmx(vcpu);
11474
Sean Christopherson4c008122018-08-27 15:21:10 -070011475 /*
11476 * A timer value of zero is architecturally guaranteed to cause
11477 * a VMExit prior to executing any instructions in the guest.
11478 */
11479 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011480 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11481 return;
11482 }
11483
Sean Christopherson4c008122018-08-27 15:21:10 -070011484 if (vcpu->arch.virtual_tsc_khz == 0)
11485 return;
11486
Jan Kiszkaf4124502014-03-07 20:03:13 +010011487 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11488 preemption_timeout *= 1000000;
11489 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11490 hrtimer_start(&vmx->nested.preemption_timer,
11491 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11492}
11493
Jim Mattson56a20512017-07-06 16:33:06 -070011494static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11495 struct vmcs12 *vmcs12)
11496{
11497 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11498 return 0;
11499
11500 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11501 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11502 return -EINVAL;
11503
11504 return 0;
11505}
11506
Wincy Van3af18d92015-02-03 23:49:31 +080011507static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11508 struct vmcs12 *vmcs12)
11509{
Wincy Van3af18d92015-02-03 23:49:31 +080011510 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11511 return 0;
11512
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011513 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011514 return -EINVAL;
11515
11516 return 0;
11517}
11518
Jim Mattson712b12d2017-08-24 13:24:47 -070011519static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11520 struct vmcs12 *vmcs12)
11521{
11522 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11523 return 0;
11524
11525 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11526 return -EINVAL;
11527
11528 return 0;
11529}
11530
Wincy Van3af18d92015-02-03 23:49:31 +080011531/*
11532 * Merge L0's and L1's MSR bitmap, return false to indicate that
11533 * we do not use the hardware.
11534 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011535static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11536 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011537{
Wincy Van82f0dd42015-02-03 23:57:18 +080011538 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011539 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011540 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011541 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011542 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011543 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011544 *
11545 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11546 * ensures that we do not accidentally generate an L02 MSR bitmap
11547 * from the L12 MSR bitmap that is too permissive.
11548 * 2. That L1 or L2s have actually used the MSR. This avoids
11549 * unnecessarily merging of the bitmap if the MSR is unused. This
11550 * works properly because we only update the L01 MSR bitmap lazily.
11551 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11552 * updated to reflect this when L1 (or its L2s) actually write to
11553 * the MSR.
11554 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011555 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11556 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011557
Paolo Bonzinic9923842017-12-13 14:16:30 +010011558 /* Nothing to do if the MSR bitmap is not in use. */
11559 if (!cpu_has_vmx_msr_bitmap() ||
11560 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11561 return false;
11562
Ashok Raj15d45072018-02-01 22:59:43 +010011563 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011564 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011565 return false;
11566
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011567 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11568 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011569 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011570
Radim Krčmářd048c092016-08-08 20:16:22 +020011571 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011572 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11573 /*
11574 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11575 * just lets the processor take the value from the virtual-APIC page;
11576 * take those 256 bits directly from the L1 bitmap.
11577 */
11578 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11579 unsigned word = msr / BITS_PER_LONG;
11580 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11581 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011582 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011583 } else {
11584 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11585 unsigned word = msr / BITS_PER_LONG;
11586 msr_bitmap_l0[word] = ~0;
11587 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11588 }
11589 }
11590
11591 nested_vmx_disable_intercept_for_msr(
11592 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011593 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011594 MSR_TYPE_W);
11595
11596 if (nested_cpu_has_vid(vmcs12)) {
11597 nested_vmx_disable_intercept_for_msr(
11598 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011599 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011600 MSR_TYPE_W);
11601 nested_vmx_disable_intercept_for_msr(
11602 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011603 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011604 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011605 }
Ashok Raj15d45072018-02-01 22:59:43 +010011606
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011607 if (spec_ctrl)
11608 nested_vmx_disable_intercept_for_msr(
11609 msr_bitmap_l1, msr_bitmap_l0,
11610 MSR_IA32_SPEC_CTRL,
11611 MSR_TYPE_R | MSR_TYPE_W);
11612
Ashok Raj15d45072018-02-01 22:59:43 +010011613 if (pred_cmd)
11614 nested_vmx_disable_intercept_for_msr(
11615 msr_bitmap_l1, msr_bitmap_l0,
11616 MSR_IA32_PRED_CMD,
11617 MSR_TYPE_W);
11618
Wincy Vanf2b93282015-02-03 23:56:03 +080011619 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011620 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011621
11622 return true;
11623}
11624
Liran Alon61ada742018-06-23 02:35:08 +030011625static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11626 struct vmcs12 *vmcs12)
11627{
11628 struct vmcs12 *shadow;
11629 struct page *page;
11630
11631 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11632 vmcs12->vmcs_link_pointer == -1ull)
11633 return;
11634
11635 shadow = get_shadow_vmcs12(vcpu);
11636 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11637
11638 memcpy(shadow, kmap(page), VMCS12_SIZE);
11639
11640 kunmap(page);
11641 kvm_release_page_clean(page);
11642}
11643
11644static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11645 struct vmcs12 *vmcs12)
11646{
11647 struct vcpu_vmx *vmx = to_vmx(vcpu);
11648
11649 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11650 vmcs12->vmcs_link_pointer == -1ull)
11651 return;
11652
11653 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11654 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11655}
11656
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011657static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11658 struct vmcs12 *vmcs12)
11659{
11660 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11661 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11662 return -EINVAL;
11663 else
11664 return 0;
11665}
11666
Wincy Vanf2b93282015-02-03 23:56:03 +080011667static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11668 struct vmcs12 *vmcs12)
11669{
Wincy Van82f0dd42015-02-03 23:57:18 +080011670 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011671 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011672 !nested_cpu_has_vid(vmcs12) &&
11673 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011674 return 0;
11675
11676 /*
11677 * If virtualize x2apic mode is enabled,
11678 * virtualize apic access must be disabled.
11679 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011680 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11681 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011682 return -EINVAL;
11683
Wincy Van608406e2015-02-03 23:57:51 +080011684 /*
11685 * If virtual interrupt delivery is enabled,
11686 * we must exit on external interrupts.
11687 */
11688 if (nested_cpu_has_vid(vmcs12) &&
11689 !nested_exit_on_intr(vcpu))
11690 return -EINVAL;
11691
Wincy Van705699a2015-02-03 23:58:17 +080011692 /*
11693 * bits 15:8 should be zero in posted_intr_nv,
11694 * the descriptor address has been already checked
11695 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011696 *
11697 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011698 */
11699 if (nested_cpu_has_posted_intr(vmcs12) &&
11700 (!nested_cpu_has_vid(vmcs12) ||
11701 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011702 (vmcs12->posted_intr_nv & 0xff00) ||
11703 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11704 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011705 return -EINVAL;
11706
Wincy Vanf2b93282015-02-03 23:56:03 +080011707 /* tpr shadow is needed by all apicv features. */
11708 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11709 return -EINVAL;
11710
11711 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011712}
11713
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011714static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11715 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011716 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011717{
Liran Alone2536742018-06-23 02:35:02 +030011718 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011719 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011720 u64 count, addr;
11721
Liran Alone2536742018-06-23 02:35:02 +030011722 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11723 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011724 WARN_ON(1);
11725 return -EINVAL;
11726 }
11727 if (count == 0)
11728 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011729 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011730 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11731 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011732 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011733 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11734 addr_field, maxphyaddr, count, addr);
11735 return -EINVAL;
11736 }
11737 return 0;
11738}
11739
11740static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11741 struct vmcs12 *vmcs12)
11742{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011743 if (vmcs12->vm_exit_msr_load_count == 0 &&
11744 vmcs12->vm_exit_msr_store_count == 0 &&
11745 vmcs12->vm_entry_msr_load_count == 0)
11746 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011747 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011748 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011749 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011750 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011751 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011752 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011753 return -EINVAL;
11754 return 0;
11755}
11756
Bandan Dasc5f983f2017-05-05 15:25:14 -040011757static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11758 struct vmcs12 *vmcs12)
11759{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011760 if (!nested_cpu_has_pml(vmcs12))
11761 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011762
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011763 if (!nested_cpu_has_ept(vmcs12) ||
11764 !page_address_valid(vcpu, vmcs12->pml_address))
11765 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011766
11767 return 0;
11768}
11769
Liran Alona8a7c022018-06-23 02:35:06 +030011770static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11771 struct vmcs12 *vmcs12)
11772{
11773 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11774 return 0;
11775
11776 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11777 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11778 return -EINVAL;
11779
11780 return 0;
11781}
11782
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011783static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11784 struct vmx_msr_entry *e)
11785{
11786 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011787 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011788 return -EINVAL;
11789 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11790 e->index == MSR_IA32_UCODE_REV)
11791 return -EINVAL;
11792 if (e->reserved != 0)
11793 return -EINVAL;
11794 return 0;
11795}
11796
11797static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11798 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011799{
11800 if (e->index == MSR_FS_BASE ||
11801 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011802 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11803 nested_vmx_msr_check_common(vcpu, e))
11804 return -EINVAL;
11805 return 0;
11806}
11807
11808static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11809 struct vmx_msr_entry *e)
11810{
11811 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11812 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011813 return -EINVAL;
11814 return 0;
11815}
11816
11817/*
11818 * Load guest's/host's msr at nested entry/exit.
11819 * return 0 for success, entry index for failure.
11820 */
11821static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11822{
11823 u32 i;
11824 struct vmx_msr_entry e;
11825 struct msr_data msr;
11826
11827 msr.host_initiated = false;
11828 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011829 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11830 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011831 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011832 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11833 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011834 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011835 }
11836 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011837 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011838 "%s check failed (%u, 0x%x, 0x%x)\n",
11839 __func__, i, e.index, e.reserved);
11840 goto fail;
11841 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011842 msr.index = e.index;
11843 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011844 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011845 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011846 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11847 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011848 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011849 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011850 }
11851 return 0;
11852fail:
11853 return i + 1;
11854}
11855
11856static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11857{
11858 u32 i;
11859 struct vmx_msr_entry e;
11860
11861 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011862 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011863 if (kvm_vcpu_read_guest(vcpu,
11864 gpa + i * sizeof(e),
11865 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011866 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011867 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11868 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011869 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011870 }
11871 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011872 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011873 "%s check failed (%u, 0x%x, 0x%x)\n",
11874 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011875 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011876 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011877 msr_info.host_initiated = false;
11878 msr_info.index = e.index;
11879 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011880 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011881 "%s cannot read MSR (%u, 0x%x)\n",
11882 __func__, i, e.index);
11883 return -EINVAL;
11884 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011885 if (kvm_vcpu_write_guest(vcpu,
11886 gpa + i * sizeof(e) +
11887 offsetof(struct vmx_msr_entry, value),
11888 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011889 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011890 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011891 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011892 return -EINVAL;
11893 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011894 }
11895 return 0;
11896}
11897
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011898static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11899{
11900 unsigned long invalid_mask;
11901
11902 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11903 return (val & invalid_mask) == 0;
11904}
11905
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011906/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011907 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11908 * emulating VM entry into a guest with EPT enabled.
11909 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11910 * is assigned to entry_failure_code on failure.
11911 */
11912static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011913 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011914{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011915 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011916 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011917 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11918 return 1;
11919 }
11920
11921 /*
11922 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11923 * must not be dereferenced.
11924 */
11925 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11926 !nested_ept) {
11927 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11928 *entry_failure_code = ENTRY_FAIL_PDPTE;
11929 return 1;
11930 }
11931 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011932 }
11933
Junaid Shahid50c28f22018-06-27 14:59:11 -070011934 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011935 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011936
11937 vcpu->arch.cr3 = cr3;
11938 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11939
11940 kvm_init_mmu(vcpu, false);
11941
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011942 return 0;
11943}
11944
Liran Alonefebf0a2018-10-08 23:42:20 +030011945/*
11946 * Returns if KVM is able to config CPU to tag TLB entries
11947 * populated by L2 differently than TLB entries populated
11948 * by L1.
11949 *
11950 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11951 *
11952 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11953 * with different VPID (L1 entries are tagged with vmx->vpid
11954 * while L2 entries are tagged with vmx->nested.vpid02).
11955 */
11956static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11957{
11958 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11959
11960 return nested_cpu_has_ept(vmcs12) ||
11961 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11962}
11963
Sean Christopherson3df5c372018-09-26 09:23:44 -070011964static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11965{
11966 if (vmx->nested.nested_run_pending &&
11967 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11968 return vmcs12->guest_ia32_efer;
11969 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11970 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11971 else
11972 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11973}
11974
Sean Christopherson09abe322018-09-26 09:23:50 -070011975static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011976{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011977 /*
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011978 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070011979 * according to L0's settings (vmcs12 is irrelevant here). Host
11980 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11981 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011982 */
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011983 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070011984 return;
Sean Christopherson9d6105b22018-09-26 09:23:51 -070011985 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011986
Sean Christopherson52017602018-09-26 09:23:57 -070011987 /*
11988 * We don't care what the EPTP value is we just need to guarantee
11989 * it's valid so we don't get a false positive when doing early
11990 * consistency checks.
11991 */
11992 if (enable_ept && nested_early_check)
11993 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
11994
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011995 /* All VMFUNCs are currently emulated through L0 vmexits. */
11996 if (cpu_has_vmx_vmfunc())
11997 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11998
Sean Christopherson09abe322018-09-26 09:23:50 -070011999 if (cpu_has_vmx_posted_intr())
12000 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12001
12002 if (cpu_has_vmx_msr_bitmap())
12003 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
12004
12005 if (enable_pml)
12006 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012007
12008 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012009 * Set the MSR load/store lists to match L0's settings. Only the
12010 * addresses are constant (for vmcs02), the counts can change based
12011 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012012 */
12013 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012014 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012015 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012016
Sean Christopherson09abe322018-09-26 09:23:50 -070012017 vmx_set_constant_host_state(vmx);
12018}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012019
Sean Christopherson09abe322018-09-26 09:23:50 -070012020static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
12021 struct vmcs12 *vmcs12)
12022{
12023 prepare_vmcs02_constant_state(vmx);
12024
12025 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012026
12027 if (enable_vpid) {
12028 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12029 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12030 else
12031 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12032 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012033}
12034
Sean Christopherson09abe322018-09-26 09:23:50 -070012035static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012036{
Bandan Das03efce62017-05-05 15:25:15 -040012037 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012038 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012039
Sean Christopherson09abe322018-09-26 09:23:50 -070012040 if (vmx->nested.dirty_vmcs12)
12041 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012042
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012043 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012044 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12045 * entry, but only if the current (host) sp changed from the value
12046 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12047 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012048 * here we just force the write to happen on entry. host_rsp will
12049 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12050 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012051 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012052 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012053
Sean Christopherson09abe322018-09-26 09:23:50 -070012054 /*
12055 * PIN CONTROLS
12056 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012057 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012058
Sean Christophersonf459a702018-08-27 15:21:11 -070012059 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012060 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012061 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12062 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012063
12064 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012065 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012066 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12067 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012068 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012069 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012070 }
Jan Kiszkaf4124502014-03-07 20:03:13 +010012071 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012072
Sean Christopherson09abe322018-09-26 09:23:50 -070012073 /*
12074 * EXEC CONTROLS
12075 */
12076 exec_control = vmx_exec_control(vmx); /* L0's desires */
12077 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12078 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12079 exec_control &= ~CPU_BASED_TPR_SHADOW;
12080 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012081
Sean Christopherson09abe322018-09-26 09:23:50 -070012082 /*
12083 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12084 * nested_get_vmcs12_pages can't fix it up, the illegal value
12085 * will result in a VM entry failure.
12086 */
12087 if (exec_control & CPU_BASED_TPR_SHADOW) {
12088 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12089 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12090 } else {
12091#ifdef CONFIG_X86_64
12092 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12093 CPU_BASED_CR8_STORE_EXITING;
12094#endif
12095 }
12096
12097 /*
12098 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12099 * for I/O port accesses.
12100 */
12101 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12102 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12103 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12104
12105 /*
12106 * SECONDARY EXEC CONTROLS
12107 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012108 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012109 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012110
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012111 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012112 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012113 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012114 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012115 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012116 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012117 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12118 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012119 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012120 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12121 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12122 ~SECONDARY_EXEC_ENABLE_PML;
12123 exec_control |= vmcs12_exec_ctrl;
12124 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012125
Liran Alon32c7acf2018-06-23 02:35:11 +030012126 /* VMCS shadowing for L2 is emulated for now */
12127 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12128
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012129 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012130 vmcs_write16(GUEST_INTR_STATUS,
12131 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012132
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012133 /*
12134 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12135 * nested_get_vmcs12_pages will either fix it up or
12136 * remove the VM execution control.
12137 */
12138 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12139 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12140
Sean Christopherson0b665d32018-08-14 09:33:34 -070012141 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12142 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12143
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012144 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12145 }
12146
Jim Mattson83bafef2016-10-04 10:48:38 -070012147 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012148 * ENTRY CONTROLS
12149 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012150 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012151 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12152 * on the related bits (if supported by the CPU) in the hope that
12153 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012154 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012155 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12156 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12157 if (cpu_has_load_ia32_efer) {
12158 if (guest_efer & EFER_LMA)
12159 exec_control |= VM_ENTRY_IA32E_MODE;
12160 if (guest_efer != host_efer)
12161 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12162 }
12163 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012164
Sean Christopherson09abe322018-09-26 09:23:50 -070012165 /*
12166 * EXIT CONTROLS
12167 *
12168 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12169 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12170 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12171 */
12172 exec_control = vmcs_config.vmexit_ctrl;
12173 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12174 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12175 vm_exit_controls_init(vmx, exec_control);
12176
12177 /*
12178 * Conceptually we want to copy the PML address and index from
12179 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12180 * since we always flush the log on each vmexit and never change
12181 * the PML address (once set), this happens to be equivalent to
12182 * simply resetting the index in vmcs02.
12183 */
12184 if (enable_pml)
12185 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12186
12187 /*
12188 * Interrupt/Exception Fields
12189 */
12190 if (vmx->nested.nested_run_pending) {
12191 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12192 vmcs12->vm_entry_intr_info_field);
12193 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12194 vmcs12->vm_entry_exception_error_code);
12195 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12196 vmcs12->vm_entry_instruction_len);
12197 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12198 vmcs12->guest_interruptibility_info);
12199 vmx->loaded_vmcs->nmi_known_unmasked =
12200 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12201 } else {
12202 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12203 }
12204}
12205
12206static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12207{
12208 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12209 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12210 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12211 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12212 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12213 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12214 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12215 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12216 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12217 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12218 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12219 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12220 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12221 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12222 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12223 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12224 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12225 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12226 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12227 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12228 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12229 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12230 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12231 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12232 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12233 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12234 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12235 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12236 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12237 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12238 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12239
12240 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12241 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12242 vmcs12->guest_pending_dbg_exceptions);
12243 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12244 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12245
12246 if (nested_cpu_has_xsaves(vmcs12))
12247 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12248
12249 /*
12250 * Whether page-faults are trapped is determined by a combination of
12251 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12252 * If enable_ept, L0 doesn't care about page faults and we should
12253 * set all of these to L1's desires. However, if !enable_ept, L0 does
12254 * care about (at least some) page faults, and because it is not easy
12255 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12256 * to exit on each and every L2 page fault. This is done by setting
12257 * MASK=MATCH=0 and (see below) EB.PF=1.
12258 * Note that below we don't need special code to set EB.PF beyond the
12259 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12260 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12261 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12262 */
12263 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12264 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12265 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12266 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12267
12268 if (cpu_has_vmx_apicv()) {
12269 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12270 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12271 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12272 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12273 }
12274
12275 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12276 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12277
12278 set_cr4_guest_host_mask(vmx);
12279
12280 if (kvm_mpx_supported()) {
12281 if (vmx->nested.nested_run_pending &&
12282 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12283 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12284 else
12285 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12286 }
12287
12288 /*
12289 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12290 */
12291 if (enable_ept) {
12292 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12293 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12294 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12295 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12296 }
12297}
12298
12299/*
12300 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12301 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12302 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12303 * guest in a way that will both be appropriate to L1's requests, and our
12304 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12305 * function also has additional necessary side-effects, like setting various
12306 * vcpu->arch fields.
12307 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12308 * is assigned to entry_failure_code on failure.
12309 */
12310static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12311 u32 *entry_failure_code)
12312{
12313 struct vcpu_vmx *vmx = to_vmx(vcpu);
12314
12315 if (vmx->nested.dirty_vmcs12) {
12316 prepare_vmcs02_full(vmx, vmcs12);
12317 vmx->nested.dirty_vmcs12 = false;
12318 }
12319
12320 /*
12321 * First, the fields that are shadowed. This must be kept in sync
12322 * with vmx_shadow_fields.h.
12323 */
12324
12325 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12326 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12327 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12328 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12329 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12330
12331 if (vmx->nested.nested_run_pending &&
12332 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12333 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12334 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12335 } else {
12336 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12337 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12338 }
12339 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12340
12341 vmx->nested.preemption_timer_expired = false;
12342 if (nested_cpu_has_preemption_timer(vmcs12))
12343 vmx_start_preemption_timer(vcpu);
12344
12345 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12346 * bitwise-or of what L1 wants to trap for L2, and what we want to
12347 * trap. Note that CR0.TS also needs updating - we do this later.
12348 */
12349 update_exception_bitmap(vcpu);
12350 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12351 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12352
Jim Mattson6514dc32018-04-26 16:09:12 -070012353 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012354 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012355 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012356 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012357 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012358 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012359 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012360
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012361 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12362
Peter Feinerc95ba922016-08-17 09:36:47 -070012363 if (kvm_has_tsc_control)
12364 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012365
12366 if (enable_vpid) {
12367 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012368 * There is no direct mapping between vpid02 and vpid12, the
12369 * vpid02 is per-vCPU for L0 and reused while the value of
12370 * vpid12 is changed w/ one invvpid during nested vmentry.
12371 * The vpid12 is allocated by L1 for L2, so it will not
12372 * influence global bitmap(for vpid01 and vpid02 allocation)
12373 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012374 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012375 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012376 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12377 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012378 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012379 }
12380 } else {
Liran Alon14389212018-10-08 23:42:17 +030012381 /*
12382 * If L1 use EPT, then L0 needs to execute INVEPT on
12383 * EPTP02 instead of EPTP01. Therefore, delay TLB
12384 * flush until vmcs02->eptp is fully updated by
12385 * KVM_REQ_LOAD_CR3. Note that this assumes
12386 * KVM_REQ_TLB_FLUSH is evaluated after
12387 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12388 */
12389 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012390 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012391 }
12392
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012393 if (nested_cpu_has_ept(vmcs12))
12394 nested_ept_init_mmu_context(vcpu);
12395 else if (nested_cpu_has2(vmcs12,
12396 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012397 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012398
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012399 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012400 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12401 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012402 * The CR0_READ_SHADOW is what L2 should have expected to read given
12403 * the specifications by L1; It's not enough to take
12404 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12405 * have more bits than L1 expected.
12406 */
12407 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12408 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12409
12410 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12411 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12412
Sean Christopherson09abe322018-09-26 09:23:50 -070012413 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012414 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012415 vmx_set_efer(vcpu, vcpu->arch.efer);
12416
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012417 /*
12418 * Guest state is invalid and unrestricted guest is disabled,
12419 * which means L1 attempted VMEntry to L2 with invalid state.
12420 * Fail the VMEntry.
12421 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012422 if (vmx->emulation_required) {
12423 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012424 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012425 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012426
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012427 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012428 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012429 entry_failure_code))
12430 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012431
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012432 if (!enable_ept)
12433 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12434
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012435 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12436 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012437 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012438}
12439
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012440static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12441{
12442 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12443 nested_cpu_has_virtual_nmis(vmcs12))
12444 return -EINVAL;
12445
12446 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12447 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12448 return -EINVAL;
12449
12450 return 0;
12451}
12452
Jim Mattsonca0bde22016-11-30 12:03:46 -080012453static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12454{
12455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012456 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012457
12458 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12459 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12460 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12461
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012462 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12463 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12464
Jim Mattson56a20512017-07-06 16:33:06 -070012465 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12466 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12467
Jim Mattsonca0bde22016-11-30 12:03:46 -080012468 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12469 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12470
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012471 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12472 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12473
Jim Mattson712b12d2017-08-24 13:24:47 -070012474 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12475 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12476
Jim Mattsonca0bde22016-11-30 12:03:46 -080012477 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12478 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12479
12480 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12481 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12482
Bandan Dasc5f983f2017-05-05 15:25:14 -040012483 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12484 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12485
Liran Alona8a7c022018-06-23 02:35:06 +030012486 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12487 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12488
Jim Mattsonca0bde22016-11-30 12:03:46 -080012489 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012490 vmx->nested.msrs.procbased_ctls_low,
12491 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012492 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12493 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012494 vmx->nested.msrs.secondary_ctls_low,
12495 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012496 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012497 vmx->nested.msrs.pinbased_ctls_low,
12498 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012499 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012500 vmx->nested.msrs.exit_ctls_low,
12501 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012502 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012503 vmx->nested.msrs.entry_ctls_low,
12504 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012505 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12506
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012507 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012508 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12509
Bandan Das41ab9372017-08-03 15:54:43 -040012510 if (nested_cpu_has_vmfunc(vmcs12)) {
12511 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012512 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012513 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12514
12515 if (nested_cpu_has_eptp_switching(vmcs12)) {
12516 if (!nested_cpu_has_ept(vmcs12) ||
12517 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12518 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12519 }
12520 }
Bandan Das27c42a12017-08-03 15:54:42 -040012521
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012522 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12523 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12524
Jim Mattsonca0bde22016-11-30 12:03:46 -080012525 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12526 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12527 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12528 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12529
Marc Orr04473782018-06-20 17:21:29 -070012530 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012531 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12532 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12533 * the values of the LMA and LME bits in the field must each be that of
12534 * the host address-space size VM-exit control.
12535 */
12536 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12537 ia32e = (vmcs12->vm_exit_controls &
12538 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12539 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12540 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12541 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12542 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12543 }
12544
12545 /*
Marc Orr04473782018-06-20 17:21:29 -070012546 * From the Intel SDM, volume 3:
12547 * Fields relevant to VM-entry event injection must be set properly.
12548 * These fields are the VM-entry interruption-information field, the
12549 * VM-entry exception error code, and the VM-entry instruction length.
12550 */
12551 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12552 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12553 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12554 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12555 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12556 bool should_have_error_code;
12557 bool urg = nested_cpu_has2(vmcs12,
12558 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12559 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12560
12561 /* VM-entry interruption-info field: interruption type */
12562 if (intr_type == INTR_TYPE_RESERVED ||
12563 (intr_type == INTR_TYPE_OTHER_EVENT &&
12564 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12565 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12566
12567 /* VM-entry interruption-info field: vector */
12568 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12569 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12570 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12571 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12572
12573 /* VM-entry interruption-info field: deliver error code */
12574 should_have_error_code =
12575 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12576 x86_exception_has_error_code(vector);
12577 if (has_error_code != should_have_error_code)
12578 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12579
12580 /* VM-entry exception error code */
12581 if (has_error_code &&
12582 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12583 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12584
12585 /* VM-entry interruption-info field: reserved bits */
12586 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12587 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12588
12589 /* VM-entry instruction length */
12590 switch (intr_type) {
12591 case INTR_TYPE_SOFT_EXCEPTION:
12592 case INTR_TYPE_SOFT_INTR:
12593 case INTR_TYPE_PRIV_SW_EXCEPTION:
12594 if ((vmcs12->vm_entry_instruction_len > 15) ||
12595 (vmcs12->vm_entry_instruction_len == 0 &&
12596 !nested_cpu_has_zero_length_injection(vcpu)))
12597 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12598 }
12599 }
12600
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012601 if (nested_cpu_has_ept(vmcs12) &&
12602 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12603 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12604
Jim Mattsonca0bde22016-11-30 12:03:46 -080012605 return 0;
12606}
12607
Liran Alonf145d902018-06-23 02:35:07 +030012608static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12609 struct vmcs12 *vmcs12)
12610{
12611 int r;
12612 struct page *page;
12613 struct vmcs12 *shadow;
12614
12615 if (vmcs12->vmcs_link_pointer == -1ull)
12616 return 0;
12617
12618 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12619 return -EINVAL;
12620
12621 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12622 if (is_error_page(page))
12623 return -EINVAL;
12624
12625 r = 0;
12626 shadow = kmap(page);
12627 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12628 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12629 r = -EINVAL;
12630 kunmap(page);
12631 kvm_release_page_clean(page);
12632 return r;
12633}
12634
Jim Mattsonca0bde22016-11-30 12:03:46 -080012635static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12636 u32 *exit_qual)
12637{
12638 bool ia32e;
12639
12640 *exit_qual = ENTRY_FAIL_DEFAULT;
12641
12642 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12643 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12644 return 1;
12645
Liran Alonf145d902018-06-23 02:35:07 +030012646 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012647 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12648 return 1;
12649 }
12650
12651 /*
12652 * If the load IA32_EFER VM-entry control is 1, the following checks
12653 * are performed on the field for the IA32_EFER MSR:
12654 * - Bits reserved in the IA32_EFER MSR must be 0.
12655 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12656 * the IA-32e mode guest VM-exit control. It must also be identical
12657 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12658 * CR0.PG) is 1.
12659 */
12660 if (to_vmx(vcpu)->nested.nested_run_pending &&
12661 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12662 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12663 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12664 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12665 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12666 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12667 return 1;
12668 }
12669
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012670 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12671 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12672 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12673 return 1;
12674
Jim Mattsonca0bde22016-11-30 12:03:46 -080012675 return 0;
12676}
12677
Sean Christopherson52017602018-09-26 09:23:57 -070012678static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
12679{
12680 struct vcpu_vmx *vmx = to_vmx(vcpu);
12681 unsigned long cr3, cr4;
12682
12683 if (!nested_early_check)
12684 return 0;
12685
12686 if (vmx->msr_autoload.host.nr)
12687 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
12688 if (vmx->msr_autoload.guest.nr)
12689 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
12690
12691 preempt_disable();
12692
12693 vmx_prepare_switch_to_guest(vcpu);
12694
12695 /*
12696 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
12697 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
12698 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
12699 * there is no need to preserve other bits or save/restore the field.
12700 */
12701 vmcs_writel(GUEST_RFLAGS, 0);
12702
12703 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
12704
12705 cr3 = __get_current_cr3_fast();
12706 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
12707 vmcs_writel(HOST_CR3, cr3);
12708 vmx->loaded_vmcs->host_state.cr3 = cr3;
12709 }
12710
12711 cr4 = cr4_read_shadow();
12712 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
12713 vmcs_writel(HOST_CR4, cr4);
12714 vmx->loaded_vmcs->host_state.cr4 = cr4;
12715 }
12716
12717 vmx->__launched = vmx->loaded_vmcs->launched;
12718
12719 asm(
12720 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012721 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012722 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
12723
12724 /* Check if vmlaunch of vmresume is needed */
12725 "cmpl $0, %c[launched](%0)\n\t"
12726 "je 1f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012727 __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012728 "jmp 2f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012729 "1: " __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012730 "jmp 2f\n\t"
12731 "2: "
12732
12733 /* Set vmx->fail accordingly */
12734 "setbe %c[fail](%0)\n\t"
12735
12736 ".pushsection .rodata\n\t"
12737 ".global vmx_early_consistency_check_return\n\t"
12738 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
12739 ".popsection"
12740 :
12741 : "c"(vmx), "d"((unsigned long)HOST_RSP),
12742 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
12743 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
12744 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
12745 : "rax", "cc", "memory"
12746 );
12747
12748 vmcs_writel(HOST_RIP, vmx_return);
12749
12750 preempt_enable();
12751
12752 if (vmx->msr_autoload.host.nr)
12753 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12754 if (vmx->msr_autoload.guest.nr)
12755 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12756
12757 if (vmx->fail) {
12758 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12759 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12760 vmx->fail = 0;
12761 return 1;
12762 }
12763
12764 /*
12765 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
12766 */
12767 local_irq_enable();
12768 if (hw_breakpoint_active())
12769 set_debugreg(__this_cpu_read(cpu_dr7), 7);
12770
12771 /*
12772 * A non-failing VMEntry means we somehow entered guest mode with
12773 * an illegal RIP, and that's just the tip of the iceberg. There
12774 * is no telling what memory has been modified or what state has
12775 * been exposed to unknown code. Hitting this all but guarantees
12776 * a (very critical) hardware issue.
12777 */
12778 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
12779 VMX_EXIT_REASONS_FAILED_VMENTRY));
12780
12781 return 0;
12782}
12783STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
12784
Sean Christophersona633e412018-09-26 09:23:47 -070012785static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12786 struct vmcs12 *vmcs12);
12787
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012788/*
Sean Christophersona633e412018-09-26 09:23:47 -070012789 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012790 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070012791+ *
12792+ * Returns:
12793+ * 0 - success, i.e. proceed with actual VMEnter
12794+ * 1 - consistency check VMExit
12795+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012796 */
Sean Christophersona633e412018-09-26 09:23:47 -070012797static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12798 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012799{
12800 struct vcpu_vmx *vmx = to_vmx(vcpu);
12801 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012802 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012803 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12804 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012805
Paolo Bonzini7e712682018-10-03 13:44:26 +020012806 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12807 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12808 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12809 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012810
Jim Mattson858e25c2016-11-30 12:03:47 -080012811 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12812 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012813 if (kvm_mpx_supported() &&
12814 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12815 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012816
Jim Mattsonde3a0022017-11-27 17:22:25 -060012817 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012818
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012819 prepare_vmcs02_early(vmx, vmcs12);
12820
12821 if (from_vmentry) {
12822 nested_get_vmcs12_pages(vcpu);
12823
Sean Christopherson52017602018-09-26 09:23:57 -070012824 if (nested_vmx_check_vmentry_hw(vcpu)) {
12825 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12826 return -1;
12827 }
12828
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012829 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12830 goto vmentry_fail_vmexit;
12831 }
12832
12833 enter_guest_mode(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012834 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12835 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12836
Sean Christophersona633e412018-09-26 09:23:47 -070012837 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012838 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012839
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012840 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070012841 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12842 exit_qual = nested_vmx_load_msr(vcpu,
12843 vmcs12->vm_entry_msr_load_addr,
12844 vmcs12->vm_entry_msr_load_count);
12845 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012846 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012847 } else {
12848 /*
12849 * The MMU is not initialized to point at the right entities yet and
12850 * "get pages" would need to read data from the guest (i.e. we will
12851 * need to perform gpa to hpa translation). Request a call
12852 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12853 * have already been set at vmentry time and should not be reset.
12854 */
12855 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12856 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012857
Jim Mattson858e25c2016-11-30 12:03:47 -080012858 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012859 * If L1 had a pending IRQ/NMI until it executed
12860 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12861 * disallowed (e.g. interrupts disabled), L0 needs to
12862 * evaluate if this pending event should cause an exit from L2
12863 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12864 * intercept EXTERNAL_INTERRUPT).
12865 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012866 * Usually this would be handled by the processor noticing an
12867 * IRQ/NMI window request, or checking RVI during evaluation of
12868 * pending virtual interrupts. However, this setting was done
12869 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12870 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012871 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012872 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012873 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012874
12875 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012876 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12877 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12878 * returned as far as L1 is concerned. It will only return (and set
12879 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12880 */
12881 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012882
Sean Christophersona633e412018-09-26 09:23:47 -070012883 /*
12884 * A failed consistency check that leads to a VMExit during L1's
12885 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12886 * 26.7 "VM-entry failures during or after loading guest state".
12887 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012888vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012889 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12890 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12891 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012892
12893vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012894 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012895
12896 if (!from_vmentry)
12897 return 1;
12898
Sean Christophersona633e412018-09-26 09:23:47 -070012899 load_vmcs12_host_state(vcpu, vmcs12);
12900 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12901 vmcs12->exit_qualification = exit_qual;
Sean Christophersona633e412018-09-26 09:23:47 -070012902 if (enable_shadow_vmcs)
12903 vmx->nested.sync_shadow_vmcs = true;
12904 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012905}
12906
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012907/*
12908 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12909 * for running an L2 nested guest.
12910 */
12911static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12912{
12913 struct vmcs12 *vmcs12;
12914 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012915 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012916 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012917
Kyle Hueyeb277562016-11-29 12:40:39 -080012918 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012919 return 1;
12920
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012921 if (vmx->nested.current_vmptr == -1ull)
12922 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080012923
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012924 vmcs12 = get_vmcs12(vcpu);
12925
Liran Alona6192d42018-06-23 02:35:04 +030012926 /*
12927 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12928 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12929 * rather than RFLAGS.ZF, and no error number is stored to the
12930 * VM-instruction error field.
12931 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012932 if (vmcs12->hdr.shadow_vmcs)
12933 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030012934
Abel Gordon012f83c2013-04-18 14:39:25 +030012935 if (enable_shadow_vmcs)
12936 copy_shadow_to_vmcs12(vmx);
12937
Nadav Har'El7c177932011-05-25 23:12:04 +030012938 /*
12939 * The nested entry process starts with enforcing various prerequisites
12940 * on vmcs12 as required by the Intel SDM, and act appropriately when
12941 * they fail: As the SDM explains, some conditions should cause the
12942 * instruction to fail, while others will cause the instruction to seem
12943 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12944 * To speed up the normal (success) code path, we should avoid checking
12945 * for misconfigurations which will anyway be caught by the processor
12946 * when using the merged vmcs02.
12947 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012948 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
12949 return nested_vmx_failValid(vcpu,
12950 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012951
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012952 if (vmcs12->launch_state == launch)
12953 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030012954 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12955 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030012956
Jim Mattsonca0bde22016-11-30 12:03:46 -080012957 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012958 if (ret)
12959 return nested_vmx_failValid(vcpu, ret);
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012960
Nadav Har'El7c177932011-05-25 23:12:04 +030012961 /*
12962 * We're finally done with prerequisite checking, and can start with
12963 * the nested entry.
12964 */
Jim Mattson6514dc32018-04-26 16:09:12 -070012965 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012966 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070012967 vmx->nested.nested_run_pending = !ret;
12968 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012969 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070012970 else if (ret)
12971 return nested_vmx_failValid(vcpu,
12972 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030012973
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012974 /* Hide L1D cache contents from the nested guest. */
12975 vmx->vcpu.arch.l1tf_flush_l1d = true;
12976
Chao Gao135a06c2018-02-11 10:06:30 +080012977 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012978 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012979 * also be used as part of restoring nVMX state for
12980 * snapshot restore (migration).
12981 *
12982 * In this flow, it is assumed that vmcs12 cache was
12983 * trasferred as part of captured nVMX state and should
12984 * therefore not be read from guest memory (which may not
12985 * exist on destination host yet).
12986 */
12987 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12988
12989 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012990 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12991 * by event injection, halt vcpu.
12992 */
12993 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012994 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12995 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012996 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012997 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012998 return 1;
12999}
13000
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013001/*
13002 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
13003 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
13004 * This function returns the new value we should put in vmcs12.guest_cr0.
13005 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
13006 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
13007 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
13008 * didn't trap the bit, because if L1 did, so would L0).
13009 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
13010 * been modified by L2, and L1 knows it. So just leave the old value of
13011 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
13012 * isn't relevant, because if L0 traps this bit it can set it to anything.
13013 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
13014 * changed these bits, and therefore they need to be updated, but L0
13015 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
13016 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
13017 */
13018static inline unsigned long
13019vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13020{
13021 return
13022 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
13023 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
13024 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
13025 vcpu->arch.cr0_guest_owned_bits));
13026}
13027
13028static inline unsigned long
13029vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
13030{
13031 return
13032 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
13033 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
13034 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13035 vcpu->arch.cr4_guest_owned_bits));
13036}
13037
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013038static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13039 struct vmcs12 *vmcs12)
13040{
13041 u32 idt_vectoring;
13042 unsigned int nr;
13043
Wanpeng Li664f8e22017-08-24 03:35:09 -070013044 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013045 nr = vcpu->arch.exception.nr;
13046 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13047
13048 if (kvm_exception_is_soft(nr)) {
13049 vmcs12->vm_exit_instruction_len =
13050 vcpu->arch.event_exit_inst_len;
13051 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13052 } else
13053 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13054
13055 if (vcpu->arch.exception.has_error_code) {
13056 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13057 vmcs12->idt_vectoring_error_code =
13058 vcpu->arch.exception.error_code;
13059 }
13060
13061 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013062 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013063 vmcs12->idt_vectoring_info_field =
13064 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013065 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013066 nr = vcpu->arch.interrupt.nr;
13067 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13068
13069 if (vcpu->arch.interrupt.soft) {
13070 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13071 vmcs12->vm_entry_instruction_len =
13072 vcpu->arch.event_exit_inst_len;
13073 } else
13074 idt_vectoring |= INTR_TYPE_EXT_INTR;
13075
13076 vmcs12->idt_vectoring_info_field = idt_vectoring;
13077 }
13078}
13079
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013080static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13081{
13082 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013083 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013084 bool block_nested_events =
13085 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013086
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013087 if (vcpu->arch.exception.pending &&
13088 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013089 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013090 return -EBUSY;
13091 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013092 return 0;
13093 }
13094
Jan Kiszkaf4124502014-03-07 20:03:13 +010013095 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13096 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013097 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010013098 return -EBUSY;
13099 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13100 return 0;
13101 }
13102
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013103 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013104 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013105 return -EBUSY;
13106 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13107 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13108 INTR_INFO_VALID_MASK, 0);
13109 /*
13110 * The NMI-triggered VM exit counts as injection:
13111 * clear this one and block further NMIs.
13112 */
13113 vcpu->arch.nmi_pending = 0;
13114 vmx_set_nmi_mask(vcpu, true);
13115 return 0;
13116 }
13117
13118 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13119 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013120 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013121 return -EBUSY;
13122 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013123 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013124 }
13125
David Hildenbrand6342c502017-01-25 11:58:58 +010013126 vmx_complete_nested_posted_interrupt(vcpu);
13127 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013128}
13129
Sean Christophersond264ee02018-08-27 15:21:12 -070013130static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13131{
13132 to_vmx(vcpu)->req_immediate_exit = true;
13133}
13134
Jan Kiszkaf4124502014-03-07 20:03:13 +010013135static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13136{
13137 ktime_t remaining =
13138 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13139 u64 value;
13140
13141 if (ktime_to_ns(remaining) <= 0)
13142 return 0;
13143
13144 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13145 do_div(value, 1000000);
13146 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13147}
13148
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013149/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013150 * Update the guest state fields of vmcs12 to reflect changes that
13151 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13152 * VM-entry controls is also updated, since this is really a guest
13153 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013154 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013155static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013156{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013157 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13158 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13159
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013160 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13161 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13162 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13163
13164 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13165 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13166 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13167 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13168 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13169 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13170 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13171 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13172 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13173 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13174 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13175 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13176 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13177 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13178 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13179 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13180 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13181 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13182 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13183 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13184 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13185 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13186 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13187 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13188 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13189 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13190 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13191 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13192 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13193 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13194 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13195 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13196 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13197 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13198 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13199 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13200
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013201 vmcs12->guest_interruptibility_info =
13202 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13203 vmcs12->guest_pending_dbg_exceptions =
13204 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013205 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13206 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13207 else
13208 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013209
Jan Kiszkaf4124502014-03-07 20:03:13 +010013210 if (nested_cpu_has_preemption_timer(vmcs12)) {
13211 if (vmcs12->vm_exit_controls &
13212 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13213 vmcs12->vmx_preemption_timer_value =
13214 vmx_get_preemption_timer_value(vcpu);
13215 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13216 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013217
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013218 /*
13219 * In some cases (usually, nested EPT), L2 is allowed to change its
13220 * own CR3 without exiting. If it has changed it, we must keep it.
13221 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13222 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13223 *
13224 * Additionally, restore L2's PDPTR to vmcs12.
13225 */
13226 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013227 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013228 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13229 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13230 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13231 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13232 }
13233
Jim Mattsond281e132017-06-01 12:44:46 -070013234 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013235
Wincy Van608406e2015-02-03 23:57:51 +080013236 if (nested_cpu_has_vid(vmcs12))
13237 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13238
Jan Kiszkac18911a2013-03-13 16:06:41 +010013239 vmcs12->vm_entry_controls =
13240 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013241 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013242
Jan Kiszka2996fca2014-06-16 13:59:43 +020013243 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13244 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13245 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13246 }
13247
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013248 /* TODO: These cannot have changed unless we have MSR bitmaps and
13249 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013250 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013251 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013252 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13253 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013254 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13255 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13256 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013257 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013258 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013259}
13260
13261/*
13262 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13263 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13264 * and this function updates it to reflect the changes to the guest state while
13265 * L2 was running (and perhaps made some exits which were handled directly by L0
13266 * without going back to L1), and to reflect the exit reason.
13267 * Note that we do not have to copy here all VMCS fields, just those that
13268 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13269 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13270 * which already writes to vmcs12 directly.
13271 */
13272static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13273 u32 exit_reason, u32 exit_intr_info,
13274 unsigned long exit_qualification)
13275{
13276 /* update guest state fields: */
13277 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013278
13279 /* update exit information fields: */
13280
Jan Kiszka533558b2014-01-04 18:47:20 +010013281 vmcs12->vm_exit_reason = exit_reason;
13282 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013283 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013284
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013285 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013286 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13287 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13288
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013289 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013290 vmcs12->launch_state = 1;
13291
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013292 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13293 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013294 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013295
13296 /*
13297 * Transfer the event that L0 or L1 may wanted to inject into
13298 * L2 to IDT_VECTORING_INFO_FIELD.
13299 */
13300 vmcs12_save_pending_event(vcpu, vmcs12);
13301 }
13302
13303 /*
13304 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13305 * preserved above and would only end up incorrectly in L1.
13306 */
13307 vcpu->arch.nmi_injected = false;
13308 kvm_clear_exception_queue(vcpu);
13309 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013310}
13311
13312/*
13313 * A part of what we need to when the nested L2 guest exits and we want to
13314 * run its L1 parent, is to reset L1's guest state to the host state specified
13315 * in vmcs12.
13316 * This function is to be called not only on normal nested exit, but also on
13317 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13318 * Failures During or After Loading Guest State").
13319 * This function should be called when the active VMCS is L1's (vmcs01).
13320 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013321static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13322 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013323{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013324 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013325 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013326
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013327 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13328 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013329 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013330 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13331 else
13332 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13333 vmx_set_efer(vcpu, vcpu->arch.efer);
13334
13335 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13336 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013337 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013338 vmx_set_interrupt_shadow(vcpu, 0);
13339
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013340 /*
13341 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013342 * actually changed, because vmx_set_cr0 refers to efer set above.
13343 *
13344 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13345 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013346 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013347 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013348 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013349
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013350 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013351 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013352 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013353
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013354 nested_ept_uninit_mmu_context(vcpu);
13355
13356 /*
13357 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13358 * couldn't have changed.
13359 */
13360 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13361 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13362
13363 if (!enable_ept)
13364 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013365
Liran Alon6f1e03b2018-05-22 17:16:14 +030013366 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013367 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013368 * VMEntry/VMExit. Thus, no need to flush TLB.
13369 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013370 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13371 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013372 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013373 * Otherwise, we can preserve TLB entries as long as we are
13374 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013375 *
13376 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13377 * and therefore we request the TLB flush to happen only after VMCS EPTP
13378 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013379 */
13380 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013381 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013382 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013383 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013384
13385 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13386 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13387 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13388 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13389 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013390 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13391 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013392
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013393 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13394 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13395 vmcs_write64(GUEST_BNDCFGS, 0);
13396
Jan Kiszka44811c02013-08-04 17:17:27 +020013397 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013398 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013399 vcpu->arch.pat = vmcs12->host_ia32_pat;
13400 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013401 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13402 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13403 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013404
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013405 /* Set L1 segment info according to Intel SDM
13406 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13407 seg = (struct kvm_segment) {
13408 .base = 0,
13409 .limit = 0xFFFFFFFF,
13410 .selector = vmcs12->host_cs_selector,
13411 .type = 11,
13412 .present = 1,
13413 .s = 1,
13414 .g = 1
13415 };
13416 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13417 seg.l = 1;
13418 else
13419 seg.db = 1;
13420 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13421 seg = (struct kvm_segment) {
13422 .base = 0,
13423 .limit = 0xFFFFFFFF,
13424 .type = 3,
13425 .present = 1,
13426 .s = 1,
13427 .db = 1,
13428 .g = 1
13429 };
13430 seg.selector = vmcs12->host_ds_selector;
13431 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13432 seg.selector = vmcs12->host_es_selector;
13433 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13434 seg.selector = vmcs12->host_ss_selector;
13435 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13436 seg.selector = vmcs12->host_fs_selector;
13437 seg.base = vmcs12->host_fs_base;
13438 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13439 seg.selector = vmcs12->host_gs_selector;
13440 seg.base = vmcs12->host_gs_base;
13441 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13442 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013443 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013444 .limit = 0x67,
13445 .selector = vmcs12->host_tr_selector,
13446 .type = 11,
13447 .present = 1
13448 };
13449 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13450
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013451 kvm_set_dr(vcpu, 7, 0x400);
13452 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013453
Wincy Van3af18d92015-02-03 23:49:31 +080013454 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013455 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013456
Wincy Vanff651cb2014-12-11 08:52:58 +030013457 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13458 vmcs12->vm_exit_msr_load_count))
13459 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013460}
13461
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013462static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13463{
13464 struct shared_msr_entry *efer_msr;
13465 unsigned int i;
13466
13467 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13468 return vmcs_read64(GUEST_IA32_EFER);
13469
13470 if (cpu_has_load_ia32_efer)
13471 return host_efer;
13472
13473 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13474 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13475 return vmx->msr_autoload.guest.val[i].value;
13476 }
13477
13478 efer_msr = find_msr_entry(vmx, MSR_EFER);
13479 if (efer_msr)
13480 return efer_msr->data;
13481
13482 return host_efer;
13483}
13484
13485static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13486{
13487 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13488 struct vcpu_vmx *vmx = to_vmx(vcpu);
13489 struct vmx_msr_entry g, h;
13490 struct msr_data msr;
13491 gpa_t gpa;
13492 u32 i, j;
13493
13494 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13495
13496 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13497 /*
13498 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13499 * as vmcs01.GUEST_DR7 contains a userspace defined value
13500 * and vcpu->arch.dr7 is not squirreled away before the
13501 * nested VMENTER (not worth adding a variable in nested_vmx).
13502 */
13503 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13504 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13505 else
13506 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13507 }
13508
13509 /*
13510 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13511 * handle a variety of side effects to KVM's software model.
13512 */
13513 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13514
13515 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13516 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13517
13518 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13519 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13520
13521 nested_ept_uninit_mmu_context(vcpu);
13522 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13523 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13524
13525 /*
13526 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13527 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13528 * VMFail, like everything else we just need to ensure our
13529 * software model is up-to-date.
13530 */
13531 ept_save_pdptrs(vcpu);
13532
13533 kvm_mmu_reset_context(vcpu);
13534
13535 if (cpu_has_vmx_msr_bitmap())
13536 vmx_update_msr_bitmap(vcpu);
13537
13538 /*
13539 * This nasty bit of open coding is a compromise between blindly
13540 * loading L1's MSRs using the exit load lists (incorrect emulation
13541 * of VMFail), leaving the nested VM's MSRs in the software model
13542 * (incorrect behavior) and snapshotting the modified MSRs (too
13543 * expensive since the lists are unbound by hardware). For each
13544 * MSR that was (prematurely) loaded from the nested VMEntry load
13545 * list, reload it from the exit load list if it exists and differs
13546 * from the guest value. The intent is to stuff host state as
13547 * silently as possible, not to fully process the exit load list.
13548 */
13549 msr.host_initiated = false;
13550 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13551 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13552 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13553 pr_debug_ratelimited(
13554 "%s read MSR index failed (%u, 0x%08llx)\n",
13555 __func__, i, gpa);
13556 goto vmabort;
13557 }
13558
13559 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13560 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13561 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13562 pr_debug_ratelimited(
13563 "%s read MSR failed (%u, 0x%08llx)\n",
13564 __func__, j, gpa);
13565 goto vmabort;
13566 }
13567 if (h.index != g.index)
13568 continue;
13569 if (h.value == g.value)
13570 break;
13571
13572 if (nested_vmx_load_msr_check(vcpu, &h)) {
13573 pr_debug_ratelimited(
13574 "%s check failed (%u, 0x%x, 0x%x)\n",
13575 __func__, j, h.index, h.reserved);
13576 goto vmabort;
13577 }
13578
13579 msr.index = h.index;
13580 msr.data = h.value;
13581 if (kvm_set_msr(vcpu, &msr)) {
13582 pr_debug_ratelimited(
13583 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13584 __func__, j, h.index, h.value);
13585 goto vmabort;
13586 }
13587 }
13588 }
13589
13590 return;
13591
13592vmabort:
13593 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13594}
13595
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013596/*
13597 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13598 * and modify vmcs12 to make it see what it would expect to see there if
13599 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13600 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013601static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13602 u32 exit_intr_info,
13603 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013604{
13605 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013606 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13607
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013608 /* trying to cancel vmlaunch/vmresume is a bug */
13609 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13610
Jim Mattson4f350c62017-09-14 16:31:44 -070013611 leave_guest_mode(vcpu);
13612
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013613 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13614 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13615
Jim Mattson4f350c62017-09-14 16:31:44 -070013616 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013617 if (exit_reason == -1)
13618 sync_vmcs12(vcpu, vmcs12);
13619 else
13620 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13621 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013622
Liran Alon61ada742018-06-23 02:35:08 +030013623 /*
13624 * Must happen outside of sync_vmcs12() as it will
13625 * also be used to capture vmcs12 cache as part of
13626 * capturing nVMX state for snapshot (migration).
13627 *
13628 * Otherwise, this flush will dirty guest memory at a
13629 * point it is already assumed by user-space to be
13630 * immutable.
13631 */
13632 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13633
Jim Mattson4f350c62017-09-14 16:31:44 -070013634 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13635 vmcs12->vm_exit_msr_store_count))
13636 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070013637 } else {
13638 /*
13639 * The only expected VM-instruction error is "VM entry with
13640 * invalid control field(s)." Anything else indicates a
13641 * problem with L0. And we should never get here with a
13642 * VMFail of any type if early consistency checks are enabled.
13643 */
13644 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13645 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13646 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013647 }
13648
Jim Mattson4f350c62017-09-14 16:31:44 -070013649 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013650
Paolo Bonzini93140062016-07-06 13:23:51 +020013651 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013652 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13653 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013654 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013655
Peter Feinerc95ba922016-08-17 09:36:47 -070013656 if (kvm_has_tsc_control)
13657 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013658
Jim Mattson8d860bb2018-05-09 16:56:05 -040013659 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13660 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13661 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013662 } else if (!nested_cpu_has_ept(vmcs12) &&
13663 nested_cpu_has2(vmcs12,
13664 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013665 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013666 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013667
13668 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13669 vmx->host_rsp = 0;
13670
13671 /* Unpin physical memory we referred to in vmcs02 */
13672 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013673 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013674 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013675 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013676 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013677 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013678 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013679 }
Wincy Van705699a2015-02-03 23:58:17 +080013680 if (vmx->nested.pi_desc_page) {
13681 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013682 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013683 vmx->nested.pi_desc_page = NULL;
13684 vmx->nested.pi_desc = NULL;
13685 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013686
13687 /*
Tang Chen38b99172014-09-24 15:57:54 +080013688 * We are now running in L2, mmu_notifier will force to reload the
13689 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13690 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013691 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013692
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013693 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013694 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013695
13696 /* in case we halted in L2 */
13697 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013698
13699 if (likely(!vmx->fail)) {
13700 /*
13701 * TODO: SDM says that with acknowledge interrupt on
13702 * exit, bit 31 of the VM-exit interrupt information
13703 * (valid interrupt) is always set to 1 on
13704 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13705 * need kvm_cpu_has_interrupt(). See the commit
13706 * message for details.
13707 */
13708 if (nested_exit_intr_ack_set(vcpu) &&
13709 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13710 kvm_cpu_has_interrupt(vcpu)) {
13711 int irq = kvm_cpu_get_interrupt(vcpu);
13712 WARN_ON(irq < 0);
13713 vmcs12->vm_exit_intr_info = irq |
13714 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13715 }
13716
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013717 if (exit_reason != -1)
13718 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13719 vmcs12->exit_qualification,
13720 vmcs12->idt_vectoring_info_field,
13721 vmcs12->vm_exit_intr_info,
13722 vmcs12->vm_exit_intr_error_code,
13723 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013724
13725 load_vmcs12_host_state(vcpu, vmcs12);
13726
13727 return;
13728 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013729
Jim Mattson4f350c62017-09-14 16:31:44 -070013730 /*
13731 * After an early L2 VM-entry failure, we're now back
13732 * in L1 which thinks it just finished a VMLAUNCH or
13733 * VMRESUME instruction, so we need to set the failure
13734 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070013735 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070013736 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013737 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013738
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013739 /*
13740 * Restore L1's host state to KVM's software model. We're here
13741 * because a consistency check was caught by hardware, which
13742 * means some amount of guest state has been propagated to KVM's
13743 * model and needs to be unwound to the host's state.
13744 */
13745 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013746
Jim Mattson4f350c62017-09-14 16:31:44 -070013747 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013748}
13749
Nadav Har'El7c177932011-05-25 23:12:04 +030013750/*
Jan Kiszka42124922014-01-04 18:47:19 +010013751 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13752 */
13753static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13754{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013755 if (is_guest_mode(vcpu)) {
13756 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013757 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013758 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020013759 free_nested(vcpu);
Jan Kiszka42124922014-01-04 18:47:19 +010013760}
13761
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013762static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13763 struct x86_instruction_info *info,
13764 enum x86_intercept_stage stage)
13765{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013766 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13767 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13768
13769 /*
13770 * RDPID causes #UD if disabled through secondary execution controls.
13771 * Because it is marked as EmulateOnUD, we need to intercept it here.
13772 */
13773 if (info->intercept == x86_intercept_rdtscp &&
13774 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13775 ctxt->exception.vector = UD_VECTOR;
13776 ctxt->exception.error_code_valid = false;
13777 return X86EMUL_PROPAGATE_FAULT;
13778 }
13779
13780 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013781 return X86EMUL_CONTINUE;
13782}
13783
Yunhong Jiang64672c92016-06-13 14:19:59 -070013784#ifdef CONFIG_X86_64
13785/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13786static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13787 u64 divisor, u64 *result)
13788{
13789 u64 low = a << shift, high = a >> (64 - shift);
13790
13791 /* To avoid the overflow on divq */
13792 if (high >= divisor)
13793 return 1;
13794
13795 /* Low hold the result, high hold rem which is discarded */
13796 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13797 "rm" (divisor), "0" (low), "1" (high));
13798 *result = low;
13799
13800 return 0;
13801}
13802
13803static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13804{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013805 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013806 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013807
13808 if (kvm_mwait_in_guest(vcpu->kvm))
13809 return -EOPNOTSUPP;
13810
13811 vmx = to_vmx(vcpu);
13812 tscl = rdtsc();
13813 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13814 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013815 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13816
13817 if (delta_tsc > lapic_timer_advance_cycles)
13818 delta_tsc -= lapic_timer_advance_cycles;
13819 else
13820 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013821
13822 /* Convert to host delta tsc if tsc scaling is enabled */
13823 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13824 u64_shl_div_u64(delta_tsc,
13825 kvm_tsc_scaling_ratio_frac_bits,
13826 vcpu->arch.tsc_scaling_ratio,
13827 &delta_tsc))
13828 return -ERANGE;
13829
13830 /*
13831 * If the delta tsc can't fit in the 32 bit after the multi shift,
13832 * we can't use the preemption timer.
13833 * It's possible that it fits on later vmentries, but checking
13834 * on every vmentry is costly so we just use an hrtimer.
13835 */
13836 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13837 return -ERANGE;
13838
13839 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013840 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013841}
13842
13843static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13844{
Sean Christophersonf459a702018-08-27 15:21:11 -070013845 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013846}
13847#endif
13848
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013849static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013850{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013851 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013852 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013853}
13854
Kai Huang843e4332015-01-28 10:54:28 +080013855static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13856 struct kvm_memory_slot *slot)
13857{
13858 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13859 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13860}
13861
13862static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13863 struct kvm_memory_slot *slot)
13864{
13865 kvm_mmu_slot_set_dirty(kvm, slot);
13866}
13867
13868static void vmx_flush_log_dirty(struct kvm *kvm)
13869{
13870 kvm_flush_pml_buffers(kvm);
13871}
13872
Bandan Dasc5f983f2017-05-05 15:25:14 -040013873static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13874{
13875 struct vmcs12 *vmcs12;
13876 struct vcpu_vmx *vmx = to_vmx(vcpu);
13877 gpa_t gpa;
13878 struct page *page = NULL;
13879 u64 *pml_address;
13880
13881 if (is_guest_mode(vcpu)) {
13882 WARN_ON_ONCE(vmx->nested.pml_full);
13883
13884 /*
13885 * Check if PML is enabled for the nested guest.
13886 * Whether eptp bit 6 is set is already checked
13887 * as part of A/D emulation.
13888 */
13889 vmcs12 = get_vmcs12(vcpu);
13890 if (!nested_cpu_has_pml(vmcs12))
13891 return 0;
13892
Dan Carpenter47698862017-05-10 22:43:17 +030013893 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013894 vmx->nested.pml_full = true;
13895 return 1;
13896 }
13897
13898 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13899
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013900 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13901 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013902 return 0;
13903
13904 pml_address = kmap(page);
13905 pml_address[vmcs12->guest_pml_index--] = gpa;
13906 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013907 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013908 }
13909
13910 return 0;
13911}
13912
Kai Huang843e4332015-01-28 10:54:28 +080013913static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13914 struct kvm_memory_slot *memslot,
13915 gfn_t offset, unsigned long mask)
13916{
13917 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13918}
13919
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013920static void __pi_post_block(struct kvm_vcpu *vcpu)
13921{
13922 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13923 struct pi_desc old, new;
13924 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013925
13926 do {
13927 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013928 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13929 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013930
13931 dest = cpu_physical_id(vcpu->cpu);
13932
13933 if (x2apic_enabled())
13934 new.ndst = dest;
13935 else
13936 new.ndst = (dest << 8) & 0xFF00;
13937
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013938 /* set 'NV' to 'notification vector' */
13939 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013940 } while (cmpxchg64(&pi_desc->control, old.control,
13941 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013942
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013943 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13944 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013945 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013946 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013947 vcpu->pre_pcpu = -1;
13948 }
13949}
13950
Feng Wuefc64402015-09-18 22:29:51 +080013951/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013952 * This routine does the following things for vCPU which is going
13953 * to be blocked if VT-d PI is enabled.
13954 * - Store the vCPU to the wakeup list, so when interrupts happen
13955 * we can find the right vCPU to wake up.
13956 * - Change the Posted-interrupt descriptor as below:
13957 * 'NDST' <-- vcpu->pre_pcpu
13958 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13959 * - If 'ON' is set during this process, which means at least one
13960 * interrupt is posted for this vCPU, we cannot block it, in
13961 * this case, return 1, otherwise, return 0.
13962 *
13963 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013964static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013965{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013966 unsigned int dest;
13967 struct pi_desc old, new;
13968 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13969
13970 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013971 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13972 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013973 return 0;
13974
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013975 WARN_ON(irqs_disabled());
13976 local_irq_disable();
13977 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13978 vcpu->pre_pcpu = vcpu->cpu;
13979 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13980 list_add_tail(&vcpu->blocked_vcpu_list,
13981 &per_cpu(blocked_vcpu_on_cpu,
13982 vcpu->pre_pcpu));
13983 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13984 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013985
13986 do {
13987 old.control = new.control = pi_desc->control;
13988
Feng Wubf9f6ac2015-09-18 22:29:55 +080013989 WARN((pi_desc->sn == 1),
13990 "Warning: SN field of posted-interrupts "
13991 "is set before blocking\n");
13992
13993 /*
13994 * Since vCPU can be preempted during this process,
13995 * vcpu->cpu could be different with pre_pcpu, we
13996 * need to set pre_pcpu as the destination of wakeup
13997 * notification event, then we can find the right vCPU
13998 * to wakeup in wakeup handler if interrupts happen
13999 * when the vCPU is in blocked state.
14000 */
14001 dest = cpu_physical_id(vcpu->pre_pcpu);
14002
14003 if (x2apic_enabled())
14004 new.ndst = dest;
14005 else
14006 new.ndst = (dest << 8) & 0xFF00;
14007
14008 /* set 'NV' to 'wakeup vector' */
14009 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020014010 } while (cmpxchg64(&pi_desc->control, old.control,
14011 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014012
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014013 /* We should not block the vCPU if an interrupt is posted for it. */
14014 if (pi_test_on(pi_desc) == 1)
14015 __pi_post_block(vcpu);
14016
14017 local_irq_enable();
14018 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080014019}
14020
Yunhong Jiangbc225122016-06-13 14:19:58 -070014021static int vmx_pre_block(struct kvm_vcpu *vcpu)
14022{
14023 if (pi_pre_block(vcpu))
14024 return 1;
14025
Yunhong Jiang64672c92016-06-13 14:19:59 -070014026 if (kvm_lapic_hv_timer_in_use(vcpu))
14027 kvm_lapic_switch_to_sw_timer(vcpu);
14028
Yunhong Jiangbc225122016-06-13 14:19:58 -070014029 return 0;
14030}
14031
14032static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014033{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014034 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014035 return;
14036
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014037 WARN_ON(irqs_disabled());
14038 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014039 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014040 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014041}
14042
Yunhong Jiangbc225122016-06-13 14:19:58 -070014043static void vmx_post_block(struct kvm_vcpu *vcpu)
14044{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014045 if (kvm_x86_ops->set_hv_timer)
14046 kvm_lapic_switch_to_hv_timer(vcpu);
14047
Yunhong Jiangbc225122016-06-13 14:19:58 -070014048 pi_post_block(vcpu);
14049}
14050
Feng Wubf9f6ac2015-09-18 22:29:55 +080014051/*
Feng Wuefc64402015-09-18 22:29:51 +080014052 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14053 *
14054 * @kvm: kvm
14055 * @host_irq: host irq of the interrupt
14056 * @guest_irq: gsi of the interrupt
14057 * @set: set or unset PI
14058 * returns 0 on success, < 0 on failure
14059 */
14060static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14061 uint32_t guest_irq, bool set)
14062{
14063 struct kvm_kernel_irq_routing_entry *e;
14064 struct kvm_irq_routing_table *irq_rt;
14065 struct kvm_lapic_irq irq;
14066 struct kvm_vcpu *vcpu;
14067 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014068 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014069
14070 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014071 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14072 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014073 return 0;
14074
14075 idx = srcu_read_lock(&kvm->irq_srcu);
14076 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014077 if (guest_irq >= irq_rt->nr_rt_entries ||
14078 hlist_empty(&irq_rt->map[guest_irq])) {
14079 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14080 guest_irq, irq_rt->nr_rt_entries);
14081 goto out;
14082 }
Feng Wuefc64402015-09-18 22:29:51 +080014083
14084 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14085 if (e->type != KVM_IRQ_ROUTING_MSI)
14086 continue;
14087 /*
14088 * VT-d PI cannot support posting multicast/broadcast
14089 * interrupts to a vCPU, we still use interrupt remapping
14090 * for these kind of interrupts.
14091 *
14092 * For lowest-priority interrupts, we only support
14093 * those with single CPU as the destination, e.g. user
14094 * configures the interrupts via /proc/irq or uses
14095 * irqbalance to make the interrupts single-CPU.
14096 *
14097 * We will support full lowest-priority interrupt later.
14098 */
14099
Radim Krčmář371313132016-07-12 22:09:27 +020014100 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014101 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14102 /*
14103 * Make sure the IRTE is in remapped mode if
14104 * we don't handle it in posted mode.
14105 */
14106 ret = irq_set_vcpu_affinity(host_irq, NULL);
14107 if (ret < 0) {
14108 printk(KERN_INFO
14109 "failed to back to remapped mode, irq: %u\n",
14110 host_irq);
14111 goto out;
14112 }
14113
Feng Wuefc64402015-09-18 22:29:51 +080014114 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014115 }
Feng Wuefc64402015-09-18 22:29:51 +080014116
14117 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14118 vcpu_info.vector = irq.vector;
14119
hu huajun2698d822018-04-11 15:16:40 +080014120 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014121 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14122
14123 if (set)
14124 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014125 else
Feng Wuefc64402015-09-18 22:29:51 +080014126 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014127
14128 if (ret < 0) {
14129 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14130 __func__);
14131 goto out;
14132 }
14133 }
14134
14135 ret = 0;
14136out:
14137 srcu_read_unlock(&kvm->irq_srcu, idx);
14138 return ret;
14139}
14140
Ashok Rajc45dcc72016-06-22 14:59:56 +080014141static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14142{
14143 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14144 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14145 FEATURE_CONTROL_LMCE;
14146 else
14147 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14148 ~FEATURE_CONTROL_LMCE;
14149}
14150
Ladi Prosek72d7b372017-10-11 16:54:41 +020014151static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14152{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014153 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14154 if (to_vmx(vcpu)->nested.nested_run_pending)
14155 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014156 return 1;
14157}
14158
Ladi Prosek0234bf82017-10-11 16:54:40 +020014159static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14160{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014161 struct vcpu_vmx *vmx = to_vmx(vcpu);
14162
14163 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14164 if (vmx->nested.smm.guest_mode)
14165 nested_vmx_vmexit(vcpu, -1, 0, 0);
14166
14167 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14168 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014169 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014170 return 0;
14171}
14172
14173static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14174{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014175 struct vcpu_vmx *vmx = to_vmx(vcpu);
14176 int ret;
14177
14178 if (vmx->nested.smm.vmxon) {
14179 vmx->nested.vmxon = true;
14180 vmx->nested.smm.vmxon = false;
14181 }
14182
14183 if (vmx->nested.smm.guest_mode) {
14184 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014185 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014186 vcpu->arch.hflags |= HF_SMM_MASK;
14187 if (ret)
14188 return ret;
14189
14190 vmx->nested.smm.guest_mode = false;
14191 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014192 return 0;
14193}
14194
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014195static int enable_smi_window(struct kvm_vcpu *vcpu)
14196{
14197 return 0;
14198}
14199
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014200static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14201 struct kvm_nested_state __user *user_kvm_nested_state,
14202 u32 user_data_size)
14203{
14204 struct vcpu_vmx *vmx;
14205 struct vmcs12 *vmcs12;
14206 struct kvm_nested_state kvm_state = {
14207 .flags = 0,
14208 .format = 0,
14209 .size = sizeof(kvm_state),
14210 .vmx.vmxon_pa = -1ull,
14211 .vmx.vmcs_pa = -1ull,
14212 };
14213
14214 if (!vcpu)
14215 return kvm_state.size + 2 * VMCS12_SIZE;
14216
14217 vmx = to_vmx(vcpu);
14218 vmcs12 = get_vmcs12(vcpu);
14219 if (nested_vmx_allowed(vcpu) &&
14220 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14221 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14222 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14223
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014224 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014225 kvm_state.size += VMCS12_SIZE;
14226
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014227 if (is_guest_mode(vcpu) &&
14228 nested_cpu_has_shadow_vmcs(vmcs12) &&
14229 vmcs12->vmcs_link_pointer != -1ull)
14230 kvm_state.size += VMCS12_SIZE;
14231 }
14232
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014233 if (vmx->nested.smm.vmxon)
14234 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14235
14236 if (vmx->nested.smm.guest_mode)
14237 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14238
14239 if (is_guest_mode(vcpu)) {
14240 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14241
14242 if (vmx->nested.nested_run_pending)
14243 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14244 }
14245 }
14246
14247 if (user_data_size < kvm_state.size)
14248 goto out;
14249
14250 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14251 return -EFAULT;
14252
14253 if (vmx->nested.current_vmptr == -1ull)
14254 goto out;
14255
14256 /*
14257 * When running L2, the authoritative vmcs12 state is in the
14258 * vmcs02. When running L1, the authoritative vmcs12 state is
14259 * in the shadow vmcs linked to vmcs01, unless
14260 * sync_shadow_vmcs is set, in which case, the authoritative
14261 * vmcs12 state is in the vmcs12 already.
14262 */
14263 if (is_guest_mode(vcpu))
14264 sync_vmcs12(vcpu, vmcs12);
14265 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14266 copy_shadow_to_vmcs12(vmx);
14267
14268 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14269 return -EFAULT;
14270
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014271 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14272 vmcs12->vmcs_link_pointer != -1ull) {
14273 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14274 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14275 return -EFAULT;
14276 }
14277
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014278out:
14279 return kvm_state.size;
14280}
14281
14282static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14283 struct kvm_nested_state __user *user_kvm_nested_state,
14284 struct kvm_nested_state *kvm_state)
14285{
14286 struct vcpu_vmx *vmx = to_vmx(vcpu);
14287 struct vmcs12 *vmcs12;
14288 u32 exit_qual;
14289 int ret;
14290
14291 if (kvm_state->format != 0)
14292 return -EINVAL;
14293
14294 if (!nested_vmx_allowed(vcpu))
14295 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14296
14297 if (kvm_state->vmx.vmxon_pa == -1ull) {
14298 if (kvm_state->vmx.smm.flags)
14299 return -EINVAL;
14300
14301 if (kvm_state->vmx.vmcs_pa != -1ull)
14302 return -EINVAL;
14303
14304 vmx_leave_nested(vcpu);
14305 return 0;
14306 }
14307
14308 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14309 return -EINVAL;
14310
14311 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14312 return -EINVAL;
14313
14314 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14315 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14316 return -EINVAL;
14317
14318 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14319 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14320 return -EINVAL;
14321
14322 if (kvm_state->vmx.smm.flags &
14323 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14324 return -EINVAL;
14325
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014326 /*
14327 * SMM temporarily disables VMX, so we cannot be in guest mode,
14328 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14329 * must be zero.
14330 */
14331 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14332 return -EINVAL;
14333
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014334 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14335 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14336 return -EINVAL;
14337
14338 vmx_leave_nested(vcpu);
14339 if (kvm_state->vmx.vmxon_pa == -1ull)
14340 return 0;
14341
14342 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14343 ret = enter_vmx_operation(vcpu);
14344 if (ret)
14345 return ret;
14346
14347 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14348
14349 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14350 vmx->nested.smm.vmxon = true;
14351 vmx->nested.vmxon = false;
14352
14353 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14354 vmx->nested.smm.guest_mode = true;
14355 }
14356
14357 vmcs12 = get_vmcs12(vcpu);
14358 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14359 return -EFAULT;
14360
Liran Alon392b2f22018-06-23 02:35:01 +030014361 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014362 return -EINVAL;
14363
14364 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14365 return 0;
14366
14367 vmx->nested.nested_run_pending =
14368 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14369
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014370 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14371 vmcs12->vmcs_link_pointer != -1ull) {
14372 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14373 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14374 return -EINVAL;
14375
14376 if (copy_from_user(shadow_vmcs12,
14377 user_kvm_nested_state->data + VMCS12_SIZE,
14378 sizeof(*vmcs12)))
14379 return -EFAULT;
14380
14381 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14382 !shadow_vmcs12->hdr.shadow_vmcs)
14383 return -EINVAL;
14384 }
14385
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014386 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14387 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14388 return -EINVAL;
14389
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014390 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014391 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014392 if (ret)
14393 return -EINVAL;
14394
14395 return 0;
14396}
14397
Kees Cook404f6aa2016-08-08 16:29:06 -070014398static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014399 .cpu_has_kvm_support = cpu_has_kvm_support,
14400 .disabled_by_bios = vmx_disabled_by_bios,
14401 .hardware_setup = hardware_setup,
14402 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014403 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014404 .hardware_enable = hardware_enable,
14405 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014406 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014407 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014408
Wanpeng Lib31c1142018-03-12 04:53:04 -070014409 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014410 .vm_alloc = vmx_vm_alloc,
14411 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014412
Avi Kivity6aa8b732006-12-10 02:21:36 -080014413 .vcpu_create = vmx_create_vcpu,
14414 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014415 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014416
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014417 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014418 .vcpu_load = vmx_vcpu_load,
14419 .vcpu_put = vmx_vcpu_put,
14420
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014421 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014422 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014423 .get_msr = vmx_get_msr,
14424 .set_msr = vmx_set_msr,
14425 .get_segment_base = vmx_get_segment_base,
14426 .get_segment = vmx_get_segment,
14427 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014428 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014429 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014430 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014431 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014432 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014433 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014434 .set_cr3 = vmx_set_cr3,
14435 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014436 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014437 .get_idt = vmx_get_idt,
14438 .set_idt = vmx_set_idt,
14439 .get_gdt = vmx_get_gdt,
14440 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014441 .get_dr6 = vmx_get_dr6,
14442 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014443 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014444 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014445 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014446 .get_rflags = vmx_get_rflags,
14447 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014448
Avi Kivity6aa8b732006-12-10 02:21:36 -080014449 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014450 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014451
Avi Kivity6aa8b732006-12-10 02:21:36 -080014452 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014453 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014454 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014455 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14456 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014457 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014458 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014459 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014460 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014461 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014462 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014463 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014464 .get_nmi_mask = vmx_get_nmi_mask,
14465 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014466 .enable_nmi_window = enable_nmi_window,
14467 .enable_irq_window = enable_irq_window,
14468 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014469 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014470 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014471 .get_enable_apicv = vmx_get_enable_apicv,
14472 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014473 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014474 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014475 .hwapic_irr_update = vmx_hwapic_irr_update,
14476 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014477 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014478 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14479 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014480
Izik Eiduscbc94022007-10-25 00:29:55 +020014481 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014482 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014483 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014484 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014485
Avi Kivity586f9602010-11-18 13:09:54 +020014486 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014487
Sheng Yang17cc3932010-01-05 19:02:27 +080014488 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014489
14490 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014491
14492 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014493 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014494
14495 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014496
14497 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014498
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014499 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014500 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014501
14502 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014503
14504 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014505 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014506 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014507 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014508 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014509
14510 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014511 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014512
14513 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014514
14515 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14516 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14517 .flush_log_dirty = vmx_flush_log_dirty,
14518 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014519 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014520
Feng Wubf9f6ac2015-09-18 22:29:55 +080014521 .pre_block = vmx_pre_block,
14522 .post_block = vmx_post_block,
14523
Wei Huang25462f72015-06-19 15:45:05 +020014524 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014525
14526 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014527
14528#ifdef CONFIG_X86_64
14529 .set_hv_timer = vmx_set_hv_timer,
14530 .cancel_hv_timer = vmx_cancel_hv_timer,
14531#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014532
14533 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014534
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014535 .get_nested_state = vmx_get_nested_state,
14536 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014537 .get_vmcs12_pages = nested_get_vmcs12_pages,
14538
Ladi Prosek72d7b372017-10-11 16:54:41 +020014539 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014540 .pre_enter_smm = vmx_pre_enter_smm,
14541 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014542 .enable_smi_window = enable_smi_window,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +020014543
14544 .nested_enable_evmcs = nested_enable_evmcs,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014545};
14546
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014547static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014548{
14549 if (vmx_l1d_flush_pages) {
14550 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14551 vmx_l1d_flush_pages = NULL;
14552 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014553 /* Restore state so sysfs ignores VMX */
14554 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014555}
14556
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014557static void vmx_exit(void)
14558{
14559#ifdef CONFIG_KEXEC_CORE
14560 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14561 synchronize_rcu();
14562#endif
14563
14564 kvm_exit();
14565
14566#if IS_ENABLED(CONFIG_HYPERV)
14567 if (static_branch_unlikely(&enable_evmcs)) {
14568 int cpu;
14569 struct hv_vp_assist_page *vp_ap;
14570 /*
14571 * Reset everything to support using non-enlightened VMCS
14572 * access later (e.g. when we reload the module with
14573 * enlightened_vmcs=0)
14574 */
14575 for_each_online_cpu(cpu) {
14576 vp_ap = hv_get_vp_assist_page(cpu);
14577
14578 if (!vp_ap)
14579 continue;
14580
14581 vp_ap->current_nested_vmcs = 0;
14582 vp_ap->enlighten_vmentry = 0;
14583 }
14584
14585 static_branch_disable(&enable_evmcs);
14586 }
14587#endif
14588 vmx_cleanup_l1d_flush();
14589}
14590module_exit(vmx_exit);
14591
Avi Kivity6aa8b732006-12-10 02:21:36 -080014592static int __init vmx_init(void)
14593{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014594 int r;
14595
14596#if IS_ENABLED(CONFIG_HYPERV)
14597 /*
14598 * Enlightened VMCS usage should be recommended and the host needs
14599 * to support eVMCS v1 or above. We can also disable eVMCS support
14600 * with module parameter.
14601 */
14602 if (enlightened_vmcs &&
14603 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14604 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14605 KVM_EVMCS_VERSION) {
14606 int cpu;
14607
14608 /* Check that we have assist pages on all online CPUs */
14609 for_each_online_cpu(cpu) {
14610 if (!hv_get_vp_assist_page(cpu)) {
14611 enlightened_vmcs = false;
14612 break;
14613 }
14614 }
14615
14616 if (enlightened_vmcs) {
14617 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14618 static_branch_enable(&enable_evmcs);
14619 }
14620 } else {
14621 enlightened_vmcs = false;
14622 }
14623#endif
14624
14625 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014626 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014627 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014628 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014629
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014630 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014631 * Must be called after kvm_init() so enable_ept is properly set
14632 * up. Hand the parameter mitigation value in which was stored in
14633 * the pre module init parser. If no parameter was given, it will
14634 * contain 'auto' which will be turned into the default 'cond'
14635 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014636 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014637 if (boot_cpu_has(X86_BUG_L1TF)) {
14638 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14639 if (r) {
14640 vmx_exit();
14641 return r;
14642 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014643 }
14644
Dave Young2965faa2015-09-09 15:38:55 -070014645#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014646 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14647 crash_vmclear_local_loaded_vmcss);
14648#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014649 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014650
He, Qingfdef3ad2007-04-30 09:45:24 +030014651 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014652}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014653module_init(vmx_init);