blob: 509d4e34dd62a852bce31a605b2a9010aa5e1e7c [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 Kiszkaf41245002014-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 Kiszkaf41245002014-03-07 20:03:13 +0100152#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
153
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800154/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300155 * Hyper-V requires all of these, so mark them as supported even though
156 * they are just treated the same as all-context.
157 */
158#define VMX_VPID_EXTENT_SUPPORTED_MASK \
159 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
160 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
161 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
162 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
163
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800164/*
165 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
166 * ple_gap: upper bound on the amount of time between two successive
167 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500168 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800169 * ple_window: upper bound on the amount of time a guest is allowed to execute
170 * in a PAUSE loop. Tests indicate that most spinlocks are held for
171 * less than 2^12 cycles
172 * Time is measured based on a counter that runs at the same rate as the TSC,
173 * refer SDM volume 3b section 21.6.13 & 22.1.3.
174 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400175static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
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 Christopherson9d6105b2018-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
Nadav Har'El644d7112011-05-25 23:12:35 +0300849 /* L2 must run next, and mustn't decide to exit to L1. */
850 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600851
852 struct loaded_vmcs vmcs02;
853
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300854 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600855 * Guest pages referred to in the vmcs02 with host-physical
856 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300857 */
858 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800859 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800860 struct page *pi_desc_page;
861 struct pi_desc *pi_desc;
862 bool pi_pending;
863 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100864
865 struct hrtimer preemption_timer;
866 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200867
868 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
869 u64 vmcs01_debugctl;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300870 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800871
Wanpeng Li5c614b32015-10-13 09:18:36 -0700872 u16 vpid02;
873 u16 last_vpid;
874
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100875 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200876
877 /* SMM related state */
878 struct {
879 /* in VMX operation on SMM entry? */
880 bool vmxon;
881 /* in guest mode on SMM entry? */
882 bool guest_mode;
883 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300884};
885
Yang Zhang01e439b2013-04-11 19:25:12 +0800886#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800887#define POSTED_INTR_SN 1
888
Yang Zhang01e439b2013-04-11 19:25:12 +0800889/* Posted-Interrupt Descriptor */
890struct pi_desc {
891 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800892 union {
893 struct {
894 /* bit 256 - Outstanding Notification */
895 u16 on : 1,
896 /* bit 257 - Suppress Notification */
897 sn : 1,
898 /* bit 271:258 - Reserved */
899 rsvd_1 : 14;
900 /* bit 279:272 - Notification Vector */
901 u8 nv;
902 /* bit 287:280 - Reserved */
903 u8 rsvd_2;
904 /* bit 319:288 - Notification Destination */
905 u32 ndst;
906 };
907 u64 control;
908 };
909 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800910} __aligned(64);
911
Yang Zhanga20ed542013-04-11 19:25:15 +0800912static bool pi_test_and_set_on(struct pi_desc *pi_desc)
913{
914 return test_and_set_bit(POSTED_INTR_ON,
915 (unsigned long *)&pi_desc->control);
916}
917
918static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
919{
920 return test_and_clear_bit(POSTED_INTR_ON,
921 (unsigned long *)&pi_desc->control);
922}
923
924static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
925{
926 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
927}
928
Feng Wuebbfc762015-09-18 22:29:46 +0800929static inline void pi_clear_sn(struct pi_desc *pi_desc)
930{
931 return clear_bit(POSTED_INTR_SN,
932 (unsigned long *)&pi_desc->control);
933}
934
935static inline void pi_set_sn(struct pi_desc *pi_desc)
936{
937 return set_bit(POSTED_INTR_SN,
938 (unsigned long *)&pi_desc->control);
939}
940
Paolo Bonziniad361092016-09-20 16:15:05 +0200941static inline void pi_clear_on(struct pi_desc *pi_desc)
942{
943 clear_bit(POSTED_INTR_ON,
944 (unsigned long *)&pi_desc->control);
945}
946
Feng Wuebbfc762015-09-18 22:29:46 +0800947static inline int pi_test_on(struct pi_desc *pi_desc)
948{
949 return test_bit(POSTED_INTR_ON,
950 (unsigned long *)&pi_desc->control);
951}
952
953static inline int pi_test_sn(struct pi_desc *pi_desc)
954{
955 return test_bit(POSTED_INTR_SN,
956 (unsigned long *)&pi_desc->control);
957}
958
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400959struct vmx_msrs {
960 unsigned int nr;
961 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
962};
963
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400964struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000965 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300966 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300967 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100968 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300969 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200970 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200971 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300972 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 int nmsrs;
974 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800975 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400976#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300977 u64 msr_host_kernel_gs_base;
978 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100980
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100981 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100982 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100983
Gleb Natapov2961e8762013-11-25 15:37:13 +0200984 u32 vm_entry_controls_shadow;
985 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200986 u32 secondary_exec_control;
987
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 /*
989 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
990 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700991 * guest (L2), it points to a different VMCS. loaded_cpu_state points
992 * to the VMCS whose state is loaded into the CPU registers that only
993 * need to be switched when transitioning to/from the kernel; a NULL
994 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300995 */
996 struct loaded_vmcs vmcs01;
997 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700998 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300999 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +03001000 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04001001 struct vmx_msrs guest;
1002 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +03001003 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07001004
Avi Kivity9c8cba32007-11-22 11:42:59 +02001005 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001006 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001007 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001008 struct kvm_segment segs[8];
1009 } rmode;
1010 struct {
1011 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001012 struct kvm_save_segment {
1013 u16 selector;
1014 unsigned long base;
1015 u32 limit;
1016 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001017 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001018 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001019 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001020 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001021
Andi Kleena0861c02009-06-08 17:37:09 +08001022 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001023
Yang Zhang01e439b2013-04-11 19:25:12 +08001024 /* Posted interrupt descriptor */
1025 struct pi_desc pi_desc;
1026
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001027 /* Support for a guest hypervisor (nested VMX) */
1028 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001029
1030 /* Dynamic PLE window. */
1031 int ple_window;
1032 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001033
Sean Christophersond264ee02018-08-27 15:21:12 -07001034 bool req_immediate_exit;
1035
Kai Huang843e4332015-01-28 10:54:28 +08001036 /* Support for PML */
1037#define PML_ENTITY_NUM 512
1038 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001039
Yunhong Jiang64672c92016-06-13 14:19:59 -07001040 /* apic deadline value in host tsc */
1041 u64 hv_deadline_tsc;
1042
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001043 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001044
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001045 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001046
Wanpeng Li74c55932017-11-29 01:31:20 -08001047 unsigned long host_debugctlmsr;
1048
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001049 /*
1050 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1051 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1052 * in msr_ia32_feature_control_valid_bits.
1053 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001054 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001055 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001056 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001057};
1058
Avi Kivity2fb92db2011-04-27 19:42:18 +03001059enum segment_cache_field {
1060 SEG_FIELD_SEL = 0,
1061 SEG_FIELD_BASE = 1,
1062 SEG_FIELD_LIMIT = 2,
1063 SEG_FIELD_AR = 3,
1064
1065 SEG_FIELD_NR = 4
1066};
1067
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001068static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1069{
1070 return container_of(kvm, struct kvm_vmx, kvm);
1071}
1072
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001073static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1074{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001075 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001076}
1077
Feng Wuefc64402015-09-18 22:29:51 +08001078static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1079{
1080 return &(to_vmx(vcpu)->pi_desc);
1081}
1082
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001083#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001084#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001085#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1086#define FIELD64(number, name) \
1087 FIELD(number, name), \
1088 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001089
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001091static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001092#define SHADOW_FIELD_RO(x) x,
1093#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001094};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001095static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001096 ARRAY_SIZE(shadow_read_only_fields);
1097
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001098static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001099#define SHADOW_FIELD_RW(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_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001103 ARRAY_SIZE(shadow_read_write_fields);
1104
Mathias Krause772e0312012-08-30 01:30:19 +02001105static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001106 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001107 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001108 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1109 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1110 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1111 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1112 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1113 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1114 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1115 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001116 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001117 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001118 FIELD(HOST_ES_SELECTOR, host_es_selector),
1119 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1120 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1121 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1122 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1123 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1124 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1125 FIELD64(IO_BITMAP_A, io_bitmap_a),
1126 FIELD64(IO_BITMAP_B, io_bitmap_b),
1127 FIELD64(MSR_BITMAP, msr_bitmap),
1128 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1129 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1130 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001131 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001132 FIELD64(TSC_OFFSET, tsc_offset),
1133 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1134 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001135 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001136 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001137 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001138 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1139 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1140 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1141 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001142 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001143 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1144 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001145 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1147 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1148 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1149 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1150 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1151 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1152 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1153 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1154 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1155 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001156 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001157 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1158 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1159 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1160 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1161 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1162 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1163 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1164 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1165 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1166 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1167 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1168 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1169 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1170 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1171 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1172 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1173 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1174 FIELD(TPR_THRESHOLD, tpr_threshold),
1175 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1176 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1177 FIELD(VM_EXIT_REASON, vm_exit_reason),
1178 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1179 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1180 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1181 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1182 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1183 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1184 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1185 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1186 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1187 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1188 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1189 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1190 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1191 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1192 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1193 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1194 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1195 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1196 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1197 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1198 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1199 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1200 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1201 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1202 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1203 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1204 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1205 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001206 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001207 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1208 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1209 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1210 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1211 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1212 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1213 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1214 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1215 FIELD(EXIT_QUALIFICATION, exit_qualification),
1216 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1217 FIELD(GUEST_CR0, guest_cr0),
1218 FIELD(GUEST_CR3, guest_cr3),
1219 FIELD(GUEST_CR4, guest_cr4),
1220 FIELD(GUEST_ES_BASE, guest_es_base),
1221 FIELD(GUEST_CS_BASE, guest_cs_base),
1222 FIELD(GUEST_SS_BASE, guest_ss_base),
1223 FIELD(GUEST_DS_BASE, guest_ds_base),
1224 FIELD(GUEST_FS_BASE, guest_fs_base),
1225 FIELD(GUEST_GS_BASE, guest_gs_base),
1226 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1227 FIELD(GUEST_TR_BASE, guest_tr_base),
1228 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1229 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1230 FIELD(GUEST_DR7, guest_dr7),
1231 FIELD(GUEST_RSP, guest_rsp),
1232 FIELD(GUEST_RIP, guest_rip),
1233 FIELD(GUEST_RFLAGS, guest_rflags),
1234 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1235 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1236 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1237 FIELD(HOST_CR0, host_cr0),
1238 FIELD(HOST_CR3, host_cr3),
1239 FIELD(HOST_CR4, host_cr4),
1240 FIELD(HOST_FS_BASE, host_fs_base),
1241 FIELD(HOST_GS_BASE, host_gs_base),
1242 FIELD(HOST_TR_BASE, host_tr_base),
1243 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1244 FIELD(HOST_IDTR_BASE, host_idtr_base),
1245 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1246 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1247 FIELD(HOST_RSP, host_rsp),
1248 FIELD(HOST_RIP, host_rip),
1249};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001250
1251static inline short vmcs_field_to_offset(unsigned long field)
1252{
Dan Williams085331d2018-01-31 17:47:03 -08001253 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1254 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001255 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001257 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001258 return -ENOENT;
1259
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001260 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001261 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001262 return -ENOENT;
1263
Linus Torvalds15303ba2018-02-10 13:16:35 -08001264 index = array_index_nospec(index, size);
1265 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001266 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001267 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001268 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001269}
1270
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001271static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1272{
David Matlack4f2777b2016-07-13 17:16:37 -07001273 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001274}
1275
Liran Alon61ada742018-06-23 02:35:08 +03001276static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1277{
1278 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1279}
1280
Peter Feiner995f00a2017-06-30 17:26:32 -07001281static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001282static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001283static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001284static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001285static void vmx_set_segment(struct kvm_vcpu *vcpu,
1286 struct kvm_segment *var, int seg);
1287static void vmx_get_segment(struct kvm_vcpu *vcpu,
1288 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001289static bool guest_state_valid(struct kvm_vcpu *vcpu);
1290static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001291static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001292static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1293static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1294static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1295 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001296static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001297static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1298 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001299
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1301static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001302/*
1303 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1304 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1305 */
1306static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307
Feng Wubf9f6ac2015-09-18 22:29:55 +08001308/*
1309 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1310 * can find which vCPU should be waken up.
1311 */
1312static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1313static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1314
Radim Krčmář23611332016-09-29 22:41:33 +02001315enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001316 VMX_VMREAD_BITMAP,
1317 VMX_VMWRITE_BITMAP,
1318 VMX_BITMAP_NR
1319};
1320
1321static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1322
Radim Krčmář23611332016-09-29 22:41:33 +02001323#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1324#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001325
Avi Kivity110312c2010-12-21 12:54:20 +02001326static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001327static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001328
Sheng Yang2384d2b2008-01-17 15:14:33 +08001329static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1330static DEFINE_SPINLOCK(vmx_vpid_lock);
1331
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 int size;
1334 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001335 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001337 u32 pin_based_exec_ctrl;
1338 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001339 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001340 u32 vmexit_ctrl;
1341 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001342 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001343} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344
Hannes Ederefff9e52008-11-28 17:02:06 +01001345static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001346 u32 ept;
1347 u32 vpid;
1348} vmx_capability;
1349
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350#define VMX_SEGMENT_FIELD(seg) \
1351 [VCPU_SREG_##seg] = { \
1352 .selector = GUEST_##seg##_SELECTOR, \
1353 .base = GUEST_##seg##_BASE, \
1354 .limit = GUEST_##seg##_LIMIT, \
1355 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1356 }
1357
Mathias Krause772e0312012-08-30 01:30:19 +02001358static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 unsigned selector;
1360 unsigned base;
1361 unsigned limit;
1362 unsigned ar_bytes;
1363} kvm_vmx_segment_fields[] = {
1364 VMX_SEGMENT_FIELD(CS),
1365 VMX_SEGMENT_FIELD(DS),
1366 VMX_SEGMENT_FIELD(ES),
1367 VMX_SEGMENT_FIELD(FS),
1368 VMX_SEGMENT_FIELD(GS),
1369 VMX_SEGMENT_FIELD(SS),
1370 VMX_SEGMENT_FIELD(TR),
1371 VMX_SEGMENT_FIELD(LDTR),
1372};
1373
Avi Kivity26bb0982009-09-07 11:14:12 +03001374static u64 host_efer;
1375
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001376static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1377
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001378/*
Brian Gerst8c065852010-07-17 09:03:26 -04001379 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001380 * away by decrementing the array size.
1381 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001383#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001384 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001386 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001389DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1390
1391#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1392
1393#define KVM_EVMCS_VERSION 1
1394
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001395/*
1396 * Enlightened VMCSv1 doesn't support these:
1397 *
1398 * POSTED_INTR_NV = 0x00000002,
1399 * GUEST_INTR_STATUS = 0x00000810,
1400 * APIC_ACCESS_ADDR = 0x00002014,
1401 * POSTED_INTR_DESC_ADDR = 0x00002016,
1402 * EOI_EXIT_BITMAP0 = 0x0000201c,
1403 * EOI_EXIT_BITMAP1 = 0x0000201e,
1404 * EOI_EXIT_BITMAP2 = 0x00002020,
1405 * EOI_EXIT_BITMAP3 = 0x00002022,
1406 * GUEST_PML_INDEX = 0x00000812,
1407 * PML_ADDRESS = 0x0000200e,
1408 * VM_FUNCTION_CONTROL = 0x00002018,
1409 * EPTP_LIST_ADDRESS = 0x00002024,
1410 * VMREAD_BITMAP = 0x00002026,
1411 * VMWRITE_BITMAP = 0x00002028,
1412 *
1413 * TSC_MULTIPLIER = 0x00002032,
1414 * PLE_GAP = 0x00004020,
1415 * PLE_WINDOW = 0x00004022,
1416 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1417 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1418 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1419 *
1420 * Currently unsupported in KVM:
1421 * GUEST_IA32_RTIT_CTL = 0x00002814,
1422 */
1423#define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \
1424 PIN_BASED_VMX_PREEMPTION_TIMER)
1425#define EVMCS1_UNSUPPORTED_2NDEXEC \
1426 (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \
1427 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \
1428 SECONDARY_EXEC_APIC_REGISTER_VIRT | \
1429 SECONDARY_EXEC_ENABLE_PML | \
1430 SECONDARY_EXEC_ENABLE_VMFUNC | \
1431 SECONDARY_EXEC_SHADOW_VMCS | \
1432 SECONDARY_EXEC_TSC_SCALING | \
1433 SECONDARY_EXEC_PAUSE_LOOP_EXITING)
1434#define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
1435#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
1436#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)
1437
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001438#if IS_ENABLED(CONFIG_HYPERV)
1439static bool __read_mostly enlightened_vmcs = true;
1440module_param(enlightened_vmcs, bool, 0444);
1441
1442static inline void evmcs_write64(unsigned long field, u64 value)
1443{
1444 u16 clean_field;
1445 int offset = get_evmcs_offset(field, &clean_field);
1446
1447 if (offset < 0)
1448 return;
1449
1450 *(u64 *)((char *)current_evmcs + offset) = value;
1451
1452 current_evmcs->hv_clean_fields &= ~clean_field;
1453}
1454
1455static inline void evmcs_write32(unsigned long field, u32 value)
1456{
1457 u16 clean_field;
1458 int offset = get_evmcs_offset(field, &clean_field);
1459
1460 if (offset < 0)
1461 return;
1462
1463 *(u32 *)((char *)current_evmcs + offset) = value;
1464 current_evmcs->hv_clean_fields &= ~clean_field;
1465}
1466
1467static inline void evmcs_write16(unsigned long field, u16 value)
1468{
1469 u16 clean_field;
1470 int offset = get_evmcs_offset(field, &clean_field);
1471
1472 if (offset < 0)
1473 return;
1474
1475 *(u16 *)((char *)current_evmcs + offset) = value;
1476 current_evmcs->hv_clean_fields &= ~clean_field;
1477}
1478
1479static inline u64 evmcs_read64(unsigned long field)
1480{
1481 int offset = get_evmcs_offset(field, NULL);
1482
1483 if (offset < 0)
1484 return 0;
1485
1486 return *(u64 *)((char *)current_evmcs + offset);
1487}
1488
1489static inline u32 evmcs_read32(unsigned long field)
1490{
1491 int offset = get_evmcs_offset(field, NULL);
1492
1493 if (offset < 0)
1494 return 0;
1495
1496 return *(u32 *)((char *)current_evmcs + offset);
1497}
1498
1499static inline u16 evmcs_read16(unsigned long field)
1500{
1501 int offset = get_evmcs_offset(field, NULL);
1502
1503 if (offset < 0)
1504 return 0;
1505
1506 return *(u16 *)((char *)current_evmcs + offset);
1507}
1508
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001509static inline void evmcs_touch_msr_bitmap(void)
1510{
1511 if (unlikely(!current_evmcs))
1512 return;
1513
1514 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1515 current_evmcs->hv_clean_fields &=
1516 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1517}
1518
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001519static void evmcs_load(u64 phys_addr)
1520{
1521 struct hv_vp_assist_page *vp_ap =
1522 hv_get_vp_assist_page(smp_processor_id());
1523
1524 vp_ap->current_nested_vmcs = phys_addr;
1525 vp_ap->enlighten_vmentry = 1;
1526}
1527
1528static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1529{
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001530 vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
1531 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001532
Vitaly Kuznetsov5d7a6442018-10-16 18:50:00 +02001533 vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL;
1534 vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001535
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001536}
Tianyu Lan877ad952018-07-19 08:40:23 +00001537
1538/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1539static void check_ept_pointer_match(struct kvm *kvm)
1540{
1541 struct kvm_vcpu *vcpu;
1542 u64 tmp_eptp = INVALID_PAGE;
1543 int i;
1544
1545 kvm_for_each_vcpu(i, vcpu, kvm) {
1546 if (!VALID_PAGE(tmp_eptp)) {
1547 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1548 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1549 to_kvm_vmx(kvm)->ept_pointers_match
1550 = EPT_POINTERS_MISMATCH;
1551 return;
1552 }
1553 }
1554
1555 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1556}
1557
1558static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1559{
Lan Tianyua5c214d2018-10-13 22:54:05 +08001560 struct kvm_vcpu *vcpu;
1561 int ret = -ENOTSUPP, i;
Tianyu Lan877ad952018-07-19 08:40:23 +00001562
1563 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1564
1565 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1566 check_ept_pointer_match(kvm);
1567
1568 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
Lan Tianyua5c214d2018-10-13 22:54:05 +08001569 kvm_for_each_vcpu(i, vcpu, kvm)
1570 ret |= hyperv_flush_guest_mapping(
1571 to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer);
1572 } else {
1573 ret = hyperv_flush_guest_mapping(
1574 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
Tianyu Lan877ad952018-07-19 08:40:23 +00001575 }
1576
Tianyu Lan877ad952018-07-19 08:40:23 +00001577 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1578 return ret;
1579}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001580#else /* !IS_ENABLED(CONFIG_HYPERV) */
1581static inline void evmcs_write64(unsigned long field, u64 value) {}
1582static inline void evmcs_write32(unsigned long field, u32 value) {}
1583static inline void evmcs_write16(unsigned long field, u16 value) {}
1584static inline u64 evmcs_read64(unsigned long field) { return 0; }
1585static inline u32 evmcs_read32(unsigned long field) { return 0; }
1586static inline u16 evmcs_read16(unsigned long field) { return 0; }
1587static inline void evmcs_load(u64 phys_addr) {}
1588static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001589static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001590#endif /* IS_ENABLED(CONFIG_HYPERV) */
1591
Jan Kiszka5bb16012016-02-09 20:14:21 +01001592static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593{
1594 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1595 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001596 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1597}
1598
Jan Kiszka6f054852016-02-09 20:15:18 +01001599static inline bool is_debug(u32 intr_info)
1600{
1601 return is_exception_n(intr_info, DB_VECTOR);
1602}
1603
1604static inline bool is_breakpoint(u32 intr_info)
1605{
1606 return is_exception_n(intr_info, BP_VECTOR);
1607}
1608
Jan Kiszka5bb16012016-02-09 20:14:21 +01001609static inline bool is_page_fault(u32 intr_info)
1610{
1611 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612}
1613
Gui Jianfeng31299942010-03-15 17:29:09 +08001614static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001615{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001616 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001617}
1618
Liran Alon9e869482018-03-12 13:12:51 +02001619static inline bool is_gp_fault(u32 intr_info)
1620{
1621 return is_exception_n(intr_info, GP_VECTOR);
1622}
1623
Gui Jianfeng31299942010-03-15 17:29:09 +08001624static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001625{
1626 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1627 INTR_INFO_VALID_MASK)) ==
1628 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1629}
1630
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001631/* Undocumented: icebp/int1 */
1632static inline bool is_icebp(u32 intr_info)
1633{
1634 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1635 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1636}
1637
Gui Jianfeng31299942010-03-15 17:29:09 +08001638static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001639{
Sheng Yang04547152009-04-01 15:52:31 +08001640 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001641}
1642
Gui Jianfeng31299942010-03-15 17:29:09 +08001643static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001644{
Sheng Yang04547152009-04-01 15:52:31 +08001645 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001646}
1647
Paolo Bonzini35754c92015-07-29 12:05:37 +02001648static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001649{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001650 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001651}
1652
Gui Jianfeng31299942010-03-15 17:29:09 +08001653static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001654{
Sheng Yang04547152009-04-01 15:52:31 +08001655 return vmcs_config.cpu_based_exec_ctrl &
1656 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001657}
1658
Avi Kivity774ead32007-12-26 13:57:04 +02001659static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001660{
Sheng Yang04547152009-04-01 15:52:31 +08001661 return vmcs_config.cpu_based_2nd_exec_ctrl &
1662 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1663}
1664
Yang Zhang8d146952013-01-25 10:18:50 +08001665static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1666{
1667 return vmcs_config.cpu_based_2nd_exec_ctrl &
1668 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1669}
1670
Yang Zhang83d4c282013-01-25 10:18:49 +08001671static inline bool cpu_has_vmx_apic_register_virt(void)
1672{
1673 return vmcs_config.cpu_based_2nd_exec_ctrl &
1674 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1675}
1676
Yang Zhangc7c9c562013-01-25 10:18:51 +08001677static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1678{
1679 return vmcs_config.cpu_based_2nd_exec_ctrl &
1680 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1681}
1682
Sean Christopherson0b665d32018-08-14 09:33:34 -07001683static inline bool cpu_has_vmx_encls_vmexit(void)
1684{
1685 return vmcs_config.cpu_based_2nd_exec_ctrl &
1686 SECONDARY_EXEC_ENCLS_EXITING;
1687}
1688
Yunhong Jiang64672c92016-06-13 14:19:59 -07001689/*
1690 * Comment's format: document - errata name - stepping - processor name.
1691 * Refer from
1692 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1693 */
1694static u32 vmx_preemption_cpu_tfms[] = {
1695/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16960x000206E6,
1697/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1698/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1699/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17000x00020652,
1701/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17020x00020655,
1703/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1704/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1705/*
1706 * 320767.pdf - AAP86 - B1 -
1707 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1708 */
17090x000106E5,
1710/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17110x000106A0,
1712/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17130x000106A1,
1714/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17150x000106A4,
1716 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1717 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1718 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17190x000106A5,
1720};
1721
1722static inline bool cpu_has_broken_vmx_preemption_timer(void)
1723{
1724 u32 eax = cpuid_eax(0x00000001), i;
1725
1726 /* Clear the reserved bits */
1727 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001728 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001729 if (eax == vmx_preemption_cpu_tfms[i])
1730 return true;
1731
1732 return false;
1733}
1734
1735static inline bool cpu_has_vmx_preemption_timer(void)
1736{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001737 return vmcs_config.pin_based_exec_ctrl &
1738 PIN_BASED_VMX_PREEMPTION_TIMER;
1739}
1740
Yang Zhang01e439b2013-04-11 19:25:12 +08001741static inline bool cpu_has_vmx_posted_intr(void)
1742{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001743 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1744 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001745}
1746
1747static inline bool cpu_has_vmx_apicv(void)
1748{
1749 return cpu_has_vmx_apic_register_virt() &&
1750 cpu_has_vmx_virtual_intr_delivery() &&
1751 cpu_has_vmx_posted_intr();
1752}
1753
Sheng Yang04547152009-04-01 15:52:31 +08001754static inline bool cpu_has_vmx_flexpriority(void)
1755{
1756 return cpu_has_vmx_tpr_shadow() &&
1757 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001758}
1759
Marcelo Tosattie7997942009-06-11 12:07:40 -03001760static inline bool cpu_has_vmx_ept_execute_only(void)
1761{
Gui Jianfeng31299942010-03-15 17:29:09 +08001762 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001763}
1764
Marcelo Tosattie7997942009-06-11 12:07:40 -03001765static inline bool cpu_has_vmx_ept_2m_page(void)
1766{
Gui Jianfeng31299942010-03-15 17:29:09 +08001767 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001768}
1769
Sheng Yang878403b2010-01-05 19:02:29 +08001770static inline bool cpu_has_vmx_ept_1g_page(void)
1771{
Gui Jianfeng31299942010-03-15 17:29:09 +08001772 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001773}
1774
Sheng Yang4bc9b982010-06-02 14:05:24 +08001775static inline bool cpu_has_vmx_ept_4levels(void)
1776{
1777 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1778}
1779
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001780static inline bool cpu_has_vmx_ept_mt_wb(void)
1781{
1782 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1783}
1784
Yu Zhang855feb62017-08-24 20:27:55 +08001785static inline bool cpu_has_vmx_ept_5levels(void)
1786{
1787 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1788}
1789
Xudong Hao83c3a332012-05-28 19:33:35 +08001790static inline bool cpu_has_vmx_ept_ad_bits(void)
1791{
1792 return vmx_capability.ept & VMX_EPT_AD_BIT;
1793}
1794
Gui Jianfeng31299942010-03-15 17:29:09 +08001795static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001796{
Gui Jianfeng31299942010-03-15 17:29:09 +08001797 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001798}
1799
Gui Jianfeng31299942010-03-15 17:29:09 +08001800static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001801{
Gui Jianfeng31299942010-03-15 17:29:09 +08001802 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001803}
1804
Liran Aloncd9a4912018-05-22 17:16:15 +03001805static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1806{
1807 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1808}
1809
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001810static inline bool cpu_has_vmx_invvpid_single(void)
1811{
1812 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1813}
1814
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001815static inline bool cpu_has_vmx_invvpid_global(void)
1816{
1817 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1818}
1819
Wanpeng Li08d839c2017-03-23 05:30:08 -07001820static inline bool cpu_has_vmx_invvpid(void)
1821{
1822 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1823}
1824
Gui Jianfeng31299942010-03-15 17:29:09 +08001825static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001826{
Sheng Yang04547152009-04-01 15:52:31 +08001827 return vmcs_config.cpu_based_2nd_exec_ctrl &
1828 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001829}
1830
Gui Jianfeng31299942010-03-15 17:29:09 +08001831static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001832{
1833 return vmcs_config.cpu_based_2nd_exec_ctrl &
1834 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1835}
1836
Gui Jianfeng31299942010-03-15 17:29:09 +08001837static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001838{
1839 return vmcs_config.cpu_based_2nd_exec_ctrl &
1840 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1841}
1842
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001843static inline bool cpu_has_vmx_basic_inout(void)
1844{
1845 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1846}
1847
Paolo Bonzini35754c92015-07-29 12:05:37 +02001848static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001849{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001850 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001851}
1852
Gui Jianfeng31299942010-03-15 17:29:09 +08001853static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001854{
Sheng Yang04547152009-04-01 15:52:31 +08001855 return vmcs_config.cpu_based_2nd_exec_ctrl &
1856 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001857}
1858
Gui Jianfeng31299942010-03-15 17:29:09 +08001859static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001860{
1861 return vmcs_config.cpu_based_2nd_exec_ctrl &
1862 SECONDARY_EXEC_RDTSCP;
1863}
1864
Mao, Junjiead756a12012-07-02 01:18:48 +00001865static inline bool cpu_has_vmx_invpcid(void)
1866{
1867 return vmcs_config.cpu_based_2nd_exec_ctrl &
1868 SECONDARY_EXEC_ENABLE_INVPCID;
1869}
1870
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001871static inline bool cpu_has_virtual_nmis(void)
1872{
1873 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1874}
1875
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001876static inline bool cpu_has_vmx_wbinvd_exit(void)
1877{
1878 return vmcs_config.cpu_based_2nd_exec_ctrl &
1879 SECONDARY_EXEC_WBINVD_EXITING;
1880}
1881
Abel Gordonabc4fc52013-04-18 14:35:25 +03001882static inline bool cpu_has_vmx_shadow_vmcs(void)
1883{
1884 u64 vmx_msr;
1885 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1886 /* check if the cpu supports writing r/o exit information fields */
1887 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1888 return false;
1889
1890 return vmcs_config.cpu_based_2nd_exec_ctrl &
1891 SECONDARY_EXEC_SHADOW_VMCS;
1892}
1893
Kai Huang843e4332015-01-28 10:54:28 +08001894static inline bool cpu_has_vmx_pml(void)
1895{
1896 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1897}
1898
Haozhong Zhang64903d62015-10-20 15:39:09 +08001899static inline bool cpu_has_vmx_tsc_scaling(void)
1900{
1901 return vmcs_config.cpu_based_2nd_exec_ctrl &
1902 SECONDARY_EXEC_TSC_SCALING;
1903}
1904
Bandan Das2a499e42017-08-03 15:54:41 -04001905static inline bool cpu_has_vmx_vmfunc(void)
1906{
1907 return vmcs_config.cpu_based_2nd_exec_ctrl &
1908 SECONDARY_EXEC_ENABLE_VMFUNC;
1909}
1910
Sean Christopherson64f7a112018-04-30 10:01:06 -07001911static bool vmx_umip_emulated(void)
1912{
1913 return vmcs_config.cpu_based_2nd_exec_ctrl &
1914 SECONDARY_EXEC_DESC;
1915}
1916
Sheng Yang04547152009-04-01 15:52:31 +08001917static inline bool report_flexpriority(void)
1918{
1919 return flexpriority_enabled;
1920}
1921
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001922static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1923{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001924 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001925}
1926
Jim Mattsonf4160e42018-05-29 09:11:33 -07001927/*
1928 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1929 * to modify any valid field of the VMCS, or are the VM-exit
1930 * information fields read-only?
1931 */
1932static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1933{
1934 return to_vmx(vcpu)->nested.msrs.misc_low &
1935 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1936}
1937
Marc Orr04473782018-06-20 17:21:29 -07001938static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1939{
1940 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1941}
1942
1943static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1944{
1945 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1946 CPU_BASED_MONITOR_TRAP_FLAG;
1947}
1948
Liran Alonfa97d7d2018-07-18 14:07:59 +02001949static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1950{
1951 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1952 SECONDARY_EXEC_SHADOW_VMCS;
1953}
1954
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001955static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1956{
1957 return vmcs12->cpu_based_vm_exec_control & bit;
1958}
1959
1960static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1961{
1962 return (vmcs12->cpu_based_vm_exec_control &
1963 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1964 (vmcs12->secondary_vm_exec_control & bit);
1965}
1966
Jan Kiszkaf41245002014-03-07 20:03:13 +01001967static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1968{
1969 return vmcs12->pin_based_vm_exec_control &
1970 PIN_BASED_VMX_PREEMPTION_TIMER;
1971}
1972
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001973static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1974{
1975 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1976}
1977
1978static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1979{
1980 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1981}
1982
Nadav Har'El155a97a2013-08-05 11:07:16 +03001983static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1984{
1985 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1986}
1987
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001988static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1989{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001990 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001991}
1992
Bandan Dasc5f983f2017-05-05 15:25:14 -04001993static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1994{
1995 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1996}
1997
Wincy Vanf2b93282015-02-03 23:56:03 +08001998static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1999{
2000 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2001}
2002
Wanpeng Li5c614b32015-10-13 09:18:36 -07002003static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2004{
2005 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2006}
2007
Wincy Van82f0dd42015-02-03 23:57:18 +08002008static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2009{
2010 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2011}
2012
Wincy Van608406e2015-02-03 23:57:51 +08002013static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2014{
2015 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2016}
2017
Wincy Van705699a2015-02-03 23:58:17 +08002018static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2019{
2020 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2021}
2022
Bandan Das27c42a12017-08-03 15:54:42 -04002023static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2024{
2025 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2026}
2027
Bandan Das41ab9372017-08-03 15:54:43 -04002028static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2029{
2030 return nested_cpu_has_vmfunc(vmcs12) &&
2031 (vmcs12->vm_function_control &
2032 VMX_VMFUNC_EPTP_SWITCHING);
2033}
2034
Liran Alonf792d272018-06-23 02:35:05 +03002035static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2036{
2037 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2038}
2039
Jim Mattsonef85b672016-12-12 11:01:37 -08002040static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002041{
2042 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002043 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002044}
2045
Jan Kiszka533558b2014-01-04 18:47:20 +01002046static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2047 u32 exit_intr_info,
2048 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002049
Rusty Russell8b9cf982007-07-30 16:31:43 +10002050static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002051{
2052 int i;
2053
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002054 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002055 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002056 return i;
2057 return -1;
2058}
2059
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002060static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002061{
2062 struct {
2063 u64 vpid : 16;
2064 u64 rsvd : 48;
2065 u64 gva;
2066 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002067 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002068
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002069 asm volatile (__ex("invvpid %2, %1") CC_SET(na)
2070 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002071 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002072}
2073
Uros Bizjak5ebb2722018-10-11 19:40:43 +02002074static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa)
Sheng Yang14394422008-04-28 12:24:45 +08002075{
2076 struct {
2077 u64 eptp, gpa;
2078 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002079 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002080
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002081 asm volatile (__ex("invept %2, %1") CC_SET(na)
2082 : CC_OUT(na) (error) : "r"(ext), "m"(operand));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002083 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002084}
2085
Avi Kivity26bb0982009-09-07 11:14:12 +03002086static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002087{
2088 int i;
2089
Rusty Russell8b9cf982007-07-30 16:31:43 +10002090 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002091 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002092 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002093 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002094}
2095
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096static void vmcs_clear(struct vmcs *vmcs)
2097{
2098 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002099 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002101 asm volatile (__ex("vmclear %1") CC_SET(na)
2102 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002103 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002104 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2105 vmcs, phys_addr);
2106}
2107
Nadav Har'Eld462b812011-05-24 15:26:10 +03002108static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2109{
2110 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002111 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2112 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002113 loaded_vmcs->cpu = -1;
2114 loaded_vmcs->launched = 0;
2115}
2116
Dongxiao Xu7725b892010-05-11 18:29:38 +08002117static void vmcs_load(struct vmcs *vmcs)
2118{
2119 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002120 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002121
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002122 if (static_branch_unlikely(&enable_evmcs))
2123 return evmcs_load(phys_addr);
2124
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002125 asm volatile (__ex("vmptrld %1") CC_SET(na)
2126 : CC_OUT(na) (error) : "m"(phys_addr));
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002127 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002128 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002129 vmcs, phys_addr);
2130}
2131
Dave Young2965faa2015-09-09 15:38:55 -07002132#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002133/*
2134 * This bitmap is used to indicate whether the vmclear
2135 * operation is enabled on all cpus. All disabled by
2136 * default.
2137 */
2138static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2139
2140static inline void crash_enable_local_vmclear(int cpu)
2141{
2142 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2143}
2144
2145static inline void crash_disable_local_vmclear(int cpu)
2146{
2147 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2148}
2149
2150static inline int crash_local_vmclear_enabled(int cpu)
2151{
2152 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2153}
2154
2155static void crash_vmclear_local_loaded_vmcss(void)
2156{
2157 int cpu = raw_smp_processor_id();
2158 struct loaded_vmcs *v;
2159
2160 if (!crash_local_vmclear_enabled(cpu))
2161 return;
2162
2163 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2164 loaded_vmcss_on_cpu_link)
2165 vmcs_clear(v->vmcs);
2166}
2167#else
2168static inline void crash_enable_local_vmclear(int cpu) { }
2169static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002170#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002171
Nadav Har'Eld462b812011-05-24 15:26:10 +03002172static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002174 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002175 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176
Nadav Har'Eld462b812011-05-24 15:26:10 +03002177 if (loaded_vmcs->cpu != cpu)
2178 return; /* vcpu migration can race with cpu offline */
2179 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002181 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002182 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002183
2184 /*
2185 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2186 * is before setting loaded_vmcs->vcpu to -1 which is done in
2187 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2188 * then adds the vmcs into percpu list before it is deleted.
2189 */
2190 smp_wmb();
2191
Nadav Har'Eld462b812011-05-24 15:26:10 +03002192 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002193 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194}
2195
Nadav Har'Eld462b812011-05-24 15:26:10 +03002196static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002197{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002198 int cpu = loaded_vmcs->cpu;
2199
2200 if (cpu != -1)
2201 smp_call_function_single(cpu,
2202 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002203}
2204
Junaid Shahidfaff8752018-06-29 13:10:05 -07002205static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2206{
2207 if (vpid == 0)
2208 return true;
2209
2210 if (cpu_has_vmx_invvpid_individual_addr()) {
2211 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2212 return true;
2213 }
2214
2215 return false;
2216}
2217
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002218static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002219{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002220 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002221 return;
2222
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002223 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002224 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002225}
2226
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002227static inline void vpid_sync_vcpu_global(void)
2228{
2229 if (cpu_has_vmx_invvpid_global())
2230 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2231}
2232
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002233static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002234{
2235 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002236 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002237 else
2238 vpid_sync_vcpu_global();
2239}
2240
Sheng Yang14394422008-04-28 12:24:45 +08002241static inline void ept_sync_global(void)
2242{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002243 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002244}
2245
2246static inline void ept_sync_context(u64 eptp)
2247{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002248 if (cpu_has_vmx_invept_context())
2249 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2250 else
2251 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002252}
2253
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002254static __always_inline void vmcs_check16(unsigned long field)
2255{
2256 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2257 "16-bit accessor invalid for 64-bit field");
2258 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2259 "16-bit accessor invalid for 64-bit high field");
2260 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2261 "16-bit accessor invalid for 32-bit high field");
2262 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2263 "16-bit accessor invalid for natural width field");
2264}
2265
2266static __always_inline void vmcs_check32(unsigned long field)
2267{
2268 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2269 "32-bit accessor invalid for 16-bit field");
2270 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2271 "32-bit accessor invalid for natural width field");
2272}
2273
2274static __always_inline void vmcs_check64(unsigned long field)
2275{
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2277 "64-bit accessor invalid for 16-bit field");
2278 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2279 "64-bit accessor invalid for 64-bit high field");
2280 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2281 "64-bit accessor invalid for 32-bit field");
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2283 "64-bit accessor invalid for natural width field");
2284}
2285
2286static __always_inline void vmcs_checkl(unsigned long field)
2287{
2288 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2289 "Natural width accessor invalid for 16-bit field");
2290 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2291 "Natural width accessor invalid for 64-bit field");
2292 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2293 "Natural width accessor invalid for 64-bit high field");
2294 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2295 "Natural width accessor invalid for 32-bit field");
2296}
2297
2298static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002299{
Avi Kivity5e520e62011-05-15 10:13:12 -04002300 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301
Uros Bizjak44c2d662018-10-11 19:40:45 +02002302 asm volatile (__ex_clear("vmread %1, %0", "%k0")
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002303 : "=r"(value) : "r"(field));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 return value;
2305}
2306
Avi Kivity96304212011-05-15 10:13:13 -04002307static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002309 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002310 if (static_branch_unlikely(&enable_evmcs))
2311 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002312 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313}
2314
Avi Kivity96304212011-05-15 10:13:13 -04002315static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002317 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002318 if (static_branch_unlikely(&enable_evmcs))
2319 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002320 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
2322
Avi Kivity96304212011-05-15 10:13:13 -04002323static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002325 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002326 if (static_branch_unlikely(&enable_evmcs))
2327 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002328#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002331 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332#endif
2333}
2334
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002335static __always_inline unsigned long vmcs_readl(unsigned long field)
2336{
2337 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002338 if (static_branch_unlikely(&enable_evmcs))
2339 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field);
2341}
2342
Avi Kivitye52de1b2007-01-05 16:36:56 -08002343static noinline void vmwrite_error(unsigned long field, unsigned long value)
2344{
2345 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2346 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2347 dump_stack();
2348}
2349
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002350static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002352 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353
Uros Bizjak4b1e5472018-10-11 19:40:44 +02002354 asm volatile (__ex("vmwrite %2, %1") CC_SET(na)
2355 : CC_OUT(na) (error) : "r"(field), "rm"(value));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002356 if (unlikely(error))
2357 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358}
2359
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002360static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002361{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002362 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002363 if (static_branch_unlikely(&enable_evmcs))
2364 return evmcs_write16(field, value);
2365
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002366 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
2368
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002369static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002371 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002372 if (static_branch_unlikely(&enable_evmcs))
2373 return evmcs_write32(field, value);
2374
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376}
2377
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002378static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002381 if (static_branch_unlikely(&enable_evmcs))
2382 return evmcs_write64(field, value);
2383
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002384 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002385#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002387 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388#endif
2389}
2390
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002392{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002393 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002394 if (static_branch_unlikely(&enable_evmcs))
2395 return evmcs_write64(field, value);
2396
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002398}
2399
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002401{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002402 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2403 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002404 if (static_branch_unlikely(&enable_evmcs))
2405 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2406
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2408}
2409
2410static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2411{
2412 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2413 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002414 if (static_branch_unlikely(&enable_evmcs))
2415 return evmcs_write32(field, evmcs_read32(field) | mask);
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418}
2419
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002420static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2421{
2422 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2423}
2424
Gleb Natapov2961e8762013-11-25 15:37:13 +02002425static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2426{
2427 vmcs_write32(VM_ENTRY_CONTROLS, val);
2428 vmx->vm_entry_controls_shadow = val;
2429}
2430
2431static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2432{
2433 if (vmx->vm_entry_controls_shadow != val)
2434 vm_entry_controls_init(vmx, val);
2435}
2436
2437static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2438{
2439 return vmx->vm_entry_controls_shadow;
2440}
2441
2442
2443static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2444{
2445 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2446}
2447
2448static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2449{
2450 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2451}
2452
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002453static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2454{
2455 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2456}
2457
Gleb Natapov2961e8762013-11-25 15:37:13 +02002458static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2459{
2460 vmcs_write32(VM_EXIT_CONTROLS, val);
2461 vmx->vm_exit_controls_shadow = val;
2462}
2463
2464static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2465{
2466 if (vmx->vm_exit_controls_shadow != val)
2467 vm_exit_controls_init(vmx, val);
2468}
2469
2470static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2471{
2472 return vmx->vm_exit_controls_shadow;
2473}
2474
2475
2476static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2477{
2478 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2479}
2480
2481static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2482{
2483 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2484}
2485
Avi Kivity2fb92db2011-04-27 19:42:18 +03002486static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2487{
2488 vmx->segment_cache.bitmask = 0;
2489}
2490
2491static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2492 unsigned field)
2493{
2494 bool ret;
2495 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2496
2497 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2498 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2499 vmx->segment_cache.bitmask = 0;
2500 }
2501 ret = vmx->segment_cache.bitmask & mask;
2502 vmx->segment_cache.bitmask |= mask;
2503 return ret;
2504}
2505
2506static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2507{
2508 u16 *p = &vmx->segment_cache.seg[seg].selector;
2509
2510 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2511 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2512 return *p;
2513}
2514
2515static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2516{
2517 ulong *p = &vmx->segment_cache.seg[seg].base;
2518
2519 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2520 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2521 return *p;
2522}
2523
2524static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2525{
2526 u32 *p = &vmx->segment_cache.seg[seg].limit;
2527
2528 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2529 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2530 return *p;
2531}
2532
2533static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2534{
2535 u32 *p = &vmx->segment_cache.seg[seg].ar;
2536
2537 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2538 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2539 return *p;
2540}
2541
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002542static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2543{
2544 u32 eb;
2545
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002546 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002547 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002548 /*
2549 * Guest access to VMware backdoor ports could legitimately
2550 * trigger #GP because of TSS I/O permission bitmap.
2551 * We intercept those #GP and allow access to them anyway
2552 * as VMware does.
2553 */
2554 if (enable_vmware_backdoor)
2555 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002556 if ((vcpu->guest_debug &
2557 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2558 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2559 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002560 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002561 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002562 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002563 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002564
2565 /* When we are running a nested L2 guest and L1 specified for it a
2566 * certain exception bitmap, we must trap the same exceptions and pass
2567 * them to L1. When running L2, we will only handle the exceptions
2568 * specified above if L1 did not want them.
2569 */
2570 if (is_guest_mode(vcpu))
2571 eb |= get_vmcs12(vcpu)->exception_bitmap;
2572
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002573 vmcs_write32(EXCEPTION_BITMAP, eb);
2574}
2575
Ashok Raj15d45072018-02-01 22:59:43 +01002576/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002577 * Check if MSR is intercepted for currently loaded MSR bitmap.
2578 */
2579static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2580{
2581 unsigned long *msr_bitmap;
2582 int f = sizeof(unsigned long);
2583
2584 if (!cpu_has_vmx_msr_bitmap())
2585 return true;
2586
2587 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2588
2589 if (msr <= 0x1fff) {
2590 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2591 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2592 msr &= 0x1fff;
2593 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2594 }
2595
2596 return true;
2597}
2598
2599/*
Ashok Raj15d45072018-02-01 22:59:43 +01002600 * Check if MSR is intercepted for L01 MSR bitmap.
2601 */
2602static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2603{
2604 unsigned long *msr_bitmap;
2605 int f = sizeof(unsigned long);
2606
2607 if (!cpu_has_vmx_msr_bitmap())
2608 return true;
2609
2610 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2611
2612 if (msr <= 0x1fff) {
2613 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2614 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2615 msr &= 0x1fff;
2616 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2617 }
2618
2619 return true;
2620}
2621
Gleb Natapov2961e8762013-11-25 15:37:13 +02002622static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2623 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002624{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002625 vm_entry_controls_clearbit(vmx, entry);
2626 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002627}
2628
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002629static int find_msr(struct vmx_msrs *m, unsigned int msr)
2630{
2631 unsigned int i;
2632
2633 for (i = 0; i < m->nr; ++i) {
2634 if (m->val[i].index == msr)
2635 return i;
2636 }
2637 return -ENOENT;
2638}
2639
Avi Kivity61d2ef22010-04-28 16:40:38 +03002640static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2641{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002642 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002643 struct msr_autoload *m = &vmx->msr_autoload;
2644
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002645 switch (msr) {
2646 case MSR_EFER:
2647 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002648 clear_atomic_switch_msr_special(vmx,
2649 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002650 VM_EXIT_LOAD_IA32_EFER);
2651 return;
2652 }
2653 break;
2654 case MSR_CORE_PERF_GLOBAL_CTRL:
2655 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002656 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002657 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2658 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2659 return;
2660 }
2661 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002662 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002663 i = find_msr(&m->guest, msr);
2664 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002665 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002666 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002667 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002668 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002669
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002670skip_guest:
2671 i = find_msr(&m->host, msr);
2672 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002673 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002674
2675 --m->host.nr;
2676 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002677 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002678}
2679
Gleb Natapov2961e8762013-11-25 15:37:13 +02002680static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2681 unsigned long entry, unsigned long exit,
2682 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2683 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002684{
2685 vmcs_write64(guest_val_vmcs, guest_val);
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07002686 if (host_val_vmcs != HOST_IA32_EFER)
2687 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002688 vm_entry_controls_setbit(vmx, entry);
2689 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002690}
2691
Avi Kivity61d2ef22010-04-28 16:40:38 +03002692static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002693 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002694{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002695 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002696 struct msr_autoload *m = &vmx->msr_autoload;
2697
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002698 switch (msr) {
2699 case MSR_EFER:
2700 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002701 add_atomic_switch_msr_special(vmx,
2702 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002703 VM_EXIT_LOAD_IA32_EFER,
2704 GUEST_IA32_EFER,
2705 HOST_IA32_EFER,
2706 guest_val, host_val);
2707 return;
2708 }
2709 break;
2710 case MSR_CORE_PERF_GLOBAL_CTRL:
2711 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002712 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002713 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2714 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2715 GUEST_IA32_PERF_GLOBAL_CTRL,
2716 HOST_IA32_PERF_GLOBAL_CTRL,
2717 guest_val, host_val);
2718 return;
2719 }
2720 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002721 case MSR_IA32_PEBS_ENABLE:
2722 /* PEBS needs a quiescent period after being disabled (to write
2723 * a record). Disabling PEBS through VMX MSR swapping doesn't
2724 * provide that period, so a CPU could write host's record into
2725 * guest's memory.
2726 */
2727 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002728 }
2729
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002730 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002731 if (!entry_only)
2732 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002733
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002734 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002735 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002736 "Can't add msr %x\n", msr);
2737 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002738 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002739 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002740 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002741 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002742 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002743 m->guest.val[i].index = msr;
2744 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002745
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002746 if (entry_only)
2747 return;
2748
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002749 if (j < 0) {
2750 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002751 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002752 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 m->host.val[j].index = msr;
2754 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002755}
2756
Avi Kivity92c0d902009-10-29 11:00:16 +02002757static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002758{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002759 u64 guest_efer = vmx->vcpu.arch.efer;
2760 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002761
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002762 if (!enable_ept) {
2763 /*
2764 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2765 * host CPUID is more efficient than testing guest CPUID
2766 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2767 */
2768 if (boot_cpu_has(X86_FEATURE_SMEP))
2769 guest_efer |= EFER_NX;
2770 else if (!(guest_efer & EFER_NX))
2771 ignore_bits |= EFER_NX;
2772 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002773
Avi Kivity51c6cf62007-08-29 03:48:05 +03002774 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002775 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002776 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002777 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002778#ifdef CONFIG_X86_64
2779 ignore_bits |= EFER_LMA | EFER_LME;
2780 /* SCE is meaningful only in long mode on Intel */
2781 if (guest_efer & EFER_LMA)
2782 ignore_bits &= ~(u64)EFER_SCE;
2783#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002784
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002785 /*
2786 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2787 * On CPUs that support "load IA32_EFER", always switch EFER
2788 * atomically, since it's faster than switching it manually.
2789 */
2790 if (cpu_has_load_ia32_efer ||
2791 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002792 if (!(guest_efer & EFER_LMA))
2793 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002794 if (guest_efer != host_efer)
2795 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002796 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002797 else
2798 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002799 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002800 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002801 clear_atomic_switch_msr(vmx, MSR_EFER);
2802
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002803 guest_efer &= ~ignore_bits;
2804 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002805
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002806 vmx->guest_msrs[efer_offset].data = guest_efer;
2807 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2808
2809 return true;
2810 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002811}
2812
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002813#ifdef CONFIG_X86_32
2814/*
2815 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2816 * VMCS rather than the segment table. KVM uses this helper to figure
2817 * out the current bases to poke them into the VMCS before entry.
2818 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002819static unsigned long segment_base(u16 selector)
2820{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002821 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002822 unsigned long v;
2823
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002824 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002825 return 0;
2826
Thomas Garnier45fc8752017-03-14 10:05:08 -07002827 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002828
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002829 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002830 u16 ldt_selector = kvm_read_ldt();
2831
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002832 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833 return 0;
2834
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002835 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 return v;
2839}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002840#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002841
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002842static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002843{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002844 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002845 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002846#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002847 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002848#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002849 unsigned long fs_base, gs_base;
2850 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002851 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002852
Sean Christophersond264ee02018-08-27 15:21:12 -07002853 vmx->req_immediate_exit = false;
2854
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002855 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002856 return;
2857
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002858 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002859 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002860
Avi Kivity33ed6322007-05-02 16:54:03 +03002861 /*
2862 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2863 * allow segment selectors with cpl > 0 or ti == 1.
2864 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002865 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002866
2867#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002868 savesegment(ds, host_state->ds_sel);
2869 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002870
2871 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002872 if (likely(is_64bit_mm(current->mm))) {
2873 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002874 fs_sel = current->thread.fsindex;
2875 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002876 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002877 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002878 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002879 savesegment(fs, fs_sel);
2880 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002881 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002882 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002883 }
2884
Paolo Bonzini4679b612018-09-24 17:23:01 +02002885 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002886#else
Sean Christophersone368b872018-07-23 12:32:41 -07002887 savesegment(fs, fs_sel);
2888 savesegment(gs, gs_sel);
2889 fs_base = segment_base(fs_sel);
2890 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002891#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002892
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002893 if (unlikely(fs_sel != host_state->fs_sel)) {
2894 if (!(fs_sel & 7))
2895 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2896 else
2897 vmcs_write16(HOST_FS_SELECTOR, 0);
2898 host_state->fs_sel = fs_sel;
2899 }
2900 if (unlikely(gs_sel != host_state->gs_sel)) {
2901 if (!(gs_sel & 7))
2902 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2903 else
2904 vmcs_write16(HOST_GS_SELECTOR, 0);
2905 host_state->gs_sel = gs_sel;
2906 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002907 if (unlikely(fs_base != host_state->fs_base)) {
2908 vmcs_writel(HOST_FS_BASE, fs_base);
2909 host_state->fs_base = fs_base;
2910 }
2911 if (unlikely(gs_base != host_state->gs_base)) {
2912 vmcs_writel(HOST_GS_BASE, gs_base);
2913 host_state->gs_base = gs_base;
2914 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002915
Avi Kivity26bb0982009-09-07 11:14:12 +03002916 for (i = 0; i < vmx->save_nmsrs; ++i)
2917 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002918 vmx->guest_msrs[i].data,
2919 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002920}
2921
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002922static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002923{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002924 struct vmcs_host_state *host_state;
2925
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002926 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002927 return;
2928
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002929 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002930 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002931
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002932 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002933 vmx->loaded_cpu_state = NULL;
2934
Avi Kivityc8770e72010-11-11 12:37:26 +02002935#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002936 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002937#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002938 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2939 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002940#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002941 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002942#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002943 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002944#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002945 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002946 if (host_state->fs_sel & 7)
2947 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002948#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002949 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2950 loadsegment(ds, host_state->ds_sel);
2951 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002952 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002953#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002954 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002955#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002956 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002957#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002958 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002959}
2960
Sean Christopherson678e3152018-07-23 12:32:43 -07002961#ifdef CONFIG_X86_64
2962static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002963{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002964 preempt_disable();
2965 if (vmx->loaded_cpu_state)
2966 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2967 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002968 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002969}
2970
Sean Christopherson678e3152018-07-23 12:32:43 -07002971static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2972{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002973 preempt_disable();
2974 if (vmx->loaded_cpu_state)
2975 wrmsrl(MSR_KERNEL_GS_BASE, data);
2976 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002977 vmx->msr_guest_kernel_gs_base = data;
2978}
2979#endif
2980
Feng Wu28b835d2015-09-18 22:29:54 +08002981static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2982{
2983 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2984 struct pi_desc old, new;
2985 unsigned int dest;
2986
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002987 /*
2988 * In case of hot-plug or hot-unplug, we may have to undo
2989 * vmx_vcpu_pi_put even if there is no assigned device. And we
2990 * always keep PI.NDST up to date for simplicity: it makes the
2991 * code easier, and CPU migration is not a fast path.
2992 */
2993 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08002994 return;
2995
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002996 /*
2997 * First handle the simple case where no cmpxchg is necessary; just
2998 * allow posting non-urgent interrupts.
2999 *
3000 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3001 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3002 * expects the VCPU to be on the blocked_vcpu_list that matches
3003 * PI.NDST.
3004 */
3005 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3006 vcpu->cpu == cpu) {
3007 pi_clear_sn(pi_desc);
3008 return;
3009 }
3010
3011 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003012 do {
3013 old.control = new.control = pi_desc->control;
3014
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003015 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003016
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003017 if (x2apic_enabled())
3018 new.ndst = dest;
3019 else
3020 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003021
Feng Wu28b835d2015-09-18 22:29:54 +08003022 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003023 } while (cmpxchg64(&pi_desc->control, old.control,
3024 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003025}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003026
Peter Feinerc95ba922016-08-17 09:36:47 -07003027static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3028{
3029 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3030 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3031}
3032
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033/*
3034 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3035 * vcpu mutex is already taken.
3036 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003037static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003039 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003040 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003042 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003043 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003044 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003045 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003046
3047 /*
3048 * Read loaded_vmcs->cpu should be before fetching
3049 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3050 * See the comments in __loaded_vmcs_clear().
3051 */
3052 smp_rmb();
3053
Nadav Har'Eld462b812011-05-24 15:26:10 +03003054 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3055 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003056 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003057 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003058 }
3059
3060 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3061 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3062 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003063 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003064 }
3065
3066 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003067 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003068 unsigned long sysenter_esp;
3069
3070 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003071
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 /*
3073 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003074 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003076 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003077 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003078 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003080 /*
3081 * VM exits change the host TR limit to 0x67 after a VM
3082 * exit. This is okay, since 0x67 covers everything except
3083 * the IO bitmap and have have code to handle the IO bitmap
3084 * being lost after a VM exit.
3085 */
3086 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3087
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3089 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003090
Nadav Har'Eld462b812011-05-24 15:26:10 +03003091 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 }
Feng Wu28b835d2015-09-18 22:29:54 +08003093
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003094 /* Setup TSC multiplier */
3095 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003096 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3097 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003098
Feng Wu28b835d2015-09-18 22:29:54 +08003099 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003100 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003101 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003102}
3103
3104static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3105{
3106 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3107
3108 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003109 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3110 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003111 return;
3112
3113 /* Set SN when the vCPU is preempted */
3114 if (vcpu->preempted)
3115 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116}
3117
3118static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3119{
Feng Wu28b835d2015-09-18 22:29:54 +08003120 vmx_vcpu_pi_put(vcpu);
3121
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003122 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123}
3124
Wanpeng Lif244dee2017-07-20 01:11:54 -07003125static bool emulation_required(struct kvm_vcpu *vcpu)
3126{
3127 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3128}
3129
Avi Kivityedcafe32009-12-30 18:07:40 +02003130static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3131
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003132/*
3133 * Return the cr0 value that a nested guest would read. This is a combination
3134 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3135 * its hypervisor (cr0_read_shadow).
3136 */
3137static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3138{
3139 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3140 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3141}
3142static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3143{
3144 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3145 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3146}
3147
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3149{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003150 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003151
Avi Kivity6de12732011-03-07 12:51:22 +02003152 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3153 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3154 rflags = vmcs_readl(GUEST_RFLAGS);
3155 if (to_vmx(vcpu)->rmode.vm86_active) {
3156 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3157 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3158 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3159 }
3160 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003161 }
Avi Kivity6de12732011-03-07 12:51:22 +02003162 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163}
3164
3165static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3166{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003167 unsigned long old_rflags = vmx_get_rflags(vcpu);
3168
Avi Kivity6de12732011-03-07 12:51:22 +02003169 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3170 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003171 if (to_vmx(vcpu)->rmode.vm86_active) {
3172 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003173 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003174 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003176
3177 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3178 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179}
3180
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003181static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003182{
3183 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3184 int ret = 0;
3185
3186 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003187 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003188 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003189 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003190
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003191 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003192}
3193
3194static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3195{
3196 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3197 u32 interruptibility = interruptibility_old;
3198
3199 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3200
Jan Kiszka48005f62010-02-19 19:38:07 +01003201 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003202 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003203 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003204 interruptibility |= GUEST_INTR_STATE_STI;
3205
3206 if ((interruptibility != interruptibility_old))
3207 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3208}
3209
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3211{
3212 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003214 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003215 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003216 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217
Glauber Costa2809f5d2009-05-12 16:21:05 -04003218 /* skipping an emulated instruction also counts */
3219 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220}
3221
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003222static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3223 unsigned long exit_qual)
3224{
3225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3226 unsigned int nr = vcpu->arch.exception.nr;
3227 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3228
3229 if (vcpu->arch.exception.has_error_code) {
3230 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3231 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3232 }
3233
3234 if (kvm_exception_is_soft(nr))
3235 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3236 else
3237 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3238
3239 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3240 vmx_get_nmi_mask(vcpu))
3241 intr_info |= INTR_INFO_UNBLOCK_NMI;
3242
3243 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3244}
3245
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003246/*
3247 * KVM wants to inject page-faults which it got to the guest. This function
3248 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003249 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003250static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003251{
3252 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003253 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003254
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003255 if (nr == PF_VECTOR) {
3256 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003257 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003258 return 1;
3259 }
3260 /*
3261 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3262 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3263 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3264 * can be written only when inject_pending_event runs. This should be
3265 * conditional on a new capability---if the capability is disabled,
3266 * kvm_multiple_exception would write the ancillary information to
3267 * CR2 or DR6, for backwards ABI-compatibility.
3268 */
3269 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3270 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003271 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003272 return 1;
3273 }
3274 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003275 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003276 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003277 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003278 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3279 *exit_qual ^= DR6_RTM;
3280 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003281 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003282 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003283 return 1;
3284 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003285 }
3286
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003287 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003288}
3289
Wanpeng Licaa057a2018-03-12 04:53:03 -07003290static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3291{
3292 /*
3293 * Ensure that we clear the HLT state in the VMCS. We don't need to
3294 * explicitly skip the instruction because if the HLT state is set,
3295 * then the instruction is already executing and RIP has already been
3296 * advanced.
3297 */
3298 if (kvm_hlt_in_guest(vcpu->kvm) &&
3299 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3300 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3301}
3302
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003303static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003304{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003305 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003306 unsigned nr = vcpu->arch.exception.nr;
3307 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003308 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003309 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003310
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003311 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003312 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003313 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3314 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003315
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003316 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003317 int inc_eip = 0;
3318 if (kvm_exception_is_soft(nr))
3319 inc_eip = vcpu->arch.event_exit_inst_len;
3320 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003321 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003322 return;
3323 }
3324
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003325 WARN_ON_ONCE(vmx->emulation_required);
3326
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003327 if (kvm_exception_is_soft(nr)) {
3328 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3329 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003330 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3331 } else
3332 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3333
3334 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003335
3336 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003337}
3338
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003339static bool vmx_rdtscp_supported(void)
3340{
3341 return cpu_has_vmx_rdtscp();
3342}
3343
Mao, Junjiead756a12012-07-02 01:18:48 +00003344static bool vmx_invpcid_supported(void)
3345{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003346 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003347}
3348
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349/*
Eddie Donga75beee2007-05-17 18:55:15 +03003350 * Swap MSR entry in host/guest MSR entry array.
3351 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003352static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003353{
Avi Kivity26bb0982009-09-07 11:14:12 +03003354 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003355
3356 tmp = vmx->guest_msrs[to];
3357 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3358 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003359}
3360
3361/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003362 * Set up the vmcs to automatically save and restore system
3363 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3364 * mode, as fiddling with msrs is very expensive.
3365 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003366static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003367{
Avi Kivity26bb0982009-09-07 11:14:12 +03003368 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003369
Eddie Donga75beee2007-05-17 18:55:15 +03003370 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003371#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003372 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003373 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003374 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003375 move_msr_up(vmx, index, save_nmsrs++);
3376 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003377 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003378 move_msr_up(vmx, index, save_nmsrs++);
3379 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003380 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003381 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003382 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003383 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003384 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003385 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003386 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003387 * if efer.sce is enabled.
3388 */
Brian Gerst8c065852010-07-17 09:03:26 -04003389 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003390 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003391 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003392 }
Eddie Donga75beee2007-05-17 18:55:15 +03003393#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003394 index = __find_msr_index(vmx, MSR_EFER);
3395 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003396 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003397
Avi Kivity26bb0982009-09-07 11:14:12 +03003398 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003399
Yang Zhang8d146952013-01-25 10:18:50 +08003400 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003401 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003402}
3403
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003404static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003406 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003408 if (is_guest_mode(vcpu) &&
3409 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3410 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3411
3412 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413}
3414
3415/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003416 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003418static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003420 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003421 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003422 * We're here if L1 chose not to trap WRMSR to TSC. According
3423 * to the spec, this should set L1's TSC; The offset that L1
3424 * set for L2 remains unchanged, and still needs to be added
3425 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003426 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003427 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003428 /* recalculate vmcs02.TSC_OFFSET: */
3429 vmcs12 = get_vmcs12(vcpu);
3430 vmcs_write64(TSC_OFFSET, offset +
3431 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3432 vmcs12->tsc_offset : 0));
3433 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003434 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3435 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003436 vmcs_write64(TSC_OFFSET, offset);
3437 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438}
3439
Nadav Har'El801d3422011-05-25 23:02:23 +03003440/*
3441 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3442 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3443 * all guests if the "nested" module option is off, and can also be disabled
3444 * for a single guest by disabling its VMX cpuid bit.
3445 */
3446static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3447{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003448 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003449}
3450
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003452 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3453 * returned for the various VMX controls MSRs when nested VMX is enabled.
3454 * The same values should also be used to verify that vmcs12 control fields are
3455 * valid during nested entry from L1 to L2.
3456 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3457 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3458 * bit in the high half is on if the corresponding bit in the control field
3459 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003460 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003461static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003462{
Paolo Bonzini13893092018-02-26 13:40:09 +01003463 if (!nested) {
3464 memset(msrs, 0, sizeof(*msrs));
3465 return;
3466 }
3467
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003468 /*
3469 * Note that as a general rule, the high half of the MSRs (bits in
3470 * the control fields which may be 1) should be initialized by the
3471 * intersection of the underlying hardware's MSR (i.e., features which
3472 * can be supported) and the list of features we want to expose -
3473 * because they are known to be properly supported in our code.
3474 * Also, usually, the low half of the MSRs (bits which must be 1) can
3475 * be set to 0, meaning that L1 may turn off any of these bits. The
3476 * reason is that if one of these bits is necessary, it will appear
3477 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3478 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003479 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003480 * These rules have exceptions below.
3481 */
3482
3483 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003484 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003485 msrs->pinbased_ctls_low,
3486 msrs->pinbased_ctls_high);
3487 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003488 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003490 PIN_BASED_EXT_INTR_MASK |
3491 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003492 PIN_BASED_VIRTUAL_NMIS |
3493 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003494 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003495 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003496 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003497
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003498 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003499 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003500 msrs->exit_ctls_low,
3501 msrs->exit_ctls_high);
3502 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003503 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003504
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003506#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003507 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003508#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003509 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003511 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003512 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003513 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3514
Jan Kiszka2996fca2014-06-16 13:59:43 +02003515 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003516 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003517
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003518 /* entry controls */
3519 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 msrs->entry_ctls_low,
3521 msrs->entry_ctls_high);
3522 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003523 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003525#ifdef CONFIG_X86_64
3526 VM_ENTRY_IA32E_MODE |
3527#endif
3528 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003530 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003531
Jan Kiszka2996fca2014-06-16 13:59:43 +02003532 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003534
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003535 /* cpu-based controls */
3536 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->procbased_ctls_low,
3538 msrs->procbased_ctls_high);
3539 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003540 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003542 CPU_BASED_VIRTUAL_INTR_PENDING |
3543 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003544 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3545 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3546 CPU_BASED_CR3_STORE_EXITING |
3547#ifdef CONFIG_X86_64
3548 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3549#endif
3550 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003551 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3552 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3553 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3554 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003555 /*
3556 * We can allow some features even when not supported by the
3557 * hardware. For example, L1 can specify an MSR bitmap - and we
3558 * can use it to avoid exits to L1 - even when L0 runs L2
3559 * without MSR bitmaps.
3560 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003561 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003562 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003563 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003564
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003565 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003566 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003567 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3568
Paolo Bonzini80154d72017-08-24 13:55:35 +02003569 /*
3570 * secondary cpu-based controls. Do not include those that
3571 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3572 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003573 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 msrs->secondary_ctls_low,
3575 msrs->secondary_ctls_high);
3576 msrs->secondary_ctls_low = 0;
3577 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003578 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003579 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003580 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003581 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003582 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003583
Liran Alon32c7acf2018-06-23 02:35:11 +03003584 /*
3585 * We can emulate "VMCS shadowing," even if the hardware
3586 * doesn't support it.
3587 */
3588 msrs->secondary_ctls_high |=
3589 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003590
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003591 if (enable_ept) {
3592 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003593 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003594 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003596 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003597 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003598 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003599 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003600 msrs->ept_caps &= vmx_capability.ept;
3601 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003602 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3603 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003604 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003605 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003606 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003608 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003609 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003610
Bandan Das27c42a12017-08-03 15:54:42 -04003611 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003613 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003614 /*
3615 * Advertise EPTP switching unconditionally
3616 * since we emulate it
3617 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003618 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003620 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003621 }
3622
Paolo Bonzinief697a72016-03-18 16:58:38 +01003623 /*
3624 * Old versions of KVM use the single-context version without
3625 * checking for support, so declare that it is supported even
3626 * though it is treated as global context. The alternative is
3627 * not failing the single-context invvpid, and it is worse.
3628 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003629 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003630 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003631 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003632 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003633 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003634 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003635
Radim Krčmář0790ec12015-03-17 14:02:32 +01003636 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003637 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003638 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3639
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003640 if (flexpriority_enabled)
3641 msrs->secondary_ctls_high |=
3642 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3643
Jan Kiszkac18911a2013-03-13 16:06:41 +01003644 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003645 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msrs->misc_low,
3647 msrs->misc_high);
3648 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3649 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003650 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003651 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003652 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003653 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003654
3655 /*
3656 * This MSR reports some information about VMX support. We
3657 * should return information about the VMX we emulate for the
3658 * guest, and the VMCS structure we give it - not about the
3659 * VMX support of the underlying hardware.
3660 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003662 VMCS12_REVISION |
3663 VMX_BASIC_TRUE_CTLS |
3664 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3665 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3666
3667 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003669
3670 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003671 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003672 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3673 * We picked the standard core2 setting.
3674 */
3675#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3676#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3678 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003679
3680 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003681 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3682 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003683
3684 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003686}
3687
David Matlack38991522016-11-29 18:14:08 -08003688/*
3689 * if fixed0[i] == 1: val[i] must be 1
3690 * if fixed1[i] == 0: val[i] must be 0
3691 */
3692static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3693{
3694 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695}
3696
3697static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3698{
David Matlack38991522016-11-29 18:14:08 -08003699 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700}
3701
3702static inline u64 vmx_control_msr(u32 low, u32 high)
3703{
3704 return low | ((u64)high << 32);
3705}
3706
David Matlack62cc6b9d2016-11-29 18:14:07 -08003707static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3708{
3709 superset &= mask;
3710 subset &= mask;
3711
3712 return (superset | subset) == superset;
3713}
3714
3715static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3716{
3717 const u64 feature_and_reserved =
3718 /* feature (except bit 48; see below) */
3719 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3720 /* reserved */
3721 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003722 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003723
3724 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3725 return -EINVAL;
3726
3727 /*
3728 * KVM does not emulate a version of VMX that constrains physical
3729 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3730 */
3731 if (data & BIT_ULL(48))
3732 return -EINVAL;
3733
3734 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3735 vmx_basic_vmcs_revision_id(data))
3736 return -EINVAL;
3737
3738 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3739 return -EINVAL;
3740
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003741 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003742 return 0;
3743}
3744
3745static int
3746vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3747{
3748 u64 supported;
3749 u32 *lowp, *highp;
3750
3751 switch (msr_index) {
3752 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003753 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3754 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003755 break;
3756 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003757 lowp = &vmx->nested.msrs.procbased_ctls_low;
3758 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003759 break;
3760 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003761 lowp = &vmx->nested.msrs.exit_ctls_low;
3762 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003763 break;
3764 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003765 lowp = &vmx->nested.msrs.entry_ctls_low;
3766 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003767 break;
3768 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003769 lowp = &vmx->nested.msrs.secondary_ctls_low;
3770 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003771 break;
3772 default:
3773 BUG();
3774 }
3775
3776 supported = vmx_control_msr(*lowp, *highp);
3777
3778 /* Check must-be-1 bits are still 1. */
3779 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3780 return -EINVAL;
3781
3782 /* Check must-be-0 bits are still 0. */
3783 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3784 return -EINVAL;
3785
3786 *lowp = data;
3787 *highp = data >> 32;
3788 return 0;
3789}
3790
3791static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3792{
3793 const u64 feature_and_reserved_bits =
3794 /* feature */
3795 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3796 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3797 /* reserved */
3798 GENMASK_ULL(13, 9) | BIT_ULL(31);
3799 u64 vmx_misc;
3800
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003801 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3802 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003803
3804 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3805 return -EINVAL;
3806
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003807 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003808 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3809 vmx_misc_preemption_timer_rate(data) !=
3810 vmx_misc_preemption_timer_rate(vmx_misc))
3811 return -EINVAL;
3812
3813 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3814 return -EINVAL;
3815
3816 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3817 return -EINVAL;
3818
3819 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3820 return -EINVAL;
3821
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003822 vmx->nested.msrs.misc_low = data;
3823 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003824
3825 /*
3826 * If L1 has read-only VM-exit information fields, use the
3827 * less permissive vmx_vmwrite_bitmap to specify write
3828 * permissions for the shadow VMCS.
3829 */
3830 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3831 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3832
David Matlack62cc6b9d2016-11-29 18:14:07 -08003833 return 0;
3834}
3835
3836static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3837{
3838 u64 vmx_ept_vpid_cap;
3839
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003840 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3841 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003842
3843 /* Every bit is either reserved or a feature bit. */
3844 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3845 return -EINVAL;
3846
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003847 vmx->nested.msrs.ept_caps = data;
3848 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003849 return 0;
3850}
3851
3852static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3853{
3854 u64 *msr;
3855
3856 switch (msr_index) {
3857 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003858 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003859 break;
3860 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003861 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003862 break;
3863 default:
3864 BUG();
3865 }
3866
3867 /*
3868 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3869 * must be 1 in the restored value.
3870 */
3871 if (!is_bitwise_subset(data, *msr, -1ULL))
3872 return -EINVAL;
3873
3874 *msr = data;
3875 return 0;
3876}
3877
3878/*
3879 * Called when userspace is restoring VMX MSRs.
3880 *
3881 * Returns 0 on success, non-0 otherwise.
3882 */
3883static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3884{
3885 struct vcpu_vmx *vmx = to_vmx(vcpu);
3886
Jim Mattsona943ac52018-05-29 09:11:32 -07003887 /*
3888 * Don't allow changes to the VMX capability MSRs while the vCPU
3889 * is in VMX operation.
3890 */
3891 if (vmx->nested.vmxon)
3892 return -EBUSY;
3893
David Matlack62cc6b9d2016-11-29 18:14:07 -08003894 switch (msr_index) {
3895 case MSR_IA32_VMX_BASIC:
3896 return vmx_restore_vmx_basic(vmx, data);
3897 case MSR_IA32_VMX_PINBASED_CTLS:
3898 case MSR_IA32_VMX_PROCBASED_CTLS:
3899 case MSR_IA32_VMX_EXIT_CTLS:
3900 case MSR_IA32_VMX_ENTRY_CTLS:
3901 /*
3902 * The "non-true" VMX capability MSRs are generated from the
3903 * "true" MSRs, so we do not support restoring them directly.
3904 *
3905 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3906 * should restore the "true" MSRs with the must-be-1 bits
3907 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3908 * DEFAULT SETTINGS".
3909 */
3910 return -EINVAL;
3911 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3912 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3913 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3914 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3915 case MSR_IA32_VMX_PROCBASED_CTLS2:
3916 return vmx_restore_control_msr(vmx, msr_index, data);
3917 case MSR_IA32_VMX_MISC:
3918 return vmx_restore_vmx_misc(vmx, data);
3919 case MSR_IA32_VMX_CR0_FIXED0:
3920 case MSR_IA32_VMX_CR4_FIXED0:
3921 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3922 case MSR_IA32_VMX_CR0_FIXED1:
3923 case MSR_IA32_VMX_CR4_FIXED1:
3924 /*
3925 * These MSRs are generated based on the vCPU's CPUID, so we
3926 * do not support restoring them directly.
3927 */
3928 return -EINVAL;
3929 case MSR_IA32_VMX_EPT_VPID_CAP:
3930 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3931 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003932 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003933 return 0;
3934 default:
3935 /*
3936 * The rest of the VMX capability MSRs do not support restore.
3937 */
3938 return -EINVAL;
3939 }
3940}
3941
Jan Kiszkacae50132014-01-04 18:47:22 +01003942/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003943static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003944{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003945 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003946 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003947 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003948 break;
3949 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3950 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003951 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003952 msrs->pinbased_ctls_low,
3953 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003954 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3955 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003956 break;
3957 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3958 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003959 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003960 msrs->procbased_ctls_low,
3961 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003962 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3963 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003964 break;
3965 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3966 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003967 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003968 msrs->exit_ctls_low,
3969 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003970 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3971 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003972 break;
3973 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3974 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003975 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003976 msrs->entry_ctls_low,
3977 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003978 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3979 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003980 break;
3981 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003982 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003983 msrs->misc_low,
3984 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003985 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003987 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003988 break;
3989 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003990 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 break;
3992 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
3995 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003996 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003999 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 break;
4001 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004002 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004003 msrs->secondary_ctls_low,
4004 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->ept_caps |
4008 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004009 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004010 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004011 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004012 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004013 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004015 }
4016
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 return 0;
4018}
4019
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004020static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4021 uint64_t val)
4022{
4023 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4024
4025 return !(val & ~valid_bits);
4026}
4027
Tom Lendacky801e4592018-02-21 13:39:51 -06004028static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4029{
Paolo Bonzini13893092018-02-26 13:40:09 +01004030 switch (msr->index) {
4031 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4032 if (!nested)
4033 return 1;
4034 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4035 default:
4036 return 1;
4037 }
4038
4039 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004040}
4041
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004042/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004043 * Reads an msr value (of 'msr_index') into 'pdata'.
4044 * Returns 0 on success, non-0 otherwise.
4045 * Assumes vcpu_load() was already called.
4046 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004047static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004049 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004050 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004051
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004052 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004053#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004055 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056 break;
4057 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004058 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004060 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004061 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004062 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004063#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004065 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004066 case MSR_IA32_SPEC_CTRL:
4067 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004068 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4069 return 1;
4070
4071 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4072 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004073 case MSR_IA32_ARCH_CAPABILITIES:
4074 if (!msr_info->host_initiated &&
4075 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4076 return 1;
4077 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4078 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004079 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004080 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 break;
4082 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004083 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 break;
4085 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004086 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004088 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004089 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004090 (!msr_info->host_initiated &&
4091 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004092 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004093 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004094 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004095 case MSR_IA32_MCG_EXT_CTL:
4096 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004097 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004098 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004099 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004100 msr_info->data = vcpu->arch.mcg_ext_ctl;
4101 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004102 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004103 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004104 break;
4105 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4106 if (!nested_vmx_allowed(vcpu))
4107 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004108 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4109 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004110 case MSR_IA32_XSS:
4111 if (!vmx_xsaves_supported())
4112 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004113 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004114 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004115 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004116 if (!msr_info->host_initiated &&
4117 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004118 return 1;
4119 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004121 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004122 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004123 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004124 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004126 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004127 }
4128
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 return 0;
4130}
4131
Jan Kiszkacae50132014-01-04 18:47:22 +01004132static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4133
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134/*
4135 * Writes msr value into into the appropriate "register".
4136 * Returns 0 on success, non-0 otherwise.
4137 * Assumes vcpu_load() was already called.
4138 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004139static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004141 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004142 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004143 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004144 u32 msr_index = msr_info->index;
4145 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004146
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004148 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004149 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004150 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004151#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004153 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154 vmcs_writel(GUEST_FS_BASE, data);
4155 break;
4156 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004157 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 vmcs_writel(GUEST_GS_BASE, data);
4159 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004160 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004161 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004162 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163#endif
4164 case MSR_IA32_SYSENTER_CS:
4165 vmcs_write32(GUEST_SYSENTER_CS, data);
4166 break;
4167 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004168 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 break;
4170 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004171 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004173 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004174 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004175 (!msr_info->host_initiated &&
4176 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004177 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004178 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004179 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004181 vmcs_write64(GUEST_BNDCFGS, data);
4182 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004183 case MSR_IA32_SPEC_CTRL:
4184 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004185 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4186 return 1;
4187
4188 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004189 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004190 return 1;
4191
4192 vmx->spec_ctrl = data;
4193
4194 if (!data)
4195 break;
4196
4197 /*
4198 * For non-nested:
4199 * When it's written (to non-zero) for the first time, pass
4200 * it through.
4201 *
4202 * For nested:
4203 * The handling of the MSR bitmap for L2 guests is done in
4204 * nested_vmx_merge_msr_bitmap. We should not touch the
4205 * vmcs02.msr_bitmap here since it gets completely overwritten
4206 * in the merging. We update the vmcs01 here for L1 as well
4207 * since it will end up touching the MSR anyway now.
4208 */
4209 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4210 MSR_IA32_SPEC_CTRL,
4211 MSR_TYPE_RW);
4212 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004213 case MSR_IA32_PRED_CMD:
4214 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004215 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4216 return 1;
4217
4218 if (data & ~PRED_CMD_IBPB)
4219 return 1;
4220
4221 if (!data)
4222 break;
4223
4224 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4225
4226 /*
4227 * For non-nested:
4228 * When it's written (to non-zero) for the first time, pass
4229 * it through.
4230 *
4231 * For nested:
4232 * The handling of the MSR bitmap for L2 guests is done in
4233 * nested_vmx_merge_msr_bitmap. We should not touch the
4234 * vmcs02.msr_bitmap here since it gets completely overwritten
4235 * in the merging.
4236 */
4237 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4238 MSR_TYPE_W);
4239 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004240 case MSR_IA32_ARCH_CAPABILITIES:
4241 if (!msr_info->host_initiated)
4242 return 1;
4243 vmx->arch_capabilities = data;
4244 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004245 case MSR_IA32_CR_PAT:
4246 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004247 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4248 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004249 vmcs_write64(GUEST_IA32_PAT, data);
4250 vcpu->arch.pat = data;
4251 break;
4252 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004253 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004254 break;
Will Auldba904632012-11-29 12:42:50 -08004255 case MSR_IA32_TSC_ADJUST:
4256 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004257 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004258 case MSR_IA32_MCG_EXT_CTL:
4259 if ((!msr_info->host_initiated &&
4260 !(to_vmx(vcpu)->msr_ia32_feature_control &
4261 FEATURE_CONTROL_LMCE)) ||
4262 (data & ~MCG_EXT_CTL_LMCE_EN))
4263 return 1;
4264 vcpu->arch.mcg_ext_ctl = data;
4265 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004266 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004267 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004268 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004269 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4270 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004271 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004272 if (msr_info->host_initiated && data == 0)
4273 vmx_leave_nested(vcpu);
4274 break;
4275 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004276 if (!msr_info->host_initiated)
4277 return 1; /* they are read-only */
4278 if (!nested_vmx_allowed(vcpu))
4279 return 1;
4280 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004281 case MSR_IA32_XSS:
4282 if (!vmx_xsaves_supported())
4283 return 1;
4284 /*
4285 * The only supported bit as of Skylake is bit 8, but
4286 * it is not supported on KVM.
4287 */
4288 if (data != 0)
4289 return 1;
4290 vcpu->arch.ia32_xss = data;
4291 if (vcpu->arch.ia32_xss != host_xss)
4292 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004293 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004294 else
4295 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4296 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004297 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004298 if (!msr_info->host_initiated &&
4299 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004300 return 1;
4301 /* Check reserved bit, higher 32 bits should be zero */
4302 if ((data >> 32) != 0)
4303 return 1;
4304 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004305 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004306 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004307 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004308 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004309 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004310 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4311 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004312 ret = kvm_set_shared_msr(msr->index, msr->data,
4313 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004314 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004315 if (ret)
4316 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004317 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004318 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004320 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004321 }
4322
Eddie Dong2cc51562007-05-21 07:28:09 +03004323 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324}
4325
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004326static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004328 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4329 switch (reg) {
4330 case VCPU_REGS_RSP:
4331 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4332 break;
4333 case VCPU_REGS_RIP:
4334 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4335 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004336 case VCPU_EXREG_PDPTR:
4337 if (enable_ept)
4338 ept_save_pdptrs(vcpu);
4339 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004340 default:
4341 break;
4342 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004343}
4344
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345static __init int cpu_has_kvm_support(void)
4346{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004347 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348}
4349
4350static __init int vmx_disabled_by_bios(void)
4351{
4352 u64 msr;
4353
4354 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004355 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004356 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004357 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4358 && tboot_enabled())
4359 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004360 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004361 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004362 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004363 && !tboot_enabled()) {
4364 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004365 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004366 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004367 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004368 /* launched w/o TXT and VMX disabled */
4369 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4370 && !tboot_enabled())
4371 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004372 }
4373
4374 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375}
4376
Dongxiao Xu7725b892010-05-11 18:29:38 +08004377static void kvm_cpu_vmxon(u64 addr)
4378{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004379 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004380 intel_pt_handle_vmx(1);
4381
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004382 asm volatile ("vmxon %0" : : "m"(addr));
Dongxiao Xu7725b892010-05-11 18:29:38 +08004383}
4384
Radim Krčmář13a34e02014-08-28 15:13:03 +02004385static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386{
4387 int cpu = raw_smp_processor_id();
4388 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004389 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004391 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004392 return -EBUSY;
4393
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004394 /*
4395 * This can happen if we hot-added a CPU but failed to allocate
4396 * VP assist page for it.
4397 */
4398 if (static_branch_unlikely(&enable_evmcs) &&
4399 !hv_get_vp_assist_page(cpu))
4400 return -EFAULT;
4401
Nadav Har'Eld462b812011-05-24 15:26:10 +03004402 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004403 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4404 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004405
4406 /*
4407 * Now we can enable the vmclear operation in kdump
4408 * since the loaded_vmcss_on_cpu list on this cpu
4409 * has been initialized.
4410 *
4411 * Though the cpu is not in VMX operation now, there
4412 * is no problem to enable the vmclear operation
4413 * for the loaded_vmcss_on_cpu list is empty!
4414 */
4415 crash_enable_local_vmclear(cpu);
4416
Avi Kivity6aa8b732006-12-10 02:21:36 -08004417 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004418
4419 test_bits = FEATURE_CONTROL_LOCKED;
4420 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4421 if (tboot_enabled())
4422 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4423
4424 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004426 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4427 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004428 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004429 if (enable_ept)
4430 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004431
4432 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433}
4434
Nadav Har'Eld462b812011-05-24 15:26:10 +03004435static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004436{
4437 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004438 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004439
Nadav Har'Eld462b812011-05-24 15:26:10 +03004440 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4441 loaded_vmcss_on_cpu_link)
4442 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004443}
4444
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004445
4446/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4447 * tricks.
4448 */
4449static void kvm_cpu_vmxoff(void)
4450{
Uros Bizjak4b1e5472018-10-11 19:40:44 +02004451 asm volatile (__ex("vmxoff"));
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004452
4453 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004454 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004455}
4456
Radim Krčmář13a34e02014-08-28 15:13:03 +02004457static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004458{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004459 vmclear_local_loaded_vmcss();
4460 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004461}
4462
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004463static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004464 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465{
4466 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004467 u32 ctl = ctl_min | ctl_opt;
4468
4469 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4470
4471 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4472 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4473
4474 /* Ensure minimum (required) set of control bits are supported. */
4475 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004476 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004477
4478 *result = ctl;
4479 return 0;
4480}
4481
Avi Kivity110312c2010-12-21 12:54:20 +02004482static __init bool allow_1_setting(u32 msr, u32 ctl)
4483{
4484 u32 vmx_msr_low, vmx_msr_high;
4485
4486 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4487 return vmx_msr_high & ctl;
4488}
4489
Yang, Sheng002c7f72007-07-31 14:23:01 +03004490static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004491{
4492 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004493 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004494 u32 _pin_based_exec_control = 0;
4495 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004496 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004497 u32 _vmexit_control = 0;
4498 u32 _vmentry_control = 0;
4499
Paolo Bonzini13893092018-02-26 13:40:09 +01004500 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304501 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004502#ifdef CONFIG_X86_64
4503 CPU_BASED_CR8_LOAD_EXITING |
4504 CPU_BASED_CR8_STORE_EXITING |
4505#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004506 CPU_BASED_CR3_LOAD_EXITING |
4507 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004508 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004509 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004510 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004511 CPU_BASED_MWAIT_EXITING |
4512 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004513 CPU_BASED_INVLPG_EXITING |
4514 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004515
Sheng Yangf78e0e22007-10-29 09:40:42 +08004516 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004517 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004518 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004519 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4520 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004521 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004522#ifdef CONFIG_X86_64
4523 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4524 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4525 ~CPU_BASED_CR8_STORE_EXITING;
4526#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004527 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004528 min2 = 0;
4529 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004530 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004531 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004532 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004533 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004534 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004535 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004536 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004537 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004538 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004539 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004540 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004541 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004542 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004543 SECONDARY_EXEC_RDSEED_EXITING |
4544 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004545 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004546 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004547 SECONDARY_EXEC_ENABLE_VMFUNC |
4548 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004549 if (adjust_vmx_controls(min2, opt2,
4550 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004551 &_cpu_based_2nd_exec_control) < 0)
4552 return -EIO;
4553 }
4554#ifndef CONFIG_X86_64
4555 if (!(_cpu_based_2nd_exec_control &
4556 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4557 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4558#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004559
4560 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4561 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004562 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004563 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4564 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004565
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004566 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4567 &vmx_capability.ept, &vmx_capability.vpid);
4568
Sheng Yangd56f5462008-04-25 10:13:16 +08004569 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004570 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4571 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004572 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4573 CPU_BASED_CR3_STORE_EXITING |
4574 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004575 } else if (vmx_capability.ept) {
4576 vmx_capability.ept = 0;
4577 pr_warn_once("EPT CAP should not exist if not support "
4578 "1-setting enable EPT VM-execution control\n");
4579 }
4580 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4581 vmx_capability.vpid) {
4582 vmx_capability.vpid = 0;
4583 pr_warn_once("VPID CAP should not exist if not support "
4584 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004585 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004586
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004587 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004588#ifdef CONFIG_X86_64
4589 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4590#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004591 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004592 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004593 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4594 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004595 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004596
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004597 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4598 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4599 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004600 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4601 &_pin_based_exec_control) < 0)
4602 return -EIO;
4603
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004604 if (cpu_has_broken_vmx_preemption_timer())
4605 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004606 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004607 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004608 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4609
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004610 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004611 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4613 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004614 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004615
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004616 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004617
4618 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4619 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004620 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004621
4622#ifdef CONFIG_X86_64
4623 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4624 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004625 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004626#endif
4627
4628 /* Require Write-Back (WB) memory type for VMCS accesses. */
4629 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004630 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631
Yang, Sheng002c7f72007-07-31 14:23:01 +03004632 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004633 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004634 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004635
Liran Alon2307af12018-06-29 22:59:04 +03004636 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637
Yang, Sheng002c7f72007-07-31 14:23:01 +03004638 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4639 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004640 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004641 vmcs_conf->vmexit_ctrl = _vmexit_control;
4642 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004643
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004644 if (static_branch_unlikely(&enable_evmcs))
4645 evmcs_sanitize_exec_ctrls(vmcs_conf);
4646
Avi Kivity110312c2010-12-21 12:54:20 +02004647 cpu_has_load_ia32_efer =
4648 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4649 VM_ENTRY_LOAD_IA32_EFER)
4650 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4651 VM_EXIT_LOAD_IA32_EFER);
4652
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004653 cpu_has_load_perf_global_ctrl =
4654 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4655 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4656 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4657 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4658
4659 /*
4660 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004661 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004662 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4663 *
4664 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4665 *
4666 * AAK155 (model 26)
4667 * AAP115 (model 30)
4668 * AAT100 (model 37)
4669 * BC86,AAY89,BD102 (model 44)
4670 * BA97 (model 46)
4671 *
4672 */
4673 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4674 switch (boot_cpu_data.x86_model) {
4675 case 26:
4676 case 30:
4677 case 37:
4678 case 44:
4679 case 46:
4680 cpu_has_load_perf_global_ctrl = false;
4681 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4682 "does not work properly. Using workaround\n");
4683 break;
4684 default:
4685 break;
4686 }
4687 }
4688
Borislav Petkov782511b2016-04-04 22:25:03 +02004689 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004690 rdmsrl(MSR_IA32_XSS, host_xss);
4691
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004692 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004693}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004694
Liran Alon491a6032018-06-23 02:35:12 +03004695static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696{
4697 int node = cpu_to_node(cpu);
4698 struct page *pages;
4699 struct vmcs *vmcs;
4700
Vlastimil Babka96db8002015-09-08 15:03:50 -07004701 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702 if (!pages)
4703 return NULL;
4704 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004705 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004706
4707 /* KVM supports Enlightened VMCS v1 only */
4708 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004709 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004710 else
Liran Alon392b2f22018-06-23 02:35:01 +03004711 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004712
Liran Alon491a6032018-06-23 02:35:12 +03004713 if (shadow)
4714 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715 return vmcs;
4716}
4717
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718static void free_vmcs(struct vmcs *vmcs)
4719{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004720 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721}
4722
Nadav Har'Eld462b812011-05-24 15:26:10 +03004723/*
4724 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4725 */
4726static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4727{
4728 if (!loaded_vmcs->vmcs)
4729 return;
4730 loaded_vmcs_clear(loaded_vmcs);
4731 free_vmcs(loaded_vmcs->vmcs);
4732 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004733 if (loaded_vmcs->msr_bitmap)
4734 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004735 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004736}
4737
Liran Alon491a6032018-06-23 02:35:12 +03004738static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004739{
Liran Alon491a6032018-06-23 02:35:12 +03004740 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004741}
4742
4743static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4744{
Liran Alon491a6032018-06-23 02:35:12 +03004745 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004746 if (!loaded_vmcs->vmcs)
4747 return -ENOMEM;
4748
4749 loaded_vmcs->shadow_vmcs = NULL;
4750 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004751
4752 if (cpu_has_vmx_msr_bitmap()) {
4753 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4754 if (!loaded_vmcs->msr_bitmap)
4755 goto out_vmcs;
4756 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004757
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004758 if (IS_ENABLED(CONFIG_HYPERV) &&
4759 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004760 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4761 struct hv_enlightened_vmcs *evmcs =
4762 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4763
4764 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4765 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004766 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004767
4768 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4769
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004770 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004771
4772out_vmcs:
4773 free_loaded_vmcs(loaded_vmcs);
4774 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004775}
4776
Sam Ravnborg39959582007-06-01 00:47:13 -07004777static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778{
4779 int cpu;
4780
Zachary Amsden3230bb42009-09-29 11:38:37 -10004781 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004782 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004783 per_cpu(vmxarea, cpu) = NULL;
4784 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785}
4786
Jim Mattsond37f4262017-12-22 12:12:16 -08004787enum vmcs_field_width {
4788 VMCS_FIELD_WIDTH_U16 = 0,
4789 VMCS_FIELD_WIDTH_U64 = 1,
4790 VMCS_FIELD_WIDTH_U32 = 2,
4791 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004792};
4793
Jim Mattsond37f4262017-12-22 12:12:16 -08004794static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004795{
4796 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004797 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004798 return (field >> 13) & 0x3 ;
4799}
4800
4801static inline int vmcs_field_readonly(unsigned long field)
4802{
4803 return (((field >> 10) & 0x3) == 1);
4804}
4805
Bandan Dasfe2b2012014-04-21 15:20:14 -04004806static void init_vmcs_shadow_fields(void)
4807{
4808 int i, j;
4809
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004810 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4811 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004812 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004813 (i + 1 == max_shadow_read_only_fields ||
4814 shadow_read_only_fields[i + 1] != field + 1))
4815 pr_err("Missing field from shadow_read_only_field %x\n",
4816 field + 1);
4817
4818 clear_bit(field, vmx_vmread_bitmap);
4819#ifdef CONFIG_X86_64
4820 if (field & 1)
4821 continue;
4822#endif
4823 if (j < i)
4824 shadow_read_only_fields[j] = field;
4825 j++;
4826 }
4827 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004828
4829 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004830 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004831 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004832 (i + 1 == max_shadow_read_write_fields ||
4833 shadow_read_write_fields[i + 1] != field + 1))
4834 pr_err("Missing field from shadow_read_write_field %x\n",
4835 field + 1);
4836
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004837 /*
4838 * PML and the preemption timer can be emulated, but the
4839 * processor cannot vmwrite to fields that don't exist
4840 * on bare metal.
4841 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004842 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004843 case GUEST_PML_INDEX:
4844 if (!cpu_has_vmx_pml())
4845 continue;
4846 break;
4847 case VMX_PREEMPTION_TIMER_VALUE:
4848 if (!cpu_has_vmx_preemption_timer())
4849 continue;
4850 break;
4851 case GUEST_INTR_STATUS:
4852 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004853 continue;
4854 break;
4855 default:
4856 break;
4857 }
4858
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004859 clear_bit(field, vmx_vmwrite_bitmap);
4860 clear_bit(field, vmx_vmread_bitmap);
4861#ifdef CONFIG_X86_64
4862 if (field & 1)
4863 continue;
4864#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004865 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004866 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004867 j++;
4868 }
4869 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004870}
4871
Avi Kivity6aa8b732006-12-10 02:21:36 -08004872static __init int alloc_kvm_area(void)
4873{
4874 int cpu;
4875
Zachary Amsden3230bb42009-09-29 11:38:37 -10004876 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 struct vmcs *vmcs;
4878
Liran Alon491a6032018-06-23 02:35:12 +03004879 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880 if (!vmcs) {
4881 free_kvm_area();
4882 return -ENOMEM;
4883 }
4884
Liran Alon2307af12018-06-29 22:59:04 +03004885 /*
4886 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4887 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4888 * revision_id reported by MSR_IA32_VMX_BASIC.
4889 *
4890 * However, even though not explictly documented by
4891 * TLFS, VMXArea passed as VMXON argument should
4892 * still be marked with revision_id reported by
4893 * physical CPU.
4894 */
4895 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004896 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004897
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898 per_cpu(vmxarea, cpu) = vmcs;
4899 }
4900 return 0;
4901}
4902
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004903static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004904 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004905{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004906 if (!emulate_invalid_guest_state) {
4907 /*
4908 * CS and SS RPL should be equal during guest entry according
4909 * to VMX spec, but in reality it is not always so. Since vcpu
4910 * is in the middle of the transition from real mode to
4911 * protected mode it is safe to assume that RPL 0 is a good
4912 * default value.
4913 */
4914 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004915 save->selector &= ~SEGMENT_RPL_MASK;
4916 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004917 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004919 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004920}
4921
4922static void enter_pmode(struct kvm_vcpu *vcpu)
4923{
4924 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004925 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926
Gleb Natapovd99e4152012-12-20 16:57:45 +02004927 /*
4928 * Update real mode segment cache. It may be not up-to-date if sement
4929 * register was written while vcpu was in a guest mode.
4930 */
4931 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4932 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4933 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4934 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4935 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4936 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4937
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004938 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939
Avi Kivity2fb92db2011-04-27 19:42:18 +03004940 vmx_segment_cache_clear(vmx);
4941
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004942 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004943
4944 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004945 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4946 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947 vmcs_writel(GUEST_RFLAGS, flags);
4948
Rusty Russell66aee912007-07-17 23:34:16 +10004949 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4950 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951
4952 update_exception_bitmap(vcpu);
4953
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004954 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4955 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4956 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4957 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4958 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4959 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960}
4961
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004962static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963{
Mathias Krause772e0312012-08-30 01:30:19 +02004964 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004965 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966
Gleb Natapovd99e4152012-12-20 16:57:45 +02004967 var.dpl = 0x3;
4968 if (seg == VCPU_SREG_CS)
4969 var.type = 0x3;
4970
4971 if (!emulate_invalid_guest_state) {
4972 var.selector = var.base >> 4;
4973 var.base = var.base & 0xffff0;
4974 var.limit = 0xffff;
4975 var.g = 0;
4976 var.db = 0;
4977 var.present = 1;
4978 var.s = 1;
4979 var.l = 0;
4980 var.unusable = 0;
4981 var.type = 0x3;
4982 var.avl = 0;
4983 if (save->base & 0xf)
4984 printk_once(KERN_WARNING "kvm: segment base is not "
4985 "paragraph aligned when entering "
4986 "protected mode (seg=%d)", seg);
4987 }
4988
4989 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004990 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004991 vmcs_write32(sf->limit, var.limit);
4992 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004993}
4994
4995static void enter_rmode(struct kvm_vcpu *vcpu)
4996{
4997 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004998 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07004999 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005000
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005001 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5002 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5003 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5004 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5005 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005006 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5007 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005008
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005009 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010
Gleb Natapov776e58e2011-03-13 12:34:27 +02005011 /*
5012 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005013 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005014 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005015 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005016 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5017 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005018
Avi Kivity2fb92db2011-04-27 19:42:18 +03005019 vmx_segment_cache_clear(vmx);
5020
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005021 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5024
5025 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005026 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005027
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005028 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029
5030 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005031 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 update_exception_bitmap(vcpu);
5033
Gleb Natapovd99e4152012-12-20 16:57:45 +02005034 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5035 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5036 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5037 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5038 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5039 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005040
Eddie Dong8668a3c2007-10-10 14:26:45 +08005041 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042}
5043
Amit Shah401d10d2009-02-20 22:53:37 +05305044static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5045{
5046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005047 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5048
5049 if (!msr)
5050 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305051
Avi Kivityf6801df2010-01-21 15:31:50 +02005052 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305053 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005054 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305055 msr->data = efer;
5056 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005057 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305058
5059 msr->data = efer & ~EFER_LME;
5060 }
5061 setup_msrs(vmx);
5062}
5063
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005064#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065
5066static void enter_lmode(struct kvm_vcpu *vcpu)
5067{
5068 u32 guest_tr_ar;
5069
Avi Kivity2fb92db2011-04-27 19:42:18 +03005070 vmx_segment_cache_clear(to_vmx(vcpu));
5071
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005073 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005074 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5075 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005077 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5078 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005079 }
Avi Kivityda38f432010-07-06 11:30:49 +03005080 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081}
5082
5083static void exit_lmode(struct kvm_vcpu *vcpu)
5084{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005085 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005086 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005087}
5088
5089#endif
5090
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005091static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5092 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005093{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005094 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005095 if (!VALID_PAGE(vcpu->arch.mmu->root_hpa))
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005096 return;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02005097 ept_sync_context(construct_eptp(vcpu,
5098 vcpu->arch.mmu->root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005099 } else {
5100 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005101 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005102}
5103
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005104static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005105{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005106 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005107}
5108
Junaid Shahidfaff8752018-06-29 13:10:05 -07005109static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5110{
5111 int vpid = to_vmx(vcpu)->vpid;
5112
5113 if (!vpid_sync_vcpu_addr(vpid, addr))
5114 vpid_sync_context(vpid);
5115
5116 /*
5117 * If VPIDs are not supported or enabled, then the above is a no-op.
5118 * But we don't really need a TLB flush in that case anyway, because
5119 * each VM entry/exit includes an implicit flush when VPID is 0.
5120 */
5121}
5122
Avi Kivitye8467fd2009-12-29 18:43:06 +02005123static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5124{
5125 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5126
5127 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5128 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5129}
5130
Avi Kivityaff48ba2010-12-05 18:56:11 +02005131static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5132{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005133 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005134 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5135 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5136}
5137
Anthony Liguori25c4c272007-04-27 09:29:21 +03005138static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005139{
Avi Kivityfc78f512009-12-07 12:16:48 +02005140 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5141
5142 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5143 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005144}
5145
Sheng Yang14394422008-04-28 12:24:45 +08005146static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5147{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005148 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5149
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005150 if (!test_bit(VCPU_EXREG_PDPTR,
5151 (unsigned long *)&vcpu->arch.regs_dirty))
5152 return;
5153
Sheng Yang14394422008-04-28 12:24:45 +08005154 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005155 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5156 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5157 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5158 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005159 }
5160}
5161
Avi Kivity8f5d5492009-05-31 18:41:29 +03005162static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5163{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005164 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5165
Avi Kivity8f5d5492009-05-31 18:41:29 +03005166 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005167 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5168 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5169 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5170 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005171 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005172
5173 __set_bit(VCPU_EXREG_PDPTR,
5174 (unsigned long *)&vcpu->arch.regs_avail);
5175 __set_bit(VCPU_EXREG_PDPTR,
5176 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005177}
5178
David Matlack38991522016-11-29 18:14:08 -08005179static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5180{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005181 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5182 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005183 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5184
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005185 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005186 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5187 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5188 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5189
5190 return fixed_bits_valid(val, fixed0, fixed1);
5191}
5192
5193static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5194{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005195 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5196 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005197
5198 return fixed_bits_valid(val, fixed0, fixed1);
5199}
5200
5201static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5202{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005203 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5204 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005205
5206 return fixed_bits_valid(val, fixed0, fixed1);
5207}
5208
5209/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5210#define nested_guest_cr4_valid nested_cr4_valid
5211#define nested_host_cr4_valid nested_cr4_valid
5212
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005213static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005214
5215static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5216 unsigned long cr0,
5217 struct kvm_vcpu *vcpu)
5218{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005219 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5220 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005221 if (!(cr0 & X86_CR0_PG)) {
5222 /* From paging/starting to nonpaging */
5223 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005224 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005225 (CPU_BASED_CR3_LOAD_EXITING |
5226 CPU_BASED_CR3_STORE_EXITING));
5227 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005228 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005229 } else if (!is_paging(vcpu)) {
5230 /* From nonpaging to paging */
5231 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005232 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005233 ~(CPU_BASED_CR3_LOAD_EXITING |
5234 CPU_BASED_CR3_STORE_EXITING));
5235 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005236 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005237 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005238
5239 if (!(cr0 & X86_CR0_WP))
5240 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005241}
5242
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5244{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005245 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005246 unsigned long hw_cr0;
5247
Sean Christopherson3de63472018-07-13 08:42:30 -07005248 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005249 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005250 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005251 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005252 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005253
Gleb Natapov218e7632013-01-21 15:36:45 +02005254 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5255 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005256
Gleb Natapov218e7632013-01-21 15:36:45 +02005257 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5258 enter_rmode(vcpu);
5259 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005261#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005262 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005263 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005264 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005265 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266 exit_lmode(vcpu);
5267 }
5268#endif
5269
Sean Christophersonb4d18512018-03-05 12:04:40 -08005270 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005271 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5272
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005274 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005275 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005276
5277 /* depends on vcpu->arch.cr0 to be set to a new value */
5278 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279}
5280
Yu Zhang855feb62017-08-24 20:27:55 +08005281static int get_ept_level(struct kvm_vcpu *vcpu)
5282{
5283 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5284 return 5;
5285 return 4;
5286}
5287
Peter Feiner995f00a2017-06-30 17:26:32 -07005288static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005289{
Yu Zhang855feb62017-08-24 20:27:55 +08005290 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005291
Yu Zhang855feb62017-08-24 20:27:55 +08005292 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005293
Peter Feiner995f00a2017-06-30 17:26:32 -07005294 if (enable_ept_ad_bits &&
5295 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005296 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005297 eptp |= (root_hpa & PAGE_MASK);
5298
5299 return eptp;
5300}
5301
Avi Kivity6aa8b732006-12-10 02:21:36 -08005302static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5303{
Tianyu Lan877ad952018-07-19 08:40:23 +00005304 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005305 unsigned long guest_cr3;
5306 u64 eptp;
5307
5308 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005309 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005310 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005311 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005312
5313 if (kvm_x86_ops->tlb_remote_flush) {
5314 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5315 to_vmx(vcpu)->ept_pointer = eptp;
5316 to_kvm_vmx(kvm)->ept_pointers_match
5317 = EPT_POINTERS_CHECK;
5318 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5319 }
5320
Sean Christophersone90008d2018-03-05 12:04:37 -08005321 if (enable_unrestricted_guest || is_paging(vcpu) ||
5322 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005323 guest_cr3 = kvm_read_cr3(vcpu);
5324 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005325 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005326 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005327 }
5328
Sheng Yang14394422008-04-28 12:24:45 +08005329 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005330}
5331
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005332static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005334 /*
5335 * Pass through host's Machine Check Enable value to hw_cr4, which
5336 * is in force while we are in guest mode. Do not let guests control
5337 * this bit, even if host CR4.MCE == 0.
5338 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005339 unsigned long hw_cr4;
5340
5341 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5342 if (enable_unrestricted_guest)
5343 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5344 else if (to_vmx(vcpu)->rmode.vm86_active)
5345 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5346 else
5347 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005348
Sean Christopherson64f7a112018-04-30 10:01:06 -07005349 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5350 if (cr4 & X86_CR4_UMIP) {
5351 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005352 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005353 hw_cr4 &= ~X86_CR4_UMIP;
5354 } else if (!is_guest_mode(vcpu) ||
5355 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5356 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5357 SECONDARY_EXEC_DESC);
5358 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005359
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005360 if (cr4 & X86_CR4_VMXE) {
5361 /*
5362 * To use VMXON (and later other VMX instructions), a guest
5363 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5364 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005365 * is here. We operate under the default treatment of SMM,
5366 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005367 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005368 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005369 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005370 }
David Matlack38991522016-11-29 18:14:08 -08005371
5372 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005373 return 1;
5374
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005375 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005376
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005377 if (!enable_unrestricted_guest) {
5378 if (enable_ept) {
5379 if (!is_paging(vcpu)) {
5380 hw_cr4 &= ~X86_CR4_PAE;
5381 hw_cr4 |= X86_CR4_PSE;
5382 } else if (!(cr4 & X86_CR4_PAE)) {
5383 hw_cr4 &= ~X86_CR4_PAE;
5384 }
5385 }
5386
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005387 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005388 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5389 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5390 * to be manually disabled when guest switches to non-paging
5391 * mode.
5392 *
5393 * If !enable_unrestricted_guest, the CPU is always running
5394 * with CR0.PG=1 and CR4 needs to be modified.
5395 * If enable_unrestricted_guest, the CPU automatically
5396 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005397 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005398 if (!is_paging(vcpu))
5399 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5400 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005401
Sheng Yang14394422008-04-28 12:24:45 +08005402 vmcs_writel(CR4_READ_SHADOW, cr4);
5403 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005404 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005405}
5406
Avi Kivity6aa8b732006-12-10 02:21:36 -08005407static void vmx_get_segment(struct kvm_vcpu *vcpu,
5408 struct kvm_segment *var, int seg)
5409{
Avi Kivitya9179492011-01-03 14:28:52 +02005410 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005411 u32 ar;
5412
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005413 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005414 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005415 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005416 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005417 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005418 var->base = vmx_read_guest_seg_base(vmx, seg);
5419 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5420 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005421 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005422 var->base = vmx_read_guest_seg_base(vmx, seg);
5423 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5424 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5425 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005426 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005427 var->type = ar & 15;
5428 var->s = (ar >> 4) & 1;
5429 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005430 /*
5431 * Some userspaces do not preserve unusable property. Since usable
5432 * segment has to be present according to VMX spec we can use present
5433 * property to amend userspace bug by making unusable segment always
5434 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5435 * segment as unusable.
5436 */
5437 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005438 var->avl = (ar >> 12) & 1;
5439 var->l = (ar >> 13) & 1;
5440 var->db = (ar >> 14) & 1;
5441 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442}
5443
Avi Kivitya9179492011-01-03 14:28:52 +02005444static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5445{
Avi Kivitya9179492011-01-03 14:28:52 +02005446 struct kvm_segment s;
5447
5448 if (to_vmx(vcpu)->rmode.vm86_active) {
5449 vmx_get_segment(vcpu, &s, seg);
5450 return s.base;
5451 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005452 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005453}
5454
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005455static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005456{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005457 struct vcpu_vmx *vmx = to_vmx(vcpu);
5458
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005459 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005460 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005461 else {
5462 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005463 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005464 }
Avi Kivity69c73022011-03-07 15:26:44 +02005465}
5466
Avi Kivity653e3102007-05-07 10:55:37 +03005467static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005469 u32 ar;
5470
Avi Kivityf0495f92012-06-07 17:06:10 +03005471 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472 ar = 1 << 16;
5473 else {
5474 ar = var->type & 15;
5475 ar |= (var->s & 1) << 4;
5476 ar |= (var->dpl & 3) << 5;
5477 ar |= (var->present & 1) << 7;
5478 ar |= (var->avl & 1) << 12;
5479 ar |= (var->l & 1) << 13;
5480 ar |= (var->db & 1) << 14;
5481 ar |= (var->g & 1) << 15;
5482 }
Avi Kivity653e3102007-05-07 10:55:37 +03005483
5484 return ar;
5485}
5486
5487static void vmx_set_segment(struct kvm_vcpu *vcpu,
5488 struct kvm_segment *var, int seg)
5489{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005491 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005492
Avi Kivity2fb92db2011-04-27 19:42:18 +03005493 vmx_segment_cache_clear(vmx);
5494
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005495 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5496 vmx->rmode.segs[seg] = *var;
5497 if (seg == VCPU_SREG_TR)
5498 vmcs_write16(sf->selector, var->selector);
5499 else if (var->s)
5500 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005501 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005502 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005503
Avi Kivity653e3102007-05-07 10:55:37 +03005504 vmcs_writel(sf->base, var->base);
5505 vmcs_write32(sf->limit, var->limit);
5506 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005507
5508 /*
5509 * Fix the "Accessed" bit in AR field of segment registers for older
5510 * qemu binaries.
5511 * IA32 arch specifies that at the time of processor reset the
5512 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005513 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005514 * state vmexit when "unrestricted guest" mode is turned on.
5515 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5516 * tree. Newer qemu binaries with that qemu fix would not need this
5517 * kvm hack.
5518 */
5519 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005520 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005521
Gleb Natapovf924d662012-12-12 19:10:55 +02005522 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005523
5524out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005525 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005526}
5527
Avi Kivity6aa8b732006-12-10 02:21:36 -08005528static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5529{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005530 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005531
5532 *db = (ar >> 14) & 1;
5533 *l = (ar >> 13) & 1;
5534}
5535
Gleb Natapov89a27f42010-02-16 10:51:48 +02005536static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005537{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005538 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5539 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540}
5541
Gleb Natapov89a27f42010-02-16 10:51:48 +02005542static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005544 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5545 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546}
5547
Gleb Natapov89a27f42010-02-16 10:51:48 +02005548static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005550 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5551 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552}
5553
Gleb Natapov89a27f42010-02-16 10:51:48 +02005554static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005556 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5557 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558}
5559
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005560static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5561{
5562 struct kvm_segment var;
5563 u32 ar;
5564
5565 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005566 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005567 if (seg == VCPU_SREG_CS)
5568 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005569 ar = vmx_segment_access_rights(&var);
5570
5571 if (var.base != (var.selector << 4))
5572 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005573 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005574 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005575 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005576 return false;
5577
5578 return true;
5579}
5580
5581static bool code_segment_valid(struct kvm_vcpu *vcpu)
5582{
5583 struct kvm_segment cs;
5584 unsigned int cs_rpl;
5585
5586 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005587 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005588
Avi Kivity1872a3f2009-01-04 23:26:52 +02005589 if (cs.unusable)
5590 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005591 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005592 return false;
5593 if (!cs.s)
5594 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005595 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005596 if (cs.dpl > cs_rpl)
5597 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005598 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005599 if (cs.dpl != cs_rpl)
5600 return false;
5601 }
5602 if (!cs.present)
5603 return false;
5604
5605 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5606 return true;
5607}
5608
5609static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5610{
5611 struct kvm_segment ss;
5612 unsigned int ss_rpl;
5613
5614 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005615 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005616
Avi Kivity1872a3f2009-01-04 23:26:52 +02005617 if (ss.unusable)
5618 return true;
5619 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005620 return false;
5621 if (!ss.s)
5622 return false;
5623 if (ss.dpl != ss_rpl) /* DPL != RPL */
5624 return false;
5625 if (!ss.present)
5626 return false;
5627
5628 return true;
5629}
5630
5631static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5632{
5633 struct kvm_segment var;
5634 unsigned int rpl;
5635
5636 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005637 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005638
Avi Kivity1872a3f2009-01-04 23:26:52 +02005639 if (var.unusable)
5640 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005641 if (!var.s)
5642 return false;
5643 if (!var.present)
5644 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005645 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005646 if (var.dpl < rpl) /* DPL < RPL */
5647 return false;
5648 }
5649
5650 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5651 * rights flags
5652 */
5653 return true;
5654}
5655
5656static bool tr_valid(struct kvm_vcpu *vcpu)
5657{
5658 struct kvm_segment tr;
5659
5660 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5661
Avi Kivity1872a3f2009-01-04 23:26:52 +02005662 if (tr.unusable)
5663 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005664 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005665 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005666 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005667 return false;
5668 if (!tr.present)
5669 return false;
5670
5671 return true;
5672}
5673
5674static bool ldtr_valid(struct kvm_vcpu *vcpu)
5675{
5676 struct kvm_segment ldtr;
5677
5678 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5679
Avi Kivity1872a3f2009-01-04 23:26:52 +02005680 if (ldtr.unusable)
5681 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005682 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005683 return false;
5684 if (ldtr.type != 2)
5685 return false;
5686 if (!ldtr.present)
5687 return false;
5688
5689 return true;
5690}
5691
5692static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5693{
5694 struct kvm_segment cs, ss;
5695
5696 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5697 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5698
Nadav Amitb32a9912015-03-29 16:33:04 +03005699 return ((cs.selector & SEGMENT_RPL_MASK) ==
5700 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005701}
5702
5703/*
5704 * Check if guest state is valid. Returns true if valid, false if
5705 * not.
5706 * We assume that registers are always usable
5707 */
5708static bool guest_state_valid(struct kvm_vcpu *vcpu)
5709{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005710 if (enable_unrestricted_guest)
5711 return true;
5712
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005713 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005714 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005715 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5716 return false;
5717 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5718 return false;
5719 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5720 return false;
5721 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5722 return false;
5723 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5724 return false;
5725 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5726 return false;
5727 } else {
5728 /* protected mode guest state checks */
5729 if (!cs_ss_rpl_check(vcpu))
5730 return false;
5731 if (!code_segment_valid(vcpu))
5732 return false;
5733 if (!stack_segment_valid(vcpu))
5734 return false;
5735 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5736 return false;
5737 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5738 return false;
5739 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5740 return false;
5741 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5742 return false;
5743 if (!tr_valid(vcpu))
5744 return false;
5745 if (!ldtr_valid(vcpu))
5746 return false;
5747 }
5748 /* TODO:
5749 * - Add checks on RIP
5750 * - Add checks on RFLAGS
5751 */
5752
5753 return true;
5754}
5755
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005756static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5757{
5758 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5759}
5760
Mike Dayd77c26f2007-10-08 09:02:08 -04005761static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005762{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005763 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005764 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005765 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005766
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005767 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005768 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005769 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5770 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005771 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005772 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005773 r = kvm_write_guest_page(kvm, fn++, &data,
5774 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005775 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005776 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005777 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5778 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005779 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005780 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5781 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005782 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005783 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005784 r = kvm_write_guest_page(kvm, fn, &data,
5785 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5786 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005787out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005788 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005789 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005790}
5791
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005792static int init_rmode_identity_map(struct kvm *kvm)
5793{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005794 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005795 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005796 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005797 u32 tmp;
5798
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005799 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005800 mutex_lock(&kvm->slots_lock);
5801
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005802 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005803 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005804
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005805 if (!kvm_vmx->ept_identity_map_addr)
5806 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5807 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005808
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005809 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005810 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005811 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005812 goto out2;
5813
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005814 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005815 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5816 if (r < 0)
5817 goto out;
5818 /* Set up identity-mapping pagetable for EPT in real mode */
5819 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5820 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5821 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5822 r = kvm_write_guest_page(kvm, identity_map_pfn,
5823 &tmp, i * sizeof(tmp), sizeof(tmp));
5824 if (r < 0)
5825 goto out;
5826 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005827 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005828
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005829out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005830 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005831
5832out2:
5833 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005834 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005835}
5836
Avi Kivity6aa8b732006-12-10 02:21:36 -08005837static void seg_setup(int seg)
5838{
Mathias Krause772e0312012-08-30 01:30:19 +02005839 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005840 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005841
5842 vmcs_write16(sf->selector, 0);
5843 vmcs_writel(sf->base, 0);
5844 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005845 ar = 0x93;
5846 if (seg == VCPU_SREG_CS)
5847 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005848
5849 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850}
5851
Sheng Yangf78e0e22007-10-29 09:40:42 +08005852static int alloc_apic_access_page(struct kvm *kvm)
5853{
Xiao Guangrong44841412012-09-07 14:14:20 +08005854 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005855 int r = 0;
5856
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005857 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005858 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005859 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005860 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5861 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005862 if (r)
5863 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005864
Tang Chen73a6d942014-09-11 13:38:00 +08005865 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005866 if (is_error_page(page)) {
5867 r = -EFAULT;
5868 goto out;
5869 }
5870
Tang Chenc24ae0d2014-09-24 15:57:58 +08005871 /*
5872 * Do not pin the page in memory, so that memory hot-unplug
5873 * is able to migrate it.
5874 */
5875 put_page(page);
5876 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005877out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005878 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005879 return r;
5880}
5881
Wanpeng Li991e7a02015-09-16 17:30:05 +08005882static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005883{
5884 int vpid;
5885
Avi Kivity919818a2009-03-23 18:01:29 +02005886 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005887 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005888 spin_lock(&vmx_vpid_lock);
5889 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005890 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005891 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005892 else
5893 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005894 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005895 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005896}
5897
Wanpeng Li991e7a02015-09-16 17:30:05 +08005898static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005899{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005900 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005901 return;
5902 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005903 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005904 spin_unlock(&vmx_vpid_lock);
5905}
5906
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005907static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5908 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005909{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005910 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005911
5912 if (!cpu_has_vmx_msr_bitmap())
5913 return;
5914
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005915 if (static_branch_unlikely(&enable_evmcs))
5916 evmcs_touch_msr_bitmap();
5917
Sheng Yang25c5f222008-03-28 13:18:56 +08005918 /*
5919 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5920 * have the write-low and read-high bitmap offsets the wrong way round.
5921 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5922 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005923 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005924 if (type & MSR_TYPE_R)
5925 /* read-low */
5926 __clear_bit(msr, msr_bitmap + 0x000 / f);
5927
5928 if (type & MSR_TYPE_W)
5929 /* write-low */
5930 __clear_bit(msr, msr_bitmap + 0x800 / f);
5931
Sheng Yang25c5f222008-03-28 13:18:56 +08005932 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5933 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005934 if (type & MSR_TYPE_R)
5935 /* read-high */
5936 __clear_bit(msr, msr_bitmap + 0x400 / f);
5937
5938 if (type & MSR_TYPE_W)
5939 /* write-high */
5940 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5941
5942 }
5943}
5944
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005945static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5946 u32 msr, int type)
5947{
5948 int f = sizeof(unsigned long);
5949
5950 if (!cpu_has_vmx_msr_bitmap())
5951 return;
5952
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005953 if (static_branch_unlikely(&enable_evmcs))
5954 evmcs_touch_msr_bitmap();
5955
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005956 /*
5957 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5958 * have the write-low and read-high bitmap offsets the wrong way round.
5959 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5960 */
5961 if (msr <= 0x1fff) {
5962 if (type & MSR_TYPE_R)
5963 /* read-low */
5964 __set_bit(msr, msr_bitmap + 0x000 / f);
5965
5966 if (type & MSR_TYPE_W)
5967 /* write-low */
5968 __set_bit(msr, msr_bitmap + 0x800 / f);
5969
5970 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5971 msr &= 0x1fff;
5972 if (type & MSR_TYPE_R)
5973 /* read-high */
5974 __set_bit(msr, msr_bitmap + 0x400 / f);
5975
5976 if (type & MSR_TYPE_W)
5977 /* write-high */
5978 __set_bit(msr, msr_bitmap + 0xc00 / f);
5979
5980 }
5981}
5982
5983static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5984 u32 msr, int type, bool value)
5985{
5986 if (value)
5987 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5988 else
5989 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5990}
5991
Wincy Vanf2b93282015-02-03 23:56:03 +08005992/*
5993 * If a msr is allowed by L0, we should check whether it is allowed by L1.
5994 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
5995 */
5996static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
5997 unsigned long *msr_bitmap_nested,
5998 u32 msr, int type)
5999{
6000 int f = sizeof(unsigned long);
6001
Wincy Vanf2b93282015-02-03 23:56:03 +08006002 /*
6003 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6004 * have the write-low and read-high bitmap offsets the wrong way round.
6005 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6006 */
6007 if (msr <= 0x1fff) {
6008 if (type & MSR_TYPE_R &&
6009 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6010 /* read-low */
6011 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6012
6013 if (type & MSR_TYPE_W &&
6014 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6015 /* write-low */
6016 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6017
6018 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6019 msr &= 0x1fff;
6020 if (type & MSR_TYPE_R &&
6021 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6022 /* read-high */
6023 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6024
6025 if (type & MSR_TYPE_W &&
6026 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6027 /* write-high */
6028 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6029
6030 }
6031}
6032
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006033static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006034{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006035 u8 mode = 0;
6036
6037 if (cpu_has_secondary_exec_ctrls() &&
6038 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6039 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6040 mode |= MSR_BITMAP_MODE_X2APIC;
6041 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6042 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6043 }
6044
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006045 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006046}
6047
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006048#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6049
6050static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6051 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006052{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006053 int msr;
6054
6055 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6056 unsigned word = msr / BITS_PER_LONG;
6057 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6058 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006059 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006060
6061 if (mode & MSR_BITMAP_MODE_X2APIC) {
6062 /*
6063 * TPR reads and writes can be virtualized even if virtual interrupt
6064 * delivery is not in use.
6065 */
6066 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6067 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6068 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6069 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6070 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6071 }
6072 }
6073}
6074
6075static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6076{
6077 struct vcpu_vmx *vmx = to_vmx(vcpu);
6078 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6079 u8 mode = vmx_msr_bitmap_mode(vcpu);
6080 u8 changed = mode ^ vmx->msr_bitmap_mode;
6081
6082 if (!changed)
6083 return;
6084
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006085 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6086 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6087
6088 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006089}
6090
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006091static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006092{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006093 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006094}
6095
David Matlackc9f04402017-08-01 14:00:40 -07006096static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6097{
6098 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6099 gfn_t gfn;
6100
6101 /*
6102 * Don't need to mark the APIC access page dirty; it is never
6103 * written to by the CPU during APIC virtualization.
6104 */
6105
6106 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6107 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6108 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6109 }
6110
6111 if (nested_cpu_has_posted_intr(vmcs12)) {
6112 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6113 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6114 }
6115}
6116
6117
David Hildenbrand6342c502017-01-25 11:58:58 +01006118static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006119{
6120 struct vcpu_vmx *vmx = to_vmx(vcpu);
6121 int max_irr;
6122 void *vapic_page;
6123 u16 status;
6124
David Matlackc9f04402017-08-01 14:00:40 -07006125 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6126 return;
Wincy Van705699a2015-02-03 23:58:17 +08006127
David Matlackc9f04402017-08-01 14:00:40 -07006128 vmx->nested.pi_pending = false;
6129 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6130 return;
Wincy Van705699a2015-02-03 23:58:17 +08006131
David Matlackc9f04402017-08-01 14:00:40 -07006132 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6133 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006134 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006135 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6136 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006137 kunmap(vmx->nested.virtual_apic_page);
6138
6139 status = vmcs_read16(GUEST_INTR_STATUS);
6140 if ((u8)max_irr > ((u8)status & 0xff)) {
6141 status &= ~0xff;
6142 status |= (u8)max_irr;
6143 vmcs_write16(GUEST_INTR_STATUS, status);
6144 }
6145 }
David Matlackc9f04402017-08-01 14:00:40 -07006146
6147 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006148}
6149
Paolo Bonzini7e712682018-10-03 13:44:26 +02006150static u8 vmx_get_rvi(void)
6151{
6152 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6153}
6154
Liran Alone6c67d82018-09-04 10:56:52 +03006155static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6156{
6157 struct vcpu_vmx *vmx = to_vmx(vcpu);
6158 void *vapic_page;
6159 u32 vppr;
6160 int rvi;
6161
6162 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6163 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6164 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6165 return false;
6166
Paolo Bonzini7e712682018-10-03 13:44:26 +02006167 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006168
6169 vapic_page = kmap(vmx->nested.virtual_apic_page);
6170 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6171 kunmap(vmx->nested.virtual_apic_page);
6172
6173 return ((rvi & 0xf0) > (vppr & 0xf0));
6174}
6175
Wincy Van06a55242017-04-28 13:13:59 +08006176static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6177 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006178{
6179#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006180 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6181
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006182 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006183 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006184 * The vector of interrupt to be delivered to vcpu had
6185 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006186 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006187 * Following cases will be reached in this block, and
6188 * we always send a notification event in all cases as
6189 * explained below.
6190 *
6191 * Case 1: vcpu keeps in non-root mode. Sending a
6192 * notification event posts the interrupt to vcpu.
6193 *
6194 * Case 2: vcpu exits to root mode and is still
6195 * runnable. PIR will be synced to vIRR before the
6196 * next vcpu entry. Sending a notification event in
6197 * this case has no effect, as vcpu is not in root
6198 * mode.
6199 *
6200 * Case 3: vcpu exits to root mode and is blocked.
6201 * vcpu_block() has already synced PIR to vIRR and
6202 * never blocks vcpu if vIRR is not cleared. Therefore,
6203 * a blocked vcpu here does not wait for any requested
6204 * interrupts in PIR, and sending a notification event
6205 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006206 */
Feng Wu28b835d2015-09-18 22:29:54 +08006207
Wincy Van06a55242017-04-28 13:13:59 +08006208 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006209 return true;
6210 }
6211#endif
6212 return false;
6213}
6214
Wincy Van705699a2015-02-03 23:58:17 +08006215static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6216 int vector)
6217{
6218 struct vcpu_vmx *vmx = to_vmx(vcpu);
6219
6220 if (is_guest_mode(vcpu) &&
6221 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006222 /*
6223 * If a posted intr is not recognized by hardware,
6224 * we will accomplish it in the next vmentry.
6225 */
6226 vmx->nested.pi_pending = true;
6227 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006228 /* the PIR and ON have been set by L1. */
6229 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6230 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006231 return 0;
6232 }
6233 return -1;
6234}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006235/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006236 * Send interrupt to vcpu via posted interrupt way.
6237 * 1. If target vcpu is running(non-root mode), send posted interrupt
6238 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6239 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6240 * interrupt from PIR in next vmentry.
6241 */
6242static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6243{
6244 struct vcpu_vmx *vmx = to_vmx(vcpu);
6245 int r;
6246
Wincy Van705699a2015-02-03 23:58:17 +08006247 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6248 if (!r)
6249 return;
6250
Yang Zhanga20ed542013-04-11 19:25:15 +08006251 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6252 return;
6253
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006254 /* If a previous notification has sent the IPI, nothing to do. */
6255 if (pi_test_and_set_on(&vmx->pi_desc))
6256 return;
6257
Wincy Van06a55242017-04-28 13:13:59 +08006258 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006259 kvm_vcpu_kick(vcpu);
6260}
6261
Avi Kivity6aa8b732006-12-10 02:21:36 -08006262/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006263 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6264 * will not change in the lifetime of the guest.
6265 * Note that host-state that does change is set elsewhere. E.g., host-state
6266 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6267 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006268static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006269{
6270 u32 low32, high32;
6271 unsigned long tmpl;
6272 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006273 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006274
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006275 cr0 = read_cr0();
6276 WARN_ON(cr0 & X86_CR0_TS);
6277 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006278
6279 /*
6280 * Save the most likely value for this task's CR3 in the VMCS.
6281 * We can't use __get_current_cr3_fast() because we're not atomic.
6282 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006283 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006284 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006285 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006286
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006287 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006288 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006289 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006290 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006291
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006292 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006293#ifdef CONFIG_X86_64
6294 /*
6295 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006296 * vmx_prepare_switch_to_host(), in case userspace uses
6297 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006298 */
6299 vmcs_write16(HOST_DS_SELECTOR, 0);
6300 vmcs_write16(HOST_ES_SELECTOR, 0);
6301#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006302 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6303 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006304#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006305 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6306 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6307
Juergen Gross87930012017-09-04 12:25:27 +02006308 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006309 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006310 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006311
Avi Kivity83287ea422012-09-16 15:10:57 +03006312 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006313
6314 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6315 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6316 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6317 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6318
6319 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6320 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6321 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6322 }
Sean Christopherson5a5e8a12018-09-26 09:23:56 -07006323
6324 if (cpu_has_load_ia32_efer)
6325 vmcs_write64(HOST_IA32_EFER, host_efer);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006326}
6327
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006328static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6329{
6330 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6331 if (enable_ept)
6332 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006333 if (is_guest_mode(&vmx->vcpu))
6334 vmx->vcpu.arch.cr4_guest_owned_bits &=
6335 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006336 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6337}
6338
Yang Zhang01e439b2013-04-11 19:25:12 +08006339static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6340{
6341 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6342
Andrey Smetanind62caab2015-11-10 15:36:33 +03006343 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006344 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006345
6346 if (!enable_vnmi)
6347 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6348
Yunhong Jiang64672c92016-06-13 14:19:59 -07006349 /* Enable the preemption timer dynamically */
6350 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006351 return pin_based_exec_ctrl;
6352}
6353
Andrey Smetanind62caab2015-11-10 15:36:33 +03006354static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6355{
6356 struct vcpu_vmx *vmx = to_vmx(vcpu);
6357
6358 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006359 if (cpu_has_secondary_exec_ctrls()) {
6360 if (kvm_vcpu_apicv_active(vcpu))
6361 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6362 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6363 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6364 else
6365 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6366 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6367 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6368 }
6369
6370 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006371 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006372}
6373
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006374static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6375{
6376 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006377
6378 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6379 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6380
Paolo Bonzini35754c92015-07-29 12:05:37 +02006381 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006382 exec_control &= ~CPU_BASED_TPR_SHADOW;
6383#ifdef CONFIG_X86_64
6384 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6385 CPU_BASED_CR8_LOAD_EXITING;
6386#endif
6387 }
6388 if (!enable_ept)
6389 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6390 CPU_BASED_CR3_LOAD_EXITING |
6391 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006392 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6393 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6394 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006395 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6396 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006397 return exec_control;
6398}
6399
Jim Mattson45ec3682017-08-23 16:32:04 -07006400static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006401{
Jim Mattson45ec3682017-08-23 16:32:04 -07006402 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006403 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006404}
6405
Jim Mattson75f4fc82017-08-23 16:32:03 -07006406static bool vmx_rdseed_supported(void)
6407{
6408 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006409 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006410}
6411
Paolo Bonzini80154d72017-08-24 13:55:35 +02006412static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006413{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006414 struct kvm_vcpu *vcpu = &vmx->vcpu;
6415
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006416 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006417
Paolo Bonzini80154d72017-08-24 13:55:35 +02006418 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006419 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6420 if (vmx->vpid == 0)
6421 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6422 if (!enable_ept) {
6423 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6424 enable_unrestricted_guest = 0;
6425 }
6426 if (!enable_unrestricted_guest)
6427 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006428 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006429 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006430 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006431 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6432 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006433 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006434
6435 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6436 * in vmx_set_cr4. */
6437 exec_control &= ~SECONDARY_EXEC_DESC;
6438
Abel Gordonabc4fc52013-04-18 14:35:25 +03006439 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6440 (handle_vmptrld).
6441 We can NOT enable shadow_vmcs here because we don't have yet
6442 a current VMCS12
6443 */
6444 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006445
6446 if (!enable_pml)
6447 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006448
Paolo Bonzini3db13482017-08-24 14:48:03 +02006449 if (vmx_xsaves_supported()) {
6450 /* Exposing XSAVES only when XSAVE is exposed */
6451 bool xsaves_enabled =
6452 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6453 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6454
6455 if (!xsaves_enabled)
6456 exec_control &= ~SECONDARY_EXEC_XSAVES;
6457
6458 if (nested) {
6459 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006460 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006461 SECONDARY_EXEC_XSAVES;
6462 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006463 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006464 ~SECONDARY_EXEC_XSAVES;
6465 }
6466 }
6467
Paolo Bonzini80154d72017-08-24 13:55:35 +02006468 if (vmx_rdtscp_supported()) {
6469 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6470 if (!rdtscp_enabled)
6471 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6472
6473 if (nested) {
6474 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006475 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006476 SECONDARY_EXEC_RDTSCP;
6477 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006478 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006479 ~SECONDARY_EXEC_RDTSCP;
6480 }
6481 }
6482
6483 if (vmx_invpcid_supported()) {
6484 /* Exposing INVPCID only when PCID is exposed */
6485 bool invpcid_enabled =
6486 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6487 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6488
6489 if (!invpcid_enabled) {
6490 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6491 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6492 }
6493
6494 if (nested) {
6495 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006496 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006497 SECONDARY_EXEC_ENABLE_INVPCID;
6498 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006499 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006500 ~SECONDARY_EXEC_ENABLE_INVPCID;
6501 }
6502 }
6503
Jim Mattson45ec3682017-08-23 16:32:04 -07006504 if (vmx_rdrand_supported()) {
6505 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6506 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006507 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006508
6509 if (nested) {
6510 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006511 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006512 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006513 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006514 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006515 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006516 }
6517 }
6518
Jim Mattson75f4fc82017-08-23 16:32:03 -07006519 if (vmx_rdseed_supported()) {
6520 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6521 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006522 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006523
6524 if (nested) {
6525 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006526 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006527 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006528 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006529 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006530 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006531 }
6532 }
6533
Paolo Bonzini80154d72017-08-24 13:55:35 +02006534 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006535}
6536
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006537static void ept_set_mmio_spte_mask(void)
6538{
6539 /*
6540 * EPT Misconfigurations can be generated if the value of bits 2:0
6541 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006542 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006543 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6544 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006545}
6546
Wanpeng Lif53cd632014-12-02 19:14:58 +08006547#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006548/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006549 * Sets up the vmcs for emulated real mode.
6550 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006551static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006552{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006553 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554
Abel Gordon4607c2d2013-04-18 14:35:55 +03006555 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006556 /*
6557 * At vCPU creation, "VMWRITE to any supported field
6558 * in the VMCS" is supported, so use the more
6559 * permissive vmx_vmread_bitmap to specify both read
6560 * and write permissions for the shadow VMCS.
6561 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006562 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006563 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006564 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006565 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006566 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006567
Avi Kivity6aa8b732006-12-10 02:21:36 -08006568 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6569
Avi Kivity6aa8b732006-12-10 02:21:36 -08006570 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006571 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006572 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006573
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006574 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006575
Dan Williamsdfa169b2016-06-02 11:17:24 -07006576 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006577 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006578 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006579 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006580 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006581
Andrey Smetanind62caab2015-11-10 15:36:33 +03006582 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006583 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6584 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6585 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6586 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6587
6588 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006589
Li RongQing0bcf2612015-12-03 13:29:34 +08006590 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006591 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006592 }
6593
Wanpeng Lib31c1142018-03-12 04:53:04 -07006594 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006595 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006596 vmx->ple_window = ple_window;
6597 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006598 }
6599
Xiao Guangrongc3707952011-07-12 03:28:04 +08006600 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6601 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006602 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6603
Avi Kivity9581d442010-10-19 16:46:55 +02006604 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6605 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006606 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006607 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6608 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609
Bandan Das2a499e42017-08-03 15:54:41 -04006610 if (cpu_has_vmx_vmfunc())
6611 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6612
Eddie Dong2cc51562007-05-21 07:28:09 +03006613 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6614 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006615 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006616 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006617 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006618
Radim Krčmář74545702015-04-27 15:11:25 +02006619 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6620 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006621
Paolo Bonzini03916db2014-07-24 14:21:57 +02006622 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006623 u32 index = vmx_msr_index[i];
6624 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006625 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006626
6627 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6628 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006629 if (wrmsr_safe(index, data_low, data_high) < 0)
6630 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006631 vmx->guest_msrs[j].index = i;
6632 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006633 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006634 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006636
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006637 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006638
6639 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640
6641 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006642 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006643
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006644 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6645 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6646
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006647 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006648
Wanpeng Lif53cd632014-12-02 19:14:58 +08006649 if (vmx_xsaves_supported())
6650 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6651
Peter Feiner4e595162016-07-07 14:49:58 -07006652 if (enable_pml) {
Peter Feiner4e595162016-07-07 14:49:58 -07006653 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6654 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6655 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006656
6657 if (cpu_has_vmx_encls_vmexit())
6658 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006659}
6660
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006661static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006662{
6663 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006664 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006665 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006666
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006667 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006668 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006669
Wanpeng Li518e7b92018-02-28 14:03:31 +08006670 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006671 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006672 kvm_set_cr8(vcpu, 0);
6673
6674 if (!init_event) {
6675 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6676 MSR_IA32_APICBASE_ENABLE;
6677 if (kvm_vcpu_is_reset_bsp(vcpu))
6678 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6679 apic_base_msr.host_initiated = true;
6680 kvm_set_apic_base(vcpu, &apic_base_msr);
6681 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006682
Avi Kivity2fb92db2011-04-27 19:42:18 +03006683 vmx_segment_cache_clear(vmx);
6684
Avi Kivity5706be02008-08-20 15:07:31 +03006685 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006686 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006687 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006688
6689 seg_setup(VCPU_SREG_DS);
6690 seg_setup(VCPU_SREG_ES);
6691 seg_setup(VCPU_SREG_FS);
6692 seg_setup(VCPU_SREG_GS);
6693 seg_setup(VCPU_SREG_SS);
6694
6695 vmcs_write16(GUEST_TR_SELECTOR, 0);
6696 vmcs_writel(GUEST_TR_BASE, 0);
6697 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6698 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6699
6700 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6701 vmcs_writel(GUEST_LDTR_BASE, 0);
6702 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6703 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6704
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006705 if (!init_event) {
6706 vmcs_write32(GUEST_SYSENTER_CS, 0);
6707 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6708 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6709 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6710 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006711
Wanpeng Lic37c2872017-11-20 14:52:21 -08006712 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006713 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006714
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006715 vmcs_writel(GUEST_GDTR_BASE, 0);
6716 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6717
6718 vmcs_writel(GUEST_IDTR_BASE, 0);
6719 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6720
Anthony Liguori443381a2010-12-06 10:53:38 -06006721 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006722 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006723 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006724 if (kvm_mpx_supported())
6725 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006726
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006727 setup_msrs(vmx);
6728
Avi Kivity6aa8b732006-12-10 02:21:36 -08006729 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6730
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006731 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006732 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006733 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006734 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006735 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006736 vmcs_write32(TPR_THRESHOLD, 0);
6737 }
6738
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006739 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006740
Sheng Yang2384d2b2008-01-17 15:14:33 +08006741 if (vmx->vpid != 0)
6742 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6743
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006744 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006745 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006746 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006747 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006748 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006749
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006750 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006751
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006752 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006753 if (init_event)
6754 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006755}
6756
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006757/*
6758 * In nested virtualization, check if L1 asked to exit on external interrupts.
6759 * For most existing hypervisors, this will always return true.
6760 */
6761static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6762{
6763 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6764 PIN_BASED_EXT_INTR_MASK;
6765}
6766
Bandan Das77b0f5d2014-04-19 18:17:45 -04006767/*
6768 * In nested virtualization, check if L1 has set
6769 * VM_EXIT_ACK_INTR_ON_EXIT
6770 */
6771static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6772{
6773 return get_vmcs12(vcpu)->vm_exit_controls &
6774 VM_EXIT_ACK_INTR_ON_EXIT;
6775}
6776
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006777static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6778{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006779 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006780}
6781
Jan Kiszkac9a79532014-03-07 20:03:15 +01006782static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006783{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006784 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6785 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006786}
6787
Jan Kiszkac9a79532014-03-07 20:03:15 +01006788static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006789{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006790 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006791 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006792 enable_irq_window(vcpu);
6793 return;
6794 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006795
Paolo Bonzini47c01522016-12-19 11:44:07 +01006796 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6797 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006798}
6799
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006800static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006801{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006802 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006803 uint32_t intr;
6804 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006805
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006806 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006807
Avi Kivityfa89a812008-09-01 15:57:51 +03006808 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006809 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006810 int inc_eip = 0;
6811 if (vcpu->arch.interrupt.soft)
6812 inc_eip = vcpu->arch.event_exit_inst_len;
6813 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006814 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006815 return;
6816 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006817 intr = irq | INTR_INFO_VALID_MASK;
6818 if (vcpu->arch.interrupt.soft) {
6819 intr |= INTR_TYPE_SOFT_INTR;
6820 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6821 vmx->vcpu.arch.event_exit_inst_len);
6822 } else
6823 intr |= INTR_TYPE_EXT_INTR;
6824 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006825
6826 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006827}
6828
Sheng Yangf08864b2008-05-15 18:23:25 +08006829static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6830{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006831 struct vcpu_vmx *vmx = to_vmx(vcpu);
6832
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006833 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006834 /*
6835 * Tracking the NMI-blocked state in software is built upon
6836 * finding the next open IRQ window. This, in turn, depends on
6837 * well-behaving guests: They have to keep IRQs disabled at
6838 * least as long as the NMI handler runs. Otherwise we may
6839 * cause NMI nesting, maybe breaking the guest. But as this is
6840 * highly unlikely, we can live with the residual risk.
6841 */
6842 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6843 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6844 }
6845
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006846 ++vcpu->stat.nmi_injections;
6847 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006848
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006849 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006850 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006851 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006852 return;
6853 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006854
Sheng Yangf08864b2008-05-15 18:23:25 +08006855 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6856 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006857
6858 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006859}
6860
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006861static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6862{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006863 struct vcpu_vmx *vmx = to_vmx(vcpu);
6864 bool masked;
6865
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006866 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006867 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006868 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006869 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006870 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6871 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6872 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006873}
6874
6875static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6876{
6877 struct vcpu_vmx *vmx = to_vmx(vcpu);
6878
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006879 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006880 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6881 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6882 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6883 }
6884 } else {
6885 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6886 if (masked)
6887 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6888 GUEST_INTR_STATE_NMI);
6889 else
6890 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6891 GUEST_INTR_STATE_NMI);
6892 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006893}
6894
Jan Kiszka2505dc92013-04-14 12:12:47 +02006895static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6896{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006897 if (to_vmx(vcpu)->nested.nested_run_pending)
6898 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006899
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006900 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006901 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6902 return 0;
6903
Jan Kiszka2505dc92013-04-14 12:12:47 +02006904 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6905 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6906 | GUEST_INTR_STATE_NMI));
6907}
6908
Gleb Natapov78646122009-03-23 12:12:11 +02006909static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6910{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006911 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6912 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006913 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6914 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006915}
6916
Izik Eiduscbc94022007-10-25 00:29:55 +02006917static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6918{
6919 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006920
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006921 if (enable_unrestricted_guest)
6922 return 0;
6923
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006924 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6925 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006926 if (ret)
6927 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006928 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006929 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006930}
6931
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006932static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6933{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006934 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006935 return 0;
6936}
6937
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006938static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006939{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006940 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006941 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006942 /*
6943 * Update instruction length as we may reinject the exception
6944 * from user space while in guest debugging mode.
6945 */
6946 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6947 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006948 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006949 return false;
6950 /* fall through */
6951 case DB_VECTOR:
6952 if (vcpu->guest_debug &
6953 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6954 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006955 /* fall through */
6956 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006957 case OF_VECTOR:
6958 case BR_VECTOR:
6959 case UD_VECTOR:
6960 case DF_VECTOR:
6961 case SS_VECTOR:
6962 case GP_VECTOR:
6963 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006964 return true;
6965 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006966 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006967 return false;
6968}
6969
6970static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6971 int vec, u32 err_code)
6972{
6973 /*
6974 * Instruction with address size override prefix opcode 0x67
6975 * Cause the #SS fault with 0 error code in VM86 mode.
6976 */
6977 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006978 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006979 if (vcpu->arch.halt_request) {
6980 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006981 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006982 }
6983 return 1;
6984 }
6985 return 0;
6986 }
6987
6988 /*
6989 * Forward all other exceptions that are valid in real mode.
6990 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6991 * the required debugging infrastructure rework.
6992 */
6993 kvm_queue_exception(vcpu, vec);
6994 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006995}
6996
Andi Kleena0861c02009-06-08 17:37:09 +08006997/*
6998 * Trigger machine check on the host. We assume all the MSRs are already set up
6999 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7000 * We pass a fake environment to the machine check handler because we want
7001 * the guest to be always treated like user space, no matter what context
7002 * it used internally.
7003 */
7004static void kvm_machine_check(void)
7005{
7006#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7007 struct pt_regs regs = {
7008 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7009 .flags = X86_EFLAGS_IF,
7010 };
7011
7012 do_machine_check(&regs, 0);
7013#endif
7014}
7015
Avi Kivity851ba692009-08-24 11:10:17 +03007016static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007017{
7018 /* already handled by vcpu_run */
7019 return 1;
7020}
7021
Avi Kivity851ba692009-08-24 11:10:17 +03007022static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007023{
Avi Kivity1155f762007-11-22 11:30:47 +02007024 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007025 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007026 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007027 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028 u32 vect_info;
7029 enum emulation_result er;
7030
Avi Kivity1155f762007-11-22 11:30:47 +02007031 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007032 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007033
Andi Kleena0861c02009-06-08 17:37:09 +08007034 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007035 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007036
Jim Mattsonef85b672016-12-12 11:01:37 -08007037 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007038 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007039
Wanpeng Li082d06e2018-04-03 16:28:48 -07007040 if (is_invalid_opcode(intr_info))
7041 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007042
Avi Kivity6aa8b732006-12-10 02:21:36 -08007043 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007044 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007045 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007046
Liran Alon9e869482018-03-12 13:12:51 +02007047 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7048 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007049 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007050 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7051 if (er == EMULATE_USER_EXIT)
7052 return 0;
7053 else if (er != EMULATE_DONE)
7054 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7055 return 1;
7056 }
7057
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007058 /*
7059 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7060 * MMIO, it is better to report an internal error.
7061 * See the comments in vmx_handle_exit.
7062 */
7063 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7064 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7065 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7066 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007067 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007068 vcpu->run->internal.data[0] = vect_info;
7069 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007070 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007071 return 0;
7072 }
7073
Avi Kivity6aa8b732006-12-10 02:21:36 -08007074 if (is_page_fault(intr_info)) {
7075 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007076 /* EPT won't cause page fault directly */
7077 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007078 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007079 }
7080
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007081 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007082
7083 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7084 return handle_rmode_exception(vcpu, ex_no, error_code);
7085
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007086 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007087 case AC_VECTOR:
7088 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7089 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007090 case DB_VECTOR:
7091 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7092 if (!(vcpu->guest_debug &
7093 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007094 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007095 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007096 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007097 skip_emulated_instruction(vcpu);
7098
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007099 kvm_queue_exception(vcpu, DB_VECTOR);
7100 return 1;
7101 }
7102 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7103 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7104 /* fall through */
7105 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01007106 /*
7107 * Update instruction length as we may reinject #BP from
7108 * user space while in guest debugging mode. Reading it for
7109 * #DB as well causes no harm, it is not used in that case.
7110 */
7111 vmx->vcpu.arch.event_exit_inst_len =
7112 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007113 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007114 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007115 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7116 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007117 break;
7118 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007119 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7120 kvm_run->ex.exception = ex_no;
7121 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007122 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007123 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007124 return 0;
7125}
7126
Avi Kivity851ba692009-08-24 11:10:17 +03007127static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007129 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130 return 1;
7131}
7132
Avi Kivity851ba692009-08-24 11:10:17 +03007133static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007134{
Avi Kivity851ba692009-08-24 11:10:17 +03007135 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007136 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007137 return 0;
7138}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007141{
He, Qingbfdaab02007-09-12 14:18:28 +08007142 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007143 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007144 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007145
He, Qingbfdaab02007-09-12 14:18:28 +08007146 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007147 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007148
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007149 ++vcpu->stat.io_exits;
7150
Sean Christopherson432baf62018-03-08 08:57:26 -08007151 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007152 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007153
7154 port = exit_qualification >> 16;
7155 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007156 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007157
Sean Christophersondca7f122018-03-08 08:57:27 -08007158 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159}
7160
Ingo Molnar102d8322007-02-19 14:37:47 +02007161static void
7162vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7163{
7164 /*
7165 * Patch in the VMCALL instruction:
7166 */
7167 hypercall[0] = 0x0f;
7168 hypercall[1] = 0x01;
7169 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007170}
7171
Guo Chao0fa06072012-06-28 15:16:19 +08007172/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007173static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7174{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007175 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007176 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7177 unsigned long orig_val = val;
7178
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007179 /*
7180 * We get here when L2 changed cr0 in a way that did not change
7181 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007182 * but did change L0 shadowed bits. So we first calculate the
7183 * effective cr0 value that L1 would like to write into the
7184 * hardware. It consists of the L2-owned bits from the new
7185 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007186 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007187 val = (val & ~vmcs12->cr0_guest_host_mask) |
7188 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7189
David Matlack38991522016-11-29 18:14:08 -08007190 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007191 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007192
7193 if (kvm_set_cr0(vcpu, val))
7194 return 1;
7195 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007196 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007197 } else {
7198 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007199 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007200 return 1;
David Matlack38991522016-11-29 18:14:08 -08007201
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007202 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007203 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007204}
7205
7206static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7207{
7208 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007209 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7210 unsigned long orig_val = val;
7211
7212 /* analogously to handle_set_cr0 */
7213 val = (val & ~vmcs12->cr4_guest_host_mask) |
7214 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7215 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007216 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007218 return 0;
7219 } else
7220 return kvm_set_cr4(vcpu, val);
7221}
7222
Paolo Bonzini0367f202016-07-12 10:44:55 +02007223static int handle_desc(struct kvm_vcpu *vcpu)
7224{
7225 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007226 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007227}
7228
Avi Kivity851ba692009-08-24 11:10:17 +03007229static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007230{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007231 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007232 int cr;
7233 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007234 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007235 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007236
He, Qingbfdaab02007-09-12 14:18:28 +08007237 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007238 cr = exit_qualification & 15;
7239 reg = (exit_qualification >> 8) & 15;
7240 switch ((exit_qualification >> 4) & 3) {
7241 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007242 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007243 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244 switch (cr) {
7245 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007246 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007247 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007248 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007249 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007250 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007251 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007255 case 8: {
7256 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007257 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007258 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007259 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007260 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007261 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007262 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return ret;
7264 /*
7265 * TODO: we might be squashing a
7266 * KVM_GUESTDBG_SINGLESTEP-triggered
7267 * KVM_EXIT_DEBUG here.
7268 */
Avi Kivity851ba692009-08-24 11:10:17 +03007269 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007270 return 0;
7271 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007272 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007274 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007275 WARN_ONCE(1, "Guest should always own CR0.TS");
7276 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007277 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007278 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279 case 1: /*mov from cr*/
7280 switch (cr) {
7281 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007282 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007283 val = kvm_read_cr3(vcpu);
7284 kvm_register_write(vcpu, reg, val);
7285 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007286 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007288 val = kvm_get_cr8(vcpu);
7289 kvm_register_write(vcpu, reg, val);
7290 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007291 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 }
7293 break;
7294 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007295 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007296 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007297 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007298
Kyle Huey6affcbe2016-11-29 12:40:40 -08007299 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007300 default:
7301 break;
7302 }
Avi Kivity851ba692009-08-24 11:10:17 +03007303 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007304 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 (int)(exit_qualification >> 4) & 3, cr);
7306 return 0;
7307}
7308
Avi Kivity851ba692009-08-24 11:10:17 +03007309static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007310{
He, Qingbfdaab02007-09-12 14:18:28 +08007311 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007312 int dr, dr7, reg;
7313
7314 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7315 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7316
7317 /* First, if DR does not exist, trigger UD */
7318 if (!kvm_require_dr(vcpu, dr))
7319 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007320
Jan Kiszkaf2483412010-01-20 18:20:20 +01007321 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007322 if (!kvm_require_cpl(vcpu, 0))
7323 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007324 dr7 = vmcs_readl(GUEST_DR7);
7325 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007326 /*
7327 * As the vm-exit takes precedence over the debug trap, we
7328 * need to emulate the latter, either for the host or the
7329 * guest debugging itself.
7330 */
7331 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007332 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007333 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007334 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007335 vcpu->run->debug.arch.exception = DB_VECTOR;
7336 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007337 return 0;
7338 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007339 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007340 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007341 kvm_queue_exception(vcpu, DB_VECTOR);
7342 return 1;
7343 }
7344 }
7345
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007346 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007347 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7348 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007349
7350 /*
7351 * No more DR vmexits; force a reload of the debug registers
7352 * and reenter on this instruction. The next vmexit will
7353 * retrieve the full state of the debug registers.
7354 */
7355 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7356 return 1;
7357 }
7358
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007359 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7360 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007361 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007362
7363 if (kvm_get_dr(vcpu, dr, &val))
7364 return 1;
7365 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007366 } else
Nadav Amit57773922014-06-18 17:19:23 +03007367 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007368 return 1;
7369
Kyle Huey6affcbe2016-11-29 12:40:40 -08007370 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007371}
7372
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007373static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7374{
7375 return vcpu->arch.dr6;
7376}
7377
7378static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7379{
7380}
7381
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007382static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7383{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007384 get_debugreg(vcpu->arch.db[0], 0);
7385 get_debugreg(vcpu->arch.db[1], 1);
7386 get_debugreg(vcpu->arch.db[2], 2);
7387 get_debugreg(vcpu->arch.db[3], 3);
7388 get_debugreg(vcpu->arch.dr6, 6);
7389 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7390
7391 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007392 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007393}
7394
Gleb Natapov020df072010-04-13 10:05:23 +03007395static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7396{
7397 vmcs_writel(GUEST_DR7, val);
7398}
7399
Avi Kivity851ba692009-08-24 11:10:17 +03007400static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007401{
Kyle Huey6a908b62016-11-29 12:40:37 -08007402 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007403}
7404
Avi Kivity851ba692009-08-24 11:10:17 +03007405static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007407 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007408 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007409
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007410 msr_info.index = ecx;
7411 msr_info.host_initiated = false;
7412 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007413 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007414 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007415 return 1;
7416 }
7417
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007418 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007419
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007421 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7422 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007423 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007424}
7425
Avi Kivity851ba692009-08-24 11:10:17 +03007426static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007427{
Will Auld8fe8ab42012-11-29 12:42:12 -08007428 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007429 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7430 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7431 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007432
Will Auld8fe8ab42012-11-29 12:42:12 -08007433 msr.data = data;
7434 msr.index = ecx;
7435 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007436 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007437 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007438 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007439 return 1;
7440 }
7441
Avi Kivity59200272010-01-25 19:47:02 +02007442 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007443 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007444}
7445
Avi Kivity851ba692009-08-24 11:10:17 +03007446static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007447{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007448 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007449 return 1;
7450}
7451
Avi Kivity851ba692009-08-24 11:10:17 +03007452static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007453{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007454 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7455 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007456
Avi Kivity3842d132010-07-27 12:30:24 +03007457 kvm_make_request(KVM_REQ_EVENT, vcpu);
7458
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007459 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007460 return 1;
7461}
7462
Avi Kivity851ba692009-08-24 11:10:17 +03007463static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007464{
Avi Kivityd3bef152007-06-05 15:53:05 +03007465 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007466}
7467
Avi Kivity851ba692009-08-24 11:10:17 +03007468static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007469{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007470 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007471}
7472
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007473static int handle_invd(struct kvm_vcpu *vcpu)
7474{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007475 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007476}
7477
Avi Kivity851ba692009-08-24 11:10:17 +03007478static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007479{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007480 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007481
7482 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007483 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007484}
7485
Avi Kivityfee84b02011-11-10 14:57:25 +02007486static int handle_rdpmc(struct kvm_vcpu *vcpu)
7487{
7488 int err;
7489
7490 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007491 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007492}
7493
Avi Kivity851ba692009-08-24 11:10:17 +03007494static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007495{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007496 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007497}
7498
Dexuan Cui2acf9232010-06-10 11:27:12 +08007499static int handle_xsetbv(struct kvm_vcpu *vcpu)
7500{
7501 u64 new_bv = kvm_read_edx_eax(vcpu);
7502 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7503
7504 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007505 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007506 return 1;
7507}
7508
Wanpeng Lif53cd632014-12-02 19:14:58 +08007509static int handle_xsaves(struct kvm_vcpu *vcpu)
7510{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007511 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007512 WARN(1, "this should never happen\n");
7513 return 1;
7514}
7515
7516static int handle_xrstors(struct kvm_vcpu *vcpu)
7517{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007518 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007519 WARN(1, "this should never happen\n");
7520 return 1;
7521}
7522
Avi Kivity851ba692009-08-24 11:10:17 +03007523static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007524{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007525 if (likely(fasteoi)) {
7526 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7527 int access_type, offset;
7528
7529 access_type = exit_qualification & APIC_ACCESS_TYPE;
7530 offset = exit_qualification & APIC_ACCESS_OFFSET;
7531 /*
7532 * Sane guest uses MOV to write EOI, with written value
7533 * not cared. So make a short-circuit here by avoiding
7534 * heavy instruction emulation.
7535 */
7536 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7537 (offset == APIC_EOI)) {
7538 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007539 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007540 }
7541 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007542 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007543}
7544
Yang Zhangc7c9c562013-01-25 10:18:51 +08007545static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7546{
7547 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7548 int vector = exit_qualification & 0xff;
7549
7550 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7551 kvm_apic_set_eoi_accelerated(vcpu, vector);
7552 return 1;
7553}
7554
Yang Zhang83d4c282013-01-25 10:18:49 +08007555static int handle_apic_write(struct kvm_vcpu *vcpu)
7556{
7557 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7558 u32 offset = exit_qualification & 0xfff;
7559
7560 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7561 kvm_apic_write_nodecode(vcpu, offset);
7562 return 1;
7563}
7564
Avi Kivity851ba692009-08-24 11:10:17 +03007565static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007566{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007567 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007568 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007569 bool has_error_code = false;
7570 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007571 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007572 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007573
7574 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007575 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007576 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007577
7578 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7579
7580 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007581 if (reason == TASK_SWITCH_GATE && idt_v) {
7582 switch (type) {
7583 case INTR_TYPE_NMI_INTR:
7584 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007585 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007586 break;
7587 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007588 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589 kvm_clear_interrupt_queue(vcpu);
7590 break;
7591 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007592 if (vmx->idt_vectoring_info &
7593 VECTORING_INFO_DELIVER_CODE_MASK) {
7594 has_error_code = true;
7595 error_code =
7596 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7597 }
7598 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007599 case INTR_TYPE_SOFT_EXCEPTION:
7600 kvm_clear_exception_queue(vcpu);
7601 break;
7602 default:
7603 break;
7604 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007605 }
Izik Eidus37817f22008-03-24 23:14:53 +02007606 tss_selector = exit_qualification;
7607
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007608 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7609 type != INTR_TYPE_EXT_INTR &&
7610 type != INTR_TYPE_NMI_INTR))
7611 skip_emulated_instruction(vcpu);
7612
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007613 if (kvm_task_switch(vcpu, tss_selector,
7614 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7615 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007616 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7617 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7618 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007619 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007620 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007621
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007622 /*
7623 * TODO: What about debug traps on tss switch?
7624 * Are we supposed to inject them and update dr6?
7625 */
7626
7627 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007628}
7629
Avi Kivity851ba692009-08-24 11:10:17 +03007630static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007631{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007632 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007633 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007634 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007635
Sheng Yangf9c617f2009-03-25 10:08:52 +08007636 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007637
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007638 /*
7639 * EPT violation happened while executing iret from NMI,
7640 * "blocked by NMI" bit has to be set before next VM entry.
7641 * There are errata that may cause this bit to not be set:
7642 * AAK134, BY25.
7643 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007644 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007645 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007646 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007647 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7648
Sheng Yang14394422008-04-28 12:24:45 +08007649 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007650 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007651
Junaid Shahid27959a42016-12-06 16:46:10 -08007652 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007653 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007654 ? PFERR_USER_MASK : 0;
7655 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007656 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007657 ? PFERR_WRITE_MASK : 0;
7658 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007659 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007660 ? PFERR_FETCH_MASK : 0;
7661 /* ept page table entry is present? */
7662 error_code |= (exit_qualification &
7663 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7664 EPT_VIOLATION_EXECUTABLE))
7665 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007666
Paolo Bonzinieebed242016-11-28 14:39:58 +01007667 error_code |= (exit_qualification & 0x100) != 0 ?
7668 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007669
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007670 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007671 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007672}
7673
Avi Kivity851ba692009-08-24 11:10:17 +03007674static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007675{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007676 gpa_t gpa;
7677
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007678 /*
7679 * A nested guest cannot optimize MMIO vmexits, because we have an
7680 * nGPA here instead of the required GPA.
7681 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007682 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007683 if (!is_guest_mode(vcpu) &&
7684 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007685 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007686 /*
7687 * Doing kvm_skip_emulated_instruction() depends on undefined
7688 * behavior: Intel's manual doesn't mandate
7689 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7690 * occurs and while on real hardware it was observed to be set,
7691 * other hypervisors (namely Hyper-V) don't set it, we end up
7692 * advancing IP with some random value. Disable fast mmio when
7693 * running nested and keep it for real hardware in hope that
7694 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7695 */
7696 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7697 return kvm_skip_emulated_instruction(vcpu);
7698 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007699 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007700 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007701 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007702
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007703 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007704}
7705
Avi Kivity851ba692009-08-24 11:10:17 +03007706static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007707{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007708 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007709 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7710 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007711 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007712 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007713
7714 return 1;
7715}
7716
Mohammed Gamal80ced182009-09-01 12:48:18 +02007717static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007718{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007719 struct vcpu_vmx *vmx = to_vmx(vcpu);
7720 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007721 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007722 u32 cpu_exec_ctrl;
7723 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007724 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007725
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007726 /*
7727 * We should never reach the point where we are emulating L2
7728 * due to invalid guest state as that means we incorrectly
7729 * allowed a nested VMEntry with an invalid vmcs12.
7730 */
7731 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7732
Avi Kivity49e9d552010-09-19 14:34:08 +02007733 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7734 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007735
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007736 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007737 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007738 return handle_interrupt_window(&vmx->vcpu);
7739
Radim Krčmář72875d82017-04-26 22:32:19 +02007740 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007741 return 1;
7742
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007743 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007744
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007745 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007746 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007747 ret = 0;
7748 goto out;
7749 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007750
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007751 if (err != EMULATE_DONE)
7752 goto emulation_error;
7753
7754 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7755 vcpu->arch.exception.pending)
7756 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007757
Gleb Natapov8d76c492013-05-08 18:38:44 +03007758 if (vcpu->arch.halt_request) {
7759 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007760 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007761 goto out;
7762 }
7763
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007764 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007765 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007766 if (need_resched())
7767 schedule();
7768 }
7769
Mohammed Gamal80ced182009-09-01 12:48:18 +02007770out:
7771 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007772
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007773emulation_error:
7774 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7775 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7776 vcpu->run->internal.ndata = 0;
7777 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007778}
7779
7780static void grow_ple_window(struct kvm_vcpu *vcpu)
7781{
7782 struct vcpu_vmx *vmx = to_vmx(vcpu);
7783 int old = vmx->ple_window;
7784
Babu Mogerc8e88712018-03-16 16:37:24 -04007785 vmx->ple_window = __grow_ple_window(old, ple_window,
7786 ple_window_grow,
7787 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007788
7789 if (vmx->ple_window != old)
7790 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007791
7792 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007793}
7794
7795static void shrink_ple_window(struct kvm_vcpu *vcpu)
7796{
7797 struct vcpu_vmx *vmx = to_vmx(vcpu);
7798 int old = vmx->ple_window;
7799
Babu Mogerc8e88712018-03-16 16:37:24 -04007800 vmx->ple_window = __shrink_ple_window(old, ple_window,
7801 ple_window_shrink,
7802 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007803
7804 if (vmx->ple_window != old)
7805 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007806
7807 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007808}
7809
7810/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007811 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7812 */
7813static void wakeup_handler(void)
7814{
7815 struct kvm_vcpu *vcpu;
7816 int cpu = smp_processor_id();
7817
7818 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7819 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7820 blocked_vcpu_list) {
7821 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7822
7823 if (pi_test_on(pi_desc) == 1)
7824 kvm_vcpu_kick(vcpu);
7825 }
7826 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7827}
7828
Peng Haoe01bca22018-04-07 05:47:32 +08007829static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007830{
7831 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7832 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7833 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7834 0ull, VMX_EPT_EXECUTABLE_MASK,
7835 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007836 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007837
7838 ept_set_mmio_spte_mask();
7839 kvm_enable_tdp();
7840}
7841
Tiejun Chenf2c76482014-10-28 10:14:47 +08007842static __init int hardware_setup(void)
7843{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007844 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007845 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007846
7847 rdmsrl_safe(MSR_EFER, &host_efer);
7848
7849 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7850 kvm_define_shared_msr(i, vmx_msr_index[i]);
7851
Radim Krčmář23611332016-09-29 22:41:33 +02007852 for (i = 0; i < VMX_BITMAP_NR; i++) {
7853 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7854 if (!vmx_bitmap[i])
7855 goto out;
7856 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007857
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007858 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7859 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7860
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007861 if (setup_vmcs_config(&vmcs_config) < 0) {
7862 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007863 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007864 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007865
7866 if (boot_cpu_has(X86_FEATURE_NX))
7867 kvm_enable_efer_bits(EFER_NX);
7868
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007869 if (boot_cpu_has(X86_FEATURE_MPX)) {
7870 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7871 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7872 }
7873
Wanpeng Li08d839c2017-03-23 05:30:08 -07007874 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7875 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007876 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007877
Tiejun Chenf2c76482014-10-28 10:14:47 +08007878 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007879 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007880 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007881 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007882 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007883
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007884 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007885 enable_ept_ad_bits = 0;
7886
Wanpeng Li8ad81822017-10-09 15:51:53 -07007887 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007888 enable_unrestricted_guest = 0;
7889
Paolo Bonziniad15a292015-01-30 16:18:49 +01007890 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891 flexpriority_enabled = 0;
7892
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007893 if (!cpu_has_virtual_nmis())
7894 enable_vnmi = 0;
7895
Paolo Bonziniad15a292015-01-30 16:18:49 +01007896 /*
7897 * set_apic_access_page_addr() is used to reload apic access
7898 * page upon invalidation. No need to do anything if not
7899 * using the APIC_ACCESS_ADDR VMCS field.
7900 */
7901 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007902 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007903
7904 if (!cpu_has_vmx_tpr_shadow())
7905 kvm_x86_ops->update_cr8_intercept = NULL;
7906
7907 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7908 kvm_disable_largepages();
7909
Tianyu Lan877ad952018-07-19 08:40:23 +00007910#if IS_ENABLED(CONFIG_HYPERV)
7911 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7912 && enable_ept)
7913 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7914#endif
7915
Wanpeng Li0f107682017-09-28 18:06:24 -07007916 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007918 ple_window = 0;
7919 ple_window_grow = 0;
7920 ple_window_max = 0;
7921 ple_window_shrink = 0;
7922 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007923
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007924 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007925 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007926 kvm_x86_ops->sync_pir_to_irr = NULL;
7927 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007928
Haozhong Zhang64903d62015-10-20 15:39:09 +08007929 if (cpu_has_vmx_tsc_scaling()) {
7930 kvm_has_tsc_control = true;
7931 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7932 kvm_tsc_scaling_ratio_frac_bits = 48;
7933 }
7934
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007935 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7936
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007937 if (enable_ept)
7938 vmx_enable_tdp();
7939 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007940 kvm_disable_tdp();
7941
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007942 if (!nested) {
7943 kvm_x86_ops->get_nested_state = NULL;
7944 kvm_x86_ops->set_nested_state = NULL;
7945 }
7946
Kai Huang843e4332015-01-28 10:54:28 +08007947 /*
7948 * Only enable PML when hardware supports PML feature, and both EPT
7949 * and EPT A/D bit features are enabled -- PML depends on them to work.
7950 */
7951 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7952 enable_pml = 0;
7953
7954 if (!enable_pml) {
7955 kvm_x86_ops->slot_enable_log_dirty = NULL;
7956 kvm_x86_ops->slot_disable_log_dirty = NULL;
7957 kvm_x86_ops->flush_log_dirty = NULL;
7958 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7959 }
7960
Sean Christophersond264ee02018-08-27 15:21:12 -07007961 if (!cpu_has_vmx_preemption_timer())
7962 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7963
Yunhong Jiang64672c92016-06-13 14:19:59 -07007964 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7965 u64 vmx_msr;
7966
7967 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7968 cpu_preemption_timer_multi =
7969 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7970 } else {
7971 kvm_x86_ops->set_hv_timer = NULL;
7972 kvm_x86_ops->cancel_hv_timer = NULL;
7973 }
7974
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007975 if (!cpu_has_vmx_shadow_vmcs())
7976 enable_shadow_vmcs = 0;
7977 if (enable_shadow_vmcs)
7978 init_vmcs_shadow_fields();
7979
Feng Wubf9f6ac2015-09-18 22:29:55 +08007980 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007981 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007982
Ashok Rajc45dcc72016-06-22 14:59:56 +08007983 kvm_mce_cap_supported |= MCG_LMCE_P;
7984
Tiejun Chenf2c76482014-10-28 10:14:47 +08007985 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007986
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007987out:
Radim Krčmář23611332016-09-29 22:41:33 +02007988 for (i = 0; i < VMX_BITMAP_NR; i++)
7989 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007990
7991 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007992}
7993
7994static __exit void hardware_unsetup(void)
7995{
Radim Krčmář23611332016-09-29 22:41:33 +02007996 int i;
7997
7998 for (i = 0; i < VMX_BITMAP_NR; i++)
7999 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008000
Tiejun Chenf2c76482014-10-28 10:14:47 +08008001 free_kvm_area();
8002}
8003
Avi Kivity6aa8b732006-12-10 02:21:36 -08008004/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008005 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8006 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8007 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008008static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008009{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008010 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008011 grow_ple_window(vcpu);
8012
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008013 /*
8014 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8015 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8016 * never set PAUSE_EXITING and just set PLE if supported,
8017 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8018 */
8019 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008020 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008021}
8022
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008023static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008024{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008025 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008026}
8027
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008028static int handle_mwait(struct kvm_vcpu *vcpu)
8029{
8030 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8031 return handle_nop(vcpu);
8032}
8033
Jim Mattson45ec3682017-08-23 16:32:04 -07008034static int handle_invalid_op(struct kvm_vcpu *vcpu)
8035{
8036 kvm_queue_exception(vcpu, UD_VECTOR);
8037 return 1;
8038}
8039
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008040static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8041{
8042 return 1;
8043}
8044
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008045static int handle_monitor(struct kvm_vcpu *vcpu)
8046{
8047 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8048 return handle_nop(vcpu);
8049}
8050
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008051/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008052 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008053 * set the success or error code of an emulated VMX instruction (as specified
8054 * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated
8055 * instruction.
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008056 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008057static int nested_vmx_succeed(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008058{
8059 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8060 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8061 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008062 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008063}
8064
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008065static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008066{
8067 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8068 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8069 X86_EFLAGS_SF | X86_EFLAGS_OF))
8070 | X86_EFLAGS_CF);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008071 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008072}
8073
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008074static int nested_vmx_failValid(struct kvm_vcpu *vcpu,
8075 u32 vm_instruction_error)
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008076{
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008077 /*
8078 * failValid writes the error number to the current VMCS, which
8079 * can't be done if there isn't a current VMCS.
8080 */
8081 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8082 return nested_vmx_failInvalid(vcpu);
8083
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008084 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8085 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8086 X86_EFLAGS_SF | X86_EFLAGS_OF))
8087 | X86_EFLAGS_ZF);
8088 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8089 /*
8090 * We don't need to force a shadow sync because
8091 * VM_INSTRUCTION_ERROR is not shadowed
8092 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008093 return kvm_skip_emulated_instruction(vcpu);
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008094}
Abel Gordon145c28d2013-04-18 14:36:55 +03008095
Wincy Vanff651cb2014-12-11 08:52:58 +03008096static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8097{
8098 /* TODO: not to reset guest simply here. */
8099 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008100 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008101}
8102
Jan Kiszkaf41245002014-03-07 20:03:13 +01008103static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8104{
8105 struct vcpu_vmx *vmx =
8106 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8107
8108 vmx->nested.preemption_timer_expired = true;
8109 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8110 kvm_vcpu_kick(&vmx->vcpu);
8111
8112 return HRTIMER_NORESTART;
8113}
8114
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008115/*
Bandan Das19677e32014-05-06 02:19:15 -04008116 * Decode the memory-address operand of a vmx instruction, as recorded on an
8117 * exit caused by such an instruction (run by a guest hypervisor).
8118 * On success, returns 0. When the operand is invalid, returns 1 and throws
8119 * #UD or #GP.
8120 */
8121static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8122 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008123 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008124{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008125 gva_t off;
8126 bool exn;
8127 struct kvm_segment s;
8128
Bandan Das19677e32014-05-06 02:19:15 -04008129 /*
8130 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8131 * Execution", on an exit, vmx_instruction_info holds most of the
8132 * addressing components of the operand. Only the displacement part
8133 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8134 * For how an actual address is calculated from all these components,
8135 * refer to Vol. 1, "Operand Addressing".
8136 */
8137 int scaling = vmx_instruction_info & 3;
8138 int addr_size = (vmx_instruction_info >> 7) & 7;
8139 bool is_reg = vmx_instruction_info & (1u << 10);
8140 int seg_reg = (vmx_instruction_info >> 15) & 7;
8141 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8142 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8143 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8144 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8145
8146 if (is_reg) {
8147 kvm_queue_exception(vcpu, UD_VECTOR);
8148 return 1;
8149 }
8150
8151 /* Addr = segment_base + offset */
8152 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008153 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008154 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008156 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off += kvm_register_read(vcpu, index_reg)<<scaling;
8158 vmx_get_segment(vcpu, &s, seg_reg);
8159 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008160
8161 if (addr_size == 1) /* 32 bit */
8162 *ret &= 0xffffffff;
8163
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008164 /* Checks for #GP/#SS exceptions. */
8165 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008166 if (is_long_mode(vcpu)) {
8167 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8168 * non-canonical form. This is the only check on the memory
8169 * destination for long mode!
8170 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008171 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008172 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008173 /* Protected mode: apply checks for segment validity in the
8174 * following order:
8175 * - segment type check (#GP(0) may be thrown)
8176 * - usability check (#GP(0)/#SS(0))
8177 * - limit check (#GP(0)/#SS(0))
8178 */
8179 if (wr)
8180 /* #GP(0) if the destination operand is located in a
8181 * read-only data segment or any code segment.
8182 */
8183 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8184 else
8185 /* #GP(0) if the source operand is located in an
8186 * execute-only code segment
8187 */
8188 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008189 if (exn) {
8190 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8191 return 1;
8192 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008193 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8194 */
8195 exn = (s.unusable != 0);
8196 /* Protected mode: #GP(0)/#SS(0) if the memory
8197 * operand is outside the segment limit.
8198 */
8199 exn = exn || (off + sizeof(u64) > s.limit);
8200 }
8201 if (exn) {
8202 kvm_queue_exception_e(vcpu,
8203 seg_reg == VCPU_SREG_SS ?
8204 SS_VECTOR : GP_VECTOR,
8205 0);
8206 return 1;
8207 }
8208
Bandan Das19677e32014-05-06 02:19:15 -04008209 return 0;
8210}
8211
Radim Krčmářcbf71272017-05-19 15:48:51 +02008212static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008213{
8214 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008215 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008216
8217 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008218 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008219 return 1;
8220
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008221 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008222 kvm_inject_page_fault(vcpu, &e);
8223 return 1;
8224 }
8225
Bandan Das3573e222014-05-06 02:19:16 -04008226 return 0;
8227}
8228
Liran Alonabfc52c2018-06-23 02:35:13 +03008229/*
8230 * Allocate a shadow VMCS and associate it with the currently loaded
8231 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8232 * VMCS is also VMCLEARed, so that it is ready for use.
8233 */
8234static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8235{
8236 struct vcpu_vmx *vmx = to_vmx(vcpu);
8237 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8238
8239 /*
8240 * We should allocate a shadow vmcs for vmcs01 only when L1
8241 * executes VMXON and free it when L1 executes VMXOFF.
8242 * As it is invalid to execute VMXON twice, we shouldn't reach
8243 * here when vmcs01 already have an allocated shadow vmcs.
8244 */
8245 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8246
8247 if (!loaded_vmcs->shadow_vmcs) {
8248 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8249 if (loaded_vmcs->shadow_vmcs)
8250 vmcs_clear(loaded_vmcs->shadow_vmcs);
8251 }
8252 return loaded_vmcs->shadow_vmcs;
8253}
8254
Jim Mattsone29acc52016-11-30 12:03:43 -08008255static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8256{
8257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008258 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008259
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008260 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8261 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008262 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008263
8264 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8265 if (!vmx->nested.cached_vmcs12)
8266 goto out_cached_vmcs12;
8267
Liran Alon61ada742018-06-23 02:35:08 +03008268 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8269 if (!vmx->nested.cached_shadow_vmcs12)
8270 goto out_cached_shadow_vmcs12;
8271
Liran Alonabfc52c2018-06-23 02:35:13 +03008272 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8273 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008274
Jim Mattsone29acc52016-11-30 12:03:43 -08008275 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8276 HRTIMER_MODE_REL_PINNED);
8277 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8278
Roman Kagan63aff652018-07-19 21:59:07 +03008279 vmx->nested.vpid02 = allocate_vpid();
8280
Sean Christopherson9d6105b2018-09-26 09:23:51 -07008281 vmx->nested.vmcs02_initialized = false;
Jim Mattsone29acc52016-11-30 12:03:43 -08008282 vmx->nested.vmxon = true;
8283 return 0;
8284
8285out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008286 kfree(vmx->nested.cached_shadow_vmcs12);
8287
8288out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008289 kfree(vmx->nested.cached_vmcs12);
8290
8291out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008292 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008293
Jim Mattsonde3a0022017-11-27 17:22:25 -06008294out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008295 return -ENOMEM;
8296}
8297
Bandan Das3573e222014-05-06 02:19:16 -04008298/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008299 * Emulate the VMXON instruction.
8300 * Currently, we just remember that VMX is active, and do not save or even
8301 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8302 * do not currently need to store anything in that guest-allocated memory
8303 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8304 * argument is different from the VMXON pointer (which the spec says they do).
8305 */
8306static int handle_vmon(struct kvm_vcpu *vcpu)
8307{
Jim Mattsone29acc52016-11-30 12:03:43 -08008308 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008309 gpa_t vmptr;
8310 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008312 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8313 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008314
Jim Mattson70f3aac2017-04-26 08:53:46 -07008315 /*
8316 * The Intel VMX Instruction Reference lists a bunch of bits that are
8317 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8318 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8319 * Otherwise, we should fail with #UD. But most faulting conditions
8320 * have already been checked by hardware, prior to the VM-exit for
8321 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8322 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008323 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008324 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008325 kvm_queue_exception(vcpu, UD_VECTOR);
8326 return 1;
8327 }
8328
Felix Wilhelm727ba742018-06-11 09:43:44 +02008329 /* CPL=0 must be checked manually. */
8330 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008331 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008332 return 1;
8333 }
8334
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008335 if (vmx->nested.vmxon)
8336 return nested_vmx_failValid(vcpu,
8337 VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008338
Haozhong Zhang3b840802016-06-22 14:59:54 +08008339 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008340 != VMXON_NEEDED_FEATURES) {
8341 kvm_inject_gp(vcpu, 0);
8342 return 1;
8343 }
8344
Radim Krčmářcbf71272017-05-19 15:48:51 +02008345 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008346 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008347
8348 /*
8349 * SDM 3: 24.11.5
8350 * The first 4 bytes of VMXON region contain the supported
8351 * VMCS revision identifier
8352 *
8353 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8354 * which replaces physical address width with 32
8355 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008356 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8357 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008358
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008359 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008360 if (is_error_page(page))
8361 return nested_vmx_failInvalid(vcpu);
8362
Radim Krčmářcbf71272017-05-19 15:48:51 +02008363 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8364 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008365 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008366 return nested_vmx_failInvalid(vcpu);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008367 }
8368 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008369 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008370
8371 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008372 ret = enter_vmx_operation(vcpu);
8373 if (ret)
8374 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008375
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008376 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008377}
8378
8379/*
8380 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8381 * for running VMX instructions (except VMXON, whose prerequisites are
8382 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008383 * Note that many of these exceptions have priority over VM exits, so they
8384 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008385 */
8386static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8387{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008388 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008389 kvm_queue_exception(vcpu, UD_VECTOR);
8390 return 0;
8391 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008392
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008393 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008394 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008395 return 0;
8396 }
8397
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008398 return 1;
8399}
8400
David Matlack8ca44e82017-08-01 14:00:39 -07008401static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8402{
8403 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8404 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8405}
8406
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008407static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu)
Abel Gordone7953d72013-04-18 14:37:55 +03008408{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008409 struct vcpu_vmx *vmx = to_vmx(vcpu);
8410
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008411 if (vmx->nested.current_vmptr == -1ull)
8412 return;
8413
Abel Gordon012f83c2013-04-18 14:39:25 +03008414 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008415 /* copy to memory all shadowed fields in case
8416 they were modified */
8417 copy_shadow_to_vmcs12(vmx);
8418 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008419 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008420 }
Wincy Van705699a2015-02-03 23:58:17 +08008421 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008422
8423 /* Flush VMCS12 to guest memory */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008424 kvm_vcpu_write_guest_page(vcpu,
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008425 vmx->nested.current_vmptr >> PAGE_SHIFT,
8426 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008427
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008428 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8429
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008430 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008431}
8432
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008433/*
8434 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8435 * just stops using VMX.
8436 */
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008437static void free_nested(struct kvm_vcpu *vcpu)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008438{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008439 struct vcpu_vmx *vmx = to_vmx(vcpu);
8440
Wanpeng Lib7455822017-11-22 14:04:00 -08008441 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008442 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008443
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008444 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008445 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008446 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008447 vmx->nested.posted_intr_nv = -1;
8448 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008449 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008450 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008451 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8452 free_vmcs(vmx->vmcs01.shadow_vmcs);
8453 vmx->vmcs01.shadow_vmcs = NULL;
8454 }
David Matlack4f2777b2016-07-13 17:16:37 -07008455 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008456 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008457 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008458 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008459 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008460 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008461 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008462 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008463 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008464 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008465 }
Wincy Van705699a2015-02-03 23:58:17 +08008466 if (vmx->nested.pi_desc_page) {
8467 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008468 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008469 vmx->nested.pi_desc_page = NULL;
8470 vmx->nested.pi_desc = NULL;
8471 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008472
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008473 kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL);
8474
Jim Mattsonde3a0022017-11-27 17:22:25 -06008475 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008476}
8477
8478/* Emulate the VMXOFF instruction */
8479static int handle_vmoff(struct kvm_vcpu *vcpu)
8480{
8481 if (!nested_vmx_check_permission(vcpu))
8482 return 1;
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008483 free_nested(vcpu);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008484 return nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008485}
8486
Nadav Har'El27d6c862011-05-25 23:06:59 +03008487/* Emulate the VMCLEAR instruction */
8488static int handle_vmclear(struct kvm_vcpu *vcpu)
8489{
8490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008491 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008492 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008493
8494 if (!nested_vmx_check_permission(vcpu))
8495 return 1;
8496
Radim Krčmářcbf71272017-05-19 15:48:51 +02008497 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498 return 1;
8499
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008500 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8501 return nested_vmx_failValid(vcpu,
8502 VMXERR_VMCLEAR_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008503
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008504 if (vmptr == vmx->nested.vmxon_ptr)
8505 return nested_vmx_failValid(vcpu,
8506 VMXERR_VMCLEAR_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008507
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008508 if (vmptr == vmx->nested.current_vmptr)
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008509 nested_release_vmcs12(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008510
Jim Mattson587d7e722017-03-02 12:41:48 -08008511 kvm_vcpu_write_guest(vcpu,
8512 vmptr + offsetof(struct vmcs12, launch_state),
8513 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008514
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008515 return nested_vmx_succeed(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008516}
8517
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008518static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8519
8520/* Emulate the VMLAUNCH instruction */
8521static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8522{
8523 return nested_vmx_run(vcpu, true);
8524}
8525
8526/* Emulate the VMRESUME instruction */
8527static int handle_vmresume(struct kvm_vcpu *vcpu)
8528{
8529
8530 return nested_vmx_run(vcpu, false);
8531}
8532
Nadav Har'El49f705c2011-05-25 23:08:30 +03008533/*
8534 * Read a vmcs12 field. Since these can have varying lengths and we return
8535 * one type, we chose the biggest type (u64) and zero-extend the return value
8536 * to that size. Note that the caller, handle_vmread, might need to use only
8537 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8538 * 64-bit fields are to be returned).
8539 */
Liran Alone2536742018-06-23 02:35:02 +03008540static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008541 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008542{
8543 short offset = vmcs_field_to_offset(field);
8544 char *p;
8545
8546 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008547 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008548
Liran Alone2536742018-06-23 02:35:02 +03008549 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008550
Jim Mattsond37f4262017-12-22 12:12:16 -08008551 switch (vmcs_field_width(field)) {
8552 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008554 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008555 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008557 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008558 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008560 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008561 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008563 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008565 WARN_ON(1);
8566 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 }
8568}
8569
Abel Gordon20b97fe2013-04-18 14:36:25 +03008570
Liran Alone2536742018-06-23 02:35:02 +03008571static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008572 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008573 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008574 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008575 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008576 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008577
Jim Mattsond37f4262017-12-22 12:12:16 -08008578 switch (vmcs_field_width(field)) {
8579 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008580 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008581 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008582 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008583 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008584 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008585 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008588 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008592 WARN_ON(1);
8593 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 }
8595
8596}
8597
Jim Mattsonf4160e42018-05-29 09:11:33 -07008598/*
8599 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8600 * they have been modified by the L1 guest. Note that the "read-only"
8601 * VM-exit information fields are actually writable if the vCPU is
8602 * configured to support "VMWRITE to any supported field in the VMCS."
8603 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008604static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8605{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008606 const u16 *fields[] = {
8607 shadow_read_write_fields,
8608 shadow_read_only_fields
8609 };
8610 const int max_fields[] = {
8611 max_shadow_read_write_fields,
8612 max_shadow_read_only_fields
8613 };
8614 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008615 unsigned long field;
8616 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008617 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008618
Jan Kiszka282da872014-10-08 18:05:39 +02008619 preempt_disable();
8620
Abel Gordon16f5b902013-04-18 14:38:25 +03008621 vmcs_load(shadow_vmcs);
8622
Jim Mattsonf4160e42018-05-29 09:11:33 -07008623 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8624 for (i = 0; i < max_fields[q]; i++) {
8625 field = fields[q][i];
8626 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008627 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008628 }
8629 /*
8630 * Skip the VM-exit information fields if they are read-only.
8631 */
8632 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8633 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008634 }
8635
8636 vmcs_clear(shadow_vmcs);
8637 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008638
8639 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008640}
8641
Abel Gordonc3114422013-04-18 14:38:55 +03008642static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8643{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008644 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008645 shadow_read_write_fields,
8646 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008647 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008648 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008649 max_shadow_read_write_fields,
8650 max_shadow_read_only_fields
8651 };
8652 int i, q;
8653 unsigned long field;
8654 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008655 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008656
8657 vmcs_load(shadow_vmcs);
8658
Mathias Krausec2bae892013-06-26 20:36:21 +02008659 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008660 for (i = 0; i < max_fields[q]; i++) {
8661 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008662 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008663 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008664 }
8665 }
8666
8667 vmcs_clear(shadow_vmcs);
8668 vmcs_load(vmx->loaded_vmcs->vmcs);
8669}
8670
Nadav Har'El49f705c2011-05-25 23:08:30 +03008671static int handle_vmread(struct kvm_vcpu *vcpu)
8672{
8673 unsigned long field;
8674 u64 field_value;
8675 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8676 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8677 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008678 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008679
Kyle Hueyeb277562016-11-29 12:40:39 -08008680 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008681 return 1;
8682
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008683 if (to_vmx(vcpu)->nested.current_vmptr == -1ull)
8684 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008685
Liran Alon6d894f42018-06-23 02:35:09 +03008686 if (!is_guest_mode(vcpu))
8687 vmcs12 = get_vmcs12(vcpu);
8688 else {
8689 /*
8690 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8691 * to shadowed-field sets the ALU flags for VMfailInvalid.
8692 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008693 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8694 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008695 vmcs12 = get_shadow_vmcs12(vcpu);
8696 }
8697
Nadav Har'El49f705c2011-05-25 23:08:30 +03008698 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008699 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008700 /* Read the field, zero-extended to a u64 field_value */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008701 if (vmcs12_read_any(vmcs12, field, &field_value) < 0)
8702 return nested_vmx_failValid(vcpu,
8703 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
8704
Nadav Har'El49f705c2011-05-25 23:08:30 +03008705 /*
8706 * Now copy part of this value to register or memory, as requested.
8707 * Note that the number of bits actually copied is 32 or 64 depending
8708 * on the guest's mode (32 or 64 bit), not on the given field's length.
8709 */
8710 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008711 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008712 field_value);
8713 } else {
8714 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008715 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008716 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008717 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008718 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8719 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008720 }
8721
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008722 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723}
8724
8725
8726static int handle_vmwrite(struct kvm_vcpu *vcpu)
8727{
8728 unsigned long field;
8729 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008730 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008731 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8732 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008733
Nadav Har'El49f705c2011-05-25 23:08:30 +03008734 /* The value to write might be 32 or 64 bits, depending on L1's long
8735 * mode, and eventually we need to write that into a field of several
8736 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008737 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008738 * bits into the vmcs12 field.
8739 */
8740 u64 field_value = 0;
8741 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008742 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008743
Kyle Hueyeb277562016-11-29 12:40:39 -08008744 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745 return 1;
8746
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008747 if (vmx->nested.current_vmptr == -1ull)
8748 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008749
Nadav Har'El49f705c2011-05-25 23:08:30 +03008750 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008751 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008752 (((vmx_instruction_info) >> 3) & 0xf));
8753 else {
8754 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008755 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008757 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8758 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008759 kvm_inject_page_fault(vcpu, &e);
8760 return 1;
8761 }
8762 }
8763
8764
Nadav Amit27e6fb52014-06-18 17:19:26 +03008765 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008766 /*
8767 * If the vCPU supports "VMWRITE to any supported field in the
8768 * VMCS," then the "read-only" fields are actually read/write.
8769 */
8770 if (vmcs_field_readonly(field) &&
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008771 !nested_cpu_has_vmwrite_any_field(vcpu))
8772 return nested_vmx_failValid(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008774
Liran Alon6d894f42018-06-23 02:35:09 +03008775 if (!is_guest_mode(vcpu))
8776 vmcs12 = get_vmcs12(vcpu);
8777 else {
8778 /*
8779 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8780 * to shadowed-field sets the ALU flags for VMfailInvalid.
8781 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008782 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull)
8783 return nested_vmx_failInvalid(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008784 vmcs12 = get_shadow_vmcs12(vcpu);
Liran Alon6d894f42018-06-23 02:35:09 +03008785 }
8786
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008787 if (vmcs12_write_any(vmcs12, field, field_value) < 0)
8788 return nested_vmx_failValid(vcpu,
8789 VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008790
Liran Alon6d894f42018-06-23 02:35:09 +03008791 /*
8792 * Do not track vmcs12 dirty-state if in guest-mode
8793 * as we actually dirty shadow vmcs12 instead of vmcs12.
8794 */
8795 if (!is_guest_mode(vcpu)) {
8796 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008797#define SHADOW_FIELD_RW(x) case x:
8798#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008799 /*
8800 * The fields that can be updated by L1 without a vmexit are
8801 * always updated in the vmcs02, the others go down the slow
8802 * path of prepare_vmcs02.
8803 */
8804 break;
8805 default:
8806 vmx->nested.dirty_vmcs12 = true;
8807 break;
8808 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008809 }
8810
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008811 return nested_vmx_succeed(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008812}
8813
Jim Mattsona8bc2842016-11-30 12:03:44 -08008814static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8815{
8816 vmx->nested.current_vmptr = vmptr;
8817 if (enable_shadow_vmcs) {
8818 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8819 SECONDARY_EXEC_SHADOW_VMCS);
8820 vmcs_write64(VMCS_LINK_POINTER,
8821 __pa(vmx->vmcs01.shadow_vmcs));
8822 vmx->nested.sync_shadow_vmcs = true;
8823 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008824 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008825}
8826
Nadav Har'El63846662011-05-25 23:07:29 +03008827/* Emulate the VMPTRLD instruction */
8828static int handle_vmptrld(struct kvm_vcpu *vcpu)
8829{
8830 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008831 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008832
8833 if (!nested_vmx_check_permission(vcpu))
8834 return 1;
8835
Radim Krčmářcbf71272017-05-19 15:48:51 +02008836 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008837 return 1;
8838
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008839 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu)))
8840 return nested_vmx_failValid(vcpu,
8841 VMXERR_VMPTRLD_INVALID_ADDRESS);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008842
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008843 if (vmptr == vmx->nested.vmxon_ptr)
8844 return nested_vmx_failValid(vcpu,
8845 VMXERR_VMPTRLD_VMXON_POINTER);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008846
Nadav Har'El63846662011-05-25 23:07:29 +03008847 if (vmx->nested.current_vmptr != vmptr) {
8848 struct vmcs12 *new_vmcs12;
8849 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008850 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008851 if (is_error_page(page))
8852 return nested_vmx_failInvalid(vcpu);
8853
Nadav Har'El63846662011-05-25 23:07:29 +03008854 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008855 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008856 (new_vmcs12->hdr.shadow_vmcs &&
8857 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008858 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008859 kvm_release_page_clean(page);
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008860 return nested_vmx_failValid(vcpu,
Nadav Har'El63846662011-05-25 23:07:29 +03008861 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Nadav Har'El63846662011-05-25 23:07:29 +03008862 }
Nadav Har'El63846662011-05-25 23:07:29 +03008863
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +02008864 nested_release_vmcs12(vcpu);
8865
David Matlack4f2777b2016-07-13 17:16:37 -07008866 /*
8867 * Load VMCS12 from guest memory since it is not already
8868 * cached.
8869 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008870 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8871 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008872 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008873
Jim Mattsona8bc2842016-11-30 12:03:44 -08008874 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008875 }
8876
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008877 return nested_vmx_succeed(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008878}
8879
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008880/* Emulate the VMPTRST instruction */
8881static int handle_vmptrst(struct kvm_vcpu *vcpu)
8882{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008883 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8884 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8885 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008886 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008887 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008888
8889 if (!nested_vmx_check_permission(vcpu))
8890 return 1;
8891
Sean Christopherson0a06d422018-07-19 10:31:00 -07008892 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008893 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008894 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008895 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8896 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008897 kvm_inject_page_fault(vcpu, &e);
8898 return 1;
8899 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008900 return nested_vmx_succeed(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008901}
8902
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008903/* Emulate the INVEPT instruction */
8904static int handle_invept(struct kvm_vcpu *vcpu)
8905{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008906 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008907 u32 vmx_instruction_info, types;
8908 unsigned long type;
8909 gva_t gva;
8910 struct x86_exception e;
8911 struct {
8912 u64 eptp, gpa;
8913 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008914
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008915 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008916 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008917 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008918 kvm_queue_exception(vcpu, UD_VECTOR);
8919 return 1;
8920 }
8921
8922 if (!nested_vmx_check_permission(vcpu))
8923 return 1;
8924
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008925 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008926 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008927
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008928 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008929
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008930 if (type >= 32 || !(types & (1 << type)))
8931 return nested_vmx_failValid(vcpu,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008932 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008933
8934 /* According to the Intel VMX instruction reference, the memory
8935 * operand is read even if it isn't needed (e.g., for type==global)
8936 */
8937 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008938 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008940 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008941 kvm_inject_page_fault(vcpu, &e);
8942 return 1;
8943 }
8944
8945 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008946 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008947 /*
8948 * TODO: track mappings and invalidate
8949 * single context requests appropriately
8950 */
8951 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008952 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008953 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008954 break;
8955 default:
8956 BUG_ON(1);
8957 break;
8958 }
8959
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008960 return nested_vmx_succeed(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961}
8962
Liran Alon3d5bdae2018-10-08 23:42:18 +03008963static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8964{
8965 struct vcpu_vmx *vmx = to_vmx(vcpu);
8966
8967 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
8968}
8969
Petr Matouseka642fc32014-09-23 20:22:30 +02008970static int handle_invvpid(struct kvm_vcpu *vcpu)
8971{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008972 struct vcpu_vmx *vmx = to_vmx(vcpu);
8973 u32 vmx_instruction_info;
8974 unsigned long type, types;
8975 gva_t gva;
8976 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008977 struct {
8978 u64 vpid;
8979 u64 gla;
8980 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03008981 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008982
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008983 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008984 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008985 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008986 kvm_queue_exception(vcpu, UD_VECTOR);
8987 return 1;
8988 }
8989
8990 if (!nested_vmx_check_permission(vcpu))
8991 return 1;
8992
8993 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8994 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
8995
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008996 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03008997 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008998
Sean Christopherson09abb5e2018-09-26 09:23:55 -07008999 if (type >= 32 || !(types & (1 << type)))
9000 return nested_vmx_failValid(vcpu,
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009001 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009002
9003 /* according to the intel vmx instruction reference, the memory
9004 * operand is read even if it isn't needed (e.g., for type==global)
9005 */
9006 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9007 vmx_instruction_info, false, &gva))
9008 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009009 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009010 kvm_inject_page_fault(vcpu, &e);
9011 return 1;
9012 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009013 if (operand.vpid >> 16)
9014 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009015 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009016
Liran Alon3d5bdae2018-10-08 23:42:18 +03009017 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009018 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009019 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009020 if (!operand.vpid ||
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009021 is_noncanonical_address(operand.gla, vcpu))
9022 return nested_vmx_failValid(vcpu,
Jim Mattson40352602017-06-28 09:37:37 -07009023 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon3d5bdae2018-10-08 23:42:18 +03009024 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009025 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009026 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009027 } else
Liran Alon327c0722018-10-08 23:42:19 +03009028 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009029 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009030 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009031 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009032 if (!operand.vpid)
9033 return nested_vmx_failValid(vcpu,
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009034 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Liran Alon327c0722018-10-08 23:42:19 +03009035 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009036 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009037 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009038 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009039 break;
9040 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009041 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009042 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009043 }
9044
Sean Christopherson09abb5e2018-09-26 09:23:55 -07009045 return nested_vmx_succeed(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009046}
9047
Junaid Shahideb4b2482018-06-27 14:59:14 -07009048static int handle_invpcid(struct kvm_vcpu *vcpu)
9049{
9050 u32 vmx_instruction_info;
9051 unsigned long type;
9052 bool pcid_enabled;
9053 gva_t gva;
9054 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009055 unsigned i;
9056 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009057 struct {
9058 u64 pcid;
9059 u64 gla;
9060 } operand;
9061
9062 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9063 kvm_queue_exception(vcpu, UD_VECTOR);
9064 return 1;
9065 }
9066
9067 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9068 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9069
9070 if (type > 3) {
9071 kvm_inject_gp(vcpu, 0);
9072 return 1;
9073 }
9074
9075 /* According to the Intel instruction reference, the memory operand
9076 * is read even if it isn't needed (e.g., for type==all)
9077 */
9078 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9079 vmx_instruction_info, false, &gva))
9080 return 1;
9081
9082 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9083 kvm_inject_page_fault(vcpu, &e);
9084 return 1;
9085 }
9086
9087 if (operand.pcid >> 12 != 0) {
9088 kvm_inject_gp(vcpu, 0);
9089 return 1;
9090 }
9091
9092 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9093
9094 switch (type) {
9095 case INVPCID_TYPE_INDIV_ADDR:
9096 if ((!pcid_enabled && (operand.pcid != 0)) ||
9097 is_noncanonical_address(operand.gla, vcpu)) {
9098 kvm_inject_gp(vcpu, 0);
9099 return 1;
9100 }
9101 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9102 return kvm_skip_emulated_instruction(vcpu);
9103
9104 case INVPCID_TYPE_SINGLE_CTXT:
9105 if (!pcid_enabled && (operand.pcid != 0)) {
9106 kvm_inject_gp(vcpu, 0);
9107 return 1;
9108 }
9109
9110 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9111 kvm_mmu_sync_roots(vcpu);
9112 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9113 }
9114
Junaid Shahidb94742c2018-06-27 14:59:20 -07009115 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +02009116 if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
Junaid Shahidb94742c2018-06-27 14:59:20 -07009117 == operand.pcid)
9118 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009119
Vitaly Kuznetsov6a82cd12018-10-08 21:28:07 +02009120 kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009121 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009122 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009123 * given PCID, then nothing needs to be done here because a
9124 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009125 */
9126
9127 return kvm_skip_emulated_instruction(vcpu);
9128
9129 case INVPCID_TYPE_ALL_NON_GLOBAL:
9130 /*
9131 * Currently, KVM doesn't mark global entries in the shadow
9132 * page tables, so a non-global flush just degenerates to a
9133 * global flush. If needed, we could optimize this later by
9134 * keeping track of global entries in shadow page tables.
9135 */
9136
9137 /* fall-through */
9138 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9139 kvm_mmu_unload(vcpu);
9140 return kvm_skip_emulated_instruction(vcpu);
9141
9142 default:
9143 BUG(); /* We have already checked above that type <= 3 */
9144 }
9145}
9146
Kai Huang843e4332015-01-28 10:54:28 +08009147static int handle_pml_full(struct kvm_vcpu *vcpu)
9148{
9149 unsigned long exit_qualification;
9150
9151 trace_kvm_pml_full(vcpu->vcpu_id);
9152
9153 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9154
9155 /*
9156 * PML buffer FULL happened while executing iret from NMI,
9157 * "blocked by NMI" bit has to be set before next VM entry.
9158 */
9159 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009160 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009161 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9162 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9163 GUEST_INTR_STATE_NMI);
9164
9165 /*
9166 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9167 * here.., and there's no userspace involvement needed for PML.
9168 */
9169 return 1;
9170}
9171
Yunhong Jiang64672c92016-06-13 14:19:59 -07009172static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9173{
Sean Christophersond264ee02018-08-27 15:21:12 -07009174 if (!to_vmx(vcpu)->req_immediate_exit)
9175 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009176 return 1;
9177}
9178
Bandan Das41ab9372017-08-03 15:54:43 -04009179static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9180{
9181 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009182 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9183
9184 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009185 switch (address & VMX_EPTP_MT_MASK) {
9186 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009187 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009188 return false;
9189 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009190 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009191 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009192 return false;
9193 break;
9194 default:
9195 return false;
9196 }
9197
David Hildenbrandbb97a012017-08-10 23:15:28 +02009198 /* only 4 levels page-walk length are valid */
9199 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009200 return false;
9201
9202 /* Reserved bits should not be set */
9203 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9204 return false;
9205
9206 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009207 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009208 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009209 return false;
9210 }
9211
9212 return true;
9213}
9214
9215static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9216 struct vmcs12 *vmcs12)
9217{
9218 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9219 u64 address;
9220 bool accessed_dirty;
9221 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9222
9223 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9224 !nested_cpu_has_ept(vmcs12))
9225 return 1;
9226
9227 if (index >= VMFUNC_EPTP_ENTRIES)
9228 return 1;
9229
9230
9231 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9232 &address, index * 8, 8))
9233 return 1;
9234
David Hildenbrandbb97a012017-08-10 23:15:28 +02009235 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009236
9237 /*
9238 * If the (L2) guest does a vmfunc to the currently
9239 * active ept pointer, we don't have to do anything else
9240 */
9241 if (vmcs12->ept_pointer != address) {
9242 if (!valid_ept_address(vcpu, address))
9243 return 1;
9244
9245 kvm_mmu_unload(vcpu);
9246 mmu->ept_ad = accessed_dirty;
Vitaly Kuznetsov36d9594d2018-10-08 21:28:10 +02009247 mmu->mmu_role.base.ad_disabled = !accessed_dirty;
Bandan Das41ab9372017-08-03 15:54:43 -04009248 vmcs12->ept_pointer = address;
9249 /*
9250 * TODO: Check what's the correct approach in case
9251 * mmu reload fails. Currently, we just let the next
9252 * reload potentially fail
9253 */
9254 kvm_mmu_reload(vcpu);
9255 }
9256
9257 return 0;
9258}
9259
Bandan Das2a499e42017-08-03 15:54:41 -04009260static int handle_vmfunc(struct kvm_vcpu *vcpu)
9261{
Bandan Das27c42a12017-08-03 15:54:42 -04009262 struct vcpu_vmx *vmx = to_vmx(vcpu);
9263 struct vmcs12 *vmcs12;
9264 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9265
9266 /*
9267 * VMFUNC is only supported for nested guests, but we always enable the
9268 * secondary control for simplicity; for non-nested mode, fake that we
9269 * didn't by injecting #UD.
9270 */
9271 if (!is_guest_mode(vcpu)) {
9272 kvm_queue_exception(vcpu, UD_VECTOR);
9273 return 1;
9274 }
9275
9276 vmcs12 = get_vmcs12(vcpu);
9277 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9278 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009279
9280 switch (function) {
9281 case 0:
9282 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9283 goto fail;
9284 break;
9285 default:
9286 goto fail;
9287 }
9288 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009289
9290fail:
9291 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9292 vmcs_read32(VM_EXIT_INTR_INFO),
9293 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009294 return 1;
9295}
9296
Sean Christopherson0b665d32018-08-14 09:33:34 -07009297static int handle_encls(struct kvm_vcpu *vcpu)
9298{
9299 /*
9300 * SGX virtualization is not yet supported. There is no software
9301 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9302 * to prevent the guest from executing ENCLS.
9303 */
9304 kvm_queue_exception(vcpu, UD_VECTOR);
9305 return 1;
9306}
9307
Nadav Har'El0140cae2011-05-25 23:06:28 +03009308/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009309 * The exit handlers return 1 if the exit was handled fully and guest execution
9310 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9311 * to be done to userspace and return 0.
9312 */
Mathias Krause772e0312012-08-30 01:30:19 +02009313static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009314 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9315 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009316 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009317 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009318 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009319 [EXIT_REASON_CR_ACCESS] = handle_cr,
9320 [EXIT_REASON_DR_ACCESS] = handle_dr,
9321 [EXIT_REASON_CPUID] = handle_cpuid,
9322 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9323 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9324 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9325 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009326 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009327 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009328 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009329 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009330 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009331 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009332 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009333 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009334 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009335 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009336 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009337 [EXIT_REASON_VMOFF] = handle_vmoff,
9338 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009339 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9340 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009341 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009342 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009343 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009344 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009345 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009346 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009347 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9348 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009349 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9350 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009351 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009352 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009353 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009354 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009355 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009356 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009357 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009358 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009359 [EXIT_REASON_XSAVES] = handle_xsaves,
9360 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009361 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009362 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009363 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009364 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009365 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009366};
9367
9368static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009369 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009370
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009371static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9372 struct vmcs12 *vmcs12)
9373{
9374 unsigned long exit_qualification;
9375 gpa_t bitmap, last_bitmap;
9376 unsigned int port;
9377 int size;
9378 u8 b;
9379
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009380 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009381 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009382
9383 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9384
9385 port = exit_qualification >> 16;
9386 size = (exit_qualification & 7) + 1;
9387
9388 last_bitmap = (gpa_t)-1;
9389 b = -1;
9390
9391 while (size > 0) {
9392 if (port < 0x8000)
9393 bitmap = vmcs12->io_bitmap_a;
9394 else if (port < 0x10000)
9395 bitmap = vmcs12->io_bitmap_b;
9396 else
Joe Perches1d804d02015-03-30 16:46:09 -07009397 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009398 bitmap += (port & 0x7fff) / 8;
9399
9400 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009401 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009402 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009403 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009404 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009405
9406 port++;
9407 size--;
9408 last_bitmap = bitmap;
9409 }
9410
Joe Perches1d804d02015-03-30 16:46:09 -07009411 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009412}
9413
Nadav Har'El644d7112011-05-25 23:12:35 +03009414/*
9415 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9416 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9417 * disinterest in the current event (read or write a specific MSR) by using an
9418 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9419 */
9420static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9421 struct vmcs12 *vmcs12, u32 exit_reason)
9422{
9423 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9424 gpa_t bitmap;
9425
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009426 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009427 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009428
9429 /*
9430 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9431 * for the four combinations of read/write and low/high MSR numbers.
9432 * First we need to figure out which of the four to use:
9433 */
9434 bitmap = vmcs12->msr_bitmap;
9435 if (exit_reason == EXIT_REASON_MSR_WRITE)
9436 bitmap += 2048;
9437 if (msr_index >= 0xc0000000) {
9438 msr_index -= 0xc0000000;
9439 bitmap += 1024;
9440 }
9441
9442 /* Then read the msr_index'th bit from this bitmap: */
9443 if (msr_index < 1024*8) {
9444 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009445 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009446 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009447 return 1 & (b >> (msr_index & 7));
9448 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009449 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009450}
9451
9452/*
9453 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9454 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9455 * intercept (via guest_host_mask etc.) the current event.
9456 */
9457static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9458 struct vmcs12 *vmcs12)
9459{
9460 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9461 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009462 int reg;
9463 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009464
9465 switch ((exit_qualification >> 4) & 3) {
9466 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009467 reg = (exit_qualification >> 8) & 15;
9468 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009469 switch (cr) {
9470 case 0:
9471 if (vmcs12->cr0_guest_host_mask &
9472 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009473 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009474 break;
9475 case 3:
9476 if ((vmcs12->cr3_target_count >= 1 &&
9477 vmcs12->cr3_target_value0 == val) ||
9478 (vmcs12->cr3_target_count >= 2 &&
9479 vmcs12->cr3_target_value1 == val) ||
9480 (vmcs12->cr3_target_count >= 3 &&
9481 vmcs12->cr3_target_value2 == val) ||
9482 (vmcs12->cr3_target_count >= 4 &&
9483 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009484 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009485 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009486 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009487 break;
9488 case 4:
9489 if (vmcs12->cr4_guest_host_mask &
9490 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009491 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009492 break;
9493 case 8:
9494 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009495 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009496 break;
9497 }
9498 break;
9499 case 2: /* clts */
9500 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9501 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009502 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009503 break;
9504 case 1: /* mov from cr */
9505 switch (cr) {
9506 case 3:
9507 if (vmcs12->cpu_based_vm_exec_control &
9508 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009509 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009510 break;
9511 case 8:
9512 if (vmcs12->cpu_based_vm_exec_control &
9513 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009514 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009515 break;
9516 }
9517 break;
9518 case 3: /* lmsw */
9519 /*
9520 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9521 * cr0. Other attempted changes are ignored, with no exit.
9522 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009523 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009524 if (vmcs12->cr0_guest_host_mask & 0xe &
9525 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009526 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009527 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9528 !(vmcs12->cr0_read_shadow & 0x1) &&
9529 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009530 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009531 break;
9532 }
Joe Perches1d804d02015-03-30 16:46:09 -07009533 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009534}
9535
Liran Alona7cde482018-06-23 02:35:10 +03009536static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9537 struct vmcs12 *vmcs12, gpa_t bitmap)
9538{
9539 u32 vmx_instruction_info;
9540 unsigned long field;
9541 u8 b;
9542
9543 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9544 return true;
9545
9546 /* Decode instruction info and find the field to access */
9547 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9548 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9549
9550 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9551 if (field >> 15)
9552 return true;
9553
9554 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9555 return true;
9556
9557 return 1 & (b >> (field & 7));
9558}
9559
Nadav Har'El644d7112011-05-25 23:12:35 +03009560/*
9561 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9562 * should handle it ourselves in L0 (and then continue L2). Only call this
9563 * when in is_guest_mode (L2).
9564 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009565static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009566{
Nadav Har'El644d7112011-05-25 23:12:35 +03009567 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9568 struct vcpu_vmx *vmx = to_vmx(vcpu);
9569 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9570
Jim Mattson4f350c62017-09-14 16:31:44 -07009571 if (vmx->nested.nested_run_pending)
9572 return false;
9573
9574 if (unlikely(vmx->fail)) {
9575 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9576 vmcs_read32(VM_INSTRUCTION_ERROR));
9577 return true;
9578 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009579
David Matlackc9f04402017-08-01 14:00:40 -07009580 /*
9581 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009582 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9583 * Page). The CPU may write to these pages via their host
9584 * physical address while L2 is running, bypassing any
9585 * address-translation-based dirty tracking (e.g. EPT write
9586 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009587 *
9588 * Mark them dirty on every exit from L2 to prevent them from
9589 * getting out of sync with dirty tracking.
9590 */
9591 nested_mark_vmcs12_pages_dirty(vcpu);
9592
Jim Mattson4f350c62017-09-14 16:31:44 -07009593 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9594 vmcs_readl(EXIT_QUALIFICATION),
9595 vmx->idt_vectoring_info,
9596 intr_info,
9597 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9598 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009599
9600 switch (exit_reason) {
9601 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009602 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009603 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009604 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009605 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009606 else if (is_debug(intr_info) &&
9607 vcpu->guest_debug &
9608 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9609 return false;
9610 else if (is_breakpoint(intr_info) &&
9611 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9612 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009613 return vmcs12->exception_bitmap &
9614 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9615 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009616 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009617 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009618 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009619 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009620 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009621 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009622 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009623 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009624 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009625 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009626 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009627 case EXIT_REASON_HLT:
9628 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9629 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009630 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009631 case EXIT_REASON_INVLPG:
9632 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9633 case EXIT_REASON_RDPMC:
9634 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009635 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009636 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009637 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009638 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009639 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009640 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009641 case EXIT_REASON_VMREAD:
9642 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9643 vmcs12->vmread_bitmap);
9644 case EXIT_REASON_VMWRITE:
9645 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9646 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009647 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9648 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009649 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009650 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009651 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009652 /*
9653 * VMX instructions trap unconditionally. This allows L1 to
9654 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9655 */
Joe Perches1d804d02015-03-30 16:46:09 -07009656 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009657 case EXIT_REASON_CR_ACCESS:
9658 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9659 case EXIT_REASON_DR_ACCESS:
9660 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9661 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009662 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009663 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9664 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009665 case EXIT_REASON_MSR_READ:
9666 case EXIT_REASON_MSR_WRITE:
9667 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9668 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_MWAIT_INSTRUCTION:
9671 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009672 case EXIT_REASON_MONITOR_TRAP_FLAG:
9673 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009674 case EXIT_REASON_MONITOR_INSTRUCTION:
9675 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9676 case EXIT_REASON_PAUSE_INSTRUCTION:
9677 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9678 nested_cpu_has2(vmcs12,
9679 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9680 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009681 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009682 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009683 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009684 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009685 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009686 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009687 /*
9688 * The controls for "virtualize APIC accesses," "APIC-
9689 * register virtualization," and "virtual-interrupt
9690 * delivery" only come from vmcs12.
9691 */
Joe Perches1d804d02015-03-30 16:46:09 -07009692 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009693 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009694 /*
9695 * L0 always deals with the EPT violation. If nested EPT is
9696 * used, and the nested mmu code discovers that the address is
9697 * missing in the guest EPT table (EPT12), the EPT violation
9698 * will be injected with nested_ept_inject_page_fault()
9699 */
Joe Perches1d804d02015-03-30 16:46:09 -07009700 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009701 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009702 /*
9703 * L2 never uses directly L1's EPT, but rather L0's own EPT
9704 * table (shadow on EPT) or a merged EPT table that L0 built
9705 * (EPT on EPT). So any problems with the structure of the
9706 * table is L0's fault.
9707 */
Joe Perches1d804d02015-03-30 16:46:09 -07009708 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009709 case EXIT_REASON_INVPCID:
9710 return
9711 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9712 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009713 case EXIT_REASON_WBINVD:
9714 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9715 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009716 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009717 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9718 /*
9719 * This should never happen, since it is not possible to
9720 * set XSS to a non-zero value---neither in L1 nor in L2.
9721 * If if it were, XSS would have to be checked against
9722 * the XSS exit bitmap in vmcs12.
9723 */
9724 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009725 case EXIT_REASON_PREEMPTION_TIMER:
9726 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009727 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009728 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009729 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009730 case EXIT_REASON_VMFUNC:
9731 /* VM functions are emulated through L2->L0 vmexits. */
9732 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009733 case EXIT_REASON_ENCLS:
9734 /* SGX is never exposed to L1 */
9735 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009736 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009737 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009738 }
9739}
9740
Paolo Bonzini7313c692017-07-27 10:31:25 +02009741static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9742{
9743 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9744
9745 /*
9746 * At this point, the exit interruption info in exit_intr_info
9747 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9748 * we need to query the in-kernel LAPIC.
9749 */
9750 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9751 if ((exit_intr_info &
9752 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9753 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9754 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9755 vmcs12->vm_exit_intr_error_code =
9756 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9757 }
9758
9759 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9760 vmcs_readl(EXIT_QUALIFICATION));
9761 return 1;
9762}
9763
Avi Kivity586f9602010-11-18 13:09:54 +02009764static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9765{
9766 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9767 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9768}
9769
Kai Huanga3eaa862015-11-04 13:46:05 +08009770static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009771{
Kai Huanga3eaa862015-11-04 13:46:05 +08009772 if (vmx->pml_pg) {
9773 __free_page(vmx->pml_pg);
9774 vmx->pml_pg = NULL;
9775 }
Kai Huang843e4332015-01-28 10:54:28 +08009776}
9777
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009778static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009779{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009781 u64 *pml_buf;
9782 u16 pml_idx;
9783
9784 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9785
9786 /* Do nothing if PML buffer is empty */
9787 if (pml_idx == (PML_ENTITY_NUM - 1))
9788 return;
9789
9790 /* PML index always points to next available PML buffer entity */
9791 if (pml_idx >= PML_ENTITY_NUM)
9792 pml_idx = 0;
9793 else
9794 pml_idx++;
9795
9796 pml_buf = page_address(vmx->pml_pg);
9797 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9798 u64 gpa;
9799
9800 gpa = pml_buf[pml_idx];
9801 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009802 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009803 }
9804
9805 /* reset PML index */
9806 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9807}
9808
9809/*
9810 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9811 * Called before reporting dirty_bitmap to userspace.
9812 */
9813static void kvm_flush_pml_buffers(struct kvm *kvm)
9814{
9815 int i;
9816 struct kvm_vcpu *vcpu;
9817 /*
9818 * We only need to kick vcpu out of guest mode here, as PML buffer
9819 * is flushed at beginning of all VMEXITs, and it's obvious that only
9820 * vcpus running in guest are possible to have unflushed GPAs in PML
9821 * buffer.
9822 */
9823 kvm_for_each_vcpu(i, vcpu, kvm)
9824 kvm_vcpu_kick(vcpu);
9825}
9826
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009827static void vmx_dump_sel(char *name, uint32_t sel)
9828{
9829 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009830 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009831 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9832 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9833 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9834}
9835
9836static void vmx_dump_dtsel(char *name, uint32_t limit)
9837{
9838 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9839 name, vmcs_read32(limit),
9840 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9841}
9842
9843static void dump_vmcs(void)
9844{
9845 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9846 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9847 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9848 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9849 u32 secondary_exec_control = 0;
9850 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009851 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009852 int i, n;
9853
9854 if (cpu_has_secondary_exec_ctrls())
9855 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9856
9857 pr_err("*** Guest State ***\n");
9858 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9859 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9860 vmcs_readl(CR0_GUEST_HOST_MASK));
9861 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9862 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9863 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9864 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9865 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9866 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009867 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9868 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9869 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9870 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009871 }
9872 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9873 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9874 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9875 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9876 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9877 vmcs_readl(GUEST_SYSENTER_ESP),
9878 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9879 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9880 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9881 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9882 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9883 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9884 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9885 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9886 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9887 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9888 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9889 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9890 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009891 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9892 efer, vmcs_read64(GUEST_IA32_PAT));
9893 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9894 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009895 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009896 if (cpu_has_load_perf_global_ctrl &&
9897 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009898 pr_err("PerfGlobCtl = 0x%016llx\n",
9899 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009900 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009901 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009902 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9903 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9904 vmcs_read32(GUEST_ACTIVITY_STATE));
9905 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9906 pr_err("InterruptStatus = %04x\n",
9907 vmcs_read16(GUEST_INTR_STATUS));
9908
9909 pr_err("*** Host State ***\n");
9910 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9911 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9912 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9913 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9914 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9915 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9916 vmcs_read16(HOST_TR_SELECTOR));
9917 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9918 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9919 vmcs_readl(HOST_TR_BASE));
9920 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9921 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9922 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9923 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9924 vmcs_readl(HOST_CR4));
9925 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9926 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9927 vmcs_read32(HOST_IA32_SYSENTER_CS),
9928 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9929 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009930 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9931 vmcs_read64(HOST_IA32_EFER),
9932 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009933 if (cpu_has_load_perf_global_ctrl &&
9934 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009935 pr_err("PerfGlobCtl = 0x%016llx\n",
9936 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009937
9938 pr_err("*** Control State ***\n");
9939 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9940 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9941 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9942 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9943 vmcs_read32(EXCEPTION_BITMAP),
9944 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9945 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9946 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9947 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9948 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9949 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9950 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9951 vmcs_read32(VM_EXIT_INTR_INFO),
9952 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9953 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9954 pr_err(" reason=%08x qualification=%016lx\n",
9955 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9956 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9957 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9958 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009959 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009960 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009961 pr_err("TSC Multiplier = 0x%016llx\n",
9962 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009963 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9964 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9965 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9966 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9967 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009968 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009969 n = vmcs_read32(CR3_TARGET_COUNT);
9970 for (i = 0; i + 1 < n; i += 4)
9971 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9972 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9973 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9974 if (i < n)
9975 pr_err("CR3 target%u=%016lx\n",
9976 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9977 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9978 pr_err("PLE Gap=%08x Window=%08x\n",
9979 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9980 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9981 pr_err("Virtual processor ID = 0x%04x\n",
9982 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9983}
9984
Avi Kivity6aa8b732006-12-10 02:21:36 -08009985/*
9986 * The guest has exited. See if we can fix it or if we need userspace
9987 * assistance.
9988 */
Avi Kivity851ba692009-08-24 11:10:17 +03009989static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009990{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009991 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08009992 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02009993 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03009994
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01009995 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
9996
Kai Huang843e4332015-01-28 10:54:28 +08009997 /*
9998 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
9999 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10000 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10001 * mode as if vcpus is in root mode, the PML buffer must has been
10002 * flushed already.
10003 */
10004 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010005 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010006
Mohammed Gamal80ced182009-09-01 12:48:18 +020010007 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010008 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010009 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010010
Paolo Bonzini7313c692017-07-27 10:31:25 +020010011 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10012 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010013
Mohammed Gamal51207022010-05-31 22:40:54 +030010014 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010015 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010016 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10017 vcpu->run->fail_entry.hardware_entry_failure_reason
10018 = exit_reason;
10019 return 0;
10020 }
10021
Avi Kivity29bd8a72007-09-10 17:27:03 +030010022 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010023 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10024 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010025 = vmcs_read32(VM_INSTRUCTION_ERROR);
10026 return 0;
10027 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010028
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010029 /*
10030 * Note:
10031 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10032 * delivery event since it indicates guest is accessing MMIO.
10033 * The vm-exit can be triggered again after return to guest that
10034 * will cause infinite loop.
10035 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010036 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010037 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010038 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010039 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010040 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10041 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10042 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010043 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010044 vcpu->run->internal.data[0] = vectoring_info;
10045 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010046 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10047 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10048 vcpu->run->internal.ndata++;
10049 vcpu->run->internal.data[3] =
10050 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10051 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010052 return 0;
10053 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010054
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010055 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010056 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10057 if (vmx_interrupt_allowed(vcpu)) {
10058 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10059 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10060 vcpu->arch.nmi_pending) {
10061 /*
10062 * This CPU don't support us in finding the end of an
10063 * NMI-blocked window if the guest runs with IRQs
10064 * disabled. So we pull the trigger after 1 s of
10065 * futile waiting, but inform the user about this.
10066 */
10067 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10068 "state on VCPU %d after 1 s timeout\n",
10069 __func__, vcpu->vcpu_id);
10070 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10071 }
10072 }
10073
Avi Kivity6aa8b732006-12-10 02:21:36 -080010074 if (exit_reason < kvm_vmx_max_exit_handlers
10075 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010076 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010077 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010078 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10079 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010080 kvm_queue_exception(vcpu, UD_VECTOR);
10081 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010082 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010083}
10084
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010085/*
10086 * Software based L1D cache flush which is used when microcode providing
10087 * the cache control MSR is not loaded.
10088 *
10089 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10090 * flush it is required to read in 64 KiB because the replacement algorithm
10091 * is not exactly LRU. This could be sized at runtime via topology
10092 * information but as all relevant affected CPUs have 32KiB L1D cache size
10093 * there is no point in doing so.
10094 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010095static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010096{
10097 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010098
10099 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010100 * This code is only executed when the the flush mode is 'cond' or
10101 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010102 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010103 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010104 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010105
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010106 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010107 * Clear the per-vcpu flush bit, it gets set again
10108 * either from vcpu_run() or from one of the unsafe
10109 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010110 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010111 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010112 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010113
10114 /*
10115 * Clear the per-cpu flush bit, it gets set again from
10116 * the interrupt handlers.
10117 */
10118 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10119 kvm_clear_cpu_l1tf_flush_l1d();
10120
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010121 if (!flush_l1d)
10122 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010123 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010124
10125 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010126
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010127 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10128 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10129 return;
10130 }
10131
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010132 asm volatile(
10133 /* First ensure the pages are in the TLB */
10134 "xorl %%eax, %%eax\n"
10135 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010136 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010137 "addl $4096, %%eax\n\t"
10138 "cmpl %%eax, %[size]\n\t"
10139 "jne .Lpopulate_tlb\n\t"
10140 "xorl %%eax, %%eax\n\t"
10141 "cpuid\n\t"
10142 /* Now fill the cache */
10143 "xorl %%eax, %%eax\n"
10144 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010145 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010146 "addl $64, %%eax\n\t"
10147 "cmpl %%eax, %[size]\n\t"
10148 "jne .Lfill_cache\n\t"
10149 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010150 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010151 [size] "r" (size)
10152 : "eax", "ebx", "ecx", "edx");
10153}
10154
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010155static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010156{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010157 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10158
10159 if (is_guest_mode(vcpu) &&
10160 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10161 return;
10162
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010163 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010164 vmcs_write32(TPR_THRESHOLD, 0);
10165 return;
10166 }
10167
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010168 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010169}
10170
Jim Mattson8d860bb2018-05-09 16:56:05 -040010171static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010172{
10173 u32 sec_exec_control;
10174
Jim Mattson8d860bb2018-05-09 16:56:05 -040010175 if (!lapic_in_kernel(vcpu))
10176 return;
10177
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010178 if (!flexpriority_enabled &&
10179 !cpu_has_vmx_virtualize_x2apic_mode())
10180 return;
10181
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010182 /* Postpone execution until vmcs01 is the current VMCS. */
10183 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010184 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010185 return;
10186 }
10187
Yang Zhang8d146952013-01-25 10:18:50 +080010188 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010189 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10190 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010191
Jim Mattson8d860bb2018-05-09 16:56:05 -040010192 switch (kvm_get_apic_mode(vcpu)) {
10193 case LAPIC_MODE_INVALID:
10194 WARN_ONCE(true, "Invalid local APIC state");
10195 case LAPIC_MODE_DISABLED:
10196 break;
10197 case LAPIC_MODE_XAPIC:
10198 if (flexpriority_enabled) {
10199 sec_exec_control |=
10200 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10201 vmx_flush_tlb(vcpu, true);
10202 }
10203 break;
10204 case LAPIC_MODE_X2APIC:
10205 if (cpu_has_vmx_virtualize_x2apic_mode())
10206 sec_exec_control |=
10207 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10208 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010209 }
10210 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10211
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010212 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010213}
10214
Tang Chen38b99172014-09-24 15:57:54 +080010215static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10216{
Jim Mattsonab5df312018-05-09 17:02:03 -040010217 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010218 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010219 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010220 }
Tang Chen38b99172014-09-24 15:57:54 +080010221}
10222
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010223static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010224{
10225 u16 status;
10226 u8 old;
10227
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010228 if (max_isr == -1)
10229 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010230
10231 status = vmcs_read16(GUEST_INTR_STATUS);
10232 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010233 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010234 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010235 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010236 vmcs_write16(GUEST_INTR_STATUS, status);
10237 }
10238}
10239
10240static void vmx_set_rvi(int vector)
10241{
10242 u16 status;
10243 u8 old;
10244
Wei Wang4114c272014-11-05 10:53:43 +080010245 if (vector == -1)
10246 vector = 0;
10247
Yang Zhangc7c9c562013-01-25 10:18:51 +080010248 status = vmcs_read16(GUEST_INTR_STATUS);
10249 old = (u8)status & 0xff;
10250 if ((u8)vector != old) {
10251 status &= ~0xff;
10252 status |= (u8)vector;
10253 vmcs_write16(GUEST_INTR_STATUS, status);
10254 }
10255}
10256
10257static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10258{
Liran Alon851c1a182017-12-24 18:12:56 +020010259 /*
10260 * When running L2, updating RVI is only relevant when
10261 * vmcs12 virtual-interrupt-delivery enabled.
10262 * However, it can be enabled only when L1 also
10263 * intercepts external-interrupts and in that case
10264 * we should not update vmcs02 RVI but instead intercept
10265 * interrupt. Therefore, do nothing when running L2.
10266 */
10267 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010268 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010269}
10270
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010271static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010272{
10273 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010274 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010275 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010276
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010277 WARN_ON(!vcpu->arch.apicv_active);
10278 if (pi_test_on(&vmx->pi_desc)) {
10279 pi_clear_on(&vmx->pi_desc);
10280 /*
10281 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10282 * But on x86 this is just a compiler barrier anyway.
10283 */
10284 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010285 max_irr_updated =
10286 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10287
10288 /*
10289 * If we are running L2 and L1 has a new pending interrupt
10290 * which can be injected, we should re-evaluate
10291 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010292 * If L1 intercepts external-interrupts, we should
10293 * exit from L2 to L1. Otherwise, interrupt should be
10294 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010295 */
Liran Alon851c1a182017-12-24 18:12:56 +020010296 if (is_guest_mode(vcpu) && max_irr_updated) {
10297 if (nested_exit_on_intr(vcpu))
10298 kvm_vcpu_exiting_guest_mode(vcpu);
10299 else
10300 kvm_make_request(KVM_REQ_EVENT, vcpu);
10301 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010302 } else {
10303 max_irr = kvm_lapic_find_highest_irr(vcpu);
10304 }
10305 vmx_hwapic_irr_update(vcpu, max_irr);
10306 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010307}
10308
Paolo Bonzini7e712682018-10-03 13:44:26 +020010309static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10310{
10311 u8 rvi = vmx_get_rvi();
10312 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10313
10314 return ((rvi & 0xf0) > (vppr & 0xf0));
10315}
10316
Andrey Smetanin63086302015-11-10 15:36:32 +030010317static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010318{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010319 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010320 return;
10321
Yang Zhangc7c9c562013-01-25 10:18:51 +080010322 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10323 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10324 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10325 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10326}
10327
Paolo Bonzini967235d2016-12-19 14:03:45 +010010328static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10329{
10330 struct vcpu_vmx *vmx = to_vmx(vcpu);
10331
10332 pi_clear_on(&vmx->pi_desc);
10333 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10334}
10335
Avi Kivity51aa01d2010-07-20 14:31:20 +030010336static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010337{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010338 u32 exit_intr_info = 0;
10339 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010340
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010341 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10342 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010343 return;
10344
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010345 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10346 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10347 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010348
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010349 /* if exit due to PF check for async PF */
10350 if (is_page_fault(exit_intr_info))
10351 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10352
Andi Kleena0861c02009-06-08 17:37:09 +080010353 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010354 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10355 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010356 kvm_machine_check();
10357
Gleb Natapov20f65982009-05-11 13:35:55 +030010358 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010359 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010360 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010361 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010362 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010363 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010364}
Gleb Natapov20f65982009-05-11 13:35:55 +030010365
Yang Zhanga547c6d2013-04-11 19:25:10 +080010366static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10367{
10368 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10369
Yang Zhanga547c6d2013-04-11 19:25:10 +080010370 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10371 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10372 unsigned int vector;
10373 unsigned long entry;
10374 gate_desc *desc;
10375 struct vcpu_vmx *vmx = to_vmx(vcpu);
10376#ifdef CONFIG_X86_64
10377 unsigned long tmp;
10378#endif
10379
10380 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10381 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010382 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010383 asm volatile(
10384#ifdef CONFIG_X86_64
10385 "mov %%" _ASM_SP ", %[sp]\n\t"
10386 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10387 "push $%c[ss]\n\t"
10388 "push %[sp]\n\t"
10389#endif
10390 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010391 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010392 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010393 :
10394#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010395 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010396#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010397 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010398 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010399 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010400 [ss]"i"(__KERNEL_DS),
10401 [cs]"i"(__KERNEL_CS)
10402 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010403 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010404}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010405STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010406
Tom Lendackybc226f02018-05-10 22:06:39 +020010407static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010408{
Tom Lendackybc226f02018-05-10 22:06:39 +020010409 switch (index) {
10410 case MSR_IA32_SMBASE:
10411 /*
10412 * We cannot do SMM unless we can run the guest in big
10413 * real mode.
10414 */
10415 return enable_unrestricted_guest || emulate_invalid_guest_state;
10416 case MSR_AMD64_VIRT_SPEC_CTRL:
10417 /* This is AMD only. */
10418 return false;
10419 default:
10420 return true;
10421 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010422}
10423
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010424static bool vmx_mpx_supported(void)
10425{
10426 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10427 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10428}
10429
Wanpeng Li55412b22014-12-02 19:21:30 +080010430static bool vmx_xsaves_supported(void)
10431{
10432 return vmcs_config.cpu_based_2nd_exec_ctrl &
10433 SECONDARY_EXEC_XSAVES;
10434}
10435
Avi Kivity51aa01d2010-07-20 14:31:20 +030010436static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10437{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010438 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010439 bool unblock_nmi;
10440 u8 vector;
10441 bool idtv_info_valid;
10442
10443 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010444
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010445 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010446 if (vmx->loaded_vmcs->nmi_known_unmasked)
10447 return;
10448 /*
10449 * Can't use vmx->exit_intr_info since we're not sure what
10450 * the exit reason is.
10451 */
10452 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10453 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10454 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10455 /*
10456 * SDM 3: 27.7.1.2 (September 2008)
10457 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10458 * a guest IRET fault.
10459 * SDM 3: 23.2.2 (September 2008)
10460 * Bit 12 is undefined in any of the following cases:
10461 * If the VM exit sets the valid bit in the IDT-vectoring
10462 * information field.
10463 * If the VM exit is due to a double fault.
10464 */
10465 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10466 vector != DF_VECTOR && !idtv_info_valid)
10467 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10468 GUEST_INTR_STATE_NMI);
10469 else
10470 vmx->loaded_vmcs->nmi_known_unmasked =
10471 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10472 & GUEST_INTR_STATE_NMI);
10473 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10474 vmx->loaded_vmcs->vnmi_blocked_time +=
10475 ktime_to_ns(ktime_sub(ktime_get(),
10476 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010477}
10478
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010479static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010480 u32 idt_vectoring_info,
10481 int instr_len_field,
10482 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010483{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010484 u8 vector;
10485 int type;
10486 bool idtv_info_valid;
10487
10488 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010489
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010490 vcpu->arch.nmi_injected = false;
10491 kvm_clear_exception_queue(vcpu);
10492 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010493
10494 if (!idtv_info_valid)
10495 return;
10496
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010497 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010498
Avi Kivity668f6122008-07-02 09:28:55 +030010499 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10500 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010501
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010502 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010503 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010504 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010505 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010506 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010507 * Clear bit "block by NMI" before VM entry if a NMI
10508 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010509 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010510 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010511 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010512 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010513 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010514 /* fall through */
10515 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010516 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010517 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010518 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010519 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010520 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010521 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010522 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010523 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010524 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010525 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010526 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010527 break;
10528 default:
10529 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010530 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010531}
10532
Avi Kivity83422e12010-07-20 14:43:23 +030010533static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10534{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010535 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010536 VM_EXIT_INSTRUCTION_LEN,
10537 IDT_VECTORING_ERROR_CODE);
10538}
10539
Avi Kivityb463a6f2010-07-20 15:06:17 +030010540static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10541{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010542 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010543 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10544 VM_ENTRY_INSTRUCTION_LEN,
10545 VM_ENTRY_EXCEPTION_ERROR_CODE);
10546
10547 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10548}
10549
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010550static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10551{
10552 int i, nr_msrs;
10553 struct perf_guest_switch_msr *msrs;
10554
10555 msrs = perf_guest_get_msrs(&nr_msrs);
10556
10557 if (!msrs)
10558 return;
10559
10560 for (i = 0; i < nr_msrs; i++)
10561 if (msrs[i].host == msrs[i].guest)
10562 clear_atomic_switch_msr(vmx, msrs[i].msr);
10563 else
10564 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010565 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010566}
10567
Sean Christophersonf459a702018-08-27 15:21:11 -070010568static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10569{
10570 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10571 if (!vmx->loaded_vmcs->hv_timer_armed)
10572 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10573 PIN_BASED_VMX_PREEMPTION_TIMER);
10574 vmx->loaded_vmcs->hv_timer_armed = true;
10575}
10576
10577static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010578{
10579 struct vcpu_vmx *vmx = to_vmx(vcpu);
10580 u64 tscl;
10581 u32 delta_tsc;
10582
Sean Christophersond264ee02018-08-27 15:21:12 -070010583 if (vmx->req_immediate_exit) {
10584 vmx_arm_hv_timer(vmx, 0);
10585 return;
10586 }
10587
Sean Christophersonf459a702018-08-27 15:21:11 -070010588 if (vmx->hv_deadline_tsc != -1) {
10589 tscl = rdtsc();
10590 if (vmx->hv_deadline_tsc > tscl)
10591 /* set_hv_timer ensures the delta fits in 32-bits */
10592 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10593 cpu_preemption_timer_multi);
10594 else
10595 delta_tsc = 0;
10596
10597 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010598 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010599 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010600
Sean Christophersonf459a702018-08-27 15:21:11 -070010601 if (vmx->loaded_vmcs->hv_timer_armed)
10602 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10603 PIN_BASED_VMX_PREEMPTION_TIMER);
10604 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010605}
10606
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010607static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010608{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010609 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010610 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010611
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010612 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010613 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010614 vmx->loaded_vmcs->soft_vnmi_blocked))
10615 vmx->loaded_vmcs->entry_time = ktime_get();
10616
Avi Kivity104f2262010-11-18 13:12:52 +020010617 /* Don't enter VMX if guest state is invalid, let the exit handler
10618 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010619 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010620 return;
10621
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010622 if (vmx->ple_window_dirty) {
10623 vmx->ple_window_dirty = false;
10624 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10625 }
10626
Abel Gordon012f83c2013-04-18 14:39:25 +030010627 if (vmx->nested.sync_shadow_vmcs) {
10628 copy_vmcs12_to_shadow(vmx);
10629 vmx->nested.sync_shadow_vmcs = false;
10630 }
10631
Avi Kivity104f2262010-11-18 13:12:52 +020010632 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10633 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10634 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10635 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10636
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010637 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010638 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010639 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010640 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010641 }
10642
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010643 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010644 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010645 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010646 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010647 }
10648
Avi Kivity104f2262010-11-18 13:12:52 +020010649 /* When single-stepping over STI and MOV SS, we must clear the
10650 * corresponding interruptibility bits in the guest state. Otherwise
10651 * vmentry fails as it then expects bit 14 (BS) in pending debug
10652 * exceptions being set, but that's not correct for the guest debugging
10653 * case. */
10654 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10655 vmx_set_interrupt_shadow(vcpu, 0);
10656
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010657 if (static_cpu_has(X86_FEATURE_PKU) &&
10658 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10659 vcpu->arch.pkru != vmx->host_pkru)
10660 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010661
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010662 atomic_switch_perf_msrs(vmx);
10663
Sean Christophersonf459a702018-08-27 15:21:11 -070010664 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010665
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010666 /*
10667 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10668 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10669 * is no need to worry about the conditional branch over the wrmsr
10670 * being speculatively taken.
10671 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010672 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010673
Nadav Har'Eld462b812011-05-24 15:26:10 +030010674 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010675
10676 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10677 (unsigned long)&current_evmcs->host_rsp : 0;
10678
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010679 if (static_branch_unlikely(&vmx_l1d_should_flush))
10680 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010681
Avi Kivity104f2262010-11-18 13:12:52 +020010682 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010683 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010684 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10685 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10686 "push %%" _ASM_CX " \n\t"
10687 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010688 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010689 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010690 /* Avoid VMWRITE when Enlightened VMCS is in use */
10691 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10692 "jz 2f \n\t"
10693 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10694 "jmp 1f \n\t"
10695 "2: \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010696 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010697 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010698 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010699 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10700 "mov %%cr2, %%" _ASM_DX " \n\t"
10701 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010702 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010703 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010704 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010705 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010706 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010707 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010708 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10709 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10710 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10711 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10712 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10713 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010714#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010715 "mov %c[r8](%0), %%r8 \n\t"
10716 "mov %c[r9](%0), %%r9 \n\t"
10717 "mov %c[r10](%0), %%r10 \n\t"
10718 "mov %c[r11](%0), %%r11 \n\t"
10719 "mov %c[r12](%0), %%r12 \n\t"
10720 "mov %c[r13](%0), %%r13 \n\t"
10721 "mov %c[r14](%0), %%r14 \n\t"
10722 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010723#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010724 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010725
Avi Kivity6aa8b732006-12-10 02:21:36 -080010726 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010727 "jne 1f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010728 __ex("vmlaunch") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010729 "jmp 2f \n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020010730 "1: " __ex("vmresume") "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010731 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010732 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010733 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010734 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010735 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010736 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10737 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10738 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10739 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10740 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10741 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10742 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010743#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010744 "mov %%r8, %c[r8](%0) \n\t"
10745 "mov %%r9, %c[r9](%0) \n\t"
10746 "mov %%r10, %c[r10](%0) \n\t"
10747 "mov %%r11, %c[r11](%0) \n\t"
10748 "mov %%r12, %c[r12](%0) \n\t"
10749 "mov %%r13, %c[r13](%0) \n\t"
10750 "mov %%r14, %c[r14](%0) \n\t"
10751 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010752 "xor %%r8d, %%r8d \n\t"
10753 "xor %%r9d, %%r9d \n\t"
10754 "xor %%r10d, %%r10d \n\t"
10755 "xor %%r11d, %%r11d \n\t"
10756 "xor %%r12d, %%r12d \n\t"
10757 "xor %%r13d, %%r13d \n\t"
10758 "xor %%r14d, %%r14d \n\t"
10759 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010760#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010761 "mov %%cr2, %%" _ASM_AX " \n\t"
10762 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010763
Jim Mattson0cb5b302018-01-03 14:31:38 -080010764 "xor %%eax, %%eax \n\t"
10765 "xor %%ebx, %%ebx \n\t"
10766 "xor %%esi, %%esi \n\t"
10767 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010768 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010769 ".pushsection .rodata \n\t"
10770 ".global vmx_return \n\t"
10771 "vmx_return: " _ASM_PTR " 2b \n\t"
10772 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010773 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010774 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010775 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010776 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010777 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10778 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10779 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10780 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10781 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10782 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10783 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010784#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010785 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10786 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10787 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10788 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10789 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10790 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10791 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10792 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010793#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010794 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10795 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010796 : "cc", "memory"
10797#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010798 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010799 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010800#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010801 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010802#endif
10803 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010804
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010805 /*
10806 * We do not use IBRS in the kernel. If this vCPU has used the
10807 * SPEC_CTRL MSR it may have left it on; save the value and
10808 * turn it off. This is much more efficient than blindly adding
10809 * it to the atomic save/restore list. Especially as the former
10810 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10811 *
10812 * For non-nested case:
10813 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10814 * save it.
10815 *
10816 * For nested case:
10817 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10818 * save it.
10819 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010820 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010821 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010822
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010823 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010824
David Woodhouse117cc7a2018-01-12 11:11:27 +000010825 /* Eliminate branch target predictions from guest mode */
10826 vmexit_fill_RSB();
10827
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010828 /* All fields are clean at this point */
10829 if (static_branch_unlikely(&enable_evmcs))
10830 current_evmcs->hv_clean_fields |=
10831 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10832
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010833 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010834 if (vmx->host_debugctlmsr)
10835 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010836
Avi Kivityaa67f602012-08-01 16:48:03 +030010837#ifndef CONFIG_X86_64
10838 /*
10839 * The sysexit path does not restore ds/es, so we must set them to
10840 * a reasonable value ourselves.
10841 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010842 * We can't defer this to vmx_prepare_switch_to_host() since that
10843 * function may be executed in interrupt context, which saves and
10844 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010845 */
10846 loadsegment(ds, __USER_DS);
10847 loadsegment(es, __USER_DS);
10848#endif
10849
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010850 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010851 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010852 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010853 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010854 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010855 vcpu->arch.regs_dirty = 0;
10856
Gleb Natapove0b890d2013-09-25 12:51:33 +030010857 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010858 * eager fpu is enabled if PKEY is supported and CR4 is switched
10859 * back on host, so it is safe to read guest PKRU from current
10860 * XSAVE.
10861 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010862 if (static_cpu_has(X86_FEATURE_PKU) &&
10863 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10864 vcpu->arch.pkru = __read_pkru();
10865 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010866 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010867 }
10868
Gleb Natapove0b890d2013-09-25 12:51:33 +030010869 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010870 vmx->idt_vectoring_info = 0;
10871
10872 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10873 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10874 return;
10875
10876 vmx->loaded_vmcs->launched = 1;
10877 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010878
Avi Kivity51aa01d2010-07-20 14:31:20 +030010879 vmx_complete_atomic_exit(vmx);
10880 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010881 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010882}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010883STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010884
Sean Christopherson434a1e92018-03-20 12:17:18 -070010885static struct kvm *vmx_vm_alloc(void)
10886{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010887 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010888 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010889}
10890
10891static void vmx_vm_free(struct kvm *kvm)
10892{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010893 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010894}
10895
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010896static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010897{
10898 struct vcpu_vmx *vmx = to_vmx(vcpu);
10899 int cpu;
10900
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010901 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010902 return;
10903
10904 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010905 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010906 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010907 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010908 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010909
10910 vm_entry_controls_reset_shadow(vmx);
10911 vm_exit_controls_reset_shadow(vmx);
10912 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010913}
10914
Jim Mattson2f1fe812016-07-08 15:36:06 -070010915/*
10916 * Ensure that the current vmcs of the logical processor is the
10917 * vmcs01 of the vcpu before calling free_nested().
10918 */
10919static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10920{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020010921 vcpu_load(vcpu);
10922 vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01);
10923 free_nested(vcpu);
10924 vcpu_put(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010925}
10926
Avi Kivity6aa8b732006-12-10 02:21:36 -080010927static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10928{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010929 struct vcpu_vmx *vmx = to_vmx(vcpu);
10930
Kai Huang843e4332015-01-28 10:54:28 +080010931 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010932 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010933 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010934 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010935 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010936 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010937 kfree(vmx->guest_msrs);
10938 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010939 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010940}
10941
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010942static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010943{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010944 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010945 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010946 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010947 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010948
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010949 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010950 return ERR_PTR(-ENOMEM);
10951
Wanpeng Li991e7a02015-09-16 17:30:05 +080010952 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010953
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010954 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10955 if (err)
10956 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010957
Peter Feiner4e595162016-07-07 14:49:58 -070010958 err = -ENOMEM;
10959
10960 /*
10961 * If PML is turned on, failure on enabling PML just results in failure
10962 * of creating the vcpu, therefore we can simplify PML logic (by
10963 * avoiding dealing with cases, such as enabling PML partially on vcpus
10964 * for the guest, etc.
10965 */
10966 if (enable_pml) {
10967 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10968 if (!vmx->pml_pg)
10969 goto uninit_vcpu;
10970 }
10971
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010972 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010973 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10974 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010975
Peter Feiner4e595162016-07-07 14:49:58 -070010976 if (!vmx->guest_msrs)
10977 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010978
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010979 err = alloc_loaded_vmcs(&vmx->vmcs01);
10980 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010981 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010982
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010983 msr_bitmap = vmx->vmcs01.msr_bitmap;
10984 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10985 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10986 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10987 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10988 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10989 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10990 vmx->msr_bitmap_mode = 0;
10991
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010992 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010993 cpu = get_cpu();
10994 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010995 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010996 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010997 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010998 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010999 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011000 err = alloc_apic_access_page(kvm);
11001 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011002 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011003 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011004
Sean Christophersone90008d2018-03-05 12:04:37 -080011005 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011006 err = init_rmode_identity_map(kvm);
11007 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011008 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011009 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011010
Roman Kagan63aff652018-07-19 21:59:07 +030011011 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011012 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11013 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011014
Wincy Van705699a2015-02-03 23:58:17 +080011015 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011016 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011017
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011018 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11019
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011020 /*
11021 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11022 * or POSTED_INTR_WAKEUP_VECTOR.
11023 */
11024 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11025 vmx->pi_desc.sn = 1;
11026
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011027 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011028
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011029free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011030 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011031free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011032 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011033free_pml:
11034 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011035uninit_vcpu:
11036 kvm_vcpu_uninit(&vmx->vcpu);
11037free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011038 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011039 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011040 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011041}
11042
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011043#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"
11044#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 -040011045
Wanpeng Lib31c1142018-03-12 04:53:04 -070011046static int vmx_vm_init(struct kvm *kvm)
11047{
Tianyu Lan877ad952018-07-19 08:40:23 +000011048 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11049
Wanpeng Lib31c1142018-03-12 04:53:04 -070011050 if (!ple_gap)
11051 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011052
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011053 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11054 switch (l1tf_mitigation) {
11055 case L1TF_MITIGATION_OFF:
11056 case L1TF_MITIGATION_FLUSH_NOWARN:
11057 /* 'I explicitly don't care' is set */
11058 break;
11059 case L1TF_MITIGATION_FLUSH:
11060 case L1TF_MITIGATION_FLUSH_NOSMT:
11061 case L1TF_MITIGATION_FULL:
11062 /*
11063 * Warn upon starting the first VM in a potentially
11064 * insecure environment.
11065 */
11066 if (cpu_smt_control == CPU_SMT_ENABLED)
11067 pr_warn_once(L1TF_MSG_SMT);
11068 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11069 pr_warn_once(L1TF_MSG_L1D);
11070 break;
11071 case L1TF_MITIGATION_FULL_FORCE:
11072 /* Flush is enforced */
11073 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011074 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011075 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011076 return 0;
11077}
11078
Yang, Sheng002c7f72007-07-31 14:23:01 +030011079static void __init vmx_check_processor_compat(void *rtn)
11080{
11081 struct vmcs_config vmcs_conf;
11082
11083 *(int *)rtn = 0;
11084 if (setup_vmcs_config(&vmcs_conf) < 0)
11085 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011086 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011087 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11088 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11089 smp_processor_id());
11090 *(int *)rtn = -EIO;
11091 }
11092}
11093
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011094static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011095{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011096 u8 cache;
11097 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011098
Sheng Yang522c68c2009-04-27 20:35:43 +080011099 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011100 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011101 * 2. EPT with VT-d:
11102 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011103 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011104 * b. VT-d with snooping control feature: snooping control feature of
11105 * VT-d engine can guarantee the cache correctness. Just set it
11106 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011107 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011108 * consistent with host MTRR
11109 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011110 if (is_mmio) {
11111 cache = MTRR_TYPE_UNCACHABLE;
11112 goto exit;
11113 }
11114
11115 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011116 ipat = VMX_EPT_IPAT_BIT;
11117 cache = MTRR_TYPE_WRBACK;
11118 goto exit;
11119 }
11120
11121 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11122 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011123 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011124 cache = MTRR_TYPE_WRBACK;
11125 else
11126 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011127 goto exit;
11128 }
11129
Xiao Guangrongff536042015-06-15 16:55:22 +080011130 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011131
11132exit:
11133 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011134}
11135
Sheng Yang17cc3932010-01-05 19:02:27 +080011136static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011137{
Sheng Yang878403b2010-01-05 19:02:29 +080011138 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11139 return PT_DIRECTORY_LEVEL;
11140 else
11141 /* For shadow and EPT supported 1GB page */
11142 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011143}
11144
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011145static void vmcs_set_secondary_exec_control(u32 new_ctl)
11146{
11147 /*
11148 * These bits in the secondary execution controls field
11149 * are dynamic, the others are mostly based on the hypervisor
11150 * architecture and the guest's CPUID. Do not touch the
11151 * dynamic bits.
11152 */
11153 u32 mask =
11154 SECONDARY_EXEC_SHADOW_VMCS |
11155 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011156 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11157 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011158
11159 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11160
11161 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11162 (new_ctl & ~mask) | (cur_ctl & mask));
11163}
11164
David Matlack8322ebb2016-11-29 18:14:09 -080011165/*
11166 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11167 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11168 */
11169static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11170{
11171 struct vcpu_vmx *vmx = to_vmx(vcpu);
11172 struct kvm_cpuid_entry2 *entry;
11173
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011174 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11175 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011176
11177#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11178 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011179 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011180} while (0)
11181
11182 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11183 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11184 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11185 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11186 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11187 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11188 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11189 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11190 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11191 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11192 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11193 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11194 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11195 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11196 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11197
11198 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11199 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11200 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11201 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11202 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011203 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011204
11205#undef cr4_fixed1_update
11206}
11207
Liran Alon5f76f6f2018-09-14 03:25:52 +030011208static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11209{
11210 struct vcpu_vmx *vmx = to_vmx(vcpu);
11211
11212 if (kvm_mpx_supported()) {
11213 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11214
11215 if (mpx_enabled) {
11216 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11217 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11218 } else {
11219 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11220 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11221 }
11222 }
11223}
11224
Sheng Yang0e851882009-12-18 16:48:46 +080011225static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11226{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011227 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011228
Paolo Bonzini80154d72017-08-24 13:55:35 +020011229 if (cpu_has_secondary_exec_ctrls()) {
11230 vmx_compute_secondary_exec_control(vmx);
11231 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011232 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011233
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011234 if (nested_vmx_allowed(vcpu))
11235 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11236 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11237 else
11238 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11239 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011240
Liran Alon5f76f6f2018-09-14 03:25:52 +030011241 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011242 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011243 nested_vmx_entry_exit_ctls_update(vcpu);
11244 }
Sheng Yang0e851882009-12-18 16:48:46 +080011245}
11246
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011247static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11248{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011249 if (func == 1 && nested)
11250 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011251}
11252
Yang Zhang25d92082013-08-06 12:00:32 +030011253static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11254 struct x86_exception *fault)
11255{
Jan Kiszka533558b2014-01-04 18:47:20 +010011256 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011258 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011259 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011260
Bandan Dasc5f983f2017-05-05 15:25:14 -040011261 if (vmx->nested.pml_full) {
11262 exit_reason = EXIT_REASON_PML_FULL;
11263 vmx->nested.pml_full = false;
11264 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11265 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011266 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011267 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011268 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011269
11270 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011271 vmcs12->guest_physical_address = fault->address;
11272}
11273
Peter Feiner995f00a2017-06-30 17:26:32 -070011274static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11275{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011276 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011277}
11278
Nadav Har'El155a97a2013-08-05 11:07:16 +030011279/* Callbacks for nested_ept_init_mmu_context: */
11280
11281static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11282{
11283 /* return the page table to be shadowed - in our case, EPT12 */
11284 return get_vmcs12(vcpu)->ept_pointer;
11285}
11286
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011287static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011288{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011289 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011290
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011291 vcpu->arch.mmu = &vcpu->arch.guest_mmu;
Paolo Bonziniad896af2013-10-02 16:56:14 +020011292 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011293 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011294 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011295 nested_ept_ad_enabled(vcpu),
11296 nested_ept_get_cr3(vcpu));
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011297 vcpu->arch.mmu->set_cr3 = vmx_set_cr3;
11298 vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3;
11299 vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault;
Vitaly Kuznetsov3dc773e2018-10-08 21:28:06 +020011300 vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011301
11302 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011303}
11304
11305static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11306{
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020011307 vcpu->arch.mmu = &vcpu->arch.root_mmu;
Vitaly Kuznetsov44dd3ff2018-10-08 21:28:05 +020011308 vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011309}
11310
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011311static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11312 u16 error_code)
11313{
11314 bool inequality, bit;
11315
11316 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11317 inequality =
11318 (error_code & vmcs12->page_fault_error_code_mask) !=
11319 vmcs12->page_fault_error_code_match;
11320 return inequality ^ bit;
11321}
11322
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011323static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11324 struct x86_exception *fault)
11325{
11326 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11327
11328 WARN_ON(!is_guest_mode(vcpu));
11329
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011330 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11331 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011332 vmcs12->vm_exit_intr_error_code = fault->error_code;
11333 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11334 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11335 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11336 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011337 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011338 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011339 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011340}
11341
Paolo Bonzinic9923842017-12-13 14:16:30 +010011342static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11343 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011344
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011345static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011346{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011347 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011348 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011349 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011350 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011351
11352 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011353 /*
11354 * Translate L1 physical address to host physical
11355 * address for vmcs02. Keep the page pinned, so this
11356 * physical address remains valid. We keep a reference
11357 * to it so we can release it later.
11358 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011359 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011360 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011361 vmx->nested.apic_access_page = NULL;
11362 }
11363 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011364 /*
11365 * If translation failed, no matter: This feature asks
11366 * to exit when accessing the given address, and if it
11367 * can never be accessed, this feature won't do
11368 * anything anyway.
11369 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011370 if (!is_error_page(page)) {
11371 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011372 hpa = page_to_phys(vmx->nested.apic_access_page);
11373 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11374 } else {
11375 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11376 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11377 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011378 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011379
11380 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011381 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011382 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011383 vmx->nested.virtual_apic_page = NULL;
11384 }
11385 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011386
11387 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011388 * If translation failed, VM entry will fail because
11389 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11390 * Failing the vm entry is _not_ what the processor
11391 * does but it's basically the only possibility we
11392 * have. We could still enter the guest if CR8 load
11393 * exits are enabled, CR8 store exits are enabled, and
11394 * virtualize APIC access is disabled; in this case
11395 * the processor would never use the TPR shadow and we
11396 * could simply clear the bit from the execution
11397 * control. But such a configuration is useless, so
11398 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011399 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011400 if (!is_error_page(page)) {
11401 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011402 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11403 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11404 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011405 }
11406
Wincy Van705699a2015-02-03 23:58:17 +080011407 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011408 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11409 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011410 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011411 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011412 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011413 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11414 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011415 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011416 vmx->nested.pi_desc_page = page;
11417 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011418 vmx->nested.pi_desc =
11419 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11420 (unsigned long)(vmcs12->posted_intr_desc_addr &
11421 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011422 vmcs_write64(POSTED_INTR_DESC_ADDR,
11423 page_to_phys(vmx->nested.pi_desc_page) +
11424 (unsigned long)(vmcs12->posted_intr_desc_addr &
11425 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011426 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011427 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011428 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11429 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011430 else
11431 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11432 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011433}
11434
Jan Kiszkaf41245002014-03-07 20:03:13 +010011435static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11436{
11437 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11438 struct vcpu_vmx *vmx = to_vmx(vcpu);
11439
Sean Christopherson4c008122018-08-27 15:21:10 -070011440 /*
11441 * A timer value of zero is architecturally guaranteed to cause
11442 * a VMExit prior to executing any instructions in the guest.
11443 */
11444 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010011445 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11446 return;
11447 }
11448
Sean Christopherson4c008122018-08-27 15:21:10 -070011449 if (vcpu->arch.virtual_tsc_khz == 0)
11450 return;
11451
Jan Kiszkaf41245002014-03-07 20:03:13 +010011452 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11453 preemption_timeout *= 1000000;
11454 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11455 hrtimer_start(&vmx->nested.preemption_timer,
11456 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11457}
11458
Jim Mattson56a20512017-07-06 16:33:06 -070011459static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11460 struct vmcs12 *vmcs12)
11461{
11462 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11463 return 0;
11464
11465 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11466 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11467 return -EINVAL;
11468
11469 return 0;
11470}
11471
Wincy Van3af18d92015-02-03 23:49:31 +080011472static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11473 struct vmcs12 *vmcs12)
11474{
Wincy Van3af18d92015-02-03 23:49:31 +080011475 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11476 return 0;
11477
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011478 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011479 return -EINVAL;
11480
11481 return 0;
11482}
11483
Jim Mattson712b12d2017-08-24 13:24:47 -070011484static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11485 struct vmcs12 *vmcs12)
11486{
11487 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11488 return 0;
11489
11490 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11491 return -EINVAL;
11492
11493 return 0;
11494}
11495
Wincy Van3af18d92015-02-03 23:49:31 +080011496/*
11497 * Merge L0's and L1's MSR bitmap, return false to indicate that
11498 * we do not use the hardware.
11499 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011500static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11501 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011502{
Wincy Van82f0dd42015-02-03 23:57:18 +080011503 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011504 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011505 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011506 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011507 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011508 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011509 *
11510 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11511 * ensures that we do not accidentally generate an L02 MSR bitmap
11512 * from the L12 MSR bitmap that is too permissive.
11513 * 2. That L1 or L2s have actually used the MSR. This avoids
11514 * unnecessarily merging of the bitmap if the MSR is unused. This
11515 * works properly because we only update the L01 MSR bitmap lazily.
11516 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11517 * updated to reflect this when L1 (or its L2s) actually write to
11518 * the MSR.
11519 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011520 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11521 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011522
Paolo Bonzinic9923842017-12-13 14:16:30 +010011523 /* Nothing to do if the MSR bitmap is not in use. */
11524 if (!cpu_has_vmx_msr_bitmap() ||
11525 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11526 return false;
11527
Ashok Raj15d45072018-02-01 22:59:43 +010011528 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011529 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011530 return false;
11531
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011532 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11533 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011534 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011535
Radim Krčmářd048c092016-08-08 20:16:22 +020011536 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011537 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11538 /*
11539 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11540 * just lets the processor take the value from the virtual-APIC page;
11541 * take those 256 bits directly from the L1 bitmap.
11542 */
11543 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11544 unsigned word = msr / BITS_PER_LONG;
11545 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11546 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011547 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011548 } else {
11549 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11550 unsigned word = msr / BITS_PER_LONG;
11551 msr_bitmap_l0[word] = ~0;
11552 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11553 }
11554 }
11555
11556 nested_vmx_disable_intercept_for_msr(
11557 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011558 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011559 MSR_TYPE_W);
11560
11561 if (nested_cpu_has_vid(vmcs12)) {
11562 nested_vmx_disable_intercept_for_msr(
11563 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011564 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011565 MSR_TYPE_W);
11566 nested_vmx_disable_intercept_for_msr(
11567 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011568 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011569 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011570 }
Ashok Raj15d45072018-02-01 22:59:43 +010011571
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011572 if (spec_ctrl)
11573 nested_vmx_disable_intercept_for_msr(
11574 msr_bitmap_l1, msr_bitmap_l0,
11575 MSR_IA32_SPEC_CTRL,
11576 MSR_TYPE_R | MSR_TYPE_W);
11577
Ashok Raj15d45072018-02-01 22:59:43 +010011578 if (pred_cmd)
11579 nested_vmx_disable_intercept_for_msr(
11580 msr_bitmap_l1, msr_bitmap_l0,
11581 MSR_IA32_PRED_CMD,
11582 MSR_TYPE_W);
11583
Wincy Vanf2b93282015-02-03 23:56:03 +080011584 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011585 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011586
11587 return true;
11588}
11589
Liran Alon61ada742018-06-23 02:35:08 +030011590static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11591 struct vmcs12 *vmcs12)
11592{
11593 struct vmcs12 *shadow;
11594 struct page *page;
11595
11596 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11597 vmcs12->vmcs_link_pointer == -1ull)
11598 return;
11599
11600 shadow = get_shadow_vmcs12(vcpu);
11601 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11602
11603 memcpy(shadow, kmap(page), VMCS12_SIZE);
11604
11605 kunmap(page);
11606 kvm_release_page_clean(page);
11607}
11608
11609static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11610 struct vmcs12 *vmcs12)
11611{
11612 struct vcpu_vmx *vmx = to_vmx(vcpu);
11613
11614 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11615 vmcs12->vmcs_link_pointer == -1ull)
11616 return;
11617
11618 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11619 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11620}
11621
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011622static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11623 struct vmcs12 *vmcs12)
11624{
11625 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11626 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11627 return -EINVAL;
11628 else
11629 return 0;
11630}
11631
Wincy Vanf2b93282015-02-03 23:56:03 +080011632static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11633 struct vmcs12 *vmcs12)
11634{
Wincy Van82f0dd42015-02-03 23:57:18 +080011635 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011636 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011637 !nested_cpu_has_vid(vmcs12) &&
11638 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011639 return 0;
11640
11641 /*
11642 * If virtualize x2apic mode is enabled,
11643 * virtualize apic access must be disabled.
11644 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011645 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11646 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011647 return -EINVAL;
11648
Wincy Van608406e2015-02-03 23:57:51 +080011649 /*
11650 * If virtual interrupt delivery is enabled,
11651 * we must exit on external interrupts.
11652 */
11653 if (nested_cpu_has_vid(vmcs12) &&
11654 !nested_exit_on_intr(vcpu))
11655 return -EINVAL;
11656
Wincy Van705699a2015-02-03 23:58:17 +080011657 /*
11658 * bits 15:8 should be zero in posted_intr_nv,
11659 * the descriptor address has been already checked
11660 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011661 *
11662 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011663 */
11664 if (nested_cpu_has_posted_intr(vmcs12) &&
11665 (!nested_cpu_has_vid(vmcs12) ||
11666 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011667 (vmcs12->posted_intr_nv & 0xff00) ||
11668 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11669 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011670 return -EINVAL;
11671
Wincy Vanf2b93282015-02-03 23:56:03 +080011672 /* tpr shadow is needed by all apicv features. */
11673 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11674 return -EINVAL;
11675
11676 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011677}
11678
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011679static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11680 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011681 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011682{
Liran Alone2536742018-06-23 02:35:02 +030011683 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011684 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011685 u64 count, addr;
11686
Liran Alone2536742018-06-23 02:35:02 +030011687 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11688 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011689 WARN_ON(1);
11690 return -EINVAL;
11691 }
11692 if (count == 0)
11693 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011694 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011695 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11696 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011697 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011698 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11699 addr_field, maxphyaddr, count, addr);
11700 return -EINVAL;
11701 }
11702 return 0;
11703}
11704
11705static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11706 struct vmcs12 *vmcs12)
11707{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011708 if (vmcs12->vm_exit_msr_load_count == 0 &&
11709 vmcs12->vm_exit_msr_store_count == 0 &&
11710 vmcs12->vm_entry_msr_load_count == 0)
11711 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011712 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011713 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011714 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011715 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011716 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011717 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011718 return -EINVAL;
11719 return 0;
11720}
11721
Bandan Dasc5f983f2017-05-05 15:25:14 -040011722static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11723 struct vmcs12 *vmcs12)
11724{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011725 if (!nested_cpu_has_pml(vmcs12))
11726 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011727
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011728 if (!nested_cpu_has_ept(vmcs12) ||
11729 !page_address_valid(vcpu, vmcs12->pml_address))
11730 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011731
11732 return 0;
11733}
11734
Liran Alona8a7c022018-06-23 02:35:06 +030011735static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11736 struct vmcs12 *vmcs12)
11737{
11738 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11739 return 0;
11740
11741 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11742 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11743 return -EINVAL;
11744
11745 return 0;
11746}
11747
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011748static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11749 struct vmx_msr_entry *e)
11750{
11751 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011752 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011753 return -EINVAL;
11754 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11755 e->index == MSR_IA32_UCODE_REV)
11756 return -EINVAL;
11757 if (e->reserved != 0)
11758 return -EINVAL;
11759 return 0;
11760}
11761
11762static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11763 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011764{
11765 if (e->index == MSR_FS_BASE ||
11766 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011767 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11768 nested_vmx_msr_check_common(vcpu, e))
11769 return -EINVAL;
11770 return 0;
11771}
11772
11773static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11774 struct vmx_msr_entry *e)
11775{
11776 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11777 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011778 return -EINVAL;
11779 return 0;
11780}
11781
11782/*
11783 * Load guest's/host's msr at nested entry/exit.
11784 * return 0 for success, entry index for failure.
11785 */
11786static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11787{
11788 u32 i;
11789 struct vmx_msr_entry e;
11790 struct msr_data msr;
11791
11792 msr.host_initiated = false;
11793 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011794 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11795 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011796 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011797 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11798 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011799 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011800 }
11801 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011802 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011803 "%s check failed (%u, 0x%x, 0x%x)\n",
11804 __func__, i, e.index, e.reserved);
11805 goto fail;
11806 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011807 msr.index = e.index;
11808 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011809 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011810 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011811 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11812 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011813 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011814 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011815 }
11816 return 0;
11817fail:
11818 return i + 1;
11819}
11820
11821static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11822{
11823 u32 i;
11824 struct vmx_msr_entry e;
11825
11826 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011827 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011828 if (kvm_vcpu_read_guest(vcpu,
11829 gpa + i * sizeof(e),
11830 &e, 2 * sizeof(u32))) {
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 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011835 }
11836 if (nested_vmx_store_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);
Wincy Vanff651cb2014-12-11 08:52:58 +030011840 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011841 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011842 msr_info.host_initiated = false;
11843 msr_info.index = e.index;
11844 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011845 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011846 "%s cannot read MSR (%u, 0x%x)\n",
11847 __func__, i, e.index);
11848 return -EINVAL;
11849 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011850 if (kvm_vcpu_write_guest(vcpu,
11851 gpa + i * sizeof(e) +
11852 offsetof(struct vmx_msr_entry, value),
11853 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011854 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011855 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011856 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011857 return -EINVAL;
11858 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011859 }
11860 return 0;
11861}
11862
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011863static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11864{
11865 unsigned long invalid_mask;
11866
11867 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11868 return (val & invalid_mask) == 0;
11869}
11870
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011871/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011872 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11873 * emulating VM entry into a guest with EPT enabled.
11874 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11875 * is assigned to entry_failure_code on failure.
11876 */
11877static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011878 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011879{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011880 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011881 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011882 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11883 return 1;
11884 }
11885
11886 /*
11887 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11888 * must not be dereferenced.
11889 */
11890 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11891 !nested_ept) {
11892 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11893 *entry_failure_code = ENTRY_FAIL_PDPTE;
11894 return 1;
11895 }
11896 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011897 }
11898
Junaid Shahid50c28f22018-06-27 14:59:11 -070011899 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011900 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011901
11902 vcpu->arch.cr3 = cr3;
11903 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11904
11905 kvm_init_mmu(vcpu, false);
11906
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011907 return 0;
11908}
11909
Liran Alonefebf0a2018-10-08 23:42:20 +030011910/*
11911 * Returns if KVM is able to config CPU to tag TLB entries
11912 * populated by L2 differently than TLB entries populated
11913 * by L1.
11914 *
11915 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11916 *
11917 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11918 * with different VPID (L1 entries are tagged with vmx->vpid
11919 * while L2 entries are tagged with vmx->nested.vpid02).
11920 */
11921static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11922{
11923 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11924
11925 return nested_cpu_has_ept(vmcs12) ||
11926 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11927}
11928
Sean Christopherson3df5c372018-09-26 09:23:44 -070011929static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11930{
11931 if (vmx->nested.nested_run_pending &&
11932 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11933 return vmcs12->guest_ia32_efer;
11934 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11935 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11936 else
11937 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11938}
11939
Sean Christopherson09abe322018-09-26 09:23:50 -070011940static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011941{
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011942 /*
Sean Christopherson9d6105b2018-09-26 09:23:51 -070011943 * If vmcs02 hasn't been initialized, set the constant vmcs02 state
Sean Christopherson09abe322018-09-26 09:23:50 -070011944 * according to L0's settings (vmcs12 is irrelevant here). Host
11945 * fields that come from L0 and are not constant, e.g. HOST_CR3,
11946 * will be set as needed prior to VMLAUNCH/VMRESUME.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011947 */
Sean Christopherson9d6105b2018-09-26 09:23:51 -070011948 if (vmx->nested.vmcs02_initialized)
Sean Christopherson09abe322018-09-26 09:23:50 -070011949 return;
Sean Christopherson9d6105b2018-09-26 09:23:51 -070011950 vmx->nested.vmcs02_initialized = true;
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011951
Sean Christopherson52017602018-09-26 09:23:57 -070011952 /*
11953 * We don't care what the EPTP value is we just need to guarantee
11954 * it's valid so we don't get a false positive when doing early
11955 * consistency checks.
11956 */
11957 if (enable_ept && nested_early_check)
11958 vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0));
11959
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011960 /* All VMFUNCs are currently emulated through L0 vmexits. */
11961 if (cpu_has_vmx_vmfunc())
11962 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11963
Sean Christopherson09abe322018-09-26 09:23:50 -070011964 if (cpu_has_vmx_posted_intr())
11965 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11966
11967 if (cpu_has_vmx_msr_bitmap())
11968 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
11969
11970 if (enable_pml)
11971 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011972
11973 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070011974 * Set the MSR load/store lists to match L0's settings. Only the
11975 * addresses are constant (for vmcs02), the counts can change based
11976 * on L2's behavior, e.g. switching to/from long mode.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011977 */
11978 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011979 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011980 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011981
Sean Christopherson09abe322018-09-26 09:23:50 -070011982 vmx_set_constant_host_state(vmx);
11983}
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011984
Sean Christopherson09abe322018-09-26 09:23:50 -070011985static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx,
11986 struct vmcs12 *vmcs12)
11987{
11988 prepare_vmcs02_constant_state(vmx);
11989
11990 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011991
11992 if (enable_vpid) {
11993 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11994 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11995 else
11996 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11997 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011998}
11999
Sean Christopherson09abe322018-09-26 09:23:50 -070012000static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012001{
Bandan Das03efce62017-05-05 15:25:15 -040012002 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson09abe322018-09-26 09:23:50 -070012003 u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012004
Sean Christopherson09abe322018-09-26 09:23:50 -070012005 if (vmx->nested.dirty_vmcs12)
12006 prepare_vmcs02_early_full(vmx, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012007
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012008 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012009 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12010 * entry, but only if the current (host) sp changed from the value
12011 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12012 * if we switch vmcs, and rather than hold a separate cache per vmcs,
Sean Christopherson52017602018-09-26 09:23:57 -070012013 * here we just force the write to happen on entry. host_rsp will
12014 * also be written unconditionally by nested_vmx_check_vmentry_hw()
12015 * if we are doing early consistency checks via hardware.
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012016 */
Sean Christopherson09abe322018-09-26 09:23:50 -070012017 vmx->host_rsp = 0;
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012018
Sean Christopherson09abe322018-09-26 09:23:50 -070012019 /*
12020 * PIN CONTROLS
12021 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012022 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012023
Sean Christophersonf459a702018-08-27 15:21:11 -070012024 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012025 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012026 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12027 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012028
12029 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012030 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012031 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12032 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012033 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012034 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012035 }
Jan Kiszkaf41245002014-03-07 20:03:13 +010012036 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012037
Sean Christopherson09abe322018-09-26 09:23:50 -070012038 /*
12039 * EXEC CONTROLS
12040 */
12041 exec_control = vmx_exec_control(vmx); /* L0's desires */
12042 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12043 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12044 exec_control &= ~CPU_BASED_TPR_SHADOW;
12045 exec_control |= vmcs12->cpu_based_vm_exec_control;
Jan Kiszka0238ea92013-03-13 11:31:24 +010012046
Sean Christopherson09abe322018-09-26 09:23:50 -070012047 /*
12048 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12049 * nested_get_vmcs12_pages can't fix it up, the illegal value
12050 * will result in a VM entry failure.
12051 */
12052 if (exec_control & CPU_BASED_TPR_SHADOW) {
12053 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
12054 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
12055 } else {
12056#ifdef CONFIG_X86_64
12057 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12058 CPU_BASED_CR8_STORE_EXITING;
12059#endif
12060 }
12061
12062 /*
12063 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12064 * for I/O port accesses.
12065 */
12066 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12067 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12068 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12069
12070 /*
12071 * SECONDARY EXEC CONTROLS
12072 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012073 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012074 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012075
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012076 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012077 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012078 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012079 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012080 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012081 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012082 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12083 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012084 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012085 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12086 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12087 ~SECONDARY_EXEC_ENABLE_PML;
12088 exec_control |= vmcs12_exec_ctrl;
12089 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012090
Liran Alon32c7acf2018-06-23 02:35:11 +030012091 /* VMCS shadowing for L2 is emulated for now */
12092 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12093
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012094 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012095 vmcs_write16(GUEST_INTR_STATUS,
12096 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012097
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012098 /*
12099 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12100 * nested_get_vmcs12_pages will either fix it up or
12101 * remove the VM execution control.
12102 */
12103 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12104 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12105
Sean Christopherson0b665d32018-08-14 09:33:34 -070012106 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12107 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12108
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012109 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12110 }
12111
Jim Mattson83bafef2016-10-04 10:48:38 -070012112 /*
Sean Christopherson09abe322018-09-26 09:23:50 -070012113 * ENTRY CONTROLS
12114 *
Sean Christopherson3df5c372018-09-26 09:23:44 -070012115 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
Sean Christopherson09abe322018-09-26 09:23:50 -070012116 * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate
12117 * on the related bits (if supported by the CPU) in the hope that
12118 * we can avoid VMWrites during vmx_set_efer().
Sean Christopherson3df5c372018-09-26 09:23:44 -070012119 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012120 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12121 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12122 if (cpu_has_load_ia32_efer) {
12123 if (guest_efer & EFER_LMA)
12124 exec_control |= VM_ENTRY_IA32E_MODE;
12125 if (guest_efer != host_efer)
12126 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12127 }
12128 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012129
Sean Christopherson09abe322018-09-26 09:23:50 -070012130 /*
12131 * EXIT CONTROLS
12132 *
12133 * L2->L1 exit controls are emulated - the hardware exit is to L0 so
12134 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12135 * bits may be modified by vmx_set_efer() in prepare_vmcs02().
12136 */
12137 exec_control = vmcs_config.vmexit_ctrl;
12138 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12139 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12140 vm_exit_controls_init(vmx, exec_control);
12141
12142 /*
12143 * Conceptually we want to copy the PML address and index from
12144 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12145 * since we always flush the log on each vmexit and never change
12146 * the PML address (once set), this happens to be equivalent to
12147 * simply resetting the index in vmcs02.
12148 */
12149 if (enable_pml)
12150 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12151
12152 /*
12153 * Interrupt/Exception Fields
12154 */
12155 if (vmx->nested.nested_run_pending) {
12156 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12157 vmcs12->vm_entry_intr_info_field);
12158 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12159 vmcs12->vm_entry_exception_error_code);
12160 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12161 vmcs12->vm_entry_instruction_len);
12162 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12163 vmcs12->guest_interruptibility_info);
12164 vmx->loaded_vmcs->nmi_known_unmasked =
12165 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
12166 } else {
12167 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12168 }
12169}
12170
12171static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
12172{
12173 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
12174 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
12175 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
12176 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
12177 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
12178 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
12179 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
12180 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
12181 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
12182 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12183 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12184 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12185 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12186 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12187 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12188 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12189 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12190 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12191 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12192 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12193 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12194 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12195 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12196 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12197 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12198 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12199 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12200 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12201 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12202 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12203 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
12204
12205 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12206 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12207 vmcs12->guest_pending_dbg_exceptions);
12208 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12209 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12210
12211 if (nested_cpu_has_xsaves(vmcs12))
12212 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12213
12214 /*
12215 * Whether page-faults are trapped is determined by a combination of
12216 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12217 * If enable_ept, L0 doesn't care about page faults and we should
12218 * set all of these to L1's desires. However, if !enable_ept, L0 does
12219 * care about (at least some) page faults, and because it is not easy
12220 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12221 * to exit on each and every L2 page fault. This is done by setting
12222 * MASK=MATCH=0 and (see below) EB.PF=1.
12223 * Note that below we don't need special code to set EB.PF beyond the
12224 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12225 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12226 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12227 */
12228 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12229 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12230 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12231 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12232
12233 if (cpu_has_vmx_apicv()) {
12234 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12235 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12236 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12237 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12238 }
12239
12240 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12241 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12242
12243 set_cr4_guest_host_mask(vmx);
12244
12245 if (kvm_mpx_supported()) {
12246 if (vmx->nested.nested_run_pending &&
12247 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12248 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12249 else
12250 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12251 }
12252
12253 /*
12254 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12255 */
12256 if (enable_ept) {
12257 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12258 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12259 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12260 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12261 }
12262}
12263
12264/*
12265 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12266 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
12267 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
12268 * guest in a way that will both be appropriate to L1's requests, and our
12269 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12270 * function also has additional necessary side-effects, like setting various
12271 * vcpu->arch fields.
12272 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12273 * is assigned to entry_failure_code on failure.
12274 */
12275static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12276 u32 *entry_failure_code)
12277{
12278 struct vcpu_vmx *vmx = to_vmx(vcpu);
12279
12280 if (vmx->nested.dirty_vmcs12) {
12281 prepare_vmcs02_full(vmx, vmcs12);
12282 vmx->nested.dirty_vmcs12 = false;
12283 }
12284
12285 /*
12286 * First, the fields that are shadowed. This must be kept in sync
12287 * with vmx_shadow_fields.h.
12288 */
12289
12290 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
12291 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
12292 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
12293 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12294 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
12295
12296 if (vmx->nested.nested_run_pending &&
12297 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
12298 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12299 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12300 } else {
12301 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12302 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12303 }
12304 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
12305
12306 vmx->nested.preemption_timer_expired = false;
12307 if (nested_cpu_has_preemption_timer(vmcs12))
12308 vmx_start_preemption_timer(vcpu);
12309
12310 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12311 * bitwise-or of what L1 wants to trap for L2, and what we want to
12312 * trap. Note that CR0.TS also needs updating - we do this later.
12313 */
12314 update_exception_bitmap(vcpu);
12315 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12316 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12317
Jim Mattson6514dc32018-04-26 16:09:12 -070012318 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012319 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012320 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012321 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012322 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012323 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012324 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012325
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012326 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12327
Peter Feinerc95ba922016-08-17 09:36:47 -070012328 if (kvm_has_tsc_control)
12329 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012330
12331 if (enable_vpid) {
12332 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012333 * There is no direct mapping between vpid02 and vpid12, the
12334 * vpid02 is per-vCPU for L0 and reused while the value of
12335 * vpid12 is changed w/ one invvpid during nested vmentry.
12336 * The vpid12 is allocated by L1 for L2, so it will not
12337 * influence global bitmap(for vpid01 and vpid02 allocation)
12338 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012339 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012340 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012341 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12342 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012343 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012344 }
12345 } else {
Liran Alon14389212018-10-08 23:42:17 +030012346 /*
12347 * If L1 use EPT, then L0 needs to execute INVEPT on
12348 * EPTP02 instead of EPTP01. Therefore, delay TLB
12349 * flush until vmcs02->eptp is fully updated by
12350 * KVM_REQ_LOAD_CR3. Note that this assumes
12351 * KVM_REQ_TLB_FLUSH is evaluated after
12352 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12353 */
12354 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012355 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012356 }
12357
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012358 if (nested_cpu_has_ept(vmcs12))
12359 nested_ept_init_mmu_context(vcpu);
12360 else if (nested_cpu_has2(vmcs12,
12361 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012362 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012363
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012364 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012365 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12366 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012367 * The CR0_READ_SHADOW is what L2 should have expected to read given
12368 * the specifications by L1; It's not enough to take
12369 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12370 * have more bits than L1 expected.
12371 */
12372 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12373 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12374
12375 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12376 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12377
Sean Christopherson09abe322018-09-26 09:23:50 -070012378 vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12);
Sean Christopherson3df5c372018-09-26 09:23:44 -070012379 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012380 vmx_set_efer(vcpu, vcpu->arch.efer);
12381
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012382 /*
12383 * Guest state is invalid and unrestricted guest is disabled,
12384 * which means L1 attempted VMEntry to L2 with invalid state.
12385 * Fail the VMEntry.
12386 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012387 if (vmx->emulation_required) {
12388 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012389 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012390 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012391
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012392 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012393 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012394 entry_failure_code))
12395 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012396
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012397 if (!enable_ept)
12398 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12399
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012400 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12401 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012402 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012403}
12404
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012405static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12406{
12407 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12408 nested_cpu_has_virtual_nmis(vmcs12))
12409 return -EINVAL;
12410
12411 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12412 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12413 return -EINVAL;
12414
12415 return 0;
12416}
12417
Jim Mattsonca0bde22016-11-30 12:03:46 -080012418static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12419{
12420 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012421 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012422
12423 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12424 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12425 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12426
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012427 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12428 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12429
Jim Mattson56a20512017-07-06 16:33:06 -070012430 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12431 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12432
Jim Mattsonca0bde22016-11-30 12:03:46 -080012433 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12434 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12435
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012436 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12437 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12438
Jim Mattson712b12d2017-08-24 13:24:47 -070012439 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12440 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12441
Jim Mattsonca0bde22016-11-30 12:03:46 -080012442 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12443 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12444
12445 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12446 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12447
Bandan Dasc5f983f2017-05-05 15:25:14 -040012448 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12449 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12450
Liran Alona8a7c022018-06-23 02:35:06 +030012451 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12452 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12453
Jim Mattsonca0bde22016-11-30 12:03:46 -080012454 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012455 vmx->nested.msrs.procbased_ctls_low,
12456 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012457 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12458 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012459 vmx->nested.msrs.secondary_ctls_low,
12460 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012461 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012462 vmx->nested.msrs.pinbased_ctls_low,
12463 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012464 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012465 vmx->nested.msrs.exit_ctls_low,
12466 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012467 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012468 vmx->nested.msrs.entry_ctls_low,
12469 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012470 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12471
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012472 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012473 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12474
Bandan Das41ab9372017-08-03 15:54:43 -040012475 if (nested_cpu_has_vmfunc(vmcs12)) {
12476 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012477 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012478 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12479
12480 if (nested_cpu_has_eptp_switching(vmcs12)) {
12481 if (!nested_cpu_has_ept(vmcs12) ||
12482 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484 }
12485 }
Bandan Das27c42a12017-08-03 15:54:42 -040012486
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012487 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12488 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12489
Jim Mattsonca0bde22016-11-30 12:03:46 -080012490 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12491 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12492 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12493 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12494
Marc Orr04473782018-06-20 17:21:29 -070012495 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012496 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12497 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12498 * the values of the LMA and LME bits in the field must each be that of
12499 * the host address-space size VM-exit control.
12500 */
12501 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12502 ia32e = (vmcs12->vm_exit_controls &
12503 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12504 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12505 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12506 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12507 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12508 }
12509
12510 /*
Marc Orr04473782018-06-20 17:21:29 -070012511 * From the Intel SDM, volume 3:
12512 * Fields relevant to VM-entry event injection must be set properly.
12513 * These fields are the VM-entry interruption-information field, the
12514 * VM-entry exception error code, and the VM-entry instruction length.
12515 */
12516 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12517 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12518 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12519 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12520 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12521 bool should_have_error_code;
12522 bool urg = nested_cpu_has2(vmcs12,
12523 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12524 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12525
12526 /* VM-entry interruption-info field: interruption type */
12527 if (intr_type == INTR_TYPE_RESERVED ||
12528 (intr_type == INTR_TYPE_OTHER_EVENT &&
12529 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12530 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12531
12532 /* VM-entry interruption-info field: vector */
12533 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12534 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12535 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12536 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12537
12538 /* VM-entry interruption-info field: deliver error code */
12539 should_have_error_code =
12540 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12541 x86_exception_has_error_code(vector);
12542 if (has_error_code != should_have_error_code)
12543 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12544
12545 /* VM-entry exception error code */
12546 if (has_error_code &&
12547 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12548 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12549
12550 /* VM-entry interruption-info field: reserved bits */
12551 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12552 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12553
12554 /* VM-entry instruction length */
12555 switch (intr_type) {
12556 case INTR_TYPE_SOFT_EXCEPTION:
12557 case INTR_TYPE_SOFT_INTR:
12558 case INTR_TYPE_PRIV_SW_EXCEPTION:
12559 if ((vmcs12->vm_entry_instruction_len > 15) ||
12560 (vmcs12->vm_entry_instruction_len == 0 &&
12561 !nested_cpu_has_zero_length_injection(vcpu)))
12562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12563 }
12564 }
12565
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012566 if (nested_cpu_has_ept(vmcs12) &&
12567 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12568 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12569
Jim Mattsonca0bde22016-11-30 12:03:46 -080012570 return 0;
12571}
12572
Liran Alonf145d902018-06-23 02:35:07 +030012573static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12574 struct vmcs12 *vmcs12)
12575{
12576 int r;
12577 struct page *page;
12578 struct vmcs12 *shadow;
12579
12580 if (vmcs12->vmcs_link_pointer == -1ull)
12581 return 0;
12582
12583 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12584 return -EINVAL;
12585
12586 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12587 if (is_error_page(page))
12588 return -EINVAL;
12589
12590 r = 0;
12591 shadow = kmap(page);
12592 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12593 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12594 r = -EINVAL;
12595 kunmap(page);
12596 kvm_release_page_clean(page);
12597 return r;
12598}
12599
Jim Mattsonca0bde22016-11-30 12:03:46 -080012600static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12601 u32 *exit_qual)
12602{
12603 bool ia32e;
12604
12605 *exit_qual = ENTRY_FAIL_DEFAULT;
12606
12607 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12608 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12609 return 1;
12610
Liran Alonf145d902018-06-23 02:35:07 +030012611 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012612 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12613 return 1;
12614 }
12615
12616 /*
12617 * If the load IA32_EFER VM-entry control is 1, the following checks
12618 * are performed on the field for the IA32_EFER MSR:
12619 * - Bits reserved in the IA32_EFER MSR must be 0.
12620 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12621 * the IA-32e mode guest VM-exit control. It must also be identical
12622 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12623 * CR0.PG) is 1.
12624 */
12625 if (to_vmx(vcpu)->nested.nested_run_pending &&
12626 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12627 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12628 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12629 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12630 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12631 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12632 return 1;
12633 }
12634
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012635 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12636 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12637 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12638 return 1;
12639
Jim Mattsonca0bde22016-11-30 12:03:46 -080012640 return 0;
12641}
12642
Sean Christopherson52017602018-09-26 09:23:57 -070012643static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
12644{
12645 struct vcpu_vmx *vmx = to_vmx(vcpu);
12646 unsigned long cr3, cr4;
12647
12648 if (!nested_early_check)
12649 return 0;
12650
12651 if (vmx->msr_autoload.host.nr)
12652 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
12653 if (vmx->msr_autoload.guest.nr)
12654 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
12655
12656 preempt_disable();
12657
12658 vmx_prepare_switch_to_guest(vcpu);
12659
12660 /*
12661 * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS,
12662 * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to
12663 * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e.
12664 * there is no need to preserve other bits or save/restore the field.
12665 */
12666 vmcs_writel(GUEST_RFLAGS, 0);
12667
12668 vmcs_writel(HOST_RIP, vmx_early_consistency_check_return);
12669
12670 cr3 = __get_current_cr3_fast();
12671 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
12672 vmcs_writel(HOST_CR3, cr3);
12673 vmx->loaded_vmcs->host_state.cr3 = cr3;
12674 }
12675
12676 cr4 = cr4_read_shadow();
12677 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
12678 vmcs_writel(HOST_CR4, cr4);
12679 vmx->loaded_vmcs->host_state.cr4 = cr4;
12680 }
12681
12682 vmx->__launched = vmx->loaded_vmcs->launched;
12683
12684 asm(
12685 /* Set HOST_RSP */
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012686 __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012687 "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t"
12688
12689 /* Check if vmlaunch of vmresume is needed */
12690 "cmpl $0, %c[launched](%0)\n\t"
12691 "je 1f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012692 __ex("vmresume") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012693 "jmp 2f\n\t"
Uros Bizjak4b1e5472018-10-11 19:40:44 +020012694 "1: " __ex("vmlaunch") "\n\t"
Sean Christopherson52017602018-09-26 09:23:57 -070012695 "jmp 2f\n\t"
12696 "2: "
12697
12698 /* Set vmx->fail accordingly */
12699 "setbe %c[fail](%0)\n\t"
12700
12701 ".pushsection .rodata\n\t"
12702 ".global vmx_early_consistency_check_return\n\t"
12703 "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t"
12704 ".popsection"
12705 :
12706 : "c"(vmx), "d"((unsigned long)HOST_RSP),
12707 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
12708 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
12709 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp))
12710 : "rax", "cc", "memory"
12711 );
12712
12713 vmcs_writel(HOST_RIP, vmx_return);
12714
12715 preempt_enable();
12716
12717 if (vmx->msr_autoload.host.nr)
12718 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12719 if (vmx->msr_autoload.guest.nr)
12720 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12721
12722 if (vmx->fail) {
12723 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
12724 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
12725 vmx->fail = 0;
12726 return 1;
12727 }
12728
12729 /*
12730 * VMExit clears RFLAGS.IF and DR7, even on a consistency check.
12731 */
12732 local_irq_enable();
12733 if (hw_breakpoint_active())
12734 set_debugreg(__this_cpu_read(cpu_dr7), 7);
12735
12736 /*
12737 * A non-failing VMEntry means we somehow entered guest mode with
12738 * an illegal RIP, and that's just the tip of the iceberg. There
12739 * is no telling what memory has been modified or what state has
12740 * been exposed to unknown code. Hitting this all but guarantees
12741 * a (very critical) hardware issue.
12742 */
12743 WARN_ON(!(vmcs_read32(VM_EXIT_REASON) &
12744 VMX_EXIT_REASONS_FAILED_VMENTRY));
12745
12746 return 0;
12747}
12748STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw);
12749
Sean Christophersona633e412018-09-26 09:23:47 -070012750static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
12751 struct vmcs12 *vmcs12);
12752
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012753/*
Sean Christophersona633e412018-09-26 09:23:47 -070012754 * If from_vmentry is false, this is being called from state restore (either RSM
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012755 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Sean Christopherson52017602018-09-26 09:23:57 -070012756+ *
12757+ * Returns:
12758+ * 0 - success, i.e. proceed with actual VMEnter
12759+ * 1 - consistency check VMExit
12760+ * -1 - consistency check VMFail
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012761 */
Sean Christophersona633e412018-09-26 09:23:47 -070012762static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu,
12763 bool from_vmentry)
Jim Mattson858e25c2016-11-30 12:03:47 -080012764{
12765 struct vcpu_vmx *vmx = to_vmx(vcpu);
12766 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7e712682018-10-03 13:44:26 +020012767 bool evaluate_pending_interrupts;
Sean Christophersona633e412018-09-26 09:23:47 -070012768 u32 exit_reason = EXIT_REASON_INVALID_STATE;
12769 u32 exit_qual;
Jim Mattson858e25c2016-11-30 12:03:47 -080012770
Paolo Bonzini7e712682018-10-03 13:44:26 +020012771 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12772 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12773 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12774 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012775
Jim Mattson858e25c2016-11-30 12:03:47 -080012776 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12777 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012778 if (kvm_mpx_supported() &&
12779 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12780 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012781
Jim Mattsonde3a0022017-11-27 17:22:25 -060012782 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012783
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012784 prepare_vmcs02_early(vmx, vmcs12);
12785
12786 if (from_vmentry) {
12787 nested_get_vmcs12_pages(vcpu);
12788
Sean Christopherson52017602018-09-26 09:23:57 -070012789 if (nested_vmx_check_vmentry_hw(vcpu)) {
12790 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
12791 return -1;
12792 }
12793
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012794 if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
12795 goto vmentry_fail_vmexit;
12796 }
12797
12798 enter_guest_mode(vcpu);
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012799 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12800 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12801
Sean Christophersona633e412018-09-26 09:23:47 -070012802 if (prepare_vmcs02(vcpu, vmcs12, &exit_qual))
Sean Christopherson39f9c382018-09-26 09:23:48 -070012803 goto vmentry_fail_vmexit_guest_mode;
Jim Mattson858e25c2016-11-30 12:03:47 -080012804
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012805 if (from_vmentry) {
Sean Christophersona633e412018-09-26 09:23:47 -070012806 exit_reason = EXIT_REASON_MSR_LOAD_FAIL;
12807 exit_qual = nested_vmx_load_msr(vcpu,
12808 vmcs12->vm_entry_msr_load_addr,
12809 vmcs12->vm_entry_msr_load_count);
12810 if (exit_qual)
Sean Christopherson39f9c382018-09-26 09:23:48 -070012811 goto vmentry_fail_vmexit_guest_mode;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012812 } else {
12813 /*
12814 * The MMU is not initialized to point at the right entities yet and
12815 * "get pages" would need to read data from the guest (i.e. we will
12816 * need to perform gpa to hpa translation). Request a call
12817 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12818 * have already been set at vmentry time and should not be reset.
12819 */
12820 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12821 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012822
Jim Mattson858e25c2016-11-30 12:03:47 -080012823 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012824 * If L1 had a pending IRQ/NMI until it executed
12825 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12826 * disallowed (e.g. interrupts disabled), L0 needs to
12827 * evaluate if this pending event should cause an exit from L2
12828 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12829 * intercept EXTERNAL_INTERRUPT).
12830 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012831 * Usually this would be handled by the processor noticing an
12832 * IRQ/NMI window request, or checking RVI during evaluation of
12833 * pending virtual interrupts. However, this setting was done
12834 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12835 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012836 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012837 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012838 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012839
12840 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012841 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12842 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12843 * returned as far as L1 is concerned. It will only return (and set
12844 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12845 */
12846 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012847
Sean Christophersona633e412018-09-26 09:23:47 -070012848 /*
12849 * A failed consistency check that leads to a VMExit during L1's
12850 * VMEnter to L2 is a variation of a normal VMexit, as explained in
12851 * 26.7 "VM-entry failures during or after loading guest state".
12852 */
Sean Christopherson39f9c382018-09-26 09:23:48 -070012853vmentry_fail_vmexit_guest_mode:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012854 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12855 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12856 leave_guest_mode(vcpu);
Sean Christopherson16fb9a42018-09-26 09:23:52 -070012857
12858vmentry_fail_vmexit:
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012859 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Sean Christophersona633e412018-09-26 09:23:47 -070012860
12861 if (!from_vmentry)
12862 return 1;
12863
Sean Christophersona633e412018-09-26 09:23:47 -070012864 load_vmcs12_host_state(vcpu, vmcs12);
12865 vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
12866 vmcs12->exit_qualification = exit_qual;
Sean Christophersona633e412018-09-26 09:23:47 -070012867 if (enable_shadow_vmcs)
12868 vmx->nested.sync_shadow_vmcs = true;
12869 return 1;
Jim Mattson858e25c2016-11-30 12:03:47 -080012870}
12871
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012872/*
12873 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12874 * for running an L2 nested guest.
12875 */
12876static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12877{
12878 struct vmcs12 *vmcs12;
12879 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012880 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012881 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012882
Kyle Hueyeb277562016-11-29 12:40:39 -080012883 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012884 return 1;
12885
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012886 if (vmx->nested.current_vmptr == -1ull)
12887 return nested_vmx_failInvalid(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -080012888
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012889 vmcs12 = get_vmcs12(vcpu);
12890
Liran Alona6192d42018-06-23 02:35:04 +030012891 /*
12892 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12893 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12894 * rather than RFLAGS.ZF, and no error number is stored to the
12895 * VM-instruction error field.
12896 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012897 if (vmcs12->hdr.shadow_vmcs)
12898 return nested_vmx_failInvalid(vcpu);
Liran Alona6192d42018-06-23 02:35:04 +030012899
Abel Gordon012f83c2013-04-18 14:39:25 +030012900 if (enable_shadow_vmcs)
12901 copy_shadow_to_vmcs12(vmx);
12902
Nadav Har'El7c177932011-05-25 23:12:04 +030012903 /*
12904 * The nested entry process starts with enforcing various prerequisites
12905 * on vmcs12 as required by the Intel SDM, and act appropriately when
12906 * they fail: As the SDM explains, some conditions should cause the
12907 * instruction to fail, while others will cause the instruction to seem
12908 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12909 * To speed up the normal (success) code path, we should avoid checking
12910 * for misconfigurations which will anyway be caught by the processor
12911 * when using the merged vmcs02.
12912 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012913 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS)
12914 return nested_vmx_failValid(vcpu,
12915 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012916
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012917 if (vmcs12->launch_state == launch)
12918 return nested_vmx_failValid(vcpu,
Nadav Har'El7c177932011-05-25 23:12:04 +030012919 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12920 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Nadav Har'El7c177932011-05-25 23:12:04 +030012921
Jim Mattsonca0bde22016-11-30 12:03:46 -080012922 ret = check_vmentry_prereqs(vcpu, vmcs12);
Sean Christopherson09abb5e2018-09-26 09:23:55 -070012923 if (ret)
12924 return nested_vmx_failValid(vcpu, ret);
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012925
Nadav Har'El7c177932011-05-25 23:12:04 +030012926 /*
12927 * We're finally done with prerequisite checking, and can start with
12928 * the nested entry.
12929 */
Jim Mattson6514dc32018-04-26 16:09:12 -070012930 vmx->nested.nested_run_pending = 1;
Sean Christophersona633e412018-09-26 09:23:47 -070012931 ret = nested_vmx_enter_non_root_mode(vcpu, true);
Sean Christopherson52017602018-09-26 09:23:57 -070012932 vmx->nested.nested_run_pending = !ret;
12933 if (ret > 0)
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012934 return 1;
Sean Christopherson52017602018-09-26 09:23:57 -070012935 else if (ret)
12936 return nested_vmx_failValid(vcpu,
12937 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wincy Vanff651cb2014-12-11 08:52:58 +030012938
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012939 /* Hide L1D cache contents from the nested guest. */
12940 vmx->vcpu.arch.l1tf_flush_l1d = true;
12941
Chao Gao135a06c2018-02-11 10:06:30 +080012942 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012943 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012944 * also be used as part of restoring nVMX state for
12945 * snapshot restore (migration).
12946 *
12947 * In this flow, it is assumed that vmcs12 cache was
12948 * trasferred as part of captured nVMX state and should
12949 * therefore not be read from guest memory (which may not
12950 * exist on destination host yet).
12951 */
12952 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12953
12954 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012955 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12956 * by event injection, halt vcpu.
12957 */
12958 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012959 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12960 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012961 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012962 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012963 return 1;
12964}
12965
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012966/*
12967 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12968 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12969 * This function returns the new value we should put in vmcs12.guest_cr0.
12970 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12971 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12972 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12973 * didn't trap the bit, because if L1 did, so would L0).
12974 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12975 * been modified by L2, and L1 knows it. So just leave the old value of
12976 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12977 * isn't relevant, because if L0 traps this bit it can set it to anything.
12978 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12979 * changed these bits, and therefore they need to be updated, but L0
12980 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12981 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12982 */
12983static inline unsigned long
12984vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12985{
12986 return
12987 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12988 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12989 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12990 vcpu->arch.cr0_guest_owned_bits));
12991}
12992
12993static inline unsigned long
12994vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12995{
12996 return
12997 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12998 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12999 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
13000 vcpu->arch.cr4_guest_owned_bits));
13001}
13002
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013003static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
13004 struct vmcs12 *vmcs12)
13005{
13006 u32 idt_vectoring;
13007 unsigned int nr;
13008
Wanpeng Li664f8e22017-08-24 03:35:09 -070013009 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013010 nr = vcpu->arch.exception.nr;
13011 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13012
13013 if (kvm_exception_is_soft(nr)) {
13014 vmcs12->vm_exit_instruction_len =
13015 vcpu->arch.event_exit_inst_len;
13016 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
13017 } else
13018 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
13019
13020 if (vcpu->arch.exception.has_error_code) {
13021 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
13022 vmcs12->idt_vectoring_error_code =
13023 vcpu->arch.exception.error_code;
13024 }
13025
13026 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010013027 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013028 vmcs12->idt_vectoring_info_field =
13029 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030013030 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013031 nr = vcpu->arch.interrupt.nr;
13032 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
13033
13034 if (vcpu->arch.interrupt.soft) {
13035 idt_vectoring |= INTR_TYPE_SOFT_INTR;
13036 vmcs12->vm_entry_instruction_len =
13037 vcpu->arch.event_exit_inst_len;
13038 } else
13039 idt_vectoring |= INTR_TYPE_EXT_INTR;
13040
13041 vmcs12->idt_vectoring_info_field = idt_vectoring;
13042 }
13043}
13044
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013045static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
13046{
13047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013048 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020013049 bool block_nested_events =
13050 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080013051
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013052 if (vcpu->arch.exception.pending &&
13053 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020013054 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013055 return -EBUSY;
13056 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070013057 return 0;
13058 }
13059
Jan Kiszkaf41245002014-03-07 20:03:13 +010013060 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
13061 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020013062 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010013063 return -EBUSY;
13064 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
13065 return 0;
13066 }
13067
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013068 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013069 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013070 return -EBUSY;
13071 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
13072 NMI_VECTOR | INTR_TYPE_NMI_INTR |
13073 INTR_INFO_VALID_MASK, 0);
13074 /*
13075 * The NMI-triggered VM exit counts as injection:
13076 * clear this one and block further NMIs.
13077 */
13078 vcpu->arch.nmi_pending = 0;
13079 vmx_set_nmi_mask(vcpu, true);
13080 return 0;
13081 }
13082
13083 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
13084 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020013085 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013086 return -EBUSY;
13087 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080013088 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013089 }
13090
David Hildenbrand6342c502017-01-25 11:58:58 +010013091 vmx_complete_nested_posted_interrupt(vcpu);
13092 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013093}
13094
Sean Christophersond264ee02018-08-27 15:21:12 -070013095static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
13096{
13097 to_vmx(vcpu)->req_immediate_exit = true;
13098}
13099
Jan Kiszkaf41245002014-03-07 20:03:13 +010013100static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
13101{
13102 ktime_t remaining =
13103 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
13104 u64 value;
13105
13106 if (ktime_to_ns(remaining) <= 0)
13107 return 0;
13108
13109 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
13110 do_div(value, 1000000);
13111 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
13112}
13113
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013114/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013115 * Update the guest state fields of vmcs12 to reflect changes that
13116 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13117 * VM-entry controls is also updated, since this is really a guest
13118 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013119 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013120static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013121{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013122 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13123 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13124
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013125 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13126 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13127 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13128
13129 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13130 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13131 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13132 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13133 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13134 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13135 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13136 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13137 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13138 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13139 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13140 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13141 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13142 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13143 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13144 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13145 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13146 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13147 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13148 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13149 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13150 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13151 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13152 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13153 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13154 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13155 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13156 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13157 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13158 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13159 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13160 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13161 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13162 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13163 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13164 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13165
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013166 vmcs12->guest_interruptibility_info =
13167 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13168 vmcs12->guest_pending_dbg_exceptions =
13169 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013170 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13171 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13172 else
13173 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013174
Jan Kiszkaf41245002014-03-07 20:03:13 +010013175 if (nested_cpu_has_preemption_timer(vmcs12)) {
13176 if (vmcs12->vm_exit_controls &
13177 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13178 vmcs12->vmx_preemption_timer_value =
13179 vmx_get_preemption_timer_value(vcpu);
13180 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13181 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013182
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013183 /*
13184 * In some cases (usually, nested EPT), L2 is allowed to change its
13185 * own CR3 without exiting. If it has changed it, we must keep it.
13186 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13187 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13188 *
13189 * Additionally, restore L2's PDPTR to vmcs12.
13190 */
13191 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013192 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013193 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13194 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13195 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13196 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13197 }
13198
Jim Mattsond281e132017-06-01 12:44:46 -070013199 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013200
Wincy Van608406e2015-02-03 23:57:51 +080013201 if (nested_cpu_has_vid(vmcs12))
13202 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13203
Jan Kiszkac18911a2013-03-13 16:06:41 +010013204 vmcs12->vm_entry_controls =
13205 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013206 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013207
Jan Kiszka2996fca2014-06-16 13:59:43 +020013208 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13209 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13210 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13211 }
13212
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013213 /* TODO: These cannot have changed unless we have MSR bitmaps and
13214 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013215 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013216 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013217 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13218 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013219 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13220 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13221 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013222 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013223 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013224}
13225
13226/*
13227 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13228 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13229 * and this function updates it to reflect the changes to the guest state while
13230 * L2 was running (and perhaps made some exits which were handled directly by L0
13231 * without going back to L1), and to reflect the exit reason.
13232 * Note that we do not have to copy here all VMCS fields, just those that
13233 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13234 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13235 * which already writes to vmcs12 directly.
13236 */
13237static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13238 u32 exit_reason, u32 exit_intr_info,
13239 unsigned long exit_qualification)
13240{
13241 /* update guest state fields: */
13242 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013243
13244 /* update exit information fields: */
13245
Jan Kiszka533558b2014-01-04 18:47:20 +010013246 vmcs12->vm_exit_reason = exit_reason;
13247 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013248 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013249
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013250 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013251 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13252 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13253
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013254 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013255 vmcs12->launch_state = 1;
13256
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013257 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13258 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013259 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013260
13261 /*
13262 * Transfer the event that L0 or L1 may wanted to inject into
13263 * L2 to IDT_VECTORING_INFO_FIELD.
13264 */
13265 vmcs12_save_pending_event(vcpu, vmcs12);
13266 }
13267
13268 /*
13269 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13270 * preserved above and would only end up incorrectly in L1.
13271 */
13272 vcpu->arch.nmi_injected = false;
13273 kvm_clear_exception_queue(vcpu);
13274 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013275}
13276
13277/*
13278 * A part of what we need to when the nested L2 guest exits and we want to
13279 * run its L1 parent, is to reset L1's guest state to the host state specified
13280 * in vmcs12.
13281 * This function is to be called not only on normal nested exit, but also on
13282 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13283 * Failures During or After Loading Guest State").
13284 * This function should be called when the active VMCS is L1's (vmcs01).
13285 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013286static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13287 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013288{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013289 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013290 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013291
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013292 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13293 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013294 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013295 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13296 else
13297 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13298 vmx_set_efer(vcpu, vcpu->arch.efer);
13299
13300 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13301 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013302 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013303 vmx_set_interrupt_shadow(vcpu, 0);
13304
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013305 /*
13306 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013307 * actually changed, because vmx_set_cr0 refers to efer set above.
13308 *
13309 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13310 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013311 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013312 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020013313 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013314
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013315 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013316 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013317 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013318
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013319 nested_ept_uninit_mmu_context(vcpu);
13320
13321 /*
13322 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13323 * couldn't have changed.
13324 */
13325 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13326 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13327
13328 if (!enable_ept)
13329 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013330
Liran Alon6f1e03b2018-05-22 17:16:14 +030013331 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013332 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013333 * VMEntry/VMExit. Thus, no need to flush TLB.
13334 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013335 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13336 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013337 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013338 * Otherwise, we can preserve TLB entries as long as we are
13339 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013340 *
13341 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13342 * and therefore we request the TLB flush to happen only after VMCS EPTP
13343 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013344 */
13345 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013346 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013347 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013348 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013349
13350 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13351 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13352 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13353 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13354 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013355 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13356 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013357
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013358 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13359 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13360 vmcs_write64(GUEST_BNDCFGS, 0);
13361
Jan Kiszka44811c02013-08-04 17:17:27 +020013362 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013363 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013364 vcpu->arch.pat = vmcs12->host_ia32_pat;
13365 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013366 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13367 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13368 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013369
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013370 /* Set L1 segment info according to Intel SDM
13371 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13372 seg = (struct kvm_segment) {
13373 .base = 0,
13374 .limit = 0xFFFFFFFF,
13375 .selector = vmcs12->host_cs_selector,
13376 .type = 11,
13377 .present = 1,
13378 .s = 1,
13379 .g = 1
13380 };
13381 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13382 seg.l = 1;
13383 else
13384 seg.db = 1;
13385 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13386 seg = (struct kvm_segment) {
13387 .base = 0,
13388 .limit = 0xFFFFFFFF,
13389 .type = 3,
13390 .present = 1,
13391 .s = 1,
13392 .db = 1,
13393 .g = 1
13394 };
13395 seg.selector = vmcs12->host_ds_selector;
13396 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13397 seg.selector = vmcs12->host_es_selector;
13398 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13399 seg.selector = vmcs12->host_ss_selector;
13400 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13401 seg.selector = vmcs12->host_fs_selector;
13402 seg.base = vmcs12->host_fs_base;
13403 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13404 seg.selector = vmcs12->host_gs_selector;
13405 seg.base = vmcs12->host_gs_base;
13406 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13407 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013408 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013409 .limit = 0x67,
13410 .selector = vmcs12->host_tr_selector,
13411 .type = 11,
13412 .present = 1
13413 };
13414 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13415
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013416 kvm_set_dr(vcpu, 7, 0x400);
13417 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013418
Wincy Van3af18d92015-02-03 23:49:31 +080013419 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013420 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013421
Wincy Vanff651cb2014-12-11 08:52:58 +030013422 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13423 vmcs12->vm_exit_msr_load_count))
13424 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013425}
13426
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013427static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13428{
13429 struct shared_msr_entry *efer_msr;
13430 unsigned int i;
13431
13432 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13433 return vmcs_read64(GUEST_IA32_EFER);
13434
13435 if (cpu_has_load_ia32_efer)
13436 return host_efer;
13437
13438 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13439 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13440 return vmx->msr_autoload.guest.val[i].value;
13441 }
13442
13443 efer_msr = find_msr_entry(vmx, MSR_EFER);
13444 if (efer_msr)
13445 return efer_msr->data;
13446
13447 return host_efer;
13448}
13449
13450static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13451{
13452 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13453 struct vcpu_vmx *vmx = to_vmx(vcpu);
13454 struct vmx_msr_entry g, h;
13455 struct msr_data msr;
13456 gpa_t gpa;
13457 u32 i, j;
13458
13459 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13460
13461 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13462 /*
13463 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13464 * as vmcs01.GUEST_DR7 contains a userspace defined value
13465 * and vcpu->arch.dr7 is not squirreled away before the
13466 * nested VMENTER (not worth adding a variable in nested_vmx).
13467 */
13468 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13469 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13470 else
13471 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13472 }
13473
13474 /*
13475 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13476 * handle a variety of side effects to KVM's software model.
13477 */
13478 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13479
13480 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13481 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13482
13483 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13484 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13485
13486 nested_ept_uninit_mmu_context(vcpu);
13487 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13488 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13489
13490 /*
13491 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13492 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13493 * VMFail, like everything else we just need to ensure our
13494 * software model is up-to-date.
13495 */
13496 ept_save_pdptrs(vcpu);
13497
13498 kvm_mmu_reset_context(vcpu);
13499
13500 if (cpu_has_vmx_msr_bitmap())
13501 vmx_update_msr_bitmap(vcpu);
13502
13503 /*
13504 * This nasty bit of open coding is a compromise between blindly
13505 * loading L1's MSRs using the exit load lists (incorrect emulation
13506 * of VMFail), leaving the nested VM's MSRs in the software model
13507 * (incorrect behavior) and snapshotting the modified MSRs (too
13508 * expensive since the lists are unbound by hardware). For each
13509 * MSR that was (prematurely) loaded from the nested VMEntry load
13510 * list, reload it from the exit load list if it exists and differs
13511 * from the guest value. The intent is to stuff host state as
13512 * silently as possible, not to fully process the exit load list.
13513 */
13514 msr.host_initiated = false;
13515 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13516 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13517 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13518 pr_debug_ratelimited(
13519 "%s read MSR index failed (%u, 0x%08llx)\n",
13520 __func__, i, gpa);
13521 goto vmabort;
13522 }
13523
13524 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13525 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13526 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13527 pr_debug_ratelimited(
13528 "%s read MSR failed (%u, 0x%08llx)\n",
13529 __func__, j, gpa);
13530 goto vmabort;
13531 }
13532 if (h.index != g.index)
13533 continue;
13534 if (h.value == g.value)
13535 break;
13536
13537 if (nested_vmx_load_msr_check(vcpu, &h)) {
13538 pr_debug_ratelimited(
13539 "%s check failed (%u, 0x%x, 0x%x)\n",
13540 __func__, j, h.index, h.reserved);
13541 goto vmabort;
13542 }
13543
13544 msr.index = h.index;
13545 msr.data = h.value;
13546 if (kvm_set_msr(vcpu, &msr)) {
13547 pr_debug_ratelimited(
13548 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13549 __func__, j, h.index, h.value);
13550 goto vmabort;
13551 }
13552 }
13553 }
13554
13555 return;
13556
13557vmabort:
13558 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13559}
13560
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013561/*
13562 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13563 * and modify vmcs12 to make it see what it would expect to see there if
13564 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13565 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013566static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13567 u32 exit_intr_info,
13568 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013569{
13570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013571 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13572
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013573 /* trying to cancel vmlaunch/vmresume is a bug */
13574 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13575
Jim Mattson4f350c62017-09-14 16:31:44 -070013576 leave_guest_mode(vcpu);
13577
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013578 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13579 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13580
Jim Mattson4f350c62017-09-14 16:31:44 -070013581 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013582 if (exit_reason == -1)
13583 sync_vmcs12(vcpu, vmcs12);
13584 else
13585 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13586 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013587
Liran Alon61ada742018-06-23 02:35:08 +030013588 /*
13589 * Must happen outside of sync_vmcs12() as it will
13590 * also be used to capture vmcs12 cache as part of
13591 * capturing nVMX state for snapshot (migration).
13592 *
13593 * Otherwise, this flush will dirty guest memory at a
13594 * point it is already assumed by user-space to be
13595 * immutable.
13596 */
13597 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13598
Jim Mattson4f350c62017-09-14 16:31:44 -070013599 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13600 vmcs12->vm_exit_msr_store_count))
13601 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Sean Christopherson2768c0c2018-09-26 09:23:58 -070013602 } else {
13603 /*
13604 * The only expected VM-instruction error is "VM entry with
13605 * invalid control field(s)." Anything else indicates a
13606 * problem with L0. And we should never get here with a
13607 * VMFail of any type if early consistency checks are enabled.
13608 */
13609 WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) !=
13610 VMXERR_ENTRY_INVALID_CONTROL_FIELD);
13611 WARN_ON_ONCE(nested_early_check);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013612 }
13613
Jim Mattson4f350c62017-09-14 16:31:44 -070013614 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013615
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013616 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013617 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13618 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013619 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013620
Peter Feinerc95ba922016-08-17 09:36:47 -070013621 if (kvm_has_tsc_control)
13622 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013623
Jim Mattson8d860bb2018-05-09 16:56:05 -040013624 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13625 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13626 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013627 } else if (!nested_cpu_has_ept(vmcs12) &&
13628 nested_cpu_has2(vmcs12,
13629 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013630 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013631 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013632
13633 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13634 vmx->host_rsp = 0;
13635
13636 /* Unpin physical memory we referred to in vmcs02 */
13637 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013638 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013639 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013640 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013641 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013642 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013643 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013644 }
Wincy Van705699a2015-02-03 23:58:17 +080013645 if (vmx->nested.pi_desc_page) {
13646 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013647 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013648 vmx->nested.pi_desc_page = NULL;
13649 vmx->nested.pi_desc = NULL;
13650 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013651
13652 /*
Tang Chen38b99172014-09-24 15:57:54 +080013653 * We are now running in L2, mmu_notifier will force to reload the
13654 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13655 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013656 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013657
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013658 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013659 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013660
13661 /* in case we halted in L2 */
13662 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013663
13664 if (likely(!vmx->fail)) {
13665 /*
13666 * TODO: SDM says that with acknowledge interrupt on
13667 * exit, bit 31 of the VM-exit interrupt information
13668 * (valid interrupt) is always set to 1 on
13669 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13670 * need kvm_cpu_has_interrupt(). See the commit
13671 * message for details.
13672 */
13673 if (nested_exit_intr_ack_set(vcpu) &&
13674 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13675 kvm_cpu_has_interrupt(vcpu)) {
13676 int irq = kvm_cpu_get_interrupt(vcpu);
13677 WARN_ON(irq < 0);
13678 vmcs12->vm_exit_intr_info = irq |
13679 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13680 }
13681
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013682 if (exit_reason != -1)
13683 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13684 vmcs12->exit_qualification,
13685 vmcs12->idt_vectoring_info_field,
13686 vmcs12->vm_exit_intr_info,
13687 vmcs12->vm_exit_intr_error_code,
13688 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013689
13690 load_vmcs12_host_state(vcpu, vmcs12);
13691
13692 return;
13693 }
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013694
Jim Mattson4f350c62017-09-14 16:31:44 -070013695 /*
13696 * After an early L2 VM-entry failure, we're now back
13697 * in L1 which thinks it just finished a VMLAUNCH or
13698 * VMRESUME instruction, so we need to set the failure
13699 * flag and the VM-instruction error field of the VMCS
Sean Christophersoncb61de22018-09-26 09:23:53 -070013700 * accordingly, and skip the emulated instruction.
Jim Mattson4f350c62017-09-14 16:31:44 -070013701 */
Sean Christopherson09abb5e2018-09-26 09:23:55 -070013702 (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Sean Christophersoncb61de22018-09-26 09:23:53 -070013703
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013704 /*
13705 * Restore L1's host state to KVM's software model. We're here
13706 * because a consistency check was caught by hardware, which
13707 * means some amount of guest state has been propagated to KVM's
13708 * model and needs to be unwound to the host's state.
13709 */
13710 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013711
Jim Mattson4f350c62017-09-14 16:31:44 -070013712 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013713}
13714
Nadav Har'El7c177932011-05-25 23:12:04 +030013715/*
Jan Kiszka42124922014-01-04 18:47:19 +010013716 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13717 */
13718static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13719{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013720 if (is_guest_mode(vcpu)) {
13721 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013722 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013723 }
Vitaly Kuznetsov14c07ad2018-10-08 21:28:08 +020013724 free_nested(vcpu);
Jan Kiszka42124922014-01-04 18:47:19 +010013725}
13726
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013727static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13728 struct x86_instruction_info *info,
13729 enum x86_intercept_stage stage)
13730{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013731 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13732 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13733
13734 /*
13735 * RDPID causes #UD if disabled through secondary execution controls.
13736 * Because it is marked as EmulateOnUD, we need to intercept it here.
13737 */
13738 if (info->intercept == x86_intercept_rdtscp &&
13739 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13740 ctxt->exception.vector = UD_VECTOR;
13741 ctxt->exception.error_code_valid = false;
13742 return X86EMUL_PROPAGATE_FAULT;
13743 }
13744
13745 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013746 return X86EMUL_CONTINUE;
13747}
13748
Yunhong Jiang64672c92016-06-13 14:19:59 -070013749#ifdef CONFIG_X86_64
13750/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13751static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13752 u64 divisor, u64 *result)
13753{
13754 u64 low = a << shift, high = a >> (64 - shift);
13755
13756 /* To avoid the overflow on divq */
13757 if (high >= divisor)
13758 return 1;
13759
13760 /* Low hold the result, high hold rem which is discarded */
13761 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13762 "rm" (divisor), "0" (low), "1" (high));
13763 *result = low;
13764
13765 return 0;
13766}
13767
13768static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13769{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013770 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013771 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013772
13773 if (kvm_mwait_in_guest(vcpu->kvm))
13774 return -EOPNOTSUPP;
13775
13776 vmx = to_vmx(vcpu);
13777 tscl = rdtsc();
13778 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13779 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013780 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13781
13782 if (delta_tsc > lapic_timer_advance_cycles)
13783 delta_tsc -= lapic_timer_advance_cycles;
13784 else
13785 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013786
13787 /* Convert to host delta tsc if tsc scaling is enabled */
13788 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13789 u64_shl_div_u64(delta_tsc,
13790 kvm_tsc_scaling_ratio_frac_bits,
13791 vcpu->arch.tsc_scaling_ratio,
13792 &delta_tsc))
13793 return -ERANGE;
13794
13795 /*
13796 * If the delta tsc can't fit in the 32 bit after the multi shift,
13797 * we can't use the preemption timer.
13798 * It's possible that it fits on later vmentries, but checking
13799 * on every vmentry is costly so we just use an hrtimer.
13800 */
13801 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13802 return -ERANGE;
13803
13804 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013805 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013806}
13807
13808static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13809{
Sean Christophersonf459a702018-08-27 15:21:11 -070013810 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013811}
13812#endif
13813
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013814static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013815{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013816 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013817 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013818}
13819
Kai Huang843e4332015-01-28 10:54:28 +080013820static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13821 struct kvm_memory_slot *slot)
13822{
13823 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13824 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13825}
13826
13827static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13828 struct kvm_memory_slot *slot)
13829{
13830 kvm_mmu_slot_set_dirty(kvm, slot);
13831}
13832
13833static void vmx_flush_log_dirty(struct kvm *kvm)
13834{
13835 kvm_flush_pml_buffers(kvm);
13836}
13837
Bandan Dasc5f983f2017-05-05 15:25:14 -040013838static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13839{
13840 struct vmcs12 *vmcs12;
13841 struct vcpu_vmx *vmx = to_vmx(vcpu);
13842 gpa_t gpa;
13843 struct page *page = NULL;
13844 u64 *pml_address;
13845
13846 if (is_guest_mode(vcpu)) {
13847 WARN_ON_ONCE(vmx->nested.pml_full);
13848
13849 /*
13850 * Check if PML is enabled for the nested guest.
13851 * Whether eptp bit 6 is set is already checked
13852 * as part of A/D emulation.
13853 */
13854 vmcs12 = get_vmcs12(vcpu);
13855 if (!nested_cpu_has_pml(vmcs12))
13856 return 0;
13857
Dan Carpenter47698862017-05-10 22:43:17 +030013858 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013859 vmx->nested.pml_full = true;
13860 return 1;
13861 }
13862
13863 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13864
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013865 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13866 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013867 return 0;
13868
13869 pml_address = kmap(page);
13870 pml_address[vmcs12->guest_pml_index--] = gpa;
13871 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013872 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013873 }
13874
13875 return 0;
13876}
13877
Kai Huang843e4332015-01-28 10:54:28 +080013878static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13879 struct kvm_memory_slot *memslot,
13880 gfn_t offset, unsigned long mask)
13881{
13882 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13883}
13884
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013885static void __pi_post_block(struct kvm_vcpu *vcpu)
13886{
13887 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13888 struct pi_desc old, new;
13889 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013890
13891 do {
13892 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013893 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13894 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013895
13896 dest = cpu_physical_id(vcpu->cpu);
13897
13898 if (x2apic_enabled())
13899 new.ndst = dest;
13900 else
13901 new.ndst = (dest << 8) & 0xFF00;
13902
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013903 /* set 'NV' to 'notification vector' */
13904 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013905 } while (cmpxchg64(&pi_desc->control, old.control,
13906 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013907
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013908 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13909 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013910 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013911 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013912 vcpu->pre_pcpu = -1;
13913 }
13914}
13915
Feng Wuefc64402015-09-18 22:29:51 +080013916/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013917 * This routine does the following things for vCPU which is going
13918 * to be blocked if VT-d PI is enabled.
13919 * - Store the vCPU to the wakeup list, so when interrupts happen
13920 * we can find the right vCPU to wake up.
13921 * - Change the Posted-interrupt descriptor as below:
13922 * 'NDST' <-- vcpu->pre_pcpu
13923 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13924 * - If 'ON' is set during this process, which means at least one
13925 * interrupt is posted for this vCPU, we cannot block it, in
13926 * this case, return 1, otherwise, return 0.
13927 *
13928 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013929static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013930{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013931 unsigned int dest;
13932 struct pi_desc old, new;
13933 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13934
13935 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013936 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13937 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013938 return 0;
13939
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013940 WARN_ON(irqs_disabled());
13941 local_irq_disable();
13942 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13943 vcpu->pre_pcpu = vcpu->cpu;
13944 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13945 list_add_tail(&vcpu->blocked_vcpu_list,
13946 &per_cpu(blocked_vcpu_on_cpu,
13947 vcpu->pre_pcpu));
13948 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13949 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013950
13951 do {
13952 old.control = new.control = pi_desc->control;
13953
Feng Wubf9f6ac2015-09-18 22:29:55 +080013954 WARN((pi_desc->sn == 1),
13955 "Warning: SN field of posted-interrupts "
13956 "is set before blocking\n");
13957
13958 /*
13959 * Since vCPU can be preempted during this process,
13960 * vcpu->cpu could be different with pre_pcpu, we
13961 * need to set pre_pcpu as the destination of wakeup
13962 * notification event, then we can find the right vCPU
13963 * to wakeup in wakeup handler if interrupts happen
13964 * when the vCPU is in blocked state.
13965 */
13966 dest = cpu_physical_id(vcpu->pre_pcpu);
13967
13968 if (x2apic_enabled())
13969 new.ndst = dest;
13970 else
13971 new.ndst = (dest << 8) & 0xFF00;
13972
13973 /* set 'NV' to 'wakeup vector' */
13974 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013975 } while (cmpxchg64(&pi_desc->control, old.control,
13976 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013977
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013978 /* We should not block the vCPU if an interrupt is posted for it. */
13979 if (pi_test_on(pi_desc) == 1)
13980 __pi_post_block(vcpu);
13981
13982 local_irq_enable();
13983 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013984}
13985
Yunhong Jiangbc225122016-06-13 14:19:58 -070013986static int vmx_pre_block(struct kvm_vcpu *vcpu)
13987{
13988 if (pi_pre_block(vcpu))
13989 return 1;
13990
Yunhong Jiang64672c92016-06-13 14:19:59 -070013991 if (kvm_lapic_hv_timer_in_use(vcpu))
13992 kvm_lapic_switch_to_sw_timer(vcpu);
13993
Yunhong Jiangbc225122016-06-13 14:19:58 -070013994 return 0;
13995}
13996
13997static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013998{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013999 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080014000 return;
14001
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014002 WARN_ON(irqs_disabled());
14003 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020014004 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020014005 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080014006}
14007
Yunhong Jiangbc225122016-06-13 14:19:58 -070014008static void vmx_post_block(struct kvm_vcpu *vcpu)
14009{
Yunhong Jiang64672c92016-06-13 14:19:59 -070014010 if (kvm_x86_ops->set_hv_timer)
14011 kvm_lapic_switch_to_hv_timer(vcpu);
14012
Yunhong Jiangbc225122016-06-13 14:19:58 -070014013 pi_post_block(vcpu);
14014}
14015
Feng Wubf9f6ac2015-09-18 22:29:55 +080014016/*
Feng Wuefc64402015-09-18 22:29:51 +080014017 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
14018 *
14019 * @kvm: kvm
14020 * @host_irq: host irq of the interrupt
14021 * @guest_irq: gsi of the interrupt
14022 * @set: set or unset PI
14023 * returns 0 on success, < 0 on failure
14024 */
14025static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
14026 uint32_t guest_irq, bool set)
14027{
14028 struct kvm_kernel_irq_routing_entry *e;
14029 struct kvm_irq_routing_table *irq_rt;
14030 struct kvm_lapic_irq irq;
14031 struct kvm_vcpu *vcpu;
14032 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014033 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080014034
14035 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080014036 !irq_remapping_cap(IRQ_POSTING_CAP) ||
14037 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080014038 return 0;
14039
14040 idx = srcu_read_lock(&kvm->irq_srcu);
14041 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010014042 if (guest_irq >= irq_rt->nr_rt_entries ||
14043 hlist_empty(&irq_rt->map[guest_irq])) {
14044 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
14045 guest_irq, irq_rt->nr_rt_entries);
14046 goto out;
14047 }
Feng Wuefc64402015-09-18 22:29:51 +080014048
14049 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
14050 if (e->type != KVM_IRQ_ROUTING_MSI)
14051 continue;
14052 /*
14053 * VT-d PI cannot support posting multicast/broadcast
14054 * interrupts to a vCPU, we still use interrupt remapping
14055 * for these kind of interrupts.
14056 *
14057 * For lowest-priority interrupts, we only support
14058 * those with single CPU as the destination, e.g. user
14059 * configures the interrupts via /proc/irq or uses
14060 * irqbalance to make the interrupts single-CPU.
14061 *
14062 * We will support full lowest-priority interrupt later.
14063 */
14064
Radim Krčmář371313132016-07-12 22:09:27 +020014065 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080014066 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
14067 /*
14068 * Make sure the IRTE is in remapped mode if
14069 * we don't handle it in posted mode.
14070 */
14071 ret = irq_set_vcpu_affinity(host_irq, NULL);
14072 if (ret < 0) {
14073 printk(KERN_INFO
14074 "failed to back to remapped mode, irq: %u\n",
14075 host_irq);
14076 goto out;
14077 }
14078
Feng Wuefc64402015-09-18 22:29:51 +080014079 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080014080 }
Feng Wuefc64402015-09-18 22:29:51 +080014081
14082 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
14083 vcpu_info.vector = irq.vector;
14084
hu huajun2698d822018-04-11 15:16:40 +080014085 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080014086 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
14087
14088 if (set)
14089 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080014090 else
Feng Wuefc64402015-09-18 22:29:51 +080014091 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014092
14093 if (ret < 0) {
14094 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14095 __func__);
14096 goto out;
14097 }
14098 }
14099
14100 ret = 0;
14101out:
14102 srcu_read_unlock(&kvm->irq_srcu, idx);
14103 return ret;
14104}
14105
Ashok Rajc45dcc72016-06-22 14:59:56 +080014106static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14107{
14108 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14109 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14110 FEATURE_CONTROL_LMCE;
14111 else
14112 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14113 ~FEATURE_CONTROL_LMCE;
14114}
14115
Ladi Prosek72d7b372017-10-11 16:54:41 +020014116static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14117{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014118 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14119 if (to_vmx(vcpu)->nested.nested_run_pending)
14120 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014121 return 1;
14122}
14123
Ladi Prosek0234bf82017-10-11 16:54:40 +020014124static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14125{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014126 struct vcpu_vmx *vmx = to_vmx(vcpu);
14127
14128 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14129 if (vmx->nested.smm.guest_mode)
14130 nested_vmx_vmexit(vcpu, -1, 0, 0);
14131
14132 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14133 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014134 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014135 return 0;
14136}
14137
14138static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14139{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014140 struct vcpu_vmx *vmx = to_vmx(vcpu);
14141 int ret;
14142
14143 if (vmx->nested.smm.vmxon) {
14144 vmx->nested.vmxon = true;
14145 vmx->nested.smm.vmxon = false;
14146 }
14147
14148 if (vmx->nested.smm.guest_mode) {
14149 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersona633e412018-09-26 09:23:47 -070014150 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014151 vcpu->arch.hflags |= HF_SMM_MASK;
14152 if (ret)
14153 return ret;
14154
14155 vmx->nested.smm.guest_mode = false;
14156 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014157 return 0;
14158}
14159
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014160static int enable_smi_window(struct kvm_vcpu *vcpu)
14161{
14162 return 0;
14163}
14164
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014165static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14166 struct kvm_nested_state __user *user_kvm_nested_state,
14167 u32 user_data_size)
14168{
14169 struct vcpu_vmx *vmx;
14170 struct vmcs12 *vmcs12;
14171 struct kvm_nested_state kvm_state = {
14172 .flags = 0,
14173 .format = 0,
14174 .size = sizeof(kvm_state),
14175 .vmx.vmxon_pa = -1ull,
14176 .vmx.vmcs_pa = -1ull,
14177 };
14178
14179 if (!vcpu)
14180 return kvm_state.size + 2 * VMCS12_SIZE;
14181
14182 vmx = to_vmx(vcpu);
14183 vmcs12 = get_vmcs12(vcpu);
14184 if (nested_vmx_allowed(vcpu) &&
14185 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14186 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14187 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14188
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014189 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014190 kvm_state.size += VMCS12_SIZE;
14191
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014192 if (is_guest_mode(vcpu) &&
14193 nested_cpu_has_shadow_vmcs(vmcs12) &&
14194 vmcs12->vmcs_link_pointer != -1ull)
14195 kvm_state.size += VMCS12_SIZE;
14196 }
14197
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014198 if (vmx->nested.smm.vmxon)
14199 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14200
14201 if (vmx->nested.smm.guest_mode)
14202 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14203
14204 if (is_guest_mode(vcpu)) {
14205 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14206
14207 if (vmx->nested.nested_run_pending)
14208 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14209 }
14210 }
14211
14212 if (user_data_size < kvm_state.size)
14213 goto out;
14214
14215 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14216 return -EFAULT;
14217
14218 if (vmx->nested.current_vmptr == -1ull)
14219 goto out;
14220
14221 /*
14222 * When running L2, the authoritative vmcs12 state is in the
14223 * vmcs02. When running L1, the authoritative vmcs12 state is
14224 * in the shadow vmcs linked to vmcs01, unless
14225 * sync_shadow_vmcs is set, in which case, the authoritative
14226 * vmcs12 state is in the vmcs12 already.
14227 */
14228 if (is_guest_mode(vcpu))
14229 sync_vmcs12(vcpu, vmcs12);
14230 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14231 copy_shadow_to_vmcs12(vmx);
14232
14233 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14234 return -EFAULT;
14235
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014236 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14237 vmcs12->vmcs_link_pointer != -1ull) {
14238 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14239 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14240 return -EFAULT;
14241 }
14242
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014243out:
14244 return kvm_state.size;
14245}
14246
14247static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14248 struct kvm_nested_state __user *user_kvm_nested_state,
14249 struct kvm_nested_state *kvm_state)
14250{
14251 struct vcpu_vmx *vmx = to_vmx(vcpu);
14252 struct vmcs12 *vmcs12;
14253 u32 exit_qual;
14254 int ret;
14255
14256 if (kvm_state->format != 0)
14257 return -EINVAL;
14258
14259 if (!nested_vmx_allowed(vcpu))
14260 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14261
14262 if (kvm_state->vmx.vmxon_pa == -1ull) {
14263 if (kvm_state->vmx.smm.flags)
14264 return -EINVAL;
14265
14266 if (kvm_state->vmx.vmcs_pa != -1ull)
14267 return -EINVAL;
14268
14269 vmx_leave_nested(vcpu);
14270 return 0;
14271 }
14272
14273 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14274 return -EINVAL;
14275
14276 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14277 return -EINVAL;
14278
14279 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14280 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14281 return -EINVAL;
14282
14283 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14284 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14285 return -EINVAL;
14286
14287 if (kvm_state->vmx.smm.flags &
14288 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14289 return -EINVAL;
14290
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014291 /*
14292 * SMM temporarily disables VMX, so we cannot be in guest mode,
14293 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14294 * must be zero.
14295 */
14296 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14297 return -EINVAL;
14298
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014299 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14300 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14301 return -EINVAL;
14302
14303 vmx_leave_nested(vcpu);
14304 if (kvm_state->vmx.vmxon_pa == -1ull)
14305 return 0;
14306
14307 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14308 ret = enter_vmx_operation(vcpu);
14309 if (ret)
14310 return ret;
14311
14312 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14313
14314 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14315 vmx->nested.smm.vmxon = true;
14316 vmx->nested.vmxon = false;
14317
14318 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14319 vmx->nested.smm.guest_mode = true;
14320 }
14321
14322 vmcs12 = get_vmcs12(vcpu);
14323 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14324 return -EFAULT;
14325
Liran Alon392b2f22018-06-23 02:35:01 +030014326 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014327 return -EINVAL;
14328
14329 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14330 return 0;
14331
14332 vmx->nested.nested_run_pending =
14333 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14334
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014335 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14336 vmcs12->vmcs_link_pointer != -1ull) {
14337 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14338 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14339 return -EINVAL;
14340
14341 if (copy_from_user(shadow_vmcs12,
14342 user_kvm_nested_state->data + VMCS12_SIZE,
14343 sizeof(*vmcs12)))
14344 return -EFAULT;
14345
14346 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14347 !shadow_vmcs12->hdr.shadow_vmcs)
14348 return -EINVAL;
14349 }
14350
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014351 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14352 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14353 return -EINVAL;
14354
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014355 vmx->nested.dirty_vmcs12 = true;
Sean Christophersona633e412018-09-26 09:23:47 -070014356 ret = nested_vmx_enter_non_root_mode(vcpu, false);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014357 if (ret)
14358 return -EINVAL;
14359
14360 return 0;
14361}
14362
Kees Cook404f6aa2016-08-08 16:29:06 -070014363static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014364 .cpu_has_kvm_support = cpu_has_kvm_support,
14365 .disabled_by_bios = vmx_disabled_by_bios,
14366 .hardware_setup = hardware_setup,
14367 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014368 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014369 .hardware_enable = hardware_enable,
14370 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014371 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014372 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014373
Wanpeng Lib31c1142018-03-12 04:53:04 -070014374 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014375 .vm_alloc = vmx_vm_alloc,
14376 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014377
Avi Kivity6aa8b732006-12-10 02:21:36 -080014378 .vcpu_create = vmx_create_vcpu,
14379 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014380 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014381
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014382 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014383 .vcpu_load = vmx_vcpu_load,
14384 .vcpu_put = vmx_vcpu_put,
14385
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014386 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014387 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014388 .get_msr = vmx_get_msr,
14389 .set_msr = vmx_set_msr,
14390 .get_segment_base = vmx_get_segment_base,
14391 .get_segment = vmx_get_segment,
14392 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014393 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014394 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014395 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014396 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014397 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014398 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014399 .set_cr3 = vmx_set_cr3,
14400 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014401 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014402 .get_idt = vmx_get_idt,
14403 .set_idt = vmx_set_idt,
14404 .get_gdt = vmx_get_gdt,
14405 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014406 .get_dr6 = vmx_get_dr6,
14407 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014408 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014409 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014410 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014411 .get_rflags = vmx_get_rflags,
14412 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014413
Avi Kivity6aa8b732006-12-10 02:21:36 -080014414 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014415 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014416
Avi Kivity6aa8b732006-12-10 02:21:36 -080014417 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014418 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014419 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014420 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14421 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014422 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014423 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014424 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014425 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014426 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014427 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014428 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014429 .get_nmi_mask = vmx_get_nmi_mask,
14430 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014431 .enable_nmi_window = enable_nmi_window,
14432 .enable_irq_window = enable_irq_window,
14433 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014434 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014435 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014436 .get_enable_apicv = vmx_get_enable_apicv,
14437 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014438 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014439 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014440 .hwapic_irr_update = vmx_hwapic_irr_update,
14441 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014442 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014443 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14444 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014445
Izik Eiduscbc94022007-10-25 00:29:55 +020014446 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014447 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014448 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014449 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014450
Avi Kivity586f9602010-11-18 13:09:54 +020014451 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014452
Sheng Yang17cc3932010-01-05 19:02:27 +080014453 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014454
14455 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014456
14457 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014458 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014459
14460 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014461
14462 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014463
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014464 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014465 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014466
14467 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014468
14469 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014470 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014471 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014472 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014473 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014474
14475 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014476 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014477
14478 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014479
14480 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14481 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14482 .flush_log_dirty = vmx_flush_log_dirty,
14483 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014484 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014485
Feng Wubf9f6ac2015-09-18 22:29:55 +080014486 .pre_block = vmx_pre_block,
14487 .post_block = vmx_post_block,
14488
Wei Huang25462f72015-06-19 15:45:05 +020014489 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014490
14491 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014492
14493#ifdef CONFIG_X86_64
14494 .set_hv_timer = vmx_set_hv_timer,
14495 .cancel_hv_timer = vmx_cancel_hv_timer,
14496#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014497
14498 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014499
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014500 .get_nested_state = vmx_get_nested_state,
14501 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014502 .get_vmcs12_pages = nested_get_vmcs12_pages,
14503
Ladi Prosek72d7b372017-10-11 16:54:41 +020014504 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014505 .pre_enter_smm = vmx_pre_enter_smm,
14506 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014507 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014508};
14509
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014510static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014511{
14512 if (vmx_l1d_flush_pages) {
14513 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14514 vmx_l1d_flush_pages = NULL;
14515 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014516 /* Restore state so sysfs ignores VMX */
14517 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014518}
14519
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014520static void vmx_exit(void)
14521{
14522#ifdef CONFIG_KEXEC_CORE
14523 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14524 synchronize_rcu();
14525#endif
14526
14527 kvm_exit();
14528
14529#if IS_ENABLED(CONFIG_HYPERV)
14530 if (static_branch_unlikely(&enable_evmcs)) {
14531 int cpu;
14532 struct hv_vp_assist_page *vp_ap;
14533 /*
14534 * Reset everything to support using non-enlightened VMCS
14535 * access later (e.g. when we reload the module with
14536 * enlightened_vmcs=0)
14537 */
14538 for_each_online_cpu(cpu) {
14539 vp_ap = hv_get_vp_assist_page(cpu);
14540
14541 if (!vp_ap)
14542 continue;
14543
14544 vp_ap->current_nested_vmcs = 0;
14545 vp_ap->enlighten_vmentry = 0;
14546 }
14547
14548 static_branch_disable(&enable_evmcs);
14549 }
14550#endif
14551 vmx_cleanup_l1d_flush();
14552}
14553module_exit(vmx_exit);
14554
Avi Kivity6aa8b732006-12-10 02:21:36 -080014555static int __init vmx_init(void)
14556{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014557 int r;
14558
14559#if IS_ENABLED(CONFIG_HYPERV)
14560 /*
14561 * Enlightened VMCS usage should be recommended and the host needs
14562 * to support eVMCS v1 or above. We can also disable eVMCS support
14563 * with module parameter.
14564 */
14565 if (enlightened_vmcs &&
14566 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14567 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14568 KVM_EVMCS_VERSION) {
14569 int cpu;
14570
14571 /* Check that we have assist pages on all online CPUs */
14572 for_each_online_cpu(cpu) {
14573 if (!hv_get_vp_assist_page(cpu)) {
14574 enlightened_vmcs = false;
14575 break;
14576 }
14577 }
14578
14579 if (enlightened_vmcs) {
14580 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14581 static_branch_enable(&enable_evmcs);
14582 }
14583 } else {
14584 enlightened_vmcs = false;
14585 }
14586#endif
14587
14588 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014589 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014590 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014591 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014592
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014593 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014594 * Must be called after kvm_init() so enable_ept is properly set
14595 * up. Hand the parameter mitigation value in which was stored in
14596 * the pre module init parser. If no parameter was given, it will
14597 * contain 'auto' which will be turned into the default 'cond'
14598 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014599 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014600 if (boot_cpu_has(X86_BUG_L1TF)) {
14601 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14602 if (r) {
14603 vmx_exit();
14604 return r;
14605 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014606 }
14607
Dave Young2965faa2015-09-09 15:38:55 -070014608#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014609 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14610 crash_vmclear_local_loaded_vmcss);
14611#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014612 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014613
He, Qingfdef3ad2007-04-30 09:45:24 +030014614 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014615}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014616module_init(vmx_init);