blob: c76ca8c4befa6e03668158b46abdb9f01acc84eb [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
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
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140#define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200143
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800144#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200145#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
Avi Kivity78ac8b42010-04-08 18:19:35 +0300148#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
Jan Kiszkaf4124502014-03-07 20:03:13 +0100150#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156#define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162/*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500166 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200195
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
201 enum vmx_l1d_flush_state cmd;
202} vmentry_l1d_param[] = {
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +0200203 {"auto", VMENTER_L1D_FLUSH_AUTO},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200204 {"never", VMENTER_L1D_FLUSH_NEVER},
205 {"cond", VMENTER_L1D_FLUSH_COND},
206 {"always", VMENTER_L1D_FLUSH_ALWAYS},
207};
208
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200209#define L1D_CACHE_ORDER 4
210static void *vmx_l1d_flush_pages;
211
212static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
213{
214 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200215 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200216
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200217 if (!enable_ept) {
218 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
219 return 0;
220 }
221
Yi Wangd806afa2018-08-16 13:42:39 +0800222 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
223 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200224
Yi Wangd806afa2018-08-16 13:42:39 +0800225 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
226 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
227 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
228 return 0;
229 }
230 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200231
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200232 /* If set to auto use the default l1tf mitigation method */
233 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
234 switch (l1tf_mitigation) {
235 case L1TF_MITIGATION_OFF:
236 l1tf = VMENTER_L1D_FLUSH_NEVER;
237 break;
238 case L1TF_MITIGATION_FLUSH_NOWARN:
239 case L1TF_MITIGATION_FLUSH:
240 case L1TF_MITIGATION_FLUSH_NOSMT:
241 l1tf = VMENTER_L1D_FLUSH_COND;
242 break;
243 case L1TF_MITIGATION_FULL:
244 case L1TF_MITIGATION_FULL_FORCE:
245 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
246 break;
247 }
248 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
249 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
250 }
251
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200252 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
253 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
254 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
255 if (!page)
256 return -ENOMEM;
257 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200258
259 /*
260 * Initialize each page with a different pattern in
261 * order to protect against KSM in the nested
262 * virtualization case.
263 */
264 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
265 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
266 PAGE_SIZE);
267 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200268 }
269
270 l1tf_vmx_mitigation = l1tf;
271
Thomas Gleixner895ae472018-07-13 16:23:22 +0200272 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
273 static_branch_enable(&vmx_l1d_should_flush);
274 else
275 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200276
Nicolai Stange427362a2018-07-21 22:25:00 +0200277 if (l1tf == VMENTER_L1D_FLUSH_COND)
278 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200279 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200280 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200281 return 0;
282}
283
284static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200285{
286 unsigned int i;
287
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200288 if (s) {
289 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Thomas Gleixner895ae472018-07-13 16:23:22 +0200290 if (sysfs_streq(s, vmentry_l1d_param[i].option))
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200291 return vmentry_l1d_param[i].cmd;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200292 }
293 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200294 return -EINVAL;
295}
296
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200297static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
298{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200299 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200300
301 if (!boot_cpu_has(X86_BUG_L1TF))
302 return 0;
303
304 l1tf = vmentry_l1d_flush_parse(s);
305 if (l1tf < 0)
306 return l1tf;
307
308 /*
309 * Has vmx_init() run already? If not then this is the pre init
310 * parameter parsing. In that case just store the value and let
311 * vmx_init() do the proper setup after enable_ept has been
312 * established.
313 */
314 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
315 vmentry_l1d_flush_param = l1tf;
316 return 0;
317 }
318
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200319 mutex_lock(&vmx_l1d_flush_mutex);
320 ret = vmx_setup_l1d_flush(l1tf);
321 mutex_unlock(&vmx_l1d_flush_mutex);
322 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200323}
324
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200325static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
326{
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200327 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200328}
329
330static const struct kernel_param_ops vmentry_l1d_flush_ops = {
331 .set = vmentry_l1d_flush_set,
332 .get = vmentry_l1d_flush_get,
333};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200334module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200335
Tianyu Lan877ad952018-07-19 08:40:23 +0000336enum ept_pointers_status {
337 EPT_POINTERS_CHECK = 0,
338 EPT_POINTERS_MATCH = 1,
339 EPT_POINTERS_MISMATCH = 2
340};
341
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700342struct kvm_vmx {
343 struct kvm kvm;
344
345 unsigned int tss_addr;
346 bool ept_identity_pagetable_done;
347 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000348
349 enum ept_pointers_status ept_pointers_match;
350 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700351};
352
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200353#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300354
Liran Alon392b2f22018-06-23 02:35:01 +0300355struct vmcs_hdr {
356 u32 revision_id:31;
357 u32 shadow_vmcs:1;
358};
359
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400360struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300361 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400362 u32 abort;
363 char data[0];
364};
365
Nadav Har'Eld462b812011-05-24 15:26:10 +0300366/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700367 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
368 * and whose values change infrequently, but are not constant. I.e. this is
369 * used as a write-through cache of the corresponding VMCS fields.
370 */
371struct vmcs_host_state {
372 unsigned long cr3; /* May not match real cr3 */
373 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700374 unsigned long gs_base;
375 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700376
377 u16 fs_sel, gs_sel, ldt_sel;
378#ifdef CONFIG_X86_64
379 u16 ds_sel, es_sel;
380#endif
381};
382
383/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300384 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
385 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
386 * loaded on this CPU (so we can clear them if the CPU goes down).
387 */
388struct loaded_vmcs {
389 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700390 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300391 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200392 bool launched;
393 bool nmi_known_unmasked;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100394 /* Support for vnmi-less CPUs */
395 int soft_vnmi_blocked;
396 ktime_t entry_time;
397 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100398 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300399 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700400 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300401};
402
Avi Kivity26bb0982009-09-07 11:14:12 +0300403struct shared_msr_entry {
404 unsigned index;
405 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200406 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300407};
408
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300409/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300410 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
411 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
412 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
413 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
414 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
415 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600416 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300417 * underlying hardware which will be used to run L2.
418 * This structure is packed to ensure that its layout is identical across
419 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700420 *
421 * IMPORTANT: Changing the layout of existing fields in this structure
422 * will break save/restore compatibility with older kvm releases. When
423 * adding new fields, either use space in the reserved padding* arrays
424 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300425 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300426typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300427struct __packed vmcs12 {
428 /* According to the Intel spec, a VMCS region must start with the
429 * following two fields. Then follow implementation-specific data.
430 */
Liran Alon392b2f22018-06-23 02:35:01 +0300431 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300432 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300433
Nadav Har'El27d6c862011-05-25 23:06:59 +0300434 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
435 u32 padding[7]; /* room for future expansion */
436
Nadav Har'El22bd0352011-05-25 23:05:57 +0300437 u64 io_bitmap_a;
438 u64 io_bitmap_b;
439 u64 msr_bitmap;
440 u64 vm_exit_msr_store_addr;
441 u64 vm_exit_msr_load_addr;
442 u64 vm_entry_msr_load_addr;
443 u64 tsc_offset;
444 u64 virtual_apic_page_addr;
445 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800446 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300447 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800448 u64 eoi_exit_bitmap0;
449 u64 eoi_exit_bitmap1;
450 u64 eoi_exit_bitmap2;
451 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800452 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300453 u64 guest_physical_address;
454 u64 vmcs_link_pointer;
455 u64 guest_ia32_debugctl;
456 u64 guest_ia32_pat;
457 u64 guest_ia32_efer;
458 u64 guest_ia32_perf_global_ctrl;
459 u64 guest_pdptr0;
460 u64 guest_pdptr1;
461 u64 guest_pdptr2;
462 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100463 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300464 u64 host_ia32_pat;
465 u64 host_ia32_efer;
466 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700467 u64 vmread_bitmap;
468 u64 vmwrite_bitmap;
469 u64 vm_function_control;
470 u64 eptp_list_address;
471 u64 pml_address;
472 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300473 /*
474 * To allow migration of L1 (complete with its L2 guests) between
475 * machines of different natural widths (32 or 64 bit), we cannot have
476 * unsigned long fields with no explict size. We use u64 (aliased
477 * natural_width) instead. Luckily, x86 is little-endian.
478 */
479 natural_width cr0_guest_host_mask;
480 natural_width cr4_guest_host_mask;
481 natural_width cr0_read_shadow;
482 natural_width cr4_read_shadow;
483 natural_width cr3_target_value0;
484 natural_width cr3_target_value1;
485 natural_width cr3_target_value2;
486 natural_width cr3_target_value3;
487 natural_width exit_qualification;
488 natural_width guest_linear_address;
489 natural_width guest_cr0;
490 natural_width guest_cr3;
491 natural_width guest_cr4;
492 natural_width guest_es_base;
493 natural_width guest_cs_base;
494 natural_width guest_ss_base;
495 natural_width guest_ds_base;
496 natural_width guest_fs_base;
497 natural_width guest_gs_base;
498 natural_width guest_ldtr_base;
499 natural_width guest_tr_base;
500 natural_width guest_gdtr_base;
501 natural_width guest_idtr_base;
502 natural_width guest_dr7;
503 natural_width guest_rsp;
504 natural_width guest_rip;
505 natural_width guest_rflags;
506 natural_width guest_pending_dbg_exceptions;
507 natural_width guest_sysenter_esp;
508 natural_width guest_sysenter_eip;
509 natural_width host_cr0;
510 natural_width host_cr3;
511 natural_width host_cr4;
512 natural_width host_fs_base;
513 natural_width host_gs_base;
514 natural_width host_tr_base;
515 natural_width host_gdtr_base;
516 natural_width host_idtr_base;
517 natural_width host_ia32_sysenter_esp;
518 natural_width host_ia32_sysenter_eip;
519 natural_width host_rsp;
520 natural_width host_rip;
521 natural_width paddingl[8]; /* room for future expansion */
522 u32 pin_based_vm_exec_control;
523 u32 cpu_based_vm_exec_control;
524 u32 exception_bitmap;
525 u32 page_fault_error_code_mask;
526 u32 page_fault_error_code_match;
527 u32 cr3_target_count;
528 u32 vm_exit_controls;
529 u32 vm_exit_msr_store_count;
530 u32 vm_exit_msr_load_count;
531 u32 vm_entry_controls;
532 u32 vm_entry_msr_load_count;
533 u32 vm_entry_intr_info_field;
534 u32 vm_entry_exception_error_code;
535 u32 vm_entry_instruction_len;
536 u32 tpr_threshold;
537 u32 secondary_vm_exec_control;
538 u32 vm_instruction_error;
539 u32 vm_exit_reason;
540 u32 vm_exit_intr_info;
541 u32 vm_exit_intr_error_code;
542 u32 idt_vectoring_info_field;
543 u32 idt_vectoring_error_code;
544 u32 vm_exit_instruction_len;
545 u32 vmx_instruction_info;
546 u32 guest_es_limit;
547 u32 guest_cs_limit;
548 u32 guest_ss_limit;
549 u32 guest_ds_limit;
550 u32 guest_fs_limit;
551 u32 guest_gs_limit;
552 u32 guest_ldtr_limit;
553 u32 guest_tr_limit;
554 u32 guest_gdtr_limit;
555 u32 guest_idtr_limit;
556 u32 guest_es_ar_bytes;
557 u32 guest_cs_ar_bytes;
558 u32 guest_ss_ar_bytes;
559 u32 guest_ds_ar_bytes;
560 u32 guest_fs_ar_bytes;
561 u32 guest_gs_ar_bytes;
562 u32 guest_ldtr_ar_bytes;
563 u32 guest_tr_ar_bytes;
564 u32 guest_interruptibility_info;
565 u32 guest_activity_state;
566 u32 guest_sysenter_cs;
567 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100568 u32 vmx_preemption_timer_value;
569 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300570 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800571 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300572 u16 guest_es_selector;
573 u16 guest_cs_selector;
574 u16 guest_ss_selector;
575 u16 guest_ds_selector;
576 u16 guest_fs_selector;
577 u16 guest_gs_selector;
578 u16 guest_ldtr_selector;
579 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800580 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300581 u16 host_es_selector;
582 u16 host_cs_selector;
583 u16 host_ss_selector;
584 u16 host_ds_selector;
585 u16 host_fs_selector;
586 u16 host_gs_selector;
587 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700588 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300589};
590
591/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700592 * For save/restore compatibility, the vmcs12 field offsets must not change.
593 */
594#define CHECK_OFFSET(field, loc) \
595 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
596 "Offset of " #field " in struct vmcs12 has changed.")
597
598static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300599 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700600 CHECK_OFFSET(abort, 4);
601 CHECK_OFFSET(launch_state, 8);
602 CHECK_OFFSET(io_bitmap_a, 40);
603 CHECK_OFFSET(io_bitmap_b, 48);
604 CHECK_OFFSET(msr_bitmap, 56);
605 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
606 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
607 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
608 CHECK_OFFSET(tsc_offset, 88);
609 CHECK_OFFSET(virtual_apic_page_addr, 96);
610 CHECK_OFFSET(apic_access_addr, 104);
611 CHECK_OFFSET(posted_intr_desc_addr, 112);
612 CHECK_OFFSET(ept_pointer, 120);
613 CHECK_OFFSET(eoi_exit_bitmap0, 128);
614 CHECK_OFFSET(eoi_exit_bitmap1, 136);
615 CHECK_OFFSET(eoi_exit_bitmap2, 144);
616 CHECK_OFFSET(eoi_exit_bitmap3, 152);
617 CHECK_OFFSET(xss_exit_bitmap, 160);
618 CHECK_OFFSET(guest_physical_address, 168);
619 CHECK_OFFSET(vmcs_link_pointer, 176);
620 CHECK_OFFSET(guest_ia32_debugctl, 184);
621 CHECK_OFFSET(guest_ia32_pat, 192);
622 CHECK_OFFSET(guest_ia32_efer, 200);
623 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
624 CHECK_OFFSET(guest_pdptr0, 216);
625 CHECK_OFFSET(guest_pdptr1, 224);
626 CHECK_OFFSET(guest_pdptr2, 232);
627 CHECK_OFFSET(guest_pdptr3, 240);
628 CHECK_OFFSET(guest_bndcfgs, 248);
629 CHECK_OFFSET(host_ia32_pat, 256);
630 CHECK_OFFSET(host_ia32_efer, 264);
631 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
632 CHECK_OFFSET(vmread_bitmap, 280);
633 CHECK_OFFSET(vmwrite_bitmap, 288);
634 CHECK_OFFSET(vm_function_control, 296);
635 CHECK_OFFSET(eptp_list_address, 304);
636 CHECK_OFFSET(pml_address, 312);
637 CHECK_OFFSET(cr0_guest_host_mask, 344);
638 CHECK_OFFSET(cr4_guest_host_mask, 352);
639 CHECK_OFFSET(cr0_read_shadow, 360);
640 CHECK_OFFSET(cr4_read_shadow, 368);
641 CHECK_OFFSET(cr3_target_value0, 376);
642 CHECK_OFFSET(cr3_target_value1, 384);
643 CHECK_OFFSET(cr3_target_value2, 392);
644 CHECK_OFFSET(cr3_target_value3, 400);
645 CHECK_OFFSET(exit_qualification, 408);
646 CHECK_OFFSET(guest_linear_address, 416);
647 CHECK_OFFSET(guest_cr0, 424);
648 CHECK_OFFSET(guest_cr3, 432);
649 CHECK_OFFSET(guest_cr4, 440);
650 CHECK_OFFSET(guest_es_base, 448);
651 CHECK_OFFSET(guest_cs_base, 456);
652 CHECK_OFFSET(guest_ss_base, 464);
653 CHECK_OFFSET(guest_ds_base, 472);
654 CHECK_OFFSET(guest_fs_base, 480);
655 CHECK_OFFSET(guest_gs_base, 488);
656 CHECK_OFFSET(guest_ldtr_base, 496);
657 CHECK_OFFSET(guest_tr_base, 504);
658 CHECK_OFFSET(guest_gdtr_base, 512);
659 CHECK_OFFSET(guest_idtr_base, 520);
660 CHECK_OFFSET(guest_dr7, 528);
661 CHECK_OFFSET(guest_rsp, 536);
662 CHECK_OFFSET(guest_rip, 544);
663 CHECK_OFFSET(guest_rflags, 552);
664 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
665 CHECK_OFFSET(guest_sysenter_esp, 568);
666 CHECK_OFFSET(guest_sysenter_eip, 576);
667 CHECK_OFFSET(host_cr0, 584);
668 CHECK_OFFSET(host_cr3, 592);
669 CHECK_OFFSET(host_cr4, 600);
670 CHECK_OFFSET(host_fs_base, 608);
671 CHECK_OFFSET(host_gs_base, 616);
672 CHECK_OFFSET(host_tr_base, 624);
673 CHECK_OFFSET(host_gdtr_base, 632);
674 CHECK_OFFSET(host_idtr_base, 640);
675 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
676 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
677 CHECK_OFFSET(host_rsp, 664);
678 CHECK_OFFSET(host_rip, 672);
679 CHECK_OFFSET(pin_based_vm_exec_control, 744);
680 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
681 CHECK_OFFSET(exception_bitmap, 752);
682 CHECK_OFFSET(page_fault_error_code_mask, 756);
683 CHECK_OFFSET(page_fault_error_code_match, 760);
684 CHECK_OFFSET(cr3_target_count, 764);
685 CHECK_OFFSET(vm_exit_controls, 768);
686 CHECK_OFFSET(vm_exit_msr_store_count, 772);
687 CHECK_OFFSET(vm_exit_msr_load_count, 776);
688 CHECK_OFFSET(vm_entry_controls, 780);
689 CHECK_OFFSET(vm_entry_msr_load_count, 784);
690 CHECK_OFFSET(vm_entry_intr_info_field, 788);
691 CHECK_OFFSET(vm_entry_exception_error_code, 792);
692 CHECK_OFFSET(vm_entry_instruction_len, 796);
693 CHECK_OFFSET(tpr_threshold, 800);
694 CHECK_OFFSET(secondary_vm_exec_control, 804);
695 CHECK_OFFSET(vm_instruction_error, 808);
696 CHECK_OFFSET(vm_exit_reason, 812);
697 CHECK_OFFSET(vm_exit_intr_info, 816);
698 CHECK_OFFSET(vm_exit_intr_error_code, 820);
699 CHECK_OFFSET(idt_vectoring_info_field, 824);
700 CHECK_OFFSET(idt_vectoring_error_code, 828);
701 CHECK_OFFSET(vm_exit_instruction_len, 832);
702 CHECK_OFFSET(vmx_instruction_info, 836);
703 CHECK_OFFSET(guest_es_limit, 840);
704 CHECK_OFFSET(guest_cs_limit, 844);
705 CHECK_OFFSET(guest_ss_limit, 848);
706 CHECK_OFFSET(guest_ds_limit, 852);
707 CHECK_OFFSET(guest_fs_limit, 856);
708 CHECK_OFFSET(guest_gs_limit, 860);
709 CHECK_OFFSET(guest_ldtr_limit, 864);
710 CHECK_OFFSET(guest_tr_limit, 868);
711 CHECK_OFFSET(guest_gdtr_limit, 872);
712 CHECK_OFFSET(guest_idtr_limit, 876);
713 CHECK_OFFSET(guest_es_ar_bytes, 880);
714 CHECK_OFFSET(guest_cs_ar_bytes, 884);
715 CHECK_OFFSET(guest_ss_ar_bytes, 888);
716 CHECK_OFFSET(guest_ds_ar_bytes, 892);
717 CHECK_OFFSET(guest_fs_ar_bytes, 896);
718 CHECK_OFFSET(guest_gs_ar_bytes, 900);
719 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
720 CHECK_OFFSET(guest_tr_ar_bytes, 908);
721 CHECK_OFFSET(guest_interruptibility_info, 912);
722 CHECK_OFFSET(guest_activity_state, 916);
723 CHECK_OFFSET(guest_sysenter_cs, 920);
724 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
725 CHECK_OFFSET(vmx_preemption_timer_value, 928);
726 CHECK_OFFSET(virtual_processor_id, 960);
727 CHECK_OFFSET(posted_intr_nv, 962);
728 CHECK_OFFSET(guest_es_selector, 964);
729 CHECK_OFFSET(guest_cs_selector, 966);
730 CHECK_OFFSET(guest_ss_selector, 968);
731 CHECK_OFFSET(guest_ds_selector, 970);
732 CHECK_OFFSET(guest_fs_selector, 972);
733 CHECK_OFFSET(guest_gs_selector, 974);
734 CHECK_OFFSET(guest_ldtr_selector, 976);
735 CHECK_OFFSET(guest_tr_selector, 978);
736 CHECK_OFFSET(guest_intr_status, 980);
737 CHECK_OFFSET(host_es_selector, 982);
738 CHECK_OFFSET(host_cs_selector, 984);
739 CHECK_OFFSET(host_ss_selector, 986);
740 CHECK_OFFSET(host_ds_selector, 988);
741 CHECK_OFFSET(host_fs_selector, 990);
742 CHECK_OFFSET(host_gs_selector, 992);
743 CHECK_OFFSET(host_tr_selector, 994);
744 CHECK_OFFSET(guest_pml_index, 996);
745}
746
747/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300748 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
749 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
750 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700751 *
752 * IMPORTANT: Changing this value will break save/restore compatibility with
753 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300754 */
755#define VMCS12_REVISION 0x11e57ed0
756
757/*
758 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
759 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
760 * current implementation, 4K are reserved to avoid future complications.
761 */
762#define VMCS12_SIZE 0x1000
763
764/*
Jim Mattson5b157062017-12-22 12:11:12 -0800765 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
766 * supported VMCS12 field encoding.
767 */
768#define VMCS12_MAX_FIELD_INDEX 0x17
769
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100770struct nested_vmx_msrs {
771 /*
772 * We only store the "true" versions of the VMX capability MSRs. We
773 * generate the "non-true" versions by setting the must-be-1 bits
774 * according to the SDM.
775 */
776 u32 procbased_ctls_low;
777 u32 procbased_ctls_high;
778 u32 secondary_ctls_low;
779 u32 secondary_ctls_high;
780 u32 pinbased_ctls_low;
781 u32 pinbased_ctls_high;
782 u32 exit_ctls_low;
783 u32 exit_ctls_high;
784 u32 entry_ctls_low;
785 u32 entry_ctls_high;
786 u32 misc_low;
787 u32 misc_high;
788 u32 ept_caps;
789 u32 vpid_caps;
790 u64 basic;
791 u64 cr0_fixed0;
792 u64 cr0_fixed1;
793 u64 cr4_fixed0;
794 u64 cr4_fixed1;
795 u64 vmcs_enum;
796 u64 vmfunc_controls;
797};
798
Jim Mattson5b157062017-12-22 12:11:12 -0800799/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300800 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
801 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
802 */
803struct nested_vmx {
804 /* Has the level1 guest done vmxon? */
805 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400806 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400807 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300808
809 /* The guest-physical address of the current VMCS L1 keeps for L2 */
810 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700811 /*
812 * Cache of the guest's VMCS, existing outside of guest memory.
813 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700814 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700815 */
816 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300817 /*
Liran Alon61ada742018-06-23 02:35:08 +0300818 * Cache of the guest's shadow VMCS, existing outside of guest
819 * memory. Loaded from guest memory during VM entry. Flushed
820 * to guest memory during VM exit.
821 */
822 struct vmcs12 *cached_shadow_vmcs12;
823 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300824 * Indicates if the shadow vmcs must be updated with the
825 * data hold by vmcs12
826 */
827 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100828 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300829
Jim Mattson8d860bb2018-05-09 16:56:05 -0400830 bool change_vmcs01_virtual_apic_mode;
831
Nadav Har'El644d7112011-05-25 23:12:35 +0300832 /* L2 must run next, and mustn't decide to exit to L1. */
833 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600834
835 struct loaded_vmcs vmcs02;
836
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300837 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600838 * Guest pages referred to in the vmcs02 with host-physical
839 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300840 */
841 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800842 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800843 struct page *pi_desc_page;
844 struct pi_desc *pi_desc;
845 bool pi_pending;
846 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100847
848 struct hrtimer preemption_timer;
849 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200850
851 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
852 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800853
Wanpeng Li5c614b32015-10-13 09:18:36 -0700854 u16 vpid02;
855 u16 last_vpid;
856
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100857 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200858
859 /* SMM related state */
860 struct {
861 /* in VMX operation on SMM entry? */
862 bool vmxon;
863 /* in guest mode on SMM entry? */
864 bool guest_mode;
865 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300866};
867
Yang Zhang01e439b2013-04-11 19:25:12 +0800868#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800869#define POSTED_INTR_SN 1
870
Yang Zhang01e439b2013-04-11 19:25:12 +0800871/* Posted-Interrupt Descriptor */
872struct pi_desc {
873 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800874 union {
875 struct {
876 /* bit 256 - Outstanding Notification */
877 u16 on : 1,
878 /* bit 257 - Suppress Notification */
879 sn : 1,
880 /* bit 271:258 - Reserved */
881 rsvd_1 : 14;
882 /* bit 279:272 - Notification Vector */
883 u8 nv;
884 /* bit 287:280 - Reserved */
885 u8 rsvd_2;
886 /* bit 319:288 - Notification Destination */
887 u32 ndst;
888 };
889 u64 control;
890 };
891 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800892} __aligned(64);
893
Yang Zhanga20ed542013-04-11 19:25:15 +0800894static bool pi_test_and_set_on(struct pi_desc *pi_desc)
895{
896 return test_and_set_bit(POSTED_INTR_ON,
897 (unsigned long *)&pi_desc->control);
898}
899
900static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
901{
902 return test_and_clear_bit(POSTED_INTR_ON,
903 (unsigned long *)&pi_desc->control);
904}
905
906static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
907{
908 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
909}
910
Feng Wuebbfc762015-09-18 22:29:46 +0800911static inline void pi_clear_sn(struct pi_desc *pi_desc)
912{
913 return clear_bit(POSTED_INTR_SN,
914 (unsigned long *)&pi_desc->control);
915}
916
917static inline void pi_set_sn(struct pi_desc *pi_desc)
918{
919 return set_bit(POSTED_INTR_SN,
920 (unsigned long *)&pi_desc->control);
921}
922
Paolo Bonziniad361092016-09-20 16:15:05 +0200923static inline void pi_clear_on(struct pi_desc *pi_desc)
924{
925 clear_bit(POSTED_INTR_ON,
926 (unsigned long *)&pi_desc->control);
927}
928
Feng Wuebbfc762015-09-18 22:29:46 +0800929static inline int pi_test_on(struct pi_desc *pi_desc)
930{
931 return test_bit(POSTED_INTR_ON,
932 (unsigned long *)&pi_desc->control);
933}
934
935static inline int pi_test_sn(struct pi_desc *pi_desc)
936{
937 return test_bit(POSTED_INTR_SN,
938 (unsigned long *)&pi_desc->control);
939}
940
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400941struct vmx_msrs {
942 unsigned int nr;
943 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
944};
945
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400946struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000947 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300948 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300949 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100950 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300951 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200952 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200953 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300954 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400955 int nmsrs;
956 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800957 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400958#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300959 u64 msr_host_kernel_gs_base;
960 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400961#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100962
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100963 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100964 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100965
Gleb Natapov2961e8762013-11-25 15:37:13 +0200966 u32 vm_entry_controls_shadow;
967 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200968 u32 secondary_exec_control;
969
Nadav Har'Eld462b812011-05-24 15:26:10 +0300970 /*
971 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
972 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700973 * guest (L2), it points to a different VMCS. loaded_cpu_state points
974 * to the VMCS whose state is loaded into the CPU registers that only
975 * need to be switched when transitioning to/from the kernel; a NULL
976 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300977 */
978 struct loaded_vmcs vmcs01;
979 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700980 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300981 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300982 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400983 struct vmx_msrs guest;
984 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300985 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700986
Avi Kivity9c8cba32007-11-22 11:42:59 +0200987 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300988 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300989 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300990 struct kvm_segment segs[8];
991 } rmode;
992 struct {
993 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300994 struct kvm_save_segment {
995 u16 selector;
996 unsigned long base;
997 u32 limit;
998 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300999 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001000 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001001 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001002 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001003
Andi Kleena0861c02009-06-08 17:37:09 +08001004 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001005
Yang Zhang01e439b2013-04-11 19:25:12 +08001006 /* Posted interrupt descriptor */
1007 struct pi_desc pi_desc;
1008
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001009 /* Support for a guest hypervisor (nested VMX) */
1010 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001011
1012 /* Dynamic PLE window. */
1013 int ple_window;
1014 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001015
1016 /* Support for PML */
1017#define PML_ENTITY_NUM 512
1018 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001019
Yunhong Jiang64672c92016-06-13 14:19:59 -07001020 /* apic deadline value in host tsc */
1021 u64 hv_deadline_tsc;
1022
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001023 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001024
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001025 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001026
Wanpeng Li74c55932017-11-29 01:31:20 -08001027 unsigned long host_debugctlmsr;
1028
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001029 /*
1030 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1031 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1032 * in msr_ia32_feature_control_valid_bits.
1033 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001034 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001035 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001036 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001037};
1038
Avi Kivity2fb92db2011-04-27 19:42:18 +03001039enum segment_cache_field {
1040 SEG_FIELD_SEL = 0,
1041 SEG_FIELD_BASE = 1,
1042 SEG_FIELD_LIMIT = 2,
1043 SEG_FIELD_AR = 3,
1044
1045 SEG_FIELD_NR = 4
1046};
1047
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001048static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1049{
1050 return container_of(kvm, struct kvm_vmx, kvm);
1051}
1052
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001053static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1054{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001055 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001056}
1057
Feng Wuefc64402015-09-18 22:29:51 +08001058static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1059{
1060 return &(to_vmx(vcpu)->pi_desc);
1061}
1062
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001063#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001064#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001065#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1066#define FIELD64(number, name) \
1067 FIELD(number, name), \
1068 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001069
Abel Gordon4607c2d2013-04-18 14:35:55 +03001070
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001071static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001072#define SHADOW_FIELD_RO(x) x,
1073#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001074};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001075static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001076 ARRAY_SIZE(shadow_read_only_fields);
1077
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001078static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001079#define SHADOW_FIELD_RW(x) x,
1080#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001081};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001082static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001083 ARRAY_SIZE(shadow_read_write_fields);
1084
Mathias Krause772e0312012-08-30 01:30:19 +02001085static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001086 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001087 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001088 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1089 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1090 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1091 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1092 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1093 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1094 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1095 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001096 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001097 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001098 FIELD(HOST_ES_SELECTOR, host_es_selector),
1099 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1100 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1101 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1102 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1103 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1104 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1105 FIELD64(IO_BITMAP_A, io_bitmap_a),
1106 FIELD64(IO_BITMAP_B, io_bitmap_b),
1107 FIELD64(MSR_BITMAP, msr_bitmap),
1108 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1109 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1110 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001111 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001112 FIELD64(TSC_OFFSET, tsc_offset),
1113 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1114 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001115 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001116 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001117 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001118 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1119 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1120 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1121 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001122 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001123 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1124 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001125 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001126 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1127 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1128 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1129 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1130 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1131 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1132 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1133 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1134 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1135 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001136 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001137 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1138 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1139 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1140 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1141 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1142 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1143 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1144 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1145 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1146 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1147 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1148 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1149 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1150 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1151 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1152 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1153 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1154 FIELD(TPR_THRESHOLD, tpr_threshold),
1155 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1156 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1157 FIELD(VM_EXIT_REASON, vm_exit_reason),
1158 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1159 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1160 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1161 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1162 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1163 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1164 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1165 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1166 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1167 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1168 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1169 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1170 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1171 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1172 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1173 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1174 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1175 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1176 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1177 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1178 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1179 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1180 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1181 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1182 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1183 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1184 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1185 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001186 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001187 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1188 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1189 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1190 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1191 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1192 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1193 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1194 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1195 FIELD(EXIT_QUALIFICATION, exit_qualification),
1196 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1197 FIELD(GUEST_CR0, guest_cr0),
1198 FIELD(GUEST_CR3, guest_cr3),
1199 FIELD(GUEST_CR4, guest_cr4),
1200 FIELD(GUEST_ES_BASE, guest_es_base),
1201 FIELD(GUEST_CS_BASE, guest_cs_base),
1202 FIELD(GUEST_SS_BASE, guest_ss_base),
1203 FIELD(GUEST_DS_BASE, guest_ds_base),
1204 FIELD(GUEST_FS_BASE, guest_fs_base),
1205 FIELD(GUEST_GS_BASE, guest_gs_base),
1206 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1207 FIELD(GUEST_TR_BASE, guest_tr_base),
1208 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1209 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1210 FIELD(GUEST_DR7, guest_dr7),
1211 FIELD(GUEST_RSP, guest_rsp),
1212 FIELD(GUEST_RIP, guest_rip),
1213 FIELD(GUEST_RFLAGS, guest_rflags),
1214 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1215 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1216 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1217 FIELD(HOST_CR0, host_cr0),
1218 FIELD(HOST_CR3, host_cr3),
1219 FIELD(HOST_CR4, host_cr4),
1220 FIELD(HOST_FS_BASE, host_fs_base),
1221 FIELD(HOST_GS_BASE, host_gs_base),
1222 FIELD(HOST_TR_BASE, host_tr_base),
1223 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1224 FIELD(HOST_IDTR_BASE, host_idtr_base),
1225 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1226 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1227 FIELD(HOST_RSP, host_rsp),
1228 FIELD(HOST_RIP, host_rip),
1229};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001230
1231static inline short vmcs_field_to_offset(unsigned long field)
1232{
Dan Williams085331d2018-01-31 17:47:03 -08001233 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1234 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001235 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001236
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001237 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001238 return -ENOENT;
1239
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001240 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001241 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001242 return -ENOENT;
1243
Linus Torvalds15303ba2018-02-10 13:16:35 -08001244 index = array_index_nospec(index, size);
1245 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001246 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001247 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001248 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001249}
1250
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001251static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1252{
David Matlack4f2777b2016-07-13 17:16:37 -07001253 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001254}
1255
Liran Alon61ada742018-06-23 02:35:08 +03001256static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1257{
1258 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1259}
1260
Peter Feiner995f00a2017-06-30 17:26:32 -07001261static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001262static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001263static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001264static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001265static void vmx_set_segment(struct kvm_vcpu *vcpu,
1266 struct kvm_segment *var, int seg);
1267static void vmx_get_segment(struct kvm_vcpu *vcpu,
1268 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001269static bool guest_state_valid(struct kvm_vcpu *vcpu);
1270static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001271static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001272static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1273static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1274static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1275 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001276static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001277static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1278 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001279
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1281static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001282/*
1283 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1284 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1285 */
1286static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287
Feng Wubf9f6ac2015-09-18 22:29:55 +08001288/*
1289 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1290 * can find which vCPU should be waken up.
1291 */
1292static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1293static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1294
Radim Krčmář23611332016-09-29 22:41:33 +02001295enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001296 VMX_VMREAD_BITMAP,
1297 VMX_VMWRITE_BITMAP,
1298 VMX_BITMAP_NR
1299};
1300
1301static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1302
Radim Krčmář23611332016-09-29 22:41:33 +02001303#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1304#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001305
Avi Kivity110312c2010-12-21 12:54:20 +02001306static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001307static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001308
Sheng Yang2384d2b2008-01-17 15:14:33 +08001309static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1310static DEFINE_SPINLOCK(vmx_vpid_lock);
1311
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001312static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 int size;
1314 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001315 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001317 u32 pin_based_exec_ctrl;
1318 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001319 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001320 u32 vmexit_ctrl;
1321 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001322 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001323} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324
Hannes Ederefff9e52008-11-28 17:02:06 +01001325static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001326 u32 ept;
1327 u32 vpid;
1328} vmx_capability;
1329
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330#define VMX_SEGMENT_FIELD(seg) \
1331 [VCPU_SREG_##seg] = { \
1332 .selector = GUEST_##seg##_SELECTOR, \
1333 .base = GUEST_##seg##_BASE, \
1334 .limit = GUEST_##seg##_LIMIT, \
1335 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1336 }
1337
Mathias Krause772e0312012-08-30 01:30:19 +02001338static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339 unsigned selector;
1340 unsigned base;
1341 unsigned limit;
1342 unsigned ar_bytes;
1343} kvm_vmx_segment_fields[] = {
1344 VMX_SEGMENT_FIELD(CS),
1345 VMX_SEGMENT_FIELD(DS),
1346 VMX_SEGMENT_FIELD(ES),
1347 VMX_SEGMENT_FIELD(FS),
1348 VMX_SEGMENT_FIELD(GS),
1349 VMX_SEGMENT_FIELD(SS),
1350 VMX_SEGMENT_FIELD(TR),
1351 VMX_SEGMENT_FIELD(LDTR),
1352};
1353
Avi Kivity26bb0982009-09-07 11:14:12 +03001354static u64 host_efer;
1355
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001356static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1357
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001358/*
Brian Gerst8c065852010-07-17 09:03:26 -04001359 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001360 * away by decrementing the array size.
1361 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001363#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001364 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001366 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001369DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1370
1371#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1372
1373#define KVM_EVMCS_VERSION 1
1374
1375#if IS_ENABLED(CONFIG_HYPERV)
1376static bool __read_mostly enlightened_vmcs = true;
1377module_param(enlightened_vmcs, bool, 0444);
1378
1379static inline void evmcs_write64(unsigned long field, u64 value)
1380{
1381 u16 clean_field;
1382 int offset = get_evmcs_offset(field, &clean_field);
1383
1384 if (offset < 0)
1385 return;
1386
1387 *(u64 *)((char *)current_evmcs + offset) = value;
1388
1389 current_evmcs->hv_clean_fields &= ~clean_field;
1390}
1391
1392static inline void evmcs_write32(unsigned long field, u32 value)
1393{
1394 u16 clean_field;
1395 int offset = get_evmcs_offset(field, &clean_field);
1396
1397 if (offset < 0)
1398 return;
1399
1400 *(u32 *)((char *)current_evmcs + offset) = value;
1401 current_evmcs->hv_clean_fields &= ~clean_field;
1402}
1403
1404static inline void evmcs_write16(unsigned long field, u16 value)
1405{
1406 u16 clean_field;
1407 int offset = get_evmcs_offset(field, &clean_field);
1408
1409 if (offset < 0)
1410 return;
1411
1412 *(u16 *)((char *)current_evmcs + offset) = value;
1413 current_evmcs->hv_clean_fields &= ~clean_field;
1414}
1415
1416static inline u64 evmcs_read64(unsigned long field)
1417{
1418 int offset = get_evmcs_offset(field, NULL);
1419
1420 if (offset < 0)
1421 return 0;
1422
1423 return *(u64 *)((char *)current_evmcs + offset);
1424}
1425
1426static inline u32 evmcs_read32(unsigned long field)
1427{
1428 int offset = get_evmcs_offset(field, NULL);
1429
1430 if (offset < 0)
1431 return 0;
1432
1433 return *(u32 *)((char *)current_evmcs + offset);
1434}
1435
1436static inline u16 evmcs_read16(unsigned long field)
1437{
1438 int offset = get_evmcs_offset(field, NULL);
1439
1440 if (offset < 0)
1441 return 0;
1442
1443 return *(u16 *)((char *)current_evmcs + offset);
1444}
1445
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001446static inline void evmcs_touch_msr_bitmap(void)
1447{
1448 if (unlikely(!current_evmcs))
1449 return;
1450
1451 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1452 current_evmcs->hv_clean_fields &=
1453 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1454}
1455
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001456static void evmcs_load(u64 phys_addr)
1457{
1458 struct hv_vp_assist_page *vp_ap =
1459 hv_get_vp_assist_page(smp_processor_id());
1460
1461 vp_ap->current_nested_vmcs = phys_addr;
1462 vp_ap->enlighten_vmentry = 1;
1463}
1464
1465static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1466{
1467 /*
1468 * Enlightened VMCSv1 doesn't support these:
1469 *
1470 * POSTED_INTR_NV = 0x00000002,
1471 * GUEST_INTR_STATUS = 0x00000810,
1472 * APIC_ACCESS_ADDR = 0x00002014,
1473 * POSTED_INTR_DESC_ADDR = 0x00002016,
1474 * EOI_EXIT_BITMAP0 = 0x0000201c,
1475 * EOI_EXIT_BITMAP1 = 0x0000201e,
1476 * EOI_EXIT_BITMAP2 = 0x00002020,
1477 * EOI_EXIT_BITMAP3 = 0x00002022,
1478 */
1479 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1480 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1481 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1482 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1483 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1484 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1485 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1486
1487 /*
1488 * GUEST_PML_INDEX = 0x00000812,
1489 * PML_ADDRESS = 0x0000200e,
1490 */
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1492
1493 /* VM_FUNCTION_CONTROL = 0x00002018, */
1494 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1495
1496 /*
1497 * EPTP_LIST_ADDRESS = 0x00002024,
1498 * VMREAD_BITMAP = 0x00002026,
1499 * VMWRITE_BITMAP = 0x00002028,
1500 */
1501 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1502
1503 /*
1504 * TSC_MULTIPLIER = 0x00002032,
1505 */
1506 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1507
1508 /*
1509 * PLE_GAP = 0x00004020,
1510 * PLE_WINDOW = 0x00004022,
1511 */
1512 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1513
1514 /*
1515 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1516 */
1517 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1518
1519 /*
1520 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1521 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1522 */
1523 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1524 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1525
1526 /*
1527 * Currently unsupported in KVM:
1528 * GUEST_IA32_RTIT_CTL = 0x00002814,
1529 */
1530}
Tianyu Lan877ad952018-07-19 08:40:23 +00001531
1532/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1533static void check_ept_pointer_match(struct kvm *kvm)
1534{
1535 struct kvm_vcpu *vcpu;
1536 u64 tmp_eptp = INVALID_PAGE;
1537 int i;
1538
1539 kvm_for_each_vcpu(i, vcpu, kvm) {
1540 if (!VALID_PAGE(tmp_eptp)) {
1541 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1542 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1543 to_kvm_vmx(kvm)->ept_pointers_match
1544 = EPT_POINTERS_MISMATCH;
1545 return;
1546 }
1547 }
1548
1549 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1550}
1551
1552static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1553{
1554 int ret;
1555
1556 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1557
1558 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1559 check_ept_pointer_match(kvm);
1560
1561 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1562 ret = -ENOTSUPP;
1563 goto out;
1564 }
1565
1566 ret = hyperv_flush_guest_mapping(
1567 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1568
1569out:
1570 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1571 return ret;
1572}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001573#else /* !IS_ENABLED(CONFIG_HYPERV) */
1574static inline void evmcs_write64(unsigned long field, u64 value) {}
1575static inline void evmcs_write32(unsigned long field, u32 value) {}
1576static inline void evmcs_write16(unsigned long field, u16 value) {}
1577static inline u64 evmcs_read64(unsigned long field) { return 0; }
1578static inline u32 evmcs_read32(unsigned long field) { return 0; }
1579static inline u16 evmcs_read16(unsigned long field) { return 0; }
1580static inline void evmcs_load(u64 phys_addr) {}
1581static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001582static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001583#endif /* IS_ENABLED(CONFIG_HYPERV) */
1584
Jan Kiszka5bb16012016-02-09 20:14:21 +01001585static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586{
1587 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1588 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001589 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1590}
1591
Jan Kiszka6f054852016-02-09 20:15:18 +01001592static inline bool is_debug(u32 intr_info)
1593{
1594 return is_exception_n(intr_info, DB_VECTOR);
1595}
1596
1597static inline bool is_breakpoint(u32 intr_info)
1598{
1599 return is_exception_n(intr_info, BP_VECTOR);
1600}
1601
Jan Kiszka5bb16012016-02-09 20:14:21 +01001602static inline bool is_page_fault(u32 intr_info)
1603{
1604 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001605}
1606
Gui Jianfeng31299942010-03-15 17:29:09 +08001607static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001608{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001609 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001610}
1611
Gui Jianfeng31299942010-03-15 17:29:09 +08001612static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001613{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001614 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001615}
1616
Liran Alon9e869482018-03-12 13:12:51 +02001617static inline bool is_gp_fault(u32 intr_info)
1618{
1619 return is_exception_n(intr_info, GP_VECTOR);
1620}
1621
Gui Jianfeng31299942010-03-15 17:29:09 +08001622static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623{
1624 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1625 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1626}
1627
Gui Jianfeng31299942010-03-15 17:29:09 +08001628static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001629{
1630 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1631 INTR_INFO_VALID_MASK)) ==
1632 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1633}
1634
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001635/* Undocumented: icebp/int1 */
1636static inline bool is_icebp(u32 intr_info)
1637{
1638 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1639 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1640}
1641
Gui Jianfeng31299942010-03-15 17:29:09 +08001642static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001643{
Sheng Yang04547152009-04-01 15:52:31 +08001644 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001645}
1646
Gui Jianfeng31299942010-03-15 17:29:09 +08001647static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001648{
Sheng Yang04547152009-04-01 15:52:31 +08001649 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001650}
1651
Paolo Bonzini35754c92015-07-29 12:05:37 +02001652static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001653{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001654 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001655}
1656
Gui Jianfeng31299942010-03-15 17:29:09 +08001657static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001658{
Sheng Yang04547152009-04-01 15:52:31 +08001659 return vmcs_config.cpu_based_exec_ctrl &
1660 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001661}
1662
Avi Kivity774ead32007-12-26 13:57:04 +02001663static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001664{
Sheng Yang04547152009-04-01 15:52:31 +08001665 return vmcs_config.cpu_based_2nd_exec_ctrl &
1666 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1667}
1668
Yang Zhang8d146952013-01-25 10:18:50 +08001669static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1670{
1671 return vmcs_config.cpu_based_2nd_exec_ctrl &
1672 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1673}
1674
Yang Zhang83d4c282013-01-25 10:18:49 +08001675static inline bool cpu_has_vmx_apic_register_virt(void)
1676{
1677 return vmcs_config.cpu_based_2nd_exec_ctrl &
1678 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1679}
1680
Yang Zhangc7c9c562013-01-25 10:18:51 +08001681static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1682{
1683 return vmcs_config.cpu_based_2nd_exec_ctrl &
1684 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1685}
1686
Sean Christopherson0b665d32018-08-14 09:33:34 -07001687static inline bool cpu_has_vmx_encls_vmexit(void)
1688{
1689 return vmcs_config.cpu_based_2nd_exec_ctrl &
1690 SECONDARY_EXEC_ENCLS_EXITING;
1691}
1692
Yunhong Jiang64672c92016-06-13 14:19:59 -07001693/*
1694 * Comment's format: document - errata name - stepping - processor name.
1695 * Refer from
1696 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1697 */
1698static u32 vmx_preemption_cpu_tfms[] = {
1699/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17000x000206E6,
1701/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1702/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1703/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17040x00020652,
1705/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17060x00020655,
1707/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1708/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1709/*
1710 * 320767.pdf - AAP86 - B1 -
1711 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1712 */
17130x000106E5,
1714/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17150x000106A0,
1716/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17170x000106A1,
1718/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17190x000106A4,
1720 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1721 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1722 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17230x000106A5,
1724};
1725
1726static inline bool cpu_has_broken_vmx_preemption_timer(void)
1727{
1728 u32 eax = cpuid_eax(0x00000001), i;
1729
1730 /* Clear the reserved bits */
1731 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001732 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001733 if (eax == vmx_preemption_cpu_tfms[i])
1734 return true;
1735
1736 return false;
1737}
1738
1739static inline bool cpu_has_vmx_preemption_timer(void)
1740{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001741 return vmcs_config.pin_based_exec_ctrl &
1742 PIN_BASED_VMX_PREEMPTION_TIMER;
1743}
1744
Yang Zhang01e439b2013-04-11 19:25:12 +08001745static inline bool cpu_has_vmx_posted_intr(void)
1746{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001747 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1748 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001749}
1750
1751static inline bool cpu_has_vmx_apicv(void)
1752{
1753 return cpu_has_vmx_apic_register_virt() &&
1754 cpu_has_vmx_virtual_intr_delivery() &&
1755 cpu_has_vmx_posted_intr();
1756}
1757
Sheng Yang04547152009-04-01 15:52:31 +08001758static inline bool cpu_has_vmx_flexpriority(void)
1759{
1760 return cpu_has_vmx_tpr_shadow() &&
1761 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001762}
1763
Marcelo Tosattie7997942009-06-11 12:07:40 -03001764static inline bool cpu_has_vmx_ept_execute_only(void)
1765{
Gui Jianfeng31299942010-03-15 17:29:09 +08001766 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001767}
1768
Marcelo Tosattie7997942009-06-11 12:07:40 -03001769static inline bool cpu_has_vmx_ept_2m_page(void)
1770{
Gui Jianfeng31299942010-03-15 17:29:09 +08001771 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001772}
1773
Sheng Yang878403b2010-01-05 19:02:29 +08001774static inline bool cpu_has_vmx_ept_1g_page(void)
1775{
Gui Jianfeng31299942010-03-15 17:29:09 +08001776 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001777}
1778
Sheng Yang4bc9b982010-06-02 14:05:24 +08001779static inline bool cpu_has_vmx_ept_4levels(void)
1780{
1781 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1782}
1783
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001784static inline bool cpu_has_vmx_ept_mt_wb(void)
1785{
1786 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1787}
1788
Yu Zhang855feb62017-08-24 20:27:55 +08001789static inline bool cpu_has_vmx_ept_5levels(void)
1790{
1791 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1792}
1793
Xudong Hao83c3a332012-05-28 19:33:35 +08001794static inline bool cpu_has_vmx_ept_ad_bits(void)
1795{
1796 return vmx_capability.ept & VMX_EPT_AD_BIT;
1797}
1798
Gui Jianfeng31299942010-03-15 17:29:09 +08001799static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001800{
Gui Jianfeng31299942010-03-15 17:29:09 +08001801 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001802}
1803
Gui Jianfeng31299942010-03-15 17:29:09 +08001804static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001805{
Gui Jianfeng31299942010-03-15 17:29:09 +08001806 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001807}
1808
Liran Aloncd9a4912018-05-22 17:16:15 +03001809static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1810{
1811 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1812}
1813
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001814static inline bool cpu_has_vmx_invvpid_single(void)
1815{
1816 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1817}
1818
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001819static inline bool cpu_has_vmx_invvpid_global(void)
1820{
1821 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1822}
1823
Wanpeng Li08d839c2017-03-23 05:30:08 -07001824static inline bool cpu_has_vmx_invvpid(void)
1825{
1826 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1827}
1828
Gui Jianfeng31299942010-03-15 17:29:09 +08001829static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001830{
Sheng Yang04547152009-04-01 15:52:31 +08001831 return vmcs_config.cpu_based_2nd_exec_ctrl &
1832 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001833}
1834
Gui Jianfeng31299942010-03-15 17:29:09 +08001835static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001836{
1837 return vmcs_config.cpu_based_2nd_exec_ctrl &
1838 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1839}
1840
Gui Jianfeng31299942010-03-15 17:29:09 +08001841static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001842{
1843 return vmcs_config.cpu_based_2nd_exec_ctrl &
1844 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1845}
1846
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001847static inline bool cpu_has_vmx_basic_inout(void)
1848{
1849 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1850}
1851
Paolo Bonzini35754c92015-07-29 12:05:37 +02001852static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001853{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001854 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001855}
1856
Gui Jianfeng31299942010-03-15 17:29:09 +08001857static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001858{
Sheng Yang04547152009-04-01 15:52:31 +08001859 return vmcs_config.cpu_based_2nd_exec_ctrl &
1860 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001861}
1862
Gui Jianfeng31299942010-03-15 17:29:09 +08001863static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001864{
1865 return vmcs_config.cpu_based_2nd_exec_ctrl &
1866 SECONDARY_EXEC_RDTSCP;
1867}
1868
Mao, Junjiead756a12012-07-02 01:18:48 +00001869static inline bool cpu_has_vmx_invpcid(void)
1870{
1871 return vmcs_config.cpu_based_2nd_exec_ctrl &
1872 SECONDARY_EXEC_ENABLE_INVPCID;
1873}
1874
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001875static inline bool cpu_has_virtual_nmis(void)
1876{
1877 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1878}
1879
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001880static inline bool cpu_has_vmx_wbinvd_exit(void)
1881{
1882 return vmcs_config.cpu_based_2nd_exec_ctrl &
1883 SECONDARY_EXEC_WBINVD_EXITING;
1884}
1885
Abel Gordonabc4fc52013-04-18 14:35:25 +03001886static inline bool cpu_has_vmx_shadow_vmcs(void)
1887{
1888 u64 vmx_msr;
1889 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1890 /* check if the cpu supports writing r/o exit information fields */
1891 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1892 return false;
1893
1894 return vmcs_config.cpu_based_2nd_exec_ctrl &
1895 SECONDARY_EXEC_SHADOW_VMCS;
1896}
1897
Kai Huang843e4332015-01-28 10:54:28 +08001898static inline bool cpu_has_vmx_pml(void)
1899{
1900 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1901}
1902
Haozhong Zhang64903d62015-10-20 15:39:09 +08001903static inline bool cpu_has_vmx_tsc_scaling(void)
1904{
1905 return vmcs_config.cpu_based_2nd_exec_ctrl &
1906 SECONDARY_EXEC_TSC_SCALING;
1907}
1908
Bandan Das2a499e42017-08-03 15:54:41 -04001909static inline bool cpu_has_vmx_vmfunc(void)
1910{
1911 return vmcs_config.cpu_based_2nd_exec_ctrl &
1912 SECONDARY_EXEC_ENABLE_VMFUNC;
1913}
1914
Sean Christopherson64f7a112018-04-30 10:01:06 -07001915static bool vmx_umip_emulated(void)
1916{
1917 return vmcs_config.cpu_based_2nd_exec_ctrl &
1918 SECONDARY_EXEC_DESC;
1919}
1920
Sheng Yang04547152009-04-01 15:52:31 +08001921static inline bool report_flexpriority(void)
1922{
1923 return flexpriority_enabled;
1924}
1925
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001926static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1927{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001928 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001929}
1930
Jim Mattsonf4160e42018-05-29 09:11:33 -07001931/*
1932 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1933 * to modify any valid field of the VMCS, or are the VM-exit
1934 * information fields read-only?
1935 */
1936static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1937{
1938 return to_vmx(vcpu)->nested.msrs.misc_low &
1939 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1940}
1941
Marc Orr04473782018-06-20 17:21:29 -07001942static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1943{
1944 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1945}
1946
1947static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1948{
1949 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1950 CPU_BASED_MONITOR_TRAP_FLAG;
1951}
1952
Liran Alonfa97d7d2018-07-18 14:07:59 +02001953static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1954{
1955 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1956 SECONDARY_EXEC_SHADOW_VMCS;
1957}
1958
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001959static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1960{
1961 return vmcs12->cpu_based_vm_exec_control & bit;
1962}
1963
1964static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1965{
1966 return (vmcs12->cpu_based_vm_exec_control &
1967 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1968 (vmcs12->secondary_vm_exec_control & bit);
1969}
1970
Jan Kiszkaf4124502014-03-07 20:03:13 +01001971static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1972{
1973 return vmcs12->pin_based_vm_exec_control &
1974 PIN_BASED_VMX_PREEMPTION_TIMER;
1975}
1976
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001977static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1978{
1979 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1980}
1981
1982static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1983{
1984 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1985}
1986
Nadav Har'El155a97a2013-08-05 11:07:16 +03001987static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1988{
1989 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1990}
1991
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001992static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1993{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001994 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001995}
1996
Bandan Dasc5f983f2017-05-05 15:25:14 -04001997static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1998{
1999 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2000}
2001
Wincy Vanf2b93282015-02-03 23:56:03 +08002002static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2003{
2004 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2005}
2006
Wanpeng Li5c614b32015-10-13 09:18:36 -07002007static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2008{
2009 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2010}
2011
Wincy Van82f0dd42015-02-03 23:57:18 +08002012static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2013{
2014 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2015}
2016
Wincy Van608406e2015-02-03 23:57:51 +08002017static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2018{
2019 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2020}
2021
Wincy Van705699a2015-02-03 23:58:17 +08002022static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2023{
2024 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2025}
2026
Bandan Das27c42a12017-08-03 15:54:42 -04002027static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2028{
2029 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2030}
2031
Bandan Das41ab9372017-08-03 15:54:43 -04002032static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2033{
2034 return nested_cpu_has_vmfunc(vmcs12) &&
2035 (vmcs12->vm_function_control &
2036 VMX_VMFUNC_EPTP_SWITCHING);
2037}
2038
Liran Alonf792d272018-06-23 02:35:05 +03002039static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2040{
2041 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2042}
2043
Jim Mattsonef85b672016-12-12 11:01:37 -08002044static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002045{
2046 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002047 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002048}
2049
Jan Kiszka533558b2014-01-04 18:47:20 +01002050static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2051 u32 exit_intr_info,
2052 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002053static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2054 struct vmcs12 *vmcs12,
2055 u32 reason, unsigned long qualification);
2056
Rusty Russell8b9cf982007-07-30 16:31:43 +10002057static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002058{
2059 int i;
2060
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002061 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002062 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002063 return i;
2064 return -1;
2065}
2066
Sheng Yang2384d2b2008-01-17 15:14:33 +08002067static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2068{
2069 struct {
2070 u64 vpid : 16;
2071 u64 rsvd : 48;
2072 u64 gva;
2073 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002074 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002075
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002076 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2077 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2078 : "memory");
2079 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002080}
2081
Sheng Yang14394422008-04-28 12:24:45 +08002082static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2083{
2084 struct {
2085 u64 eptp, gpa;
2086 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002087 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002088
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002089 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2090 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2091 : "memory");
2092 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002093}
2094
Avi Kivity26bb0982009-09-07 11:14:12 +03002095static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002096{
2097 int i;
2098
Rusty Russell8b9cf982007-07-30 16:31:43 +10002099 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002100 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002101 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002102 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002103}
2104
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105static void vmcs_clear(struct vmcs *vmcs)
2106{
2107 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002108 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002110 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2111 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2112 : "memory");
2113 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2115 vmcs, phys_addr);
2116}
2117
Nadav Har'Eld462b812011-05-24 15:26:10 +03002118static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2119{
2120 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002121 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2122 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002123 loaded_vmcs->cpu = -1;
2124 loaded_vmcs->launched = 0;
2125}
2126
Dongxiao Xu7725b892010-05-11 18:29:38 +08002127static void vmcs_load(struct vmcs *vmcs)
2128{
2129 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002130 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002131
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002132 if (static_branch_unlikely(&enable_evmcs))
2133 return evmcs_load(phys_addr);
2134
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002135 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2136 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2137 : "memory");
2138 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002139 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002140 vmcs, phys_addr);
2141}
2142
Dave Young2965faa2015-09-09 15:38:55 -07002143#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002144/*
2145 * This bitmap is used to indicate whether the vmclear
2146 * operation is enabled on all cpus. All disabled by
2147 * default.
2148 */
2149static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2150
2151static inline void crash_enable_local_vmclear(int cpu)
2152{
2153 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2154}
2155
2156static inline void crash_disable_local_vmclear(int cpu)
2157{
2158 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2159}
2160
2161static inline int crash_local_vmclear_enabled(int cpu)
2162{
2163 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2164}
2165
2166static void crash_vmclear_local_loaded_vmcss(void)
2167{
2168 int cpu = raw_smp_processor_id();
2169 struct loaded_vmcs *v;
2170
2171 if (!crash_local_vmclear_enabled(cpu))
2172 return;
2173
2174 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2175 loaded_vmcss_on_cpu_link)
2176 vmcs_clear(v->vmcs);
2177}
2178#else
2179static inline void crash_enable_local_vmclear(int cpu) { }
2180static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002181#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002182
Nadav Har'Eld462b812011-05-24 15:26:10 +03002183static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002185 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002186 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187
Nadav Har'Eld462b812011-05-24 15:26:10 +03002188 if (loaded_vmcs->cpu != cpu)
2189 return; /* vcpu migration can race with cpu offline */
2190 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002192 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002193 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002194
2195 /*
2196 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2197 * is before setting loaded_vmcs->vcpu to -1 which is done in
2198 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2199 * then adds the vmcs into percpu list before it is deleted.
2200 */
2201 smp_wmb();
2202
Nadav Har'Eld462b812011-05-24 15:26:10 +03002203 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002204 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205}
2206
Nadav Har'Eld462b812011-05-24 15:26:10 +03002207static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002208{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002209 int cpu = loaded_vmcs->cpu;
2210
2211 if (cpu != -1)
2212 smp_call_function_single(cpu,
2213 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002214}
2215
Junaid Shahidfaff8752018-06-29 13:10:05 -07002216static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2217{
2218 if (vpid == 0)
2219 return true;
2220
2221 if (cpu_has_vmx_invvpid_individual_addr()) {
2222 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2223 return true;
2224 }
2225
2226 return false;
2227}
2228
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002229static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002230{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002231 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002232 return;
2233
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002234 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002235 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002236}
2237
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002238static inline void vpid_sync_vcpu_global(void)
2239{
2240 if (cpu_has_vmx_invvpid_global())
2241 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2242}
2243
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002244static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002245{
2246 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002247 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002248 else
2249 vpid_sync_vcpu_global();
2250}
2251
Sheng Yang14394422008-04-28 12:24:45 +08002252static inline void ept_sync_global(void)
2253{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002254 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002255}
2256
2257static inline void ept_sync_context(u64 eptp)
2258{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002259 if (cpu_has_vmx_invept_context())
2260 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2261 else
2262 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002263}
2264
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002265static __always_inline void vmcs_check16(unsigned long field)
2266{
2267 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2268 "16-bit accessor invalid for 64-bit field");
2269 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2270 "16-bit accessor invalid for 64-bit high field");
2271 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2272 "16-bit accessor invalid for 32-bit high field");
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2274 "16-bit accessor invalid for natural width field");
2275}
2276
2277static __always_inline void vmcs_check32(unsigned long field)
2278{
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2280 "32-bit accessor invalid for 16-bit field");
2281 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2282 "32-bit accessor invalid for natural width field");
2283}
2284
2285static __always_inline void vmcs_check64(unsigned long field)
2286{
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2288 "64-bit accessor invalid for 16-bit field");
2289 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2290 "64-bit accessor invalid for 64-bit high field");
2291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2292 "64-bit accessor invalid for 32-bit field");
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2294 "64-bit accessor invalid for natural width field");
2295}
2296
2297static __always_inline void vmcs_checkl(unsigned long field)
2298{
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2300 "Natural width accessor invalid for 16-bit field");
2301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2302 "Natural width accessor invalid for 64-bit field");
2303 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2304 "Natural width accessor invalid for 64-bit high field");
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2306 "Natural width accessor invalid for 32-bit field");
2307}
2308
2309static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310{
Avi Kivity5e520e62011-05-15 10:13:12 -04002311 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
Avi Kivity5e520e62011-05-15 10:13:12 -04002313 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2314 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315 return value;
2316}
2317
Avi Kivity96304212011-05-15 10:13:13 -04002318static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002320 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002321 if (static_branch_unlikely(&enable_evmcs))
2322 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002323 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324}
2325
Avi Kivity96304212011-05-15 10:13:13 -04002326static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002328 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002329 if (static_branch_unlikely(&enable_evmcs))
2330 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002331 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332}
2333
Avi Kivity96304212011-05-15 10:13:13 -04002334static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002336 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002337 if (static_branch_unlikely(&enable_evmcs))
2338 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002339#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002342 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343#endif
2344}
2345
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002346static __always_inline unsigned long vmcs_readl(unsigned long field)
2347{
2348 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002349 if (static_branch_unlikely(&enable_evmcs))
2350 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002351 return __vmcs_readl(field);
2352}
2353
Avi Kivitye52de1b2007-01-05 16:36:56 -08002354static noinline void vmwrite_error(unsigned long field, unsigned long value)
2355{
2356 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2357 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2358 dump_stack();
2359}
2360
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002361static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002363 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002365 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2366 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002367 if (unlikely(error))
2368 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002369}
2370
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002371static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002373 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002374 if (static_branch_unlikely(&enable_evmcs))
2375 return evmcs_write16(field, value);
2376
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002383 if (static_branch_unlikely(&enable_evmcs))
2384 return evmcs_write32(field, value);
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387}
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002392 if (static_branch_unlikely(&enable_evmcs))
2393 return evmcs_write64(field, value);
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002396#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399#endif
2400}
2401
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002402static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002403{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002405 if (static_branch_unlikely(&enable_evmcs))
2406 return evmcs_write64(field, value);
2407
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002408 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002409}
2410
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002412{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2414 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002415 if (static_branch_unlikely(&enable_evmcs))
2416 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2417
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002418 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2419}
2420
2421static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2422{
2423 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2424 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002425 if (static_branch_unlikely(&enable_evmcs))
2426 return evmcs_write32(field, evmcs_read32(field) | mask);
2427
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002428 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002429}
2430
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002431static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2432{
2433 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2434}
2435
Gleb Natapov2961e8762013-11-25 15:37:13 +02002436static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2437{
2438 vmcs_write32(VM_ENTRY_CONTROLS, val);
2439 vmx->vm_entry_controls_shadow = val;
2440}
2441
2442static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2443{
2444 if (vmx->vm_entry_controls_shadow != val)
2445 vm_entry_controls_init(vmx, val);
2446}
2447
2448static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2449{
2450 return vmx->vm_entry_controls_shadow;
2451}
2452
2453
2454static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2455{
2456 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2457}
2458
2459static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2460{
2461 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2462}
2463
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002464static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2465{
2466 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2467}
2468
Gleb Natapov2961e8762013-11-25 15:37:13 +02002469static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2470{
2471 vmcs_write32(VM_EXIT_CONTROLS, val);
2472 vmx->vm_exit_controls_shadow = val;
2473}
2474
2475static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2476{
2477 if (vmx->vm_exit_controls_shadow != val)
2478 vm_exit_controls_init(vmx, val);
2479}
2480
2481static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2482{
2483 return vmx->vm_exit_controls_shadow;
2484}
2485
2486
2487static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2488{
2489 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2490}
2491
2492static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2493{
2494 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2495}
2496
Avi Kivity2fb92db2011-04-27 19:42:18 +03002497static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2498{
2499 vmx->segment_cache.bitmask = 0;
2500}
2501
2502static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2503 unsigned field)
2504{
2505 bool ret;
2506 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2507
2508 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2509 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2510 vmx->segment_cache.bitmask = 0;
2511 }
2512 ret = vmx->segment_cache.bitmask & mask;
2513 vmx->segment_cache.bitmask |= mask;
2514 return ret;
2515}
2516
2517static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2518{
2519 u16 *p = &vmx->segment_cache.seg[seg].selector;
2520
2521 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2522 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2523 return *p;
2524}
2525
2526static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2527{
2528 ulong *p = &vmx->segment_cache.seg[seg].base;
2529
2530 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2531 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2532 return *p;
2533}
2534
2535static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2536{
2537 u32 *p = &vmx->segment_cache.seg[seg].limit;
2538
2539 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2540 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2541 return *p;
2542}
2543
2544static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2545{
2546 u32 *p = &vmx->segment_cache.seg[seg].ar;
2547
2548 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2549 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2550 return *p;
2551}
2552
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002553static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2554{
2555 u32 eb;
2556
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002557 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002558 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002559 /*
2560 * Guest access to VMware backdoor ports could legitimately
2561 * trigger #GP because of TSS I/O permission bitmap.
2562 * We intercept those #GP and allow access to them anyway
2563 * as VMware does.
2564 */
2565 if (enable_vmware_backdoor)
2566 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002567 if ((vcpu->guest_debug &
2568 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2569 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2570 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002571 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002572 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002573 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002574 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002575
2576 /* When we are running a nested L2 guest and L1 specified for it a
2577 * certain exception bitmap, we must trap the same exceptions and pass
2578 * them to L1. When running L2, we will only handle the exceptions
2579 * specified above if L1 did not want them.
2580 */
2581 if (is_guest_mode(vcpu))
2582 eb |= get_vmcs12(vcpu)->exception_bitmap;
2583
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002584 vmcs_write32(EXCEPTION_BITMAP, eb);
2585}
2586
Ashok Raj15d45072018-02-01 22:59:43 +01002587/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002588 * Check if MSR is intercepted for currently loaded MSR bitmap.
2589 */
2590static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2591{
2592 unsigned long *msr_bitmap;
2593 int f = sizeof(unsigned long);
2594
2595 if (!cpu_has_vmx_msr_bitmap())
2596 return true;
2597
2598 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2599
2600 if (msr <= 0x1fff) {
2601 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2602 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2603 msr &= 0x1fff;
2604 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2605 }
2606
2607 return true;
2608}
2609
2610/*
Ashok Raj15d45072018-02-01 22:59:43 +01002611 * Check if MSR is intercepted for L01 MSR bitmap.
2612 */
2613static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2614{
2615 unsigned long *msr_bitmap;
2616 int f = sizeof(unsigned long);
2617
2618 if (!cpu_has_vmx_msr_bitmap())
2619 return true;
2620
2621 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2622
2623 if (msr <= 0x1fff) {
2624 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2625 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2626 msr &= 0x1fff;
2627 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2628 }
2629
2630 return true;
2631}
2632
Gleb Natapov2961e8762013-11-25 15:37:13 +02002633static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2634 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002635{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002636 vm_entry_controls_clearbit(vmx, entry);
2637 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002638}
2639
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002640static int find_msr(struct vmx_msrs *m, unsigned int msr)
2641{
2642 unsigned int i;
2643
2644 for (i = 0; i < m->nr; ++i) {
2645 if (m->val[i].index == msr)
2646 return i;
2647 }
2648 return -ENOENT;
2649}
2650
Avi Kivity61d2ef22010-04-28 16:40:38 +03002651static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2652{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002653 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002654 struct msr_autoload *m = &vmx->msr_autoload;
2655
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002656 switch (msr) {
2657 case MSR_EFER:
2658 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002659 clear_atomic_switch_msr_special(vmx,
2660 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002661 VM_EXIT_LOAD_IA32_EFER);
2662 return;
2663 }
2664 break;
2665 case MSR_CORE_PERF_GLOBAL_CTRL:
2666 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002667 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002668 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2669 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2670 return;
2671 }
2672 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002673 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002674 i = find_msr(&m->guest, msr);
2675 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002676 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002677 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002678 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002679 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002680
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002681skip_guest:
2682 i = find_msr(&m->host, msr);
2683 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002684 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002685
2686 --m->host.nr;
2687 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002688 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002689}
2690
Gleb Natapov2961e8762013-11-25 15:37:13 +02002691static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2692 unsigned long entry, unsigned long exit,
2693 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2694 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002695{
2696 vmcs_write64(guest_val_vmcs, guest_val);
2697 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002698 vm_entry_controls_setbit(vmx, entry);
2699 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002700}
2701
Avi Kivity61d2ef22010-04-28 16:40:38 +03002702static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002703 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002704{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002705 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002706 struct msr_autoload *m = &vmx->msr_autoload;
2707
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002708 switch (msr) {
2709 case MSR_EFER:
2710 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002711 add_atomic_switch_msr_special(vmx,
2712 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002713 VM_EXIT_LOAD_IA32_EFER,
2714 GUEST_IA32_EFER,
2715 HOST_IA32_EFER,
2716 guest_val, host_val);
2717 return;
2718 }
2719 break;
2720 case MSR_CORE_PERF_GLOBAL_CTRL:
2721 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002722 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002723 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2724 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2725 GUEST_IA32_PERF_GLOBAL_CTRL,
2726 HOST_IA32_PERF_GLOBAL_CTRL,
2727 guest_val, host_val);
2728 return;
2729 }
2730 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002731 case MSR_IA32_PEBS_ENABLE:
2732 /* PEBS needs a quiescent period after being disabled (to write
2733 * a record). Disabling PEBS through VMX MSR swapping doesn't
2734 * provide that period, so a CPU could write host's record into
2735 * guest's memory.
2736 */
2737 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002738 }
2739
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002740 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002741 if (!entry_only)
2742 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002743
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002744 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002745 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002746 "Can't add msr %x\n", msr);
2747 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002748 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002749 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002750 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002751 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002752 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002753 m->guest.val[i].index = msr;
2754 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002755
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002756 if (entry_only)
2757 return;
2758
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002759 if (j < 0) {
2760 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002761 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002762 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002763 m->host.val[j].index = msr;
2764 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002765}
2766
Avi Kivity92c0d902009-10-29 11:00:16 +02002767static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002768{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002769 u64 guest_efer = vmx->vcpu.arch.efer;
2770 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002771
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002772 if (!enable_ept) {
2773 /*
2774 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2775 * host CPUID is more efficient than testing guest CPUID
2776 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2777 */
2778 if (boot_cpu_has(X86_FEATURE_SMEP))
2779 guest_efer |= EFER_NX;
2780 else if (!(guest_efer & EFER_NX))
2781 ignore_bits |= EFER_NX;
2782 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002783
Avi Kivity51c6cf62007-08-29 03:48:05 +03002784 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002785 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002786 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002787 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002788#ifdef CONFIG_X86_64
2789 ignore_bits |= EFER_LMA | EFER_LME;
2790 /* SCE is meaningful only in long mode on Intel */
2791 if (guest_efer & EFER_LMA)
2792 ignore_bits &= ~(u64)EFER_SCE;
2793#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002794
2795 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002796
2797 /*
2798 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2799 * On CPUs that support "load IA32_EFER", always switch EFER
2800 * atomically, since it's faster than switching it manually.
2801 */
2802 if (cpu_has_load_ia32_efer ||
2803 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002804 if (!(guest_efer & EFER_LMA))
2805 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002806 if (guest_efer != host_efer)
2807 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002808 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002809 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002810 } else {
2811 guest_efer &= ~ignore_bits;
2812 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002814 vmx->guest_msrs[efer_offset].data = guest_efer;
2815 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2816
2817 return true;
2818 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002819}
2820
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002821#ifdef CONFIG_X86_32
2822/*
2823 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2824 * VMCS rather than the segment table. KVM uses this helper to figure
2825 * out the current bases to poke them into the VMCS before entry.
2826 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002827static unsigned long segment_base(u16 selector)
2828{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002829 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002830 unsigned long v;
2831
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002832 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833 return 0;
2834
Thomas Garnier45fc8752017-03-14 10:05:08 -07002835 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 u16 ldt_selector = kvm_read_ldt();
2839
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002840 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002841 return 0;
2842
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002843 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002845 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002846 return v;
2847}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002848#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002849
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002850static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002851{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002853 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002854#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002855 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002856#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002857 unsigned long fs_base, gs_base;
2858 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002859 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002860
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002861 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002862 return;
2863
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002864 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002865 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002866
Avi Kivity33ed6322007-05-02 16:54:03 +03002867 /*
2868 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2869 * allow segment selectors with cpl > 0 or ti == 1.
2870 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002871 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002872
2873#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002874 savesegment(ds, host_state->ds_sel);
2875 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002876
2877 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002878 if (likely(is_64bit_mm(current->mm))) {
2879 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002880 fs_sel = current->thread.fsindex;
2881 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002882 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002883 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002884 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002885 savesegment(fs, fs_sel);
2886 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002887 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002888 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002889 }
2890
Avi Kivityc8770e72010-11-11 12:37:26 +02002891 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002892 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002893#else
Sean Christophersone368b872018-07-23 12:32:41 -07002894 savesegment(fs, fs_sel);
2895 savesegment(gs, gs_sel);
2896 fs_base = segment_base(fs_sel);
2897 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002898#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002899
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002900 if (unlikely(fs_sel != host_state->fs_sel)) {
2901 if (!(fs_sel & 7))
2902 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2903 else
2904 vmcs_write16(HOST_FS_SELECTOR, 0);
2905 host_state->fs_sel = fs_sel;
2906 }
2907 if (unlikely(gs_sel != host_state->gs_sel)) {
2908 if (!(gs_sel & 7))
2909 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2910 else
2911 vmcs_write16(HOST_GS_SELECTOR, 0);
2912 host_state->gs_sel = gs_sel;
2913 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002914 if (unlikely(fs_base != host_state->fs_base)) {
2915 vmcs_writel(HOST_FS_BASE, fs_base);
2916 host_state->fs_base = fs_base;
2917 }
2918 if (unlikely(gs_base != host_state->gs_base)) {
2919 vmcs_writel(HOST_GS_BASE, gs_base);
2920 host_state->gs_base = gs_base;
2921 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002922
Avi Kivity26bb0982009-09-07 11:14:12 +03002923 for (i = 0; i < vmx->save_nmsrs; ++i)
2924 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002925 vmx->guest_msrs[i].data,
2926 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002927}
2928
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002929static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002930{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002931 struct vmcs_host_state *host_state;
2932
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002933 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002934 return;
2935
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002936 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002937 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002938
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002939 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002940 vmx->loaded_cpu_state = NULL;
2941
Avi Kivityc8770e72010-11-11 12:37:26 +02002942#ifdef CONFIG_X86_64
2943 if (is_long_mode(&vmx->vcpu))
2944 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2945#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002946 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2947 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002948#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002949 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002950#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002951 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002952#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002953 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002954 if (host_state->fs_sel & 7)
2955 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002956#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2958 loadsegment(ds, host_state->ds_sel);
2959 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002960 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002961#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002962 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002963#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002964 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002965#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002966 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002967}
2968
Sean Christopherson678e3152018-07-23 12:32:43 -07002969#ifdef CONFIG_X86_64
2970static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002971{
Sean Christopherson678e3152018-07-23 12:32:43 -07002972 if (is_long_mode(&vmx->vcpu)) {
2973 preempt_disable();
2974 if (vmx->loaded_cpu_state)
2975 rdmsrl(MSR_KERNEL_GS_BASE,
2976 vmx->msr_guest_kernel_gs_base);
2977 preempt_enable();
2978 }
2979 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002980}
2981
Sean Christopherson678e3152018-07-23 12:32:43 -07002982static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2983{
2984 if (is_long_mode(&vmx->vcpu)) {
2985 preempt_disable();
2986 if (vmx->loaded_cpu_state)
2987 wrmsrl(MSR_KERNEL_GS_BASE, data);
2988 preempt_enable();
2989 }
2990 vmx->msr_guest_kernel_gs_base = data;
2991}
2992#endif
2993
Feng Wu28b835d2015-09-18 22:29:54 +08002994static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2995{
2996 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2997 struct pi_desc old, new;
2998 unsigned int dest;
2999
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003000 /*
3001 * In case of hot-plug or hot-unplug, we may have to undo
3002 * vmx_vcpu_pi_put even if there is no assigned device. And we
3003 * always keep PI.NDST up to date for simplicity: it makes the
3004 * code easier, and CPU migration is not a fast path.
3005 */
3006 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003007 return;
3008
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003009 /*
3010 * First handle the simple case where no cmpxchg is necessary; just
3011 * allow posting non-urgent interrupts.
3012 *
3013 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3014 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3015 * expects the VCPU to be on the blocked_vcpu_list that matches
3016 * PI.NDST.
3017 */
3018 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3019 vcpu->cpu == cpu) {
3020 pi_clear_sn(pi_desc);
3021 return;
3022 }
3023
3024 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003025 do {
3026 old.control = new.control = pi_desc->control;
3027
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003028 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003029
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003030 if (x2apic_enabled())
3031 new.ndst = dest;
3032 else
3033 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003034
Feng Wu28b835d2015-09-18 22:29:54 +08003035 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003036 } while (cmpxchg64(&pi_desc->control, old.control,
3037 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003038}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003039
Peter Feinerc95ba922016-08-17 09:36:47 -07003040static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3041{
3042 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3043 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3044}
3045
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046/*
3047 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3048 * vcpu mutex is already taken.
3049 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003050static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003051{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003052 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003053 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003055 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003056 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003057 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003058 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003059
3060 /*
3061 * Read loaded_vmcs->cpu should be before fetching
3062 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3063 * See the comments in __loaded_vmcs_clear().
3064 */
3065 smp_rmb();
3066
Nadav Har'Eld462b812011-05-24 15:26:10 +03003067 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3068 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003069 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003070 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003071 }
3072
3073 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3074 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3075 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003076 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003077 }
3078
3079 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003080 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003081 unsigned long sysenter_esp;
3082
3083 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003084
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085 /*
3086 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003087 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003089 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003090 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003091 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003093 /*
3094 * VM exits change the host TR limit to 0x67 after a VM
3095 * exit. This is okay, since 0x67 covers everything except
3096 * the IO bitmap and have have code to handle the IO bitmap
3097 * being lost after a VM exit.
3098 */
3099 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3100
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3102 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003103
Nadav Har'Eld462b812011-05-24 15:26:10 +03003104 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 }
Feng Wu28b835d2015-09-18 22:29:54 +08003106
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003107 /* Setup TSC multiplier */
3108 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003109 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3110 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003111
Feng Wu28b835d2015-09-18 22:29:54 +08003112 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003113 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003114 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003115}
3116
3117static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3118{
3119 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3120
3121 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003122 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3123 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003124 return;
3125
3126 /* Set SN when the vCPU is preempted */
3127 if (vcpu->preempted)
3128 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129}
3130
3131static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3132{
Feng Wu28b835d2015-09-18 22:29:54 +08003133 vmx_vcpu_pi_put(vcpu);
3134
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003135 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136}
3137
Wanpeng Lif244dee2017-07-20 01:11:54 -07003138static bool emulation_required(struct kvm_vcpu *vcpu)
3139{
3140 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3141}
3142
Avi Kivityedcafe32009-12-30 18:07:40 +02003143static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3144
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003145/*
3146 * Return the cr0 value that a nested guest would read. This is a combination
3147 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3148 * its hypervisor (cr0_read_shadow).
3149 */
3150static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3151{
3152 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3153 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3154}
3155static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3156{
3157 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3158 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3159}
3160
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3162{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003163 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003164
Avi Kivity6de12732011-03-07 12:51:22 +02003165 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3166 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3167 rflags = vmcs_readl(GUEST_RFLAGS);
3168 if (to_vmx(vcpu)->rmode.vm86_active) {
3169 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3170 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3171 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3172 }
3173 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003174 }
Avi Kivity6de12732011-03-07 12:51:22 +02003175 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176}
3177
3178static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3179{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003180 unsigned long old_rflags = vmx_get_rflags(vcpu);
3181
Avi Kivity6de12732011-03-07 12:51:22 +02003182 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3183 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003184 if (to_vmx(vcpu)->rmode.vm86_active) {
3185 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003186 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003187 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003189
3190 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3191 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192}
3193
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003194static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003195{
3196 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3197 int ret = 0;
3198
3199 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003200 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003201 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003202 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003203
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003204 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003205}
3206
3207static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3208{
3209 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3210 u32 interruptibility = interruptibility_old;
3211
3212 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3213
Jan Kiszka48005f62010-02-19 19:38:07 +01003214 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003215 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003216 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003217 interruptibility |= GUEST_INTR_STATE_STI;
3218
3219 if ((interruptibility != interruptibility_old))
3220 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3221}
3222
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3224{
3225 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003227 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003229 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230
Glauber Costa2809f5d2009-05-12 16:21:05 -04003231 /* skipping an emulated instruction also counts */
3232 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233}
3234
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003235static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3236 unsigned long exit_qual)
3237{
3238 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3239 unsigned int nr = vcpu->arch.exception.nr;
3240 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3241
3242 if (vcpu->arch.exception.has_error_code) {
3243 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3244 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3245 }
3246
3247 if (kvm_exception_is_soft(nr))
3248 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3249 else
3250 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3251
3252 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3253 vmx_get_nmi_mask(vcpu))
3254 intr_info |= INTR_INFO_UNBLOCK_NMI;
3255
3256 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3257}
3258
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003259/*
3260 * KVM wants to inject page-faults which it got to the guest. This function
3261 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003262 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003263static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003264{
3265 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003266 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003267
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003268 if (nr == PF_VECTOR) {
3269 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003270 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003271 return 1;
3272 }
3273 /*
3274 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3275 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3276 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3277 * can be written only when inject_pending_event runs. This should be
3278 * conditional on a new capability---if the capability is disabled,
3279 * kvm_multiple_exception would write the ancillary information to
3280 * CR2 or DR6, for backwards ABI-compatibility.
3281 */
3282 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3283 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003284 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003285 return 1;
3286 }
3287 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003288 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003289 if (nr == DB_VECTOR)
3290 *exit_qual = vcpu->arch.dr6;
3291 else
3292 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003293 return 1;
3294 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003295 }
3296
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003297 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003298}
3299
Wanpeng Licaa057a2018-03-12 04:53:03 -07003300static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3301{
3302 /*
3303 * Ensure that we clear the HLT state in the VMCS. We don't need to
3304 * explicitly skip the instruction because if the HLT state is set,
3305 * then the instruction is already executing and RIP has already been
3306 * advanced.
3307 */
3308 if (kvm_hlt_in_guest(vcpu->kvm) &&
3309 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3310 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3311}
3312
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003313static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003314{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003315 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003316 unsigned nr = vcpu->arch.exception.nr;
3317 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003318 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003319 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003320
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003321 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003322 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003323 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3324 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003325
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003326 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003327 int inc_eip = 0;
3328 if (kvm_exception_is_soft(nr))
3329 inc_eip = vcpu->arch.event_exit_inst_len;
3330 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003331 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003332 return;
3333 }
3334
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003335 WARN_ON_ONCE(vmx->emulation_required);
3336
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003337 if (kvm_exception_is_soft(nr)) {
3338 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3339 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003340 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3341 } else
3342 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3343
3344 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003345
3346 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003347}
3348
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003349static bool vmx_rdtscp_supported(void)
3350{
3351 return cpu_has_vmx_rdtscp();
3352}
3353
Mao, Junjiead756a12012-07-02 01:18:48 +00003354static bool vmx_invpcid_supported(void)
3355{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003356 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003357}
3358
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359/*
Eddie Donga75beee2007-05-17 18:55:15 +03003360 * Swap MSR entry in host/guest MSR entry array.
3361 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003362static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003363{
Avi Kivity26bb0982009-09-07 11:14:12 +03003364 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003365
3366 tmp = vmx->guest_msrs[to];
3367 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3368 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003369}
3370
3371/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003372 * Set up the vmcs to automatically save and restore system
3373 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3374 * mode, as fiddling with msrs is very expensive.
3375 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003376static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003377{
Avi Kivity26bb0982009-09-07 11:14:12 +03003378 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003379
Eddie Donga75beee2007-05-17 18:55:15 +03003380 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003381#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003382 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003383 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003384 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003385 move_msr_up(vmx, index, save_nmsrs++);
3386 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003387 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003388 move_msr_up(vmx, index, save_nmsrs++);
3389 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003390 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003391 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003392 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003393 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003394 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003395 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003396 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003397 * if efer.sce is enabled.
3398 */
Brian Gerst8c065852010-07-17 09:03:26 -04003399 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003400 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003401 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003402 }
Eddie Donga75beee2007-05-17 18:55:15 +03003403#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003404 index = __find_msr_index(vmx, MSR_EFER);
3405 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003406 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003407
Avi Kivity26bb0982009-09-07 11:14:12 +03003408 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003409
Yang Zhang8d146952013-01-25 10:18:50 +08003410 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003411 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003412}
3413
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003414static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003416 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003418 if (is_guest_mode(vcpu) &&
3419 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3420 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3421
3422 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423}
3424
3425/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003426 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003428static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003430 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003431 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003432 * We're here if L1 chose not to trap WRMSR to TSC. According
3433 * to the spec, this should set L1's TSC; The offset that L1
3434 * set for L2 remains unchanged, and still needs to be added
3435 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003436 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003437 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003438 /* recalculate vmcs02.TSC_OFFSET: */
3439 vmcs12 = get_vmcs12(vcpu);
3440 vmcs_write64(TSC_OFFSET, offset +
3441 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3442 vmcs12->tsc_offset : 0));
3443 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003444 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3445 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003446 vmcs_write64(TSC_OFFSET, offset);
3447 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003448}
3449
Nadav Har'El801d3422011-05-25 23:02:23 +03003450/*
3451 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3452 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3453 * all guests if the "nested" module option is off, and can also be disabled
3454 * for a single guest by disabling its VMX cpuid bit.
3455 */
3456static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3457{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003458 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003459}
3460
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003462 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3463 * returned for the various VMX controls MSRs when nested VMX is enabled.
3464 * The same values should also be used to verify that vmcs12 control fields are
3465 * valid during nested entry from L1 to L2.
3466 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3467 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3468 * bit in the high half is on if the corresponding bit in the control field
3469 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003470 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003471static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472{
Paolo Bonzini13893092018-02-26 13:40:09 +01003473 if (!nested) {
3474 memset(msrs, 0, sizeof(*msrs));
3475 return;
3476 }
3477
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003478 /*
3479 * Note that as a general rule, the high half of the MSRs (bits in
3480 * the control fields which may be 1) should be initialized by the
3481 * intersection of the underlying hardware's MSR (i.e., features which
3482 * can be supported) and the list of features we want to expose -
3483 * because they are known to be properly supported in our code.
3484 * Also, usually, the low half of the MSRs (bits which must be 1) can
3485 * be set to 0, meaning that L1 may turn off any of these bits. The
3486 * reason is that if one of these bits is necessary, it will appear
3487 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3488 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003489 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003490 * These rules have exceptions below.
3491 */
3492
3493 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003494 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003495 msrs->pinbased_ctls_low,
3496 msrs->pinbased_ctls_high);
3497 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003498 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003499 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003500 PIN_BASED_EXT_INTR_MASK |
3501 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003502 PIN_BASED_VIRTUAL_NMIS |
3503 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003504 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003505 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003506 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003507
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003508 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003509 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->exit_ctls_low,
3511 msrs->exit_ctls_high);
3512 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003513 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003514
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003516#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003517 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003518#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003519 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003521 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003522 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003523 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3524
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003525 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003526 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003527
Jan Kiszka2996fca2014-06-16 13:59:43 +02003528 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003530
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003531 /* entry controls */
3532 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msrs->entry_ctls_low,
3534 msrs->entry_ctls_high);
3535 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003536 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003538#ifdef CONFIG_X86_64
3539 VM_ENTRY_IA32E_MODE |
3540#endif
3541 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003542 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003543 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003544 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003546
Jan Kiszka2996fca2014-06-16 13:59:43 +02003547 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003549
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003550 /* cpu-based controls */
3551 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003552 msrs->procbased_ctls_low,
3553 msrs->procbased_ctls_high);
3554 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003555 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003557 CPU_BASED_VIRTUAL_INTR_PENDING |
3558 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003559 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3560 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3561 CPU_BASED_CR3_STORE_EXITING |
3562#ifdef CONFIG_X86_64
3563 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3564#endif
3565 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003566 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3567 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3568 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3569 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003570 /*
3571 * We can allow some features even when not supported by the
3572 * hardware. For example, L1 can specify an MSR bitmap - and we
3573 * can use it to avoid exits to L1 - even when L0 runs L2
3574 * without MSR bitmaps.
3575 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003576 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003577 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003578 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003579
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003580 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003581 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003582 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3583
Paolo Bonzini80154d72017-08-24 13:55:35 +02003584 /*
3585 * secondary cpu-based controls. Do not include those that
3586 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3587 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003588 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003589 msrs->secondary_ctls_low,
3590 msrs->secondary_ctls_high);
3591 msrs->secondary_ctls_low = 0;
3592 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003593 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003594 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003595 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003596 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003597 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003598 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003599 /*
3600 * We can emulate "VMCS shadowing," even if the hardware
3601 * doesn't support it.
3602 */
3603 msrs->secondary_ctls_high |=
3604 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003605
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003606 if (enable_ept) {
3607 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003608 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003609 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003610 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003611 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003612 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003613 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003614 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->ept_caps &= vmx_capability.ept;
3616 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003617 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3618 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003619 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003620 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003621 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003622 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003623 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003624 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003625
Bandan Das27c42a12017-08-03 15:54:42 -04003626 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003628 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003629 /*
3630 * Advertise EPTP switching unconditionally
3631 * since we emulate it
3632 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003633 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003634 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003635 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003636 }
3637
Paolo Bonzinief697a72016-03-18 16:58:38 +01003638 /*
3639 * Old versions of KVM use the single-context version without
3640 * checking for support, so declare that it is supported even
3641 * though it is treated as global context. The alternative is
3642 * not failing the single-context invvpid, and it is worse.
3643 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003644 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003646 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003647 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003648 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003649 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003650
Radim Krčmář0790ec12015-03-17 14:02:32 +01003651 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003652 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003653 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3654
Jan Kiszkac18911a2013-03-13 16:06:41 +01003655 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003656 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003657 msrs->misc_low,
3658 msrs->misc_high);
3659 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3660 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003661 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003662 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003663 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003664 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003665
3666 /*
3667 * This MSR reports some information about VMX support. We
3668 * should return information about the VMX we emulate for the
3669 * guest, and the VMCS structure we give it - not about the
3670 * VMX support of the underlying hardware.
3671 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003672 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003673 VMCS12_REVISION |
3674 VMX_BASIC_TRUE_CTLS |
3675 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3676 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3677
3678 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003679 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003680
3681 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003682 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003683 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3684 * We picked the standard core2 setting.
3685 */
3686#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3687#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003688 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3689 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003690
3691 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003692 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3693 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003694
3695 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003696 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003697}
3698
David Matlack38991522016-11-29 18:14:08 -08003699/*
3700 * if fixed0[i] == 1: val[i] must be 1
3701 * if fixed1[i] == 0: val[i] must be 0
3702 */
3703static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3704{
3705 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003706}
3707
3708static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3709{
David Matlack38991522016-11-29 18:14:08 -08003710 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003711}
3712
3713static inline u64 vmx_control_msr(u32 low, u32 high)
3714{
3715 return low | ((u64)high << 32);
3716}
3717
David Matlack62cc6b9d2016-11-29 18:14:07 -08003718static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3719{
3720 superset &= mask;
3721 subset &= mask;
3722
3723 return (superset | subset) == superset;
3724}
3725
3726static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3727{
3728 const u64 feature_and_reserved =
3729 /* feature (except bit 48; see below) */
3730 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3731 /* reserved */
3732 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003733 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003734
3735 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3736 return -EINVAL;
3737
3738 /*
3739 * KVM does not emulate a version of VMX that constrains physical
3740 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3741 */
3742 if (data & BIT_ULL(48))
3743 return -EINVAL;
3744
3745 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3746 vmx_basic_vmcs_revision_id(data))
3747 return -EINVAL;
3748
3749 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3750 return -EINVAL;
3751
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003752 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003753 return 0;
3754}
3755
3756static int
3757vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3758{
3759 u64 supported;
3760 u32 *lowp, *highp;
3761
3762 switch (msr_index) {
3763 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003764 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3765 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003766 break;
3767 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003768 lowp = &vmx->nested.msrs.procbased_ctls_low;
3769 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003770 break;
3771 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003772 lowp = &vmx->nested.msrs.exit_ctls_low;
3773 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003774 break;
3775 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003776 lowp = &vmx->nested.msrs.entry_ctls_low;
3777 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003778 break;
3779 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003780 lowp = &vmx->nested.msrs.secondary_ctls_low;
3781 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003782 break;
3783 default:
3784 BUG();
3785 }
3786
3787 supported = vmx_control_msr(*lowp, *highp);
3788
3789 /* Check must-be-1 bits are still 1. */
3790 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3791 return -EINVAL;
3792
3793 /* Check must-be-0 bits are still 0. */
3794 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3795 return -EINVAL;
3796
3797 *lowp = data;
3798 *highp = data >> 32;
3799 return 0;
3800}
3801
3802static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3803{
3804 const u64 feature_and_reserved_bits =
3805 /* feature */
3806 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3807 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3808 /* reserved */
3809 GENMASK_ULL(13, 9) | BIT_ULL(31);
3810 u64 vmx_misc;
3811
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003812 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3813 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003814
3815 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3816 return -EINVAL;
3817
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003819 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3820 vmx_misc_preemption_timer_rate(data) !=
3821 vmx_misc_preemption_timer_rate(vmx_misc))
3822 return -EINVAL;
3823
3824 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3825 return -EINVAL;
3826
3827 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3831 return -EINVAL;
3832
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003833 vmx->nested.msrs.misc_low = data;
3834 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003835
3836 /*
3837 * If L1 has read-only VM-exit information fields, use the
3838 * less permissive vmx_vmwrite_bitmap to specify write
3839 * permissions for the shadow VMCS.
3840 */
3841 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3842 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3843
David Matlack62cc6b9d2016-11-29 18:14:07 -08003844 return 0;
3845}
3846
3847static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3848{
3849 u64 vmx_ept_vpid_cap;
3850
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003851 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3852 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003853
3854 /* Every bit is either reserved or a feature bit. */
3855 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3856 return -EINVAL;
3857
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003858 vmx->nested.msrs.ept_caps = data;
3859 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003860 return 0;
3861}
3862
3863static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3864{
3865 u64 *msr;
3866
3867 switch (msr_index) {
3868 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003869 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003870 break;
3871 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003872 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003873 break;
3874 default:
3875 BUG();
3876 }
3877
3878 /*
3879 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3880 * must be 1 in the restored value.
3881 */
3882 if (!is_bitwise_subset(data, *msr, -1ULL))
3883 return -EINVAL;
3884
3885 *msr = data;
3886 return 0;
3887}
3888
3889/*
3890 * Called when userspace is restoring VMX MSRs.
3891 *
3892 * Returns 0 on success, non-0 otherwise.
3893 */
3894static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3895{
3896 struct vcpu_vmx *vmx = to_vmx(vcpu);
3897
Jim Mattsona943ac52018-05-29 09:11:32 -07003898 /*
3899 * Don't allow changes to the VMX capability MSRs while the vCPU
3900 * is in VMX operation.
3901 */
3902 if (vmx->nested.vmxon)
3903 return -EBUSY;
3904
David Matlack62cc6b9d2016-11-29 18:14:07 -08003905 switch (msr_index) {
3906 case MSR_IA32_VMX_BASIC:
3907 return vmx_restore_vmx_basic(vmx, data);
3908 case MSR_IA32_VMX_PINBASED_CTLS:
3909 case MSR_IA32_VMX_PROCBASED_CTLS:
3910 case MSR_IA32_VMX_EXIT_CTLS:
3911 case MSR_IA32_VMX_ENTRY_CTLS:
3912 /*
3913 * The "non-true" VMX capability MSRs are generated from the
3914 * "true" MSRs, so we do not support restoring them directly.
3915 *
3916 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3917 * should restore the "true" MSRs with the must-be-1 bits
3918 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3919 * DEFAULT SETTINGS".
3920 */
3921 return -EINVAL;
3922 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3923 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3924 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3925 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3926 case MSR_IA32_VMX_PROCBASED_CTLS2:
3927 return vmx_restore_control_msr(vmx, msr_index, data);
3928 case MSR_IA32_VMX_MISC:
3929 return vmx_restore_vmx_misc(vmx, data);
3930 case MSR_IA32_VMX_CR0_FIXED0:
3931 case MSR_IA32_VMX_CR4_FIXED0:
3932 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3933 case MSR_IA32_VMX_CR0_FIXED1:
3934 case MSR_IA32_VMX_CR4_FIXED1:
3935 /*
3936 * These MSRs are generated based on the vCPU's CPUID, so we
3937 * do not support restoring them directly.
3938 */
3939 return -EINVAL;
3940 case MSR_IA32_VMX_EPT_VPID_CAP:
3941 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3942 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003943 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003944 return 0;
3945 default:
3946 /*
3947 * The rest of the VMX capability MSRs do not support restore.
3948 */
3949 return -EINVAL;
3950 }
3951}
3952
Jan Kiszkacae50132014-01-04 18:47:22 +01003953/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003954static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003955{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003956 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003957 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003958 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003959 break;
3960 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3961 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003962 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003963 msrs->pinbased_ctls_low,
3964 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003965 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3966 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003967 break;
3968 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3969 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003970 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003971 msrs->procbased_ctls_low,
3972 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003973 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3974 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003975 break;
3976 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3977 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003978 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003979 msrs->exit_ctls_low,
3980 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003981 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3982 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003983 break;
3984 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3985 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003986 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003987 msrs->entry_ctls_low,
3988 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003989 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3990 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 break;
3992 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003993 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003994 msrs->misc_low,
3995 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003996 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003998 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
4000 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004013 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004014 msrs->secondary_ctls_low,
4015 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004016 break;
4017 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004018 *pdata = msrs->ept_caps |
4019 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004020 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004021 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004022 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004023 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004024 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004025 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004026 }
4027
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004028 return 0;
4029}
4030
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004031static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4032 uint64_t val)
4033{
4034 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4035
4036 return !(val & ~valid_bits);
4037}
4038
Tom Lendacky801e4592018-02-21 13:39:51 -06004039static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4040{
Paolo Bonzini13893092018-02-26 13:40:09 +01004041 switch (msr->index) {
4042 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4043 if (!nested)
4044 return 1;
4045 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4046 default:
4047 return 1;
4048 }
4049
4050 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004051}
4052
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004053/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054 * Reads an msr value (of 'msr_index') into 'pdata'.
4055 * Returns 0 on success, non-0 otherwise.
4056 * Assumes vcpu_load() was already called.
4057 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004058static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004061 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004063 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004064#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
4068 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004071 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004072 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004073 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004074#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004076 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004077 case MSR_IA32_SPEC_CTRL:
4078 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004079 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4080 return 1;
4081
4082 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4083 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004084 case MSR_IA32_ARCH_CAPABILITIES:
4085 if (!msr_info->host_initiated &&
4086 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4087 return 1;
4088 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4089 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004091 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 break;
4093 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004094 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
4096 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004099 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004100 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004101 (!msr_info->host_initiated &&
4102 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004103 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004104 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004105 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004106 case MSR_IA32_MCG_EXT_CTL:
4107 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004108 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004109 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004110 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004111 msr_info->data = vcpu->arch.mcg_ext_ctl;
4112 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004113 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004114 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004115 break;
4116 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4117 if (!nested_vmx_allowed(vcpu))
4118 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004119 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4120 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004121 case MSR_IA32_XSS:
4122 if (!vmx_xsaves_supported())
4123 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004124 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004125 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004126 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004127 if (!msr_info->host_initiated &&
4128 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004129 return 1;
4130 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004132 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004133 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004134 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004137 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 }
4139
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140 return 0;
4141}
4142
Jan Kiszkacae50132014-01-04 18:47:22 +01004143static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4144
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145/*
4146 * Writes msr value into into the appropriate "register".
4147 * Returns 0 on success, non-0 otherwise.
4148 * Assumes vcpu_load() was already called.
4149 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004150static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004152 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004153 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004154 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004155 u32 msr_index = msr_info->index;
4156 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004157
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004159 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004160 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004161 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004162#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004164 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 vmcs_writel(GUEST_FS_BASE, data);
4166 break;
4167 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004168 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 vmcs_writel(GUEST_GS_BASE, data);
4170 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004171 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004172 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004173 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174#endif
4175 case MSR_IA32_SYSENTER_CS:
4176 vmcs_write32(GUEST_SYSENTER_CS, data);
4177 break;
4178 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004179 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 break;
4181 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004182 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004184 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004185 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004186 (!msr_info->host_initiated &&
4187 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004188 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004189 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004190 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004192 vmcs_write64(GUEST_BNDCFGS, data);
4193 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004194 case MSR_IA32_SPEC_CTRL:
4195 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004196 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4197 return 1;
4198
4199 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004200 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004201 return 1;
4202
4203 vmx->spec_ctrl = data;
4204
4205 if (!data)
4206 break;
4207
4208 /*
4209 * For non-nested:
4210 * When it's written (to non-zero) for the first time, pass
4211 * it through.
4212 *
4213 * For nested:
4214 * The handling of the MSR bitmap for L2 guests is done in
4215 * nested_vmx_merge_msr_bitmap. We should not touch the
4216 * vmcs02.msr_bitmap here since it gets completely overwritten
4217 * in the merging. We update the vmcs01 here for L1 as well
4218 * since it will end up touching the MSR anyway now.
4219 */
4220 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4221 MSR_IA32_SPEC_CTRL,
4222 MSR_TYPE_RW);
4223 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004224 case MSR_IA32_PRED_CMD:
4225 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004226 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4227 return 1;
4228
4229 if (data & ~PRED_CMD_IBPB)
4230 return 1;
4231
4232 if (!data)
4233 break;
4234
4235 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4236
4237 /*
4238 * For non-nested:
4239 * When it's written (to non-zero) for the first time, pass
4240 * it through.
4241 *
4242 * For nested:
4243 * The handling of the MSR bitmap for L2 guests is done in
4244 * nested_vmx_merge_msr_bitmap. We should not touch the
4245 * vmcs02.msr_bitmap here since it gets completely overwritten
4246 * in the merging.
4247 */
4248 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4249 MSR_TYPE_W);
4250 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004251 case MSR_IA32_ARCH_CAPABILITIES:
4252 if (!msr_info->host_initiated)
4253 return 1;
4254 vmx->arch_capabilities = data;
4255 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004256 case MSR_IA32_CR_PAT:
4257 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004258 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4259 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004260 vmcs_write64(GUEST_IA32_PAT, data);
4261 vcpu->arch.pat = data;
4262 break;
4263 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004264 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004265 break;
Will Auldba904632012-11-29 12:42:50 -08004266 case MSR_IA32_TSC_ADJUST:
4267 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004268 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004269 case MSR_IA32_MCG_EXT_CTL:
4270 if ((!msr_info->host_initiated &&
4271 !(to_vmx(vcpu)->msr_ia32_feature_control &
4272 FEATURE_CONTROL_LMCE)) ||
4273 (data & ~MCG_EXT_CTL_LMCE_EN))
4274 return 1;
4275 vcpu->arch.mcg_ext_ctl = data;
4276 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004277 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004278 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004279 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004280 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4281 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004282 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 if (msr_info->host_initiated && data == 0)
4284 vmx_leave_nested(vcpu);
4285 break;
4286 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004287 if (!msr_info->host_initiated)
4288 return 1; /* they are read-only */
4289 if (!nested_vmx_allowed(vcpu))
4290 return 1;
4291 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004292 case MSR_IA32_XSS:
4293 if (!vmx_xsaves_supported())
4294 return 1;
4295 /*
4296 * The only supported bit as of Skylake is bit 8, but
4297 * it is not supported on KVM.
4298 */
4299 if (data != 0)
4300 return 1;
4301 vcpu->arch.ia32_xss = data;
4302 if (vcpu->arch.ia32_xss != host_xss)
4303 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004304 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004305 else
4306 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4307 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004308 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004309 if (!msr_info->host_initiated &&
4310 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004311 return 1;
4312 /* Check reserved bit, higher 32 bits should be zero */
4313 if ((data >> 32) != 0)
4314 return 1;
4315 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004317 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004318 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004319 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004320 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004321 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4322 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004323 ret = kvm_set_shared_msr(msr->index, msr->data,
4324 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004325 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004326 if (ret)
4327 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004328 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004329 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004331 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332 }
4333
Eddie Dong2cc51562007-05-21 07:28:09 +03004334 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335}
4336
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004337static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004339 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4340 switch (reg) {
4341 case VCPU_REGS_RSP:
4342 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4343 break;
4344 case VCPU_REGS_RIP:
4345 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4346 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004347 case VCPU_EXREG_PDPTR:
4348 if (enable_ept)
4349 ept_save_pdptrs(vcpu);
4350 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004351 default:
4352 break;
4353 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354}
4355
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356static __init int cpu_has_kvm_support(void)
4357{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004358 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359}
4360
4361static __init int vmx_disabled_by_bios(void)
4362{
4363 u64 msr;
4364
4365 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004366 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004367 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004368 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4369 && tboot_enabled())
4370 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004371 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004372 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004374 && !tboot_enabled()) {
4375 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004376 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004377 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004378 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 /* launched w/o TXT and VMX disabled */
4380 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4381 && !tboot_enabled())
4382 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004383 }
4384
4385 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386}
4387
Dongxiao Xu7725b892010-05-11 18:29:38 +08004388static void kvm_cpu_vmxon(u64 addr)
4389{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004390 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004391 intel_pt_handle_vmx(1);
4392
Dongxiao Xu7725b892010-05-11 18:29:38 +08004393 asm volatile (ASM_VMX_VMXON_RAX
4394 : : "a"(&addr), "m"(addr)
4395 : "memory", "cc");
4396}
4397
Radim Krčmář13a34e02014-08-28 15:13:03 +02004398static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399{
4400 int cpu = raw_smp_processor_id();
4401 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004402 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004404 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004405 return -EBUSY;
4406
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004407 /*
4408 * This can happen if we hot-added a CPU but failed to allocate
4409 * VP assist page for it.
4410 */
4411 if (static_branch_unlikely(&enable_evmcs) &&
4412 !hv_get_vp_assist_page(cpu))
4413 return -EFAULT;
4414
Nadav Har'Eld462b812011-05-24 15:26:10 +03004415 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004416 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4417 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004418
4419 /*
4420 * Now we can enable the vmclear operation in kdump
4421 * since the loaded_vmcss_on_cpu list on this cpu
4422 * has been initialized.
4423 *
4424 * Though the cpu is not in VMX operation now, there
4425 * is no problem to enable the vmclear operation
4426 * for the loaded_vmcss_on_cpu list is empty!
4427 */
4428 crash_enable_local_vmclear(cpu);
4429
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004431
4432 test_bits = FEATURE_CONTROL_LOCKED;
4433 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4434 if (tboot_enabled())
4435 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4436
4437 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004439 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4440 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004441 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004442 if (enable_ept)
4443 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004444
4445 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446}
4447
Nadav Har'Eld462b812011-05-24 15:26:10 +03004448static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004449{
4450 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004451 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004452
Nadav Har'Eld462b812011-05-24 15:26:10 +03004453 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4454 loaded_vmcss_on_cpu_link)
4455 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004456}
4457
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004458
4459/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4460 * tricks.
4461 */
4462static void kvm_cpu_vmxoff(void)
4463{
4464 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004465
4466 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004467 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004468}
4469
Radim Krčmář13a34e02014-08-28 15:13:03 +02004470static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004472 vmclear_local_loaded_vmcss();
4473 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474}
4475
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004476static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004477 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478{
4479 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004480 u32 ctl = ctl_min | ctl_opt;
4481
4482 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4483
4484 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4485 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4486
4487 /* Ensure minimum (required) set of control bits are supported. */
4488 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004489 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004490
4491 *result = ctl;
4492 return 0;
4493}
4494
Avi Kivity110312c2010-12-21 12:54:20 +02004495static __init bool allow_1_setting(u32 msr, u32 ctl)
4496{
4497 u32 vmx_msr_low, vmx_msr_high;
4498
4499 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4500 return vmx_msr_high & ctl;
4501}
4502
Yang, Sheng002c7f72007-07-31 14:23:01 +03004503static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504{
4505 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004506 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004507 u32 _pin_based_exec_control = 0;
4508 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004509 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510 u32 _vmexit_control = 0;
4511 u32 _vmentry_control = 0;
4512
Paolo Bonzini13893092018-02-26 13:40:09 +01004513 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304514 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004515#ifdef CONFIG_X86_64
4516 CPU_BASED_CR8_LOAD_EXITING |
4517 CPU_BASED_CR8_STORE_EXITING |
4518#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004519 CPU_BASED_CR3_LOAD_EXITING |
4520 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004521 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004522 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004523 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004524 CPU_BASED_MWAIT_EXITING |
4525 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004526 CPU_BASED_INVLPG_EXITING |
4527 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004528
Sheng Yangf78e0e22007-10-29 09:40:42 +08004529 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004530 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004531 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004532 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4533 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004534 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004535#ifdef CONFIG_X86_64
4536 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4537 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4538 ~CPU_BASED_CR8_STORE_EXITING;
4539#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004540 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004541 min2 = 0;
4542 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004543 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004544 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004545 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004546 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004547 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004548 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004549 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004550 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004551 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004552 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004553 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004554 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004555 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004556 SECONDARY_EXEC_RDSEED_EXITING |
4557 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004558 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004559 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004560 SECONDARY_EXEC_ENABLE_VMFUNC |
4561 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004562 if (adjust_vmx_controls(min2, opt2,
4563 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004564 &_cpu_based_2nd_exec_control) < 0)
4565 return -EIO;
4566 }
4567#ifndef CONFIG_X86_64
4568 if (!(_cpu_based_2nd_exec_control &
4569 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4570 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4571#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004572
4573 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4574 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004575 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004576 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4577 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004578
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004579 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4580 &vmx_capability.ept, &vmx_capability.vpid);
4581
Sheng Yangd56f5462008-04-25 10:13:16 +08004582 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004583 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4584 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004585 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4586 CPU_BASED_CR3_STORE_EXITING |
4587 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004588 } else if (vmx_capability.ept) {
4589 vmx_capability.ept = 0;
4590 pr_warn_once("EPT CAP should not exist if not support "
4591 "1-setting enable EPT VM-execution control\n");
4592 }
4593 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4594 vmx_capability.vpid) {
4595 vmx_capability.vpid = 0;
4596 pr_warn_once("VPID CAP should not exist if not support "
4597 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004598 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004599
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004600 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004601#ifdef CONFIG_X86_64
4602 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4603#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004604 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004605 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004606 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4607 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004608 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004609
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004610 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4611 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4612 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004613 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4614 &_pin_based_exec_control) < 0)
4615 return -EIO;
4616
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004617 if (cpu_has_broken_vmx_preemption_timer())
4618 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004619 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004620 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004621 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4622
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004623 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004624 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004625 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4626 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004627 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004628
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004629 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004630
4631 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4632 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004633 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004634
4635#ifdef CONFIG_X86_64
4636 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4637 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004638 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004639#endif
4640
4641 /* Require Write-Back (WB) memory type for VMCS accesses. */
4642 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004643 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004644
Yang, Sheng002c7f72007-07-31 14:23:01 +03004645 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004646 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004647 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004648
Liran Alon2307af12018-06-29 22:59:04 +03004649 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Yang, Sheng002c7f72007-07-31 14:23:01 +03004651 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4652 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004653 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004654 vmcs_conf->vmexit_ctrl = _vmexit_control;
4655 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004656
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004657 if (static_branch_unlikely(&enable_evmcs))
4658 evmcs_sanitize_exec_ctrls(vmcs_conf);
4659
Avi Kivity110312c2010-12-21 12:54:20 +02004660 cpu_has_load_ia32_efer =
4661 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4662 VM_ENTRY_LOAD_IA32_EFER)
4663 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4664 VM_EXIT_LOAD_IA32_EFER);
4665
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004666 cpu_has_load_perf_global_ctrl =
4667 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4668 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4669 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4670 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4671
4672 /*
4673 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004674 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004675 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4676 *
4677 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4678 *
4679 * AAK155 (model 26)
4680 * AAP115 (model 30)
4681 * AAT100 (model 37)
4682 * BC86,AAY89,BD102 (model 44)
4683 * BA97 (model 46)
4684 *
4685 */
4686 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4687 switch (boot_cpu_data.x86_model) {
4688 case 26:
4689 case 30:
4690 case 37:
4691 case 44:
4692 case 46:
4693 cpu_has_load_perf_global_ctrl = false;
4694 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4695 "does not work properly. Using workaround\n");
4696 break;
4697 default:
4698 break;
4699 }
4700 }
4701
Borislav Petkov782511b2016-04-04 22:25:03 +02004702 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004703 rdmsrl(MSR_IA32_XSS, host_xss);
4704
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004705 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004706}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707
Liran Alon491a6032018-06-23 02:35:12 +03004708static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709{
4710 int node = cpu_to_node(cpu);
4711 struct page *pages;
4712 struct vmcs *vmcs;
4713
Vlastimil Babka96db8002015-09-08 15:03:50 -07004714 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715 if (!pages)
4716 return NULL;
4717 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004718 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004719
4720 /* KVM supports Enlightened VMCS v1 only */
4721 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004722 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004723 else
Liran Alon392b2f22018-06-23 02:35:01 +03004724 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004725
Liran Alon491a6032018-06-23 02:35:12 +03004726 if (shadow)
4727 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728 return vmcs;
4729}
4730
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731static void free_vmcs(struct vmcs *vmcs)
4732{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004733 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734}
4735
Nadav Har'Eld462b812011-05-24 15:26:10 +03004736/*
4737 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4738 */
4739static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4740{
4741 if (!loaded_vmcs->vmcs)
4742 return;
4743 loaded_vmcs_clear(loaded_vmcs);
4744 free_vmcs(loaded_vmcs->vmcs);
4745 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004746 if (loaded_vmcs->msr_bitmap)
4747 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004748 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004749}
4750
Liran Alon491a6032018-06-23 02:35:12 +03004751static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004752{
Liran Alon491a6032018-06-23 02:35:12 +03004753 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004754}
4755
4756static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4757{
Liran Alon491a6032018-06-23 02:35:12 +03004758 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004759 if (!loaded_vmcs->vmcs)
4760 return -ENOMEM;
4761
4762 loaded_vmcs->shadow_vmcs = NULL;
4763 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004764
4765 if (cpu_has_vmx_msr_bitmap()) {
4766 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4767 if (!loaded_vmcs->msr_bitmap)
4768 goto out_vmcs;
4769 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004770
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004771 if (IS_ENABLED(CONFIG_HYPERV) &&
4772 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004773 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4774 struct hv_enlightened_vmcs *evmcs =
4775 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4776
4777 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4778 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004779 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004780
4781 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4782
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004783 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004784
4785out_vmcs:
4786 free_loaded_vmcs(loaded_vmcs);
4787 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004788}
4789
Sam Ravnborg39959582007-06-01 00:47:13 -07004790static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791{
4792 int cpu;
4793
Zachary Amsden3230bb42009-09-29 11:38:37 -10004794 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004796 per_cpu(vmxarea, cpu) = NULL;
4797 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798}
4799
Jim Mattsond37f4262017-12-22 12:12:16 -08004800enum vmcs_field_width {
4801 VMCS_FIELD_WIDTH_U16 = 0,
4802 VMCS_FIELD_WIDTH_U64 = 1,
4803 VMCS_FIELD_WIDTH_U32 = 2,
4804 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004805};
4806
Jim Mattsond37f4262017-12-22 12:12:16 -08004807static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004808{
4809 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004810 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004811 return (field >> 13) & 0x3 ;
4812}
4813
4814static inline int vmcs_field_readonly(unsigned long field)
4815{
4816 return (((field >> 10) & 0x3) == 1);
4817}
4818
Bandan Dasfe2b2012014-04-21 15:20:14 -04004819static void init_vmcs_shadow_fields(void)
4820{
4821 int i, j;
4822
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004823 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4824 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004825 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004826 (i + 1 == max_shadow_read_only_fields ||
4827 shadow_read_only_fields[i + 1] != field + 1))
4828 pr_err("Missing field from shadow_read_only_field %x\n",
4829 field + 1);
4830
4831 clear_bit(field, vmx_vmread_bitmap);
4832#ifdef CONFIG_X86_64
4833 if (field & 1)
4834 continue;
4835#endif
4836 if (j < i)
4837 shadow_read_only_fields[j] = field;
4838 j++;
4839 }
4840 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004841
4842 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004843 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004844 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004845 (i + 1 == max_shadow_read_write_fields ||
4846 shadow_read_write_fields[i + 1] != field + 1))
4847 pr_err("Missing field from shadow_read_write_field %x\n",
4848 field + 1);
4849
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004850 /*
4851 * PML and the preemption timer can be emulated, but the
4852 * processor cannot vmwrite to fields that don't exist
4853 * on bare metal.
4854 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004855 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004856 case GUEST_PML_INDEX:
4857 if (!cpu_has_vmx_pml())
4858 continue;
4859 break;
4860 case VMX_PREEMPTION_TIMER_VALUE:
4861 if (!cpu_has_vmx_preemption_timer())
4862 continue;
4863 break;
4864 case GUEST_INTR_STATUS:
4865 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004866 continue;
4867 break;
4868 default:
4869 break;
4870 }
4871
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004872 clear_bit(field, vmx_vmwrite_bitmap);
4873 clear_bit(field, vmx_vmread_bitmap);
4874#ifdef CONFIG_X86_64
4875 if (field & 1)
4876 continue;
4877#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004878 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004879 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004880 j++;
4881 }
4882 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004883}
4884
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885static __init int alloc_kvm_area(void)
4886{
4887 int cpu;
4888
Zachary Amsden3230bb42009-09-29 11:38:37 -10004889 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 struct vmcs *vmcs;
4891
Liran Alon491a6032018-06-23 02:35:12 +03004892 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 if (!vmcs) {
4894 free_kvm_area();
4895 return -ENOMEM;
4896 }
4897
Liran Alon2307af12018-06-29 22:59:04 +03004898 /*
4899 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4900 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4901 * revision_id reported by MSR_IA32_VMX_BASIC.
4902 *
4903 * However, even though not explictly documented by
4904 * TLFS, VMXArea passed as VMXON argument should
4905 * still be marked with revision_id reported by
4906 * physical CPU.
4907 */
4908 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004909 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004910
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 per_cpu(vmxarea, cpu) = vmcs;
4912 }
4913 return 0;
4914}
4915
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004916static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004917 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004919 if (!emulate_invalid_guest_state) {
4920 /*
4921 * CS and SS RPL should be equal during guest entry according
4922 * to VMX spec, but in reality it is not always so. Since vcpu
4923 * is in the middle of the transition from real mode to
4924 * protected mode it is safe to assume that RPL 0 is a good
4925 * default value.
4926 */
4927 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004928 save->selector &= ~SEGMENT_RPL_MASK;
4929 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004930 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004931 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004932 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004933}
4934
4935static void enter_pmode(struct kvm_vcpu *vcpu)
4936{
4937 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004938 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939
Gleb Natapovd99e4152012-12-20 16:57:45 +02004940 /*
4941 * Update real mode segment cache. It may be not up-to-date if sement
4942 * register was written while vcpu was in a guest mode.
4943 */
4944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4950
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004951 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952
Avi Kivity2fb92db2011-04-27 19:42:18 +03004953 vmx_segment_cache_clear(vmx);
4954
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004955 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956
4957 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004958 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4959 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004960 vmcs_writel(GUEST_RFLAGS, flags);
4961
Rusty Russell66aee912007-07-17 23:34:16 +10004962 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4963 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004964
4965 update_exception_bitmap(vcpu);
4966
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004967 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4968 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4969 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4970 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4971 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4972 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973}
4974
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004975static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004976{
Mathias Krause772e0312012-08-30 01:30:19 +02004977 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004978 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979
Gleb Natapovd99e4152012-12-20 16:57:45 +02004980 var.dpl = 0x3;
4981 if (seg == VCPU_SREG_CS)
4982 var.type = 0x3;
4983
4984 if (!emulate_invalid_guest_state) {
4985 var.selector = var.base >> 4;
4986 var.base = var.base & 0xffff0;
4987 var.limit = 0xffff;
4988 var.g = 0;
4989 var.db = 0;
4990 var.present = 1;
4991 var.s = 1;
4992 var.l = 0;
4993 var.unusable = 0;
4994 var.type = 0x3;
4995 var.avl = 0;
4996 if (save->base & 0xf)
4997 printk_once(KERN_WARNING "kvm: segment base is not "
4998 "paragraph aligned when entering "
4999 "protected mode (seg=%d)", seg);
5000 }
5001
5002 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005003 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005004 vmcs_write32(sf->limit, var.limit);
5005 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006}
5007
5008static void enter_rmode(struct kvm_vcpu *vcpu)
5009{
5010 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005011 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005012 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005013
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005021
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005022 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005023
Gleb Natapov776e58e2011-03-13 12:34:27 +02005024 /*
5025 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005026 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005027 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005028 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005029 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5030 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005031
Avi Kivity2fb92db2011-04-27 19:42:18 +03005032 vmx_segment_cache_clear(vmx);
5033
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005034 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005036 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5037
5038 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005039 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005041 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042
5043 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005044 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045 update_exception_bitmap(vcpu);
5046
Gleb Natapovd99e4152012-12-20 16:57:45 +02005047 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5048 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5049 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5050 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5051 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5052 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005053
Eddie Dong8668a3c2007-10-10 14:26:45 +08005054 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055}
5056
Amit Shah401d10d2009-02-20 22:53:37 +05305057static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5058{
5059 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005060 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5061
5062 if (!msr)
5063 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305064
Avi Kivity44ea2b12009-09-06 15:55:37 +03005065 /*
Sean Christopherson678e3152018-07-23 12:32:43 -07005066 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
5067 * 64-bit mode as a 64-bit kernel may frequently access the
5068 * MSR. This means we need to manually save/restore the MSR
5069 * when switching between guest and host state, but only if
5070 * the guest is in 64-bit mode. Sync our cached value if the
5071 * guest is transitioning to 32-bit mode and the CPU contains
5072 * guest state, i.e. the cache is stale.
Avi Kivity44ea2b12009-09-06 15:55:37 +03005073 */
Sean Christopherson678e3152018-07-23 12:32:43 -07005074#ifdef CONFIG_X86_64
5075 if (!(efer & EFER_LMA))
5076 (void)vmx_read_guest_kernel_gs_base(vmx);
5077#endif
Avi Kivityf6801df2010-01-21 15:31:50 +02005078 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305079 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005080 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305081 msr->data = efer;
5082 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005083 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305084
5085 msr->data = efer & ~EFER_LME;
5086 }
5087 setup_msrs(vmx);
5088}
5089
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005090#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091
5092static void enter_lmode(struct kvm_vcpu *vcpu)
5093{
5094 u32 guest_tr_ar;
5095
Avi Kivity2fb92db2011-04-27 19:42:18 +03005096 vmx_segment_cache_clear(to_vmx(vcpu));
5097
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005099 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005100 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5101 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005102 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005103 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5104 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005105 }
Avi Kivityda38f432010-07-06 11:30:49 +03005106 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005107}
5108
5109static void exit_lmode(struct kvm_vcpu *vcpu)
5110{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005111 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005112 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113}
5114
5115#endif
5116
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005117static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5118 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005119{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005120 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005121 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5122 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005123 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005124 } else {
5125 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005126 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005127}
5128
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005129static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005130{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005131 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005132}
5133
Junaid Shahidfaff8752018-06-29 13:10:05 -07005134static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5135{
5136 int vpid = to_vmx(vcpu)->vpid;
5137
5138 if (!vpid_sync_vcpu_addr(vpid, addr))
5139 vpid_sync_context(vpid);
5140
5141 /*
5142 * If VPIDs are not supported or enabled, then the above is a no-op.
5143 * But we don't really need a TLB flush in that case anyway, because
5144 * each VM entry/exit includes an implicit flush when VPID is 0.
5145 */
5146}
5147
Avi Kivitye8467fd2009-12-29 18:43:06 +02005148static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5149{
5150 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5151
5152 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5153 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5154}
5155
Avi Kivityaff48ba2010-12-05 18:56:11 +02005156static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5157{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005158 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005159 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5160 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5161}
5162
Anthony Liguori25c4c272007-04-27 09:29:21 +03005163static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005164{
Avi Kivityfc78f512009-12-07 12:16:48 +02005165 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5166
5167 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5168 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005169}
5170
Sheng Yang14394422008-04-28 12:24:45 +08005171static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5172{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005173 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5174
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005175 if (!test_bit(VCPU_EXREG_PDPTR,
5176 (unsigned long *)&vcpu->arch.regs_dirty))
5177 return;
5178
Sheng Yang14394422008-04-28 12:24:45 +08005179 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005180 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5181 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5182 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5183 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005184 }
5185}
5186
Avi Kivity8f5d5492009-05-31 18:41:29 +03005187static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5188{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005189 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5190
Avi Kivity8f5d5492009-05-31 18:41:29 +03005191 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005192 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5193 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5194 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5195 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005196 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005197
5198 __set_bit(VCPU_EXREG_PDPTR,
5199 (unsigned long *)&vcpu->arch.regs_avail);
5200 __set_bit(VCPU_EXREG_PDPTR,
5201 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005202}
5203
David Matlack38991522016-11-29 18:14:08 -08005204static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5205{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005206 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5207 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005208 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5209
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005210 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005211 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5212 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5213 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5214
5215 return fixed_bits_valid(val, fixed0, fixed1);
5216}
5217
5218static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5219{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005220 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5221 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005222
5223 return fixed_bits_valid(val, fixed0, fixed1);
5224}
5225
5226static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5227{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005228 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5229 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005230
5231 return fixed_bits_valid(val, fixed0, fixed1);
5232}
5233
5234/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5235#define nested_guest_cr4_valid nested_cr4_valid
5236#define nested_host_cr4_valid nested_cr4_valid
5237
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005238static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005239
5240static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5241 unsigned long cr0,
5242 struct kvm_vcpu *vcpu)
5243{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005244 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5245 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005246 if (!(cr0 & X86_CR0_PG)) {
5247 /* From paging/starting to nonpaging */
5248 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005249 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005250 (CPU_BASED_CR3_LOAD_EXITING |
5251 CPU_BASED_CR3_STORE_EXITING));
5252 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005253 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005254 } else if (!is_paging(vcpu)) {
5255 /* From nonpaging to paging */
5256 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005257 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005258 ~(CPU_BASED_CR3_LOAD_EXITING |
5259 CPU_BASED_CR3_STORE_EXITING));
5260 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005261 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005262 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005263
5264 if (!(cr0 & X86_CR0_WP))
5265 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005266}
5267
Avi Kivity6aa8b732006-12-10 02:21:36 -08005268static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5269{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005270 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005271 unsigned long hw_cr0;
5272
Gleb Natapov50378782013-02-04 16:00:28 +02005273 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005274 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005275 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005276 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005277 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005278
Gleb Natapov218e7632013-01-21 15:36:45 +02005279 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5280 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281
Gleb Natapov218e7632013-01-21 15:36:45 +02005282 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5283 enter_rmode(vcpu);
5284 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005285
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005286#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005287 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005288 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005289 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005290 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291 exit_lmode(vcpu);
5292 }
5293#endif
5294
Sean Christophersonb4d18512018-03-05 12:04:40 -08005295 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005296 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5297
Avi Kivity6aa8b732006-12-10 02:21:36 -08005298 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005299 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005300 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005301
5302 /* depends on vcpu->arch.cr0 to be set to a new value */
5303 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304}
5305
Yu Zhang855feb62017-08-24 20:27:55 +08005306static int get_ept_level(struct kvm_vcpu *vcpu)
5307{
5308 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5309 return 5;
5310 return 4;
5311}
5312
Peter Feiner995f00a2017-06-30 17:26:32 -07005313static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005314{
Yu Zhang855feb62017-08-24 20:27:55 +08005315 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005316
Yu Zhang855feb62017-08-24 20:27:55 +08005317 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005318
Peter Feiner995f00a2017-06-30 17:26:32 -07005319 if (enable_ept_ad_bits &&
5320 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005321 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005322 eptp |= (root_hpa & PAGE_MASK);
5323
5324 return eptp;
5325}
5326
Avi Kivity6aa8b732006-12-10 02:21:36 -08005327static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5328{
Tianyu Lan877ad952018-07-19 08:40:23 +00005329 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005330 unsigned long guest_cr3;
5331 u64 eptp;
5332
5333 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005334 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005335 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005336 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005337
5338 if (kvm_x86_ops->tlb_remote_flush) {
5339 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5340 to_vmx(vcpu)->ept_pointer = eptp;
5341 to_kvm_vmx(kvm)->ept_pointers_match
5342 = EPT_POINTERS_CHECK;
5343 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5344 }
5345
Sean Christophersone90008d2018-03-05 12:04:37 -08005346 if (enable_unrestricted_guest || is_paging(vcpu) ||
5347 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005348 guest_cr3 = kvm_read_cr3(vcpu);
5349 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005350 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005351 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005352 }
5353
Sheng Yang14394422008-04-28 12:24:45 +08005354 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005355}
5356
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005357static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005358{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005359 /*
5360 * Pass through host's Machine Check Enable value to hw_cr4, which
5361 * is in force while we are in guest mode. Do not let guests control
5362 * this bit, even if host CR4.MCE == 0.
5363 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005364 unsigned long hw_cr4;
5365
5366 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5367 if (enable_unrestricted_guest)
5368 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5369 else if (to_vmx(vcpu)->rmode.vm86_active)
5370 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5371 else
5372 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005373
Sean Christopherson64f7a112018-04-30 10:01:06 -07005374 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5375 if (cr4 & X86_CR4_UMIP) {
5376 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005377 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005378 hw_cr4 &= ~X86_CR4_UMIP;
5379 } else if (!is_guest_mode(vcpu) ||
5380 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5381 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5382 SECONDARY_EXEC_DESC);
5383 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005384
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005385 if (cr4 & X86_CR4_VMXE) {
5386 /*
5387 * To use VMXON (and later other VMX instructions), a guest
5388 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5389 * So basically the check on whether to allow nested VMX
5390 * is here.
5391 */
5392 if (!nested_vmx_allowed(vcpu))
5393 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005394 }
David Matlack38991522016-11-29 18:14:08 -08005395
5396 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005397 return 1;
5398
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005399 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005400
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005401 if (!enable_unrestricted_guest) {
5402 if (enable_ept) {
5403 if (!is_paging(vcpu)) {
5404 hw_cr4 &= ~X86_CR4_PAE;
5405 hw_cr4 |= X86_CR4_PSE;
5406 } else if (!(cr4 & X86_CR4_PAE)) {
5407 hw_cr4 &= ~X86_CR4_PAE;
5408 }
5409 }
5410
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005411 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005412 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5413 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5414 * to be manually disabled when guest switches to non-paging
5415 * mode.
5416 *
5417 * If !enable_unrestricted_guest, the CPU is always running
5418 * with CR0.PG=1 and CR4 needs to be modified.
5419 * If enable_unrestricted_guest, the CPU automatically
5420 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005421 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005422 if (!is_paging(vcpu))
5423 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5424 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005425
Sheng Yang14394422008-04-28 12:24:45 +08005426 vmcs_writel(CR4_READ_SHADOW, cr4);
5427 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005428 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005429}
5430
Avi Kivity6aa8b732006-12-10 02:21:36 -08005431static void vmx_get_segment(struct kvm_vcpu *vcpu,
5432 struct kvm_segment *var, int seg)
5433{
Avi Kivitya9179492011-01-03 14:28:52 +02005434 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005435 u32 ar;
5436
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005437 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005438 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005439 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005440 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005441 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005442 var->base = vmx_read_guest_seg_base(vmx, seg);
5443 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5444 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005445 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005446 var->base = vmx_read_guest_seg_base(vmx, seg);
5447 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5448 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5449 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005450 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451 var->type = ar & 15;
5452 var->s = (ar >> 4) & 1;
5453 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005454 /*
5455 * Some userspaces do not preserve unusable property. Since usable
5456 * segment has to be present according to VMX spec we can use present
5457 * property to amend userspace bug by making unusable segment always
5458 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5459 * segment as unusable.
5460 */
5461 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005462 var->avl = (ar >> 12) & 1;
5463 var->l = (ar >> 13) & 1;
5464 var->db = (ar >> 14) & 1;
5465 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005466}
5467
Avi Kivitya9179492011-01-03 14:28:52 +02005468static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5469{
Avi Kivitya9179492011-01-03 14:28:52 +02005470 struct kvm_segment s;
5471
5472 if (to_vmx(vcpu)->rmode.vm86_active) {
5473 vmx_get_segment(vcpu, &s, seg);
5474 return s.base;
5475 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005476 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005477}
5478
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005479static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005480{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005481 struct vcpu_vmx *vmx = to_vmx(vcpu);
5482
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005483 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005484 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005485 else {
5486 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005487 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005488 }
Avi Kivity69c73022011-03-07 15:26:44 +02005489}
5490
Avi Kivity653e3102007-05-07 10:55:37 +03005491static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005492{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005493 u32 ar;
5494
Avi Kivityf0495f92012-06-07 17:06:10 +03005495 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005496 ar = 1 << 16;
5497 else {
5498 ar = var->type & 15;
5499 ar |= (var->s & 1) << 4;
5500 ar |= (var->dpl & 3) << 5;
5501 ar |= (var->present & 1) << 7;
5502 ar |= (var->avl & 1) << 12;
5503 ar |= (var->l & 1) << 13;
5504 ar |= (var->db & 1) << 14;
5505 ar |= (var->g & 1) << 15;
5506 }
Avi Kivity653e3102007-05-07 10:55:37 +03005507
5508 return ar;
5509}
5510
5511static void vmx_set_segment(struct kvm_vcpu *vcpu,
5512 struct kvm_segment *var, int seg)
5513{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005514 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005515 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005516
Avi Kivity2fb92db2011-04-27 19:42:18 +03005517 vmx_segment_cache_clear(vmx);
5518
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005519 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5520 vmx->rmode.segs[seg] = *var;
5521 if (seg == VCPU_SREG_TR)
5522 vmcs_write16(sf->selector, var->selector);
5523 else if (var->s)
5524 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005525 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005526 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005527
Avi Kivity653e3102007-05-07 10:55:37 +03005528 vmcs_writel(sf->base, var->base);
5529 vmcs_write32(sf->limit, var->limit);
5530 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005531
5532 /*
5533 * Fix the "Accessed" bit in AR field of segment registers for older
5534 * qemu binaries.
5535 * IA32 arch specifies that at the time of processor reset the
5536 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005537 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005538 * state vmexit when "unrestricted guest" mode is turned on.
5539 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5540 * tree. Newer qemu binaries with that qemu fix would not need this
5541 * kvm hack.
5542 */
5543 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005544 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005545
Gleb Natapovf924d662012-12-12 19:10:55 +02005546 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005547
5548out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005549 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550}
5551
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5553{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005554 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555
5556 *db = (ar >> 14) & 1;
5557 *l = (ar >> 13) & 1;
5558}
5559
Gleb Natapov89a27f42010-02-16 10:51:48 +02005560static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005562 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5563 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564}
5565
Gleb Natapov89a27f42010-02-16 10:51:48 +02005566static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005568 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5569 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570}
5571
Gleb Natapov89a27f42010-02-16 10:51:48 +02005572static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005574 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5575 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
5577
Gleb Natapov89a27f42010-02-16 10:51:48 +02005578static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005580 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5581 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005582}
5583
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005584static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5585{
5586 struct kvm_segment var;
5587 u32 ar;
5588
5589 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005590 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005591 if (seg == VCPU_SREG_CS)
5592 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005593 ar = vmx_segment_access_rights(&var);
5594
5595 if (var.base != (var.selector << 4))
5596 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005597 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005598 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005599 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005600 return false;
5601
5602 return true;
5603}
5604
5605static bool code_segment_valid(struct kvm_vcpu *vcpu)
5606{
5607 struct kvm_segment cs;
5608 unsigned int cs_rpl;
5609
5610 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005611 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005612
Avi Kivity1872a3f2009-01-04 23:26:52 +02005613 if (cs.unusable)
5614 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005615 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005616 return false;
5617 if (!cs.s)
5618 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005619 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005620 if (cs.dpl > cs_rpl)
5621 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005622 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005623 if (cs.dpl != cs_rpl)
5624 return false;
5625 }
5626 if (!cs.present)
5627 return false;
5628
5629 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5630 return true;
5631}
5632
5633static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5634{
5635 struct kvm_segment ss;
5636 unsigned int ss_rpl;
5637
5638 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005639 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005640
Avi Kivity1872a3f2009-01-04 23:26:52 +02005641 if (ss.unusable)
5642 return true;
5643 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005644 return false;
5645 if (!ss.s)
5646 return false;
5647 if (ss.dpl != ss_rpl) /* DPL != RPL */
5648 return false;
5649 if (!ss.present)
5650 return false;
5651
5652 return true;
5653}
5654
5655static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5656{
5657 struct kvm_segment var;
5658 unsigned int rpl;
5659
5660 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005661 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005662
Avi Kivity1872a3f2009-01-04 23:26:52 +02005663 if (var.unusable)
5664 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005665 if (!var.s)
5666 return false;
5667 if (!var.present)
5668 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005669 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005670 if (var.dpl < rpl) /* DPL < RPL */
5671 return false;
5672 }
5673
5674 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5675 * rights flags
5676 */
5677 return true;
5678}
5679
5680static bool tr_valid(struct kvm_vcpu *vcpu)
5681{
5682 struct kvm_segment tr;
5683
5684 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5685
Avi Kivity1872a3f2009-01-04 23:26:52 +02005686 if (tr.unusable)
5687 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005688 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005689 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005690 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005691 return false;
5692 if (!tr.present)
5693 return false;
5694
5695 return true;
5696}
5697
5698static bool ldtr_valid(struct kvm_vcpu *vcpu)
5699{
5700 struct kvm_segment ldtr;
5701
5702 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5703
Avi Kivity1872a3f2009-01-04 23:26:52 +02005704 if (ldtr.unusable)
5705 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005706 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005707 return false;
5708 if (ldtr.type != 2)
5709 return false;
5710 if (!ldtr.present)
5711 return false;
5712
5713 return true;
5714}
5715
5716static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5717{
5718 struct kvm_segment cs, ss;
5719
5720 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5721 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5722
Nadav Amitb32a9912015-03-29 16:33:04 +03005723 return ((cs.selector & SEGMENT_RPL_MASK) ==
5724 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005725}
5726
5727/*
5728 * Check if guest state is valid. Returns true if valid, false if
5729 * not.
5730 * We assume that registers are always usable
5731 */
5732static bool guest_state_valid(struct kvm_vcpu *vcpu)
5733{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005734 if (enable_unrestricted_guest)
5735 return true;
5736
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005737 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005738 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005739 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5740 return false;
5741 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5742 return false;
5743 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5744 return false;
5745 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5746 return false;
5747 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5748 return false;
5749 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5750 return false;
5751 } else {
5752 /* protected mode guest state checks */
5753 if (!cs_ss_rpl_check(vcpu))
5754 return false;
5755 if (!code_segment_valid(vcpu))
5756 return false;
5757 if (!stack_segment_valid(vcpu))
5758 return false;
5759 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5760 return false;
5761 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5762 return false;
5763 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5764 return false;
5765 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5766 return false;
5767 if (!tr_valid(vcpu))
5768 return false;
5769 if (!ldtr_valid(vcpu))
5770 return false;
5771 }
5772 /* TODO:
5773 * - Add checks on RIP
5774 * - Add checks on RFLAGS
5775 */
5776
5777 return true;
5778}
5779
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005780static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5781{
5782 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5783}
5784
Mike Dayd77c26f2007-10-08 09:02:08 -04005785static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005786{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005787 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005788 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005789 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005790
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005791 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005792 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005793 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5794 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005795 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005796 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005797 r = kvm_write_guest_page(kvm, fn++, &data,
5798 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005799 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005800 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005801 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5802 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005803 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005804 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5805 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005806 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005807 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005808 r = kvm_write_guest_page(kvm, fn, &data,
5809 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5810 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005811out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005812 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005813 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005814}
5815
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005816static int init_rmode_identity_map(struct kvm *kvm)
5817{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005818 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005819 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005820 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005821 u32 tmp;
5822
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005823 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005824 mutex_lock(&kvm->slots_lock);
5825
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005826 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005827 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005828
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005829 if (!kvm_vmx->ept_identity_map_addr)
5830 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5831 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005832
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005833 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005834 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005835 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005836 goto out2;
5837
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005838 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005839 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5840 if (r < 0)
5841 goto out;
5842 /* Set up identity-mapping pagetable for EPT in real mode */
5843 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5844 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5845 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5846 r = kvm_write_guest_page(kvm, identity_map_pfn,
5847 &tmp, i * sizeof(tmp), sizeof(tmp));
5848 if (r < 0)
5849 goto out;
5850 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005851 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005852
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005853out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005854 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005855
5856out2:
5857 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005858 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005859}
5860
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861static void seg_setup(int seg)
5862{
Mathias Krause772e0312012-08-30 01:30:19 +02005863 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005864 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865
5866 vmcs_write16(sf->selector, 0);
5867 vmcs_writel(sf->base, 0);
5868 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005869 ar = 0x93;
5870 if (seg == VCPU_SREG_CS)
5871 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005872
5873 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005874}
5875
Sheng Yangf78e0e22007-10-29 09:40:42 +08005876static int alloc_apic_access_page(struct kvm *kvm)
5877{
Xiao Guangrong44841412012-09-07 14:14:20 +08005878 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005879 int r = 0;
5880
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005881 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005882 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005883 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005884 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5885 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005886 if (r)
5887 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005888
Tang Chen73a6d942014-09-11 13:38:00 +08005889 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005890 if (is_error_page(page)) {
5891 r = -EFAULT;
5892 goto out;
5893 }
5894
Tang Chenc24ae0d2014-09-24 15:57:58 +08005895 /*
5896 * Do not pin the page in memory, so that memory hot-unplug
5897 * is able to migrate it.
5898 */
5899 put_page(page);
5900 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005901out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005902 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005903 return r;
5904}
5905
Wanpeng Li991e7a02015-09-16 17:30:05 +08005906static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005907{
5908 int vpid;
5909
Avi Kivity919818a2009-03-23 18:01:29 +02005910 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005911 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005912 spin_lock(&vmx_vpid_lock);
5913 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005914 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005915 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005916 else
5917 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005918 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005919 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005920}
5921
Wanpeng Li991e7a02015-09-16 17:30:05 +08005922static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005923{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005924 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005925 return;
5926 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005927 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005928 spin_unlock(&vmx_vpid_lock);
5929}
5930
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005931static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5932 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005933{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005934 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005935
5936 if (!cpu_has_vmx_msr_bitmap())
5937 return;
5938
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005939 if (static_branch_unlikely(&enable_evmcs))
5940 evmcs_touch_msr_bitmap();
5941
Sheng Yang25c5f222008-03-28 13:18:56 +08005942 /*
5943 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5944 * have the write-low and read-high bitmap offsets the wrong way round.
5945 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5946 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005947 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005948 if (type & MSR_TYPE_R)
5949 /* read-low */
5950 __clear_bit(msr, msr_bitmap + 0x000 / f);
5951
5952 if (type & MSR_TYPE_W)
5953 /* write-low */
5954 __clear_bit(msr, msr_bitmap + 0x800 / f);
5955
Sheng Yang25c5f222008-03-28 13:18:56 +08005956 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5957 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005958 if (type & MSR_TYPE_R)
5959 /* read-high */
5960 __clear_bit(msr, msr_bitmap + 0x400 / f);
5961
5962 if (type & MSR_TYPE_W)
5963 /* write-high */
5964 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5965
5966 }
5967}
5968
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005969static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5970 u32 msr, int type)
5971{
5972 int f = sizeof(unsigned long);
5973
5974 if (!cpu_has_vmx_msr_bitmap())
5975 return;
5976
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005977 if (static_branch_unlikely(&enable_evmcs))
5978 evmcs_touch_msr_bitmap();
5979
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005980 /*
5981 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5982 * have the write-low and read-high bitmap offsets the wrong way round.
5983 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5984 */
5985 if (msr <= 0x1fff) {
5986 if (type & MSR_TYPE_R)
5987 /* read-low */
5988 __set_bit(msr, msr_bitmap + 0x000 / f);
5989
5990 if (type & MSR_TYPE_W)
5991 /* write-low */
5992 __set_bit(msr, msr_bitmap + 0x800 / f);
5993
5994 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5995 msr &= 0x1fff;
5996 if (type & MSR_TYPE_R)
5997 /* read-high */
5998 __set_bit(msr, msr_bitmap + 0x400 / f);
5999
6000 if (type & MSR_TYPE_W)
6001 /* write-high */
6002 __set_bit(msr, msr_bitmap + 0xc00 / f);
6003
6004 }
6005}
6006
6007static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6008 u32 msr, int type, bool value)
6009{
6010 if (value)
6011 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6012 else
6013 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6014}
6015
Wincy Vanf2b93282015-02-03 23:56:03 +08006016/*
6017 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6018 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6019 */
6020static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6021 unsigned long *msr_bitmap_nested,
6022 u32 msr, int type)
6023{
6024 int f = sizeof(unsigned long);
6025
Wincy Vanf2b93282015-02-03 23:56:03 +08006026 /*
6027 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6028 * have the write-low and read-high bitmap offsets the wrong way round.
6029 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6030 */
6031 if (msr <= 0x1fff) {
6032 if (type & MSR_TYPE_R &&
6033 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6034 /* read-low */
6035 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6036
6037 if (type & MSR_TYPE_W &&
6038 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6039 /* write-low */
6040 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6041
6042 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6043 msr &= 0x1fff;
6044 if (type & MSR_TYPE_R &&
6045 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6046 /* read-high */
6047 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6048
6049 if (type & MSR_TYPE_W &&
6050 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6051 /* write-high */
6052 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6053
6054 }
6055}
6056
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006057static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006058{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006059 u8 mode = 0;
6060
6061 if (cpu_has_secondary_exec_ctrls() &&
6062 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6063 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6064 mode |= MSR_BITMAP_MODE_X2APIC;
6065 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6066 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6067 }
6068
6069 if (is_long_mode(vcpu))
6070 mode |= MSR_BITMAP_MODE_LM;
6071
6072 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006073}
6074
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006075#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6076
6077static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6078 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006079{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006080 int msr;
6081
6082 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6083 unsigned word = msr / BITS_PER_LONG;
6084 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6085 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006086 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006087
6088 if (mode & MSR_BITMAP_MODE_X2APIC) {
6089 /*
6090 * TPR reads and writes can be virtualized even if virtual interrupt
6091 * delivery is not in use.
6092 */
6093 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6094 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6095 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6096 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6097 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6098 }
6099 }
6100}
6101
6102static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6103{
6104 struct vcpu_vmx *vmx = to_vmx(vcpu);
6105 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6106 u8 mode = vmx_msr_bitmap_mode(vcpu);
6107 u8 changed = mode ^ vmx->msr_bitmap_mode;
6108
6109 if (!changed)
6110 return;
6111
6112 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
6113 !(mode & MSR_BITMAP_MODE_LM));
6114
6115 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6116 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6117
6118 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006119}
6120
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006121static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006122{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006123 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006124}
6125
David Matlackc9f04402017-08-01 14:00:40 -07006126static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6127{
6128 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6129 gfn_t gfn;
6130
6131 /*
6132 * Don't need to mark the APIC access page dirty; it is never
6133 * written to by the CPU during APIC virtualization.
6134 */
6135
6136 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6137 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6138 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6139 }
6140
6141 if (nested_cpu_has_posted_intr(vmcs12)) {
6142 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6143 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6144 }
6145}
6146
6147
David Hildenbrand6342c502017-01-25 11:58:58 +01006148static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006149{
6150 struct vcpu_vmx *vmx = to_vmx(vcpu);
6151 int max_irr;
6152 void *vapic_page;
6153 u16 status;
6154
David Matlackc9f04402017-08-01 14:00:40 -07006155 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6156 return;
Wincy Van705699a2015-02-03 23:58:17 +08006157
David Matlackc9f04402017-08-01 14:00:40 -07006158 vmx->nested.pi_pending = false;
6159 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6160 return;
Wincy Van705699a2015-02-03 23:58:17 +08006161
David Matlackc9f04402017-08-01 14:00:40 -07006162 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6163 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006164 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006165 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6166 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006167 kunmap(vmx->nested.virtual_apic_page);
6168
6169 status = vmcs_read16(GUEST_INTR_STATUS);
6170 if ((u8)max_irr > ((u8)status & 0xff)) {
6171 status &= ~0xff;
6172 status |= (u8)max_irr;
6173 vmcs_write16(GUEST_INTR_STATUS, status);
6174 }
6175 }
David Matlackc9f04402017-08-01 14:00:40 -07006176
6177 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006178}
6179
Wincy Van06a55242017-04-28 13:13:59 +08006180static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6181 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006182{
6183#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006184 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6185
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006186 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006187 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006188 * The vector of interrupt to be delivered to vcpu had
6189 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006190 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006191 * Following cases will be reached in this block, and
6192 * we always send a notification event in all cases as
6193 * explained below.
6194 *
6195 * Case 1: vcpu keeps in non-root mode. Sending a
6196 * notification event posts the interrupt to vcpu.
6197 *
6198 * Case 2: vcpu exits to root mode and is still
6199 * runnable. PIR will be synced to vIRR before the
6200 * next vcpu entry. Sending a notification event in
6201 * this case has no effect, as vcpu is not in root
6202 * mode.
6203 *
6204 * Case 3: vcpu exits to root mode and is blocked.
6205 * vcpu_block() has already synced PIR to vIRR and
6206 * never blocks vcpu if vIRR is not cleared. Therefore,
6207 * a blocked vcpu here does not wait for any requested
6208 * interrupts in PIR, and sending a notification event
6209 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006210 */
Feng Wu28b835d2015-09-18 22:29:54 +08006211
Wincy Van06a55242017-04-28 13:13:59 +08006212 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006213 return true;
6214 }
6215#endif
6216 return false;
6217}
6218
Wincy Van705699a2015-02-03 23:58:17 +08006219static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6220 int vector)
6221{
6222 struct vcpu_vmx *vmx = to_vmx(vcpu);
6223
6224 if (is_guest_mode(vcpu) &&
6225 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006226 /*
6227 * If a posted intr is not recognized by hardware,
6228 * we will accomplish it in the next vmentry.
6229 */
6230 vmx->nested.pi_pending = true;
6231 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006232 /* the PIR and ON have been set by L1. */
6233 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6234 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006235 return 0;
6236 }
6237 return -1;
6238}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006239/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006240 * Send interrupt to vcpu via posted interrupt way.
6241 * 1. If target vcpu is running(non-root mode), send posted interrupt
6242 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6243 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6244 * interrupt from PIR in next vmentry.
6245 */
6246static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6247{
6248 struct vcpu_vmx *vmx = to_vmx(vcpu);
6249 int r;
6250
Wincy Van705699a2015-02-03 23:58:17 +08006251 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6252 if (!r)
6253 return;
6254
Yang Zhanga20ed542013-04-11 19:25:15 +08006255 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6256 return;
6257
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006258 /* If a previous notification has sent the IPI, nothing to do. */
6259 if (pi_test_and_set_on(&vmx->pi_desc))
6260 return;
6261
Wincy Van06a55242017-04-28 13:13:59 +08006262 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006263 kvm_vcpu_kick(vcpu);
6264}
6265
Avi Kivity6aa8b732006-12-10 02:21:36 -08006266/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006267 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6268 * will not change in the lifetime of the guest.
6269 * Note that host-state that does change is set elsewhere. E.g., host-state
6270 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6271 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006272static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006273{
6274 u32 low32, high32;
6275 unsigned long tmpl;
6276 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006277 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006278
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006279 cr0 = read_cr0();
6280 WARN_ON(cr0 & X86_CR0_TS);
6281 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006282
6283 /*
6284 * Save the most likely value for this task's CR3 in the VMCS.
6285 * We can't use __get_current_cr3_fast() because we're not atomic.
6286 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006287 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006288 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006289 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006290
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006291 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006292 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006293 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006294 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006295
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006296 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006297#ifdef CONFIG_X86_64
6298 /*
6299 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006300 * vmx_prepare_switch_to_host(), in case userspace uses
6301 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006302 */
6303 vmcs_write16(HOST_DS_SELECTOR, 0);
6304 vmcs_write16(HOST_ES_SELECTOR, 0);
6305#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006306 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6307 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006308#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006309 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6310 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6311
Juergen Gross87930012017-09-04 12:25:27 +02006312 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006313 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006314 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006315
Avi Kivity83287ea422012-09-16 15:10:57 +03006316 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006317
6318 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6319 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6320 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6321 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6322
6323 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6324 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6325 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6326 }
6327}
6328
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006329static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6330{
6331 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6332 if (enable_ept)
6333 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006334 if (is_guest_mode(&vmx->vcpu))
6335 vmx->vcpu.arch.cr4_guest_owned_bits &=
6336 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006337 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6338}
6339
Yang Zhang01e439b2013-04-11 19:25:12 +08006340static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6341{
6342 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6343
Andrey Smetanind62caab2015-11-10 15:36:33 +03006344 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006345 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006346
6347 if (!enable_vnmi)
6348 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6349
Yunhong Jiang64672c92016-06-13 14:19:59 -07006350 /* Enable the preemption timer dynamically */
6351 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006352 return pin_based_exec_ctrl;
6353}
6354
Andrey Smetanind62caab2015-11-10 15:36:33 +03006355static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6356{
6357 struct vcpu_vmx *vmx = to_vmx(vcpu);
6358
6359 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006360 if (cpu_has_secondary_exec_ctrls()) {
6361 if (kvm_vcpu_apicv_active(vcpu))
6362 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6363 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6364 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6365 else
6366 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6367 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6368 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6369 }
6370
6371 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006372 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006373}
6374
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006375static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6376{
6377 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006378
6379 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6380 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6381
Paolo Bonzini35754c92015-07-29 12:05:37 +02006382 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006383 exec_control &= ~CPU_BASED_TPR_SHADOW;
6384#ifdef CONFIG_X86_64
6385 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6386 CPU_BASED_CR8_LOAD_EXITING;
6387#endif
6388 }
6389 if (!enable_ept)
6390 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6391 CPU_BASED_CR3_LOAD_EXITING |
6392 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006393 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6394 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6395 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006396 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6397 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006398 return exec_control;
6399}
6400
Jim Mattson45ec3682017-08-23 16:32:04 -07006401static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006402{
Jim Mattson45ec3682017-08-23 16:32:04 -07006403 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006404 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006405}
6406
Jim Mattson75f4fc82017-08-23 16:32:03 -07006407static bool vmx_rdseed_supported(void)
6408{
6409 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006410 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006411}
6412
Paolo Bonzini80154d72017-08-24 13:55:35 +02006413static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006414{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006415 struct kvm_vcpu *vcpu = &vmx->vcpu;
6416
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006417 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006418
Paolo Bonzini80154d72017-08-24 13:55:35 +02006419 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006420 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6421 if (vmx->vpid == 0)
6422 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6423 if (!enable_ept) {
6424 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6425 enable_unrestricted_guest = 0;
6426 }
6427 if (!enable_unrestricted_guest)
6428 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006429 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006430 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006431 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006432 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6433 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006434 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006435
6436 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6437 * in vmx_set_cr4. */
6438 exec_control &= ~SECONDARY_EXEC_DESC;
6439
Abel Gordonabc4fc52013-04-18 14:35:25 +03006440 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6441 (handle_vmptrld).
6442 We can NOT enable shadow_vmcs here because we don't have yet
6443 a current VMCS12
6444 */
6445 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006446
6447 if (!enable_pml)
6448 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006449
Paolo Bonzini3db13482017-08-24 14:48:03 +02006450 if (vmx_xsaves_supported()) {
6451 /* Exposing XSAVES only when XSAVE is exposed */
6452 bool xsaves_enabled =
6453 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6454 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6455
6456 if (!xsaves_enabled)
6457 exec_control &= ~SECONDARY_EXEC_XSAVES;
6458
6459 if (nested) {
6460 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006461 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006462 SECONDARY_EXEC_XSAVES;
6463 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006464 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006465 ~SECONDARY_EXEC_XSAVES;
6466 }
6467 }
6468
Paolo Bonzini80154d72017-08-24 13:55:35 +02006469 if (vmx_rdtscp_supported()) {
6470 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6471 if (!rdtscp_enabled)
6472 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6473
6474 if (nested) {
6475 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006476 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006477 SECONDARY_EXEC_RDTSCP;
6478 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006479 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006480 ~SECONDARY_EXEC_RDTSCP;
6481 }
6482 }
6483
6484 if (vmx_invpcid_supported()) {
6485 /* Exposing INVPCID only when PCID is exposed */
6486 bool invpcid_enabled =
6487 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6488 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6489
6490 if (!invpcid_enabled) {
6491 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6492 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6493 }
6494
6495 if (nested) {
6496 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006497 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006498 SECONDARY_EXEC_ENABLE_INVPCID;
6499 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006500 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006501 ~SECONDARY_EXEC_ENABLE_INVPCID;
6502 }
6503 }
6504
Jim Mattson45ec3682017-08-23 16:32:04 -07006505 if (vmx_rdrand_supported()) {
6506 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6507 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006508 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006509
6510 if (nested) {
6511 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006512 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006513 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006514 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006515 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006516 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006517 }
6518 }
6519
Jim Mattson75f4fc82017-08-23 16:32:03 -07006520 if (vmx_rdseed_supported()) {
6521 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6522 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006523 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006524
6525 if (nested) {
6526 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006527 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006528 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006529 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006530 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006531 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006532 }
6533 }
6534
Paolo Bonzini80154d72017-08-24 13:55:35 +02006535 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006536}
6537
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006538static void ept_set_mmio_spte_mask(void)
6539{
6540 /*
6541 * EPT Misconfigurations can be generated if the value of bits 2:0
6542 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006543 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006544 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6545 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006546}
6547
Wanpeng Lif53cd632014-12-02 19:14:58 +08006548#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006549/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006550 * Sets up the vmcs for emulated real mode.
6551 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006552static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006553{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006555
Abel Gordon4607c2d2013-04-18 14:35:55 +03006556 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006557 /*
6558 * At vCPU creation, "VMWRITE to any supported field
6559 * in the VMCS" is supported, so use the more
6560 * permissive vmx_vmread_bitmap to specify both read
6561 * and write permissions for the shadow VMCS.
6562 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006563 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006564 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006565 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006566 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006567 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006568
Avi Kivity6aa8b732006-12-10 02:21:36 -08006569 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6570
Avi Kivity6aa8b732006-12-10 02:21:36 -08006571 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006572 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006573 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006574
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006575 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006576
Dan Williamsdfa169b2016-06-02 11:17:24 -07006577 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006578 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006579 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006580 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006581 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006582
Andrey Smetanind62caab2015-11-10 15:36:33 +03006583 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006584 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6585 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6586 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6587 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6588
6589 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006590
Li RongQing0bcf2612015-12-03 13:29:34 +08006591 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006592 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006593 }
6594
Wanpeng Lib31c1142018-03-12 04:53:04 -07006595 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006596 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006597 vmx->ple_window = ple_window;
6598 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006599 }
6600
Xiao Guangrongc3707952011-07-12 03:28:04 +08006601 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6602 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006603 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6604
Avi Kivity9581d442010-10-19 16:46:55 +02006605 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6606 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006607 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006608 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6609 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006610
Bandan Das2a499e42017-08-03 15:54:41 -04006611 if (cpu_has_vmx_vmfunc())
6612 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6613
Eddie Dong2cc51562007-05-21 07:28:09 +03006614 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6615 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006616 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006617 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006618 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006619
Radim Krčmář74545702015-04-27 15:11:25 +02006620 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6621 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006622
Paolo Bonzini03916db2014-07-24 14:21:57 +02006623 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006624 u32 index = vmx_msr_index[i];
6625 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006626 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627
6628 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6629 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006630 if (wrmsr_safe(index, data_low, data_high) < 0)
6631 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006632 vmx->guest_msrs[j].index = i;
6633 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006634 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006635 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006636 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006638 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006639
6640 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641
6642 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006643 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006644
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006645 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6646 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6647
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006648 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006649
Wanpeng Lif53cd632014-12-02 19:14:58 +08006650 if (vmx_xsaves_supported())
6651 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6652
Peter Feiner4e595162016-07-07 14:49:58 -07006653 if (enable_pml) {
6654 ASSERT(vmx->pml_pg);
6655 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6656 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6657 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006658
6659 if (cpu_has_vmx_encls_vmexit())
6660 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006661}
6662
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006663static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006664{
6665 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006666 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006667 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006668
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006669 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006670 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006671
Wanpeng Li518e7b92018-02-28 14:03:31 +08006672 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006673 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006674 kvm_set_cr8(vcpu, 0);
6675
6676 if (!init_event) {
6677 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6678 MSR_IA32_APICBASE_ENABLE;
6679 if (kvm_vcpu_is_reset_bsp(vcpu))
6680 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6681 apic_base_msr.host_initiated = true;
6682 kvm_set_apic_base(vcpu, &apic_base_msr);
6683 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006684
Avi Kivity2fb92db2011-04-27 19:42:18 +03006685 vmx_segment_cache_clear(vmx);
6686
Avi Kivity5706be02008-08-20 15:07:31 +03006687 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006688 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006689 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006690
6691 seg_setup(VCPU_SREG_DS);
6692 seg_setup(VCPU_SREG_ES);
6693 seg_setup(VCPU_SREG_FS);
6694 seg_setup(VCPU_SREG_GS);
6695 seg_setup(VCPU_SREG_SS);
6696
6697 vmcs_write16(GUEST_TR_SELECTOR, 0);
6698 vmcs_writel(GUEST_TR_BASE, 0);
6699 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6700 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6701
6702 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6703 vmcs_writel(GUEST_LDTR_BASE, 0);
6704 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6705 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6706
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006707 if (!init_event) {
6708 vmcs_write32(GUEST_SYSENTER_CS, 0);
6709 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6710 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6711 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6712 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006713
Wanpeng Lic37c2872017-11-20 14:52:21 -08006714 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006715 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006716
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006717 vmcs_writel(GUEST_GDTR_BASE, 0);
6718 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6719
6720 vmcs_writel(GUEST_IDTR_BASE, 0);
6721 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6722
Anthony Liguori443381a2010-12-06 10:53:38 -06006723 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006724 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006725 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006726 if (kvm_mpx_supported())
6727 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006728
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729 setup_msrs(vmx);
6730
Avi Kivity6aa8b732006-12-10 02:21:36 -08006731 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6732
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006733 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006734 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006735 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006736 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006737 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006738 vmcs_write32(TPR_THRESHOLD, 0);
6739 }
6740
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006741 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006742
Sheng Yang2384d2b2008-01-17 15:14:33 +08006743 if (vmx->vpid != 0)
6744 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6745
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006746 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006747 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006748 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006749 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006750 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006751
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006752 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006754 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006755 if (init_event)
6756 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006757}
6758
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006759/*
6760 * In nested virtualization, check if L1 asked to exit on external interrupts.
6761 * For most existing hypervisors, this will always return true.
6762 */
6763static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6764{
6765 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6766 PIN_BASED_EXT_INTR_MASK;
6767}
6768
Bandan Das77b0f5d2014-04-19 18:17:45 -04006769/*
6770 * In nested virtualization, check if L1 has set
6771 * VM_EXIT_ACK_INTR_ON_EXIT
6772 */
6773static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6774{
6775 return get_vmcs12(vcpu)->vm_exit_controls &
6776 VM_EXIT_ACK_INTR_ON_EXIT;
6777}
6778
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006779static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6780{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006781 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006782}
6783
Jan Kiszkac9a79532014-03-07 20:03:15 +01006784static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006785{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006786 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6787 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006788}
6789
Jan Kiszkac9a79532014-03-07 20:03:15 +01006790static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006791{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006792 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006793 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006794 enable_irq_window(vcpu);
6795 return;
6796 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006797
Paolo Bonzini47c01522016-12-19 11:44:07 +01006798 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6799 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006800}
6801
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006802static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006803{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006804 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006805 uint32_t intr;
6806 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006807
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006808 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006809
Avi Kivityfa89a812008-09-01 15:57:51 +03006810 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006811 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006812 int inc_eip = 0;
6813 if (vcpu->arch.interrupt.soft)
6814 inc_eip = vcpu->arch.event_exit_inst_len;
6815 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006816 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006817 return;
6818 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006819 intr = irq | INTR_INFO_VALID_MASK;
6820 if (vcpu->arch.interrupt.soft) {
6821 intr |= INTR_TYPE_SOFT_INTR;
6822 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6823 vmx->vcpu.arch.event_exit_inst_len);
6824 } else
6825 intr |= INTR_TYPE_EXT_INTR;
6826 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006827
6828 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006829}
6830
Sheng Yangf08864b2008-05-15 18:23:25 +08006831static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6832{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006833 struct vcpu_vmx *vmx = to_vmx(vcpu);
6834
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006835 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006836 /*
6837 * Tracking the NMI-blocked state in software is built upon
6838 * finding the next open IRQ window. This, in turn, depends on
6839 * well-behaving guests: They have to keep IRQs disabled at
6840 * least as long as the NMI handler runs. Otherwise we may
6841 * cause NMI nesting, maybe breaking the guest. But as this is
6842 * highly unlikely, we can live with the residual risk.
6843 */
6844 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6845 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6846 }
6847
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006848 ++vcpu->stat.nmi_injections;
6849 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006850
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006851 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006852 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006853 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006854 return;
6855 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006856
Sheng Yangf08864b2008-05-15 18:23:25 +08006857 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6858 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006859
6860 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006861}
6862
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006863static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6864{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006865 struct vcpu_vmx *vmx = to_vmx(vcpu);
6866 bool masked;
6867
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006868 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006869 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006870 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006871 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006872 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6873 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6874 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006875}
6876
6877static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6878{
6879 struct vcpu_vmx *vmx = to_vmx(vcpu);
6880
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006881 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006882 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6883 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6884 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6885 }
6886 } else {
6887 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6888 if (masked)
6889 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6890 GUEST_INTR_STATE_NMI);
6891 else
6892 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6893 GUEST_INTR_STATE_NMI);
6894 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006895}
6896
Jan Kiszka2505dc92013-04-14 12:12:47 +02006897static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6898{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006899 if (to_vmx(vcpu)->nested.nested_run_pending)
6900 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006901
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006902 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006903 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6904 return 0;
6905
Jan Kiszka2505dc92013-04-14 12:12:47 +02006906 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6907 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6908 | GUEST_INTR_STATE_NMI));
6909}
6910
Gleb Natapov78646122009-03-23 12:12:11 +02006911static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6912{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006913 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6914 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006915 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6916 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006917}
6918
Izik Eiduscbc94022007-10-25 00:29:55 +02006919static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6920{
6921 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006922
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006923 if (enable_unrestricted_guest)
6924 return 0;
6925
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006926 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6927 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006928 if (ret)
6929 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006930 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006931 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006932}
6933
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006934static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6935{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006936 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006937 return 0;
6938}
6939
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006940static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006941{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006942 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006943 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006944 /*
6945 * Update instruction length as we may reinject the exception
6946 * from user space while in guest debugging mode.
6947 */
6948 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6949 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006950 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006951 return false;
6952 /* fall through */
6953 case DB_VECTOR:
6954 if (vcpu->guest_debug &
6955 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6956 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006957 /* fall through */
6958 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006959 case OF_VECTOR:
6960 case BR_VECTOR:
6961 case UD_VECTOR:
6962 case DF_VECTOR:
6963 case SS_VECTOR:
6964 case GP_VECTOR:
6965 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006966 return true;
6967 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006968 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006969 return false;
6970}
6971
6972static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6973 int vec, u32 err_code)
6974{
6975 /*
6976 * Instruction with address size override prefix opcode 0x67
6977 * Cause the #SS fault with 0 error code in VM86 mode.
6978 */
6979 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6980 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6981 if (vcpu->arch.halt_request) {
6982 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006983 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006984 }
6985 return 1;
6986 }
6987 return 0;
6988 }
6989
6990 /*
6991 * Forward all other exceptions that are valid in real mode.
6992 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6993 * the required debugging infrastructure rework.
6994 */
6995 kvm_queue_exception(vcpu, vec);
6996 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006997}
6998
Andi Kleena0861c02009-06-08 17:37:09 +08006999/*
7000 * Trigger machine check on the host. We assume all the MSRs are already set up
7001 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7002 * We pass a fake environment to the machine check handler because we want
7003 * the guest to be always treated like user space, no matter what context
7004 * it used internally.
7005 */
7006static void kvm_machine_check(void)
7007{
7008#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7009 struct pt_regs regs = {
7010 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7011 .flags = X86_EFLAGS_IF,
7012 };
7013
7014 do_machine_check(&regs, 0);
7015#endif
7016}
7017
Avi Kivity851ba692009-08-24 11:10:17 +03007018static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007019{
7020 /* already handled by vcpu_run */
7021 return 1;
7022}
7023
Avi Kivity851ba692009-08-24 11:10:17 +03007024static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007025{
Avi Kivity1155f762007-11-22 11:30:47 +02007026 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007027 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007028 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007029 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030 u32 vect_info;
7031 enum emulation_result er;
7032
Avi Kivity1155f762007-11-22 11:30:47 +02007033 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007034 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007035
Andi Kleena0861c02009-06-08 17:37:09 +08007036 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007037 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007038
Jim Mattsonef85b672016-12-12 11:01:37 -08007039 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007040 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007041
Wanpeng Li082d06e2018-04-03 16:28:48 -07007042 if (is_invalid_opcode(intr_info))
7043 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007044
Avi Kivity6aa8b732006-12-10 02:21:36 -08007045 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007046 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007047 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007048
Liran Alon9e869482018-03-12 13:12:51 +02007049 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7050 WARN_ON_ONCE(!enable_vmware_backdoor);
7051 er = emulate_instruction(vcpu,
7052 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7053 if (er == EMULATE_USER_EXIT)
7054 return 0;
7055 else if (er != EMULATE_DONE)
7056 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7057 return 1;
7058 }
7059
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007060 /*
7061 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7062 * MMIO, it is better to report an internal error.
7063 * See the comments in vmx_handle_exit.
7064 */
7065 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7066 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7067 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7068 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007069 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007070 vcpu->run->internal.data[0] = vect_info;
7071 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007072 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007073 return 0;
7074 }
7075
Avi Kivity6aa8b732006-12-10 02:21:36 -08007076 if (is_page_fault(intr_info)) {
7077 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007078 /* EPT won't cause page fault directly */
7079 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007080 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007081 }
7082
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007083 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007084
7085 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7086 return handle_rmode_exception(vcpu, ex_no, error_code);
7087
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007088 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007089 case AC_VECTOR:
7090 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7091 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007092 case DB_VECTOR:
7093 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7094 if (!(vcpu->guest_debug &
7095 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007096 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007097 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007098 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007099 skip_emulated_instruction(vcpu);
7100
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007101 kvm_queue_exception(vcpu, DB_VECTOR);
7102 return 1;
7103 }
7104 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7105 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7106 /* fall through */
7107 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007108 /*
7109 * Update instruction length as we may reinject #BP from
7110 * user space while in guest debugging mode. Reading it for
7111 * #DB as well causes no harm, it is not used in that case.
7112 */
7113 vmx->vcpu.arch.event_exit_inst_len =
7114 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007116 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007117 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7118 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007119 break;
7120 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007121 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7122 kvm_run->ex.exception = ex_no;
7123 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007124 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007125 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007126 return 0;
7127}
7128
Avi Kivity851ba692009-08-24 11:10:17 +03007129static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007131 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007132 return 1;
7133}
7134
Avi Kivity851ba692009-08-24 11:10:17 +03007135static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007136{
Avi Kivity851ba692009-08-24 11:10:17 +03007137 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007138 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007139 return 0;
7140}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007141
Avi Kivity851ba692009-08-24 11:10:17 +03007142static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143{
He, Qingbfdaab02007-09-12 14:18:28 +08007144 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007145 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007146 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147
He, Qingbfdaab02007-09-12 14:18:28 +08007148 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007149 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007150
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007151 ++vcpu->stat.io_exits;
7152
Sean Christopherson432baf62018-03-08 08:57:26 -08007153 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01007154 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007155
7156 port = exit_qualification >> 16;
7157 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007158 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007159
Sean Christophersondca7f122018-03-08 08:57:27 -08007160 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007161}
7162
Ingo Molnar102d8322007-02-19 14:37:47 +02007163static void
7164vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7165{
7166 /*
7167 * Patch in the VMCALL instruction:
7168 */
7169 hypercall[0] = 0x0f;
7170 hypercall[1] = 0x01;
7171 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007172}
7173
Guo Chao0fa06072012-06-28 15:16:19 +08007174/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007175static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7176{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007177 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007178 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7179 unsigned long orig_val = val;
7180
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007181 /*
7182 * We get here when L2 changed cr0 in a way that did not change
7183 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007184 * but did change L0 shadowed bits. So we first calculate the
7185 * effective cr0 value that L1 would like to write into the
7186 * hardware. It consists of the L2-owned bits from the new
7187 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007188 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007189 val = (val & ~vmcs12->cr0_guest_host_mask) |
7190 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7191
David Matlack38991522016-11-29 18:14:08 -08007192 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007193 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007194
7195 if (kvm_set_cr0(vcpu, val))
7196 return 1;
7197 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007198 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007199 } else {
7200 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007201 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007202 return 1;
David Matlack38991522016-11-29 18:14:08 -08007203
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007204 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007205 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007206}
7207
7208static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7209{
7210 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7212 unsigned long orig_val = val;
7213
7214 /* analogously to handle_set_cr0 */
7215 val = (val & ~vmcs12->cr4_guest_host_mask) |
7216 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7217 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007218 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007219 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007220 return 0;
7221 } else
7222 return kvm_set_cr4(vcpu, val);
7223}
7224
Paolo Bonzini0367f202016-07-12 10:44:55 +02007225static int handle_desc(struct kvm_vcpu *vcpu)
7226{
7227 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7228 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7229}
7230
Avi Kivity851ba692009-08-24 11:10:17 +03007231static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007232{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007233 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007234 int cr;
7235 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007236 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007237 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007238
He, Qingbfdaab02007-09-12 14:18:28 +08007239 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 cr = exit_qualification & 15;
7241 reg = (exit_qualification >> 8) & 15;
7242 switch ((exit_qualification >> 4) & 3) {
7243 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007244 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007245 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246 switch (cr) {
7247 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007248 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007249 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007250 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007251 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007252 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007253 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007254 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007255 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007256 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007257 case 8: {
7258 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007259 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007260 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007261 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007262 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007263 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007264 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007265 return ret;
7266 /*
7267 * TODO: we might be squashing a
7268 * KVM_GUESTDBG_SINGLESTEP-triggered
7269 * KVM_EXIT_DEBUG here.
7270 */
Avi Kivity851ba692009-08-24 11:10:17 +03007271 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007272 return 0;
7273 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007274 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007275 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007276 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007277 WARN_ONCE(1, "Guest should always own CR0.TS");
7278 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007279 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007280 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007281 case 1: /*mov from cr*/
7282 switch (cr) {
7283 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007284 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007285 val = kvm_read_cr3(vcpu);
7286 kvm_register_write(vcpu, reg, val);
7287 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007288 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007289 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007290 val = kvm_get_cr8(vcpu);
7291 kvm_register_write(vcpu, reg, val);
7292 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007293 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007294 }
7295 break;
7296 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007297 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007298 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007299 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007300
Kyle Huey6affcbe2016-11-29 12:40:40 -08007301 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007302 default:
7303 break;
7304 }
Avi Kivity851ba692009-08-24 11:10:17 +03007305 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007306 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007307 (int)(exit_qualification >> 4) & 3, cr);
7308 return 0;
7309}
7310
Avi Kivity851ba692009-08-24 11:10:17 +03007311static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312{
He, Qingbfdaab02007-09-12 14:18:28 +08007313 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007314 int dr, dr7, reg;
7315
7316 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7317 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7318
7319 /* First, if DR does not exist, trigger UD */
7320 if (!kvm_require_dr(vcpu, dr))
7321 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322
Jan Kiszkaf2483412010-01-20 18:20:20 +01007323 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007324 if (!kvm_require_cpl(vcpu, 0))
7325 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007326 dr7 = vmcs_readl(GUEST_DR7);
7327 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007328 /*
7329 * As the vm-exit takes precedence over the debug trap, we
7330 * need to emulate the latter, either for the host or the
7331 * guest debugging itself.
7332 */
7333 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007334 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007335 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007336 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007337 vcpu->run->debug.arch.exception = DB_VECTOR;
7338 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007339 return 0;
7340 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007341 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007342 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007343 kvm_queue_exception(vcpu, DB_VECTOR);
7344 return 1;
7345 }
7346 }
7347
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007348 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007349 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7350 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007351
7352 /*
7353 * No more DR vmexits; force a reload of the debug registers
7354 * and reenter on this instruction. The next vmexit will
7355 * retrieve the full state of the debug registers.
7356 */
7357 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7358 return 1;
7359 }
7360
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007361 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7362 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007363 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007364
7365 if (kvm_get_dr(vcpu, dr, &val))
7366 return 1;
7367 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007368 } else
Nadav Amit57773922014-06-18 17:19:23 +03007369 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007370 return 1;
7371
Kyle Huey6affcbe2016-11-29 12:40:40 -08007372 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007373}
7374
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007375static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7376{
7377 return vcpu->arch.dr6;
7378}
7379
7380static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7381{
7382}
7383
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007384static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7385{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007386 get_debugreg(vcpu->arch.db[0], 0);
7387 get_debugreg(vcpu->arch.db[1], 1);
7388 get_debugreg(vcpu->arch.db[2], 2);
7389 get_debugreg(vcpu->arch.db[3], 3);
7390 get_debugreg(vcpu->arch.dr6, 6);
7391 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7392
7393 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007394 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007395}
7396
Gleb Natapov020df072010-04-13 10:05:23 +03007397static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7398{
7399 vmcs_writel(GUEST_DR7, val);
7400}
7401
Avi Kivity851ba692009-08-24 11:10:17 +03007402static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007403{
Kyle Huey6a908b62016-11-29 12:40:37 -08007404 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007405}
7406
Avi Kivity851ba692009-08-24 11:10:17 +03007407static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007408{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007409 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007410 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007411
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007412 msr_info.index = ecx;
7413 msr_info.host_initiated = false;
7414 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007415 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007416 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007417 return 1;
7418 }
7419
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007420 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007421
Avi Kivity6aa8b732006-12-10 02:21:36 -08007422 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007423 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7424 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007425 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007426}
7427
Avi Kivity851ba692009-08-24 11:10:17 +03007428static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007429{
Will Auld8fe8ab42012-11-29 12:42:12 -08007430 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007431 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7432 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7433 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007434
Will Auld8fe8ab42012-11-29 12:42:12 -08007435 msr.data = data;
7436 msr.index = ecx;
7437 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007438 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007439 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007440 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007441 return 1;
7442 }
7443
Avi Kivity59200272010-01-25 19:47:02 +02007444 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007445 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007446}
7447
Avi Kivity851ba692009-08-24 11:10:17 +03007448static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007449{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007450 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007451 return 1;
7452}
7453
Avi Kivity851ba692009-08-24 11:10:17 +03007454static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007456 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7457 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007458
Avi Kivity3842d132010-07-27 12:30:24 +03007459 kvm_make_request(KVM_REQ_EVENT, vcpu);
7460
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007461 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007462 return 1;
7463}
7464
Avi Kivity851ba692009-08-24 11:10:17 +03007465static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007466{
Avi Kivityd3bef152007-06-05 15:53:05 +03007467 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007468}
7469
Avi Kivity851ba692009-08-24 11:10:17 +03007470static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007471{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007472 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007473}
7474
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007475static int handle_invd(struct kvm_vcpu *vcpu)
7476{
Andre Przywara51d8b662010-12-21 11:12:02 +01007477 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007478}
7479
Avi Kivity851ba692009-08-24 11:10:17 +03007480static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007481{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007482 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007483
7484 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007485 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007486}
7487
Avi Kivityfee84b02011-11-10 14:57:25 +02007488static int handle_rdpmc(struct kvm_vcpu *vcpu)
7489{
7490 int err;
7491
7492 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007493 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007494}
7495
Avi Kivity851ba692009-08-24 11:10:17 +03007496static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007497{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007498 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007499}
7500
Dexuan Cui2acf9232010-06-10 11:27:12 +08007501static int handle_xsetbv(struct kvm_vcpu *vcpu)
7502{
7503 u64 new_bv = kvm_read_edx_eax(vcpu);
7504 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7505
7506 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007507 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007508 return 1;
7509}
7510
Wanpeng Lif53cd632014-12-02 19:14:58 +08007511static int handle_xsaves(struct kvm_vcpu *vcpu)
7512{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007513 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007514 WARN(1, "this should never happen\n");
7515 return 1;
7516}
7517
7518static int handle_xrstors(struct kvm_vcpu *vcpu)
7519{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007520 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007521 WARN(1, "this should never happen\n");
7522 return 1;
7523}
7524
Avi Kivity851ba692009-08-24 11:10:17 +03007525static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007526{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007527 if (likely(fasteoi)) {
7528 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7529 int access_type, offset;
7530
7531 access_type = exit_qualification & APIC_ACCESS_TYPE;
7532 offset = exit_qualification & APIC_ACCESS_OFFSET;
7533 /*
7534 * Sane guest uses MOV to write EOI, with written value
7535 * not cared. So make a short-circuit here by avoiding
7536 * heavy instruction emulation.
7537 */
7538 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7539 (offset == APIC_EOI)) {
7540 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007541 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007542 }
7543 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007544 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007545}
7546
Yang Zhangc7c9c562013-01-25 10:18:51 +08007547static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7548{
7549 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7550 int vector = exit_qualification & 0xff;
7551
7552 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7553 kvm_apic_set_eoi_accelerated(vcpu, vector);
7554 return 1;
7555}
7556
Yang Zhang83d4c282013-01-25 10:18:49 +08007557static int handle_apic_write(struct kvm_vcpu *vcpu)
7558{
7559 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7560 u32 offset = exit_qualification & 0xfff;
7561
7562 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7563 kvm_apic_write_nodecode(vcpu, offset);
7564 return 1;
7565}
7566
Avi Kivity851ba692009-08-24 11:10:17 +03007567static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007568{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007569 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007570 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007571 bool has_error_code = false;
7572 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007573 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007574 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007575
7576 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007577 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007578 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007579
7580 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7581
7582 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007583 if (reason == TASK_SWITCH_GATE && idt_v) {
7584 switch (type) {
7585 case INTR_TYPE_NMI_INTR:
7586 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007587 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007588 break;
7589 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007590 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007591 kvm_clear_interrupt_queue(vcpu);
7592 break;
7593 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007594 if (vmx->idt_vectoring_info &
7595 VECTORING_INFO_DELIVER_CODE_MASK) {
7596 has_error_code = true;
7597 error_code =
7598 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7599 }
7600 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007601 case INTR_TYPE_SOFT_EXCEPTION:
7602 kvm_clear_exception_queue(vcpu);
7603 break;
7604 default:
7605 break;
7606 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007607 }
Izik Eidus37817f22008-03-24 23:14:53 +02007608 tss_selector = exit_qualification;
7609
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007610 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7611 type != INTR_TYPE_EXT_INTR &&
7612 type != INTR_TYPE_NMI_INTR))
7613 skip_emulated_instruction(vcpu);
7614
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007615 if (kvm_task_switch(vcpu, tss_selector,
7616 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7617 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007618 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7619 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7620 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007621 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007622 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007623
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007624 /*
7625 * TODO: What about debug traps on tss switch?
7626 * Are we supposed to inject them and update dr6?
7627 */
7628
7629 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007630}
7631
Avi Kivity851ba692009-08-24 11:10:17 +03007632static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007633{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007634 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007635 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007636 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007637
Sheng Yangf9c617f2009-03-25 10:08:52 +08007638 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007639
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007640 /*
7641 * EPT violation happened while executing iret from NMI,
7642 * "blocked by NMI" bit has to be set before next VM entry.
7643 * There are errata that may cause this bit to not be set:
7644 * AAK134, BY25.
7645 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007646 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007647 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007648 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007649 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7650
Sheng Yang14394422008-04-28 12:24:45 +08007651 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007652 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007653
Junaid Shahid27959a42016-12-06 16:46:10 -08007654 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007655 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007656 ? PFERR_USER_MASK : 0;
7657 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007658 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007659 ? PFERR_WRITE_MASK : 0;
7660 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007661 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007662 ? PFERR_FETCH_MASK : 0;
7663 /* ept page table entry is present? */
7664 error_code |= (exit_qualification &
7665 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7666 EPT_VIOLATION_EXECUTABLE))
7667 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007668
Paolo Bonzinieebed242016-11-28 14:39:58 +01007669 error_code |= (exit_qualification & 0x100) != 0 ?
7670 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007671
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007672 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007673 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007674}
7675
Avi Kivity851ba692009-08-24 11:10:17 +03007676static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007677{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007678 gpa_t gpa;
7679
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007680 /*
7681 * A nested guest cannot optimize MMIO vmexits, because we have an
7682 * nGPA here instead of the required GPA.
7683 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007684 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007685 if (!is_guest_mode(vcpu) &&
7686 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007687 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007688 /*
7689 * Doing kvm_skip_emulated_instruction() depends on undefined
7690 * behavior: Intel's manual doesn't mandate
7691 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7692 * occurs and while on real hardware it was observed to be set,
7693 * other hypervisors (namely Hyper-V) don't set it, we end up
7694 * advancing IP with some random value. Disable fast mmio when
7695 * running nested and keep it for real hardware in hope that
7696 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7697 */
7698 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7699 return kvm_skip_emulated_instruction(vcpu);
7700 else
7701 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7702 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007703 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007704
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007705 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007706}
7707
Avi Kivity851ba692009-08-24 11:10:17 +03007708static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007709{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007710 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007711 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7712 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007713 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007714 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007715
7716 return 1;
7717}
7718
Mohammed Gamal80ced182009-09-01 12:48:18 +02007719static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007720{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007721 struct vcpu_vmx *vmx = to_vmx(vcpu);
7722 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007723 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007724 u32 cpu_exec_ctrl;
7725 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007726 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007727
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007728 /*
7729 * We should never reach the point where we are emulating L2
7730 * due to invalid guest state as that means we incorrectly
7731 * allowed a nested VMEntry with an invalid vmcs12.
7732 */
7733 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7734
Avi Kivity49e9d552010-09-19 14:34:08 +02007735 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7736 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007737
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007738 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007739 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007740 return handle_interrupt_window(&vmx->vcpu);
7741
Radim Krčmář72875d82017-04-26 22:32:19 +02007742 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007743 return 1;
7744
Liran Alon9b8ae632017-11-05 16:56:34 +02007745 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007746
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007747 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007748 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007749 ret = 0;
7750 goto out;
7751 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007752
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007753 if (err != EMULATE_DONE)
7754 goto emulation_error;
7755
7756 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7757 vcpu->arch.exception.pending)
7758 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007759
Gleb Natapov8d76c492013-05-08 18:38:44 +03007760 if (vcpu->arch.halt_request) {
7761 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007762 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007763 goto out;
7764 }
7765
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007766 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007767 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007768 if (need_resched())
7769 schedule();
7770 }
7771
Mohammed Gamal80ced182009-09-01 12:48:18 +02007772out:
7773 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007774
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007775emulation_error:
7776 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7777 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7778 vcpu->run->internal.ndata = 0;
7779 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007780}
7781
7782static void grow_ple_window(struct kvm_vcpu *vcpu)
7783{
7784 struct vcpu_vmx *vmx = to_vmx(vcpu);
7785 int old = vmx->ple_window;
7786
Babu Mogerc8e88712018-03-16 16:37:24 -04007787 vmx->ple_window = __grow_ple_window(old, ple_window,
7788 ple_window_grow,
7789 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007790
7791 if (vmx->ple_window != old)
7792 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007793
7794 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007795}
7796
7797static void shrink_ple_window(struct kvm_vcpu *vcpu)
7798{
7799 struct vcpu_vmx *vmx = to_vmx(vcpu);
7800 int old = vmx->ple_window;
7801
Babu Mogerc8e88712018-03-16 16:37:24 -04007802 vmx->ple_window = __shrink_ple_window(old, ple_window,
7803 ple_window_shrink,
7804 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007805
7806 if (vmx->ple_window != old)
7807 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007808
7809 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007810}
7811
7812/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007813 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7814 */
7815static void wakeup_handler(void)
7816{
7817 struct kvm_vcpu *vcpu;
7818 int cpu = smp_processor_id();
7819
7820 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7821 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7822 blocked_vcpu_list) {
7823 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7824
7825 if (pi_test_on(pi_desc) == 1)
7826 kvm_vcpu_kick(vcpu);
7827 }
7828 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7829}
7830
Peng Haoe01bca22018-04-07 05:47:32 +08007831static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007832{
7833 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7834 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7835 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7836 0ull, VMX_EPT_EXECUTABLE_MASK,
7837 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007838 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007839
7840 ept_set_mmio_spte_mask();
7841 kvm_enable_tdp();
7842}
7843
Tiejun Chenf2c76482014-10-28 10:14:47 +08007844static __init int hardware_setup(void)
7845{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007846 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007847 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007848
7849 rdmsrl_safe(MSR_EFER, &host_efer);
7850
7851 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7852 kvm_define_shared_msr(i, vmx_msr_index[i]);
7853
Radim Krčmář23611332016-09-29 22:41:33 +02007854 for (i = 0; i < VMX_BITMAP_NR; i++) {
7855 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7856 if (!vmx_bitmap[i])
7857 goto out;
7858 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007859
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007860 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7861 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7862
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007863 if (setup_vmcs_config(&vmcs_config) < 0) {
7864 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007865 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007866 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007867
7868 if (boot_cpu_has(X86_FEATURE_NX))
7869 kvm_enable_efer_bits(EFER_NX);
7870
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007871 if (boot_cpu_has(X86_FEATURE_MPX)) {
7872 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7873 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7874 }
7875
Wanpeng Li08d839c2017-03-23 05:30:08 -07007876 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7877 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007878 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007879
Tiejun Chenf2c76482014-10-28 10:14:47 +08007880 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007881 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007882 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007883 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007884 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007885
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007886 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007887 enable_ept_ad_bits = 0;
7888
Wanpeng Li8ad81822017-10-09 15:51:53 -07007889 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007890 enable_unrestricted_guest = 0;
7891
Paolo Bonziniad15a292015-01-30 16:18:49 +01007892 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007893 flexpriority_enabled = 0;
7894
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007895 if (!cpu_has_virtual_nmis())
7896 enable_vnmi = 0;
7897
Paolo Bonziniad15a292015-01-30 16:18:49 +01007898 /*
7899 * set_apic_access_page_addr() is used to reload apic access
7900 * page upon invalidation. No need to do anything if not
7901 * using the APIC_ACCESS_ADDR VMCS field.
7902 */
7903 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007904 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007905
7906 if (!cpu_has_vmx_tpr_shadow())
7907 kvm_x86_ops->update_cr8_intercept = NULL;
7908
7909 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7910 kvm_disable_largepages();
7911
Tianyu Lan877ad952018-07-19 08:40:23 +00007912#if IS_ENABLED(CONFIG_HYPERV)
7913 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7914 && enable_ept)
7915 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7916#endif
7917
Wanpeng Li0f107682017-09-28 18:06:24 -07007918 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007919 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007920 ple_window = 0;
7921 ple_window_grow = 0;
7922 ple_window_max = 0;
7923 ple_window_shrink = 0;
7924 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007925
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007926 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007927 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007928 kvm_x86_ops->sync_pir_to_irr = NULL;
7929 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007930
Haozhong Zhang64903d62015-10-20 15:39:09 +08007931 if (cpu_has_vmx_tsc_scaling()) {
7932 kvm_has_tsc_control = true;
7933 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7934 kvm_tsc_scaling_ratio_frac_bits = 48;
7935 }
7936
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007937 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7938
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007939 if (enable_ept)
7940 vmx_enable_tdp();
7941 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007942 kvm_disable_tdp();
7943
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007944 if (!nested) {
7945 kvm_x86_ops->get_nested_state = NULL;
7946 kvm_x86_ops->set_nested_state = NULL;
7947 }
7948
Kai Huang843e4332015-01-28 10:54:28 +08007949 /*
7950 * Only enable PML when hardware supports PML feature, and both EPT
7951 * and EPT A/D bit features are enabled -- PML depends on them to work.
7952 */
7953 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7954 enable_pml = 0;
7955
7956 if (!enable_pml) {
7957 kvm_x86_ops->slot_enable_log_dirty = NULL;
7958 kvm_x86_ops->slot_disable_log_dirty = NULL;
7959 kvm_x86_ops->flush_log_dirty = NULL;
7960 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7961 }
7962
Yunhong Jiang64672c92016-06-13 14:19:59 -07007963 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7964 u64 vmx_msr;
7965
7966 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7967 cpu_preemption_timer_multi =
7968 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7969 } else {
7970 kvm_x86_ops->set_hv_timer = NULL;
7971 kvm_x86_ops->cancel_hv_timer = NULL;
7972 }
7973
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007974 if (!cpu_has_vmx_shadow_vmcs())
7975 enable_shadow_vmcs = 0;
7976 if (enable_shadow_vmcs)
7977 init_vmcs_shadow_fields();
7978
Feng Wubf9f6ac2015-09-18 22:29:55 +08007979 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007980 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007981
Ashok Rajc45dcc72016-06-22 14:59:56 +08007982 kvm_mce_cap_supported |= MCG_LMCE_P;
7983
Tiejun Chenf2c76482014-10-28 10:14:47 +08007984 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007985
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007986out:
Radim Krčmář23611332016-09-29 22:41:33 +02007987 for (i = 0; i < VMX_BITMAP_NR; i++)
7988 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007989
7990 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007991}
7992
7993static __exit void hardware_unsetup(void)
7994{
Radim Krčmář23611332016-09-29 22:41:33 +02007995 int i;
7996
7997 for (i = 0; i < VMX_BITMAP_NR; i++)
7998 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007999
Tiejun Chenf2c76482014-10-28 10:14:47 +08008000 free_kvm_area();
8001}
8002
Avi Kivity6aa8b732006-12-10 02:21:36 -08008003/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008004 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8005 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8006 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008007static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008008{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008009 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008010 grow_ple_window(vcpu);
8011
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008012 /*
8013 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8014 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8015 * never set PAUSE_EXITING and just set PLE if supported,
8016 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8017 */
8018 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008019 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008020}
8021
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008022static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008023{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008024 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008025}
8026
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008027static int handle_mwait(struct kvm_vcpu *vcpu)
8028{
8029 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8030 return handle_nop(vcpu);
8031}
8032
Jim Mattson45ec3682017-08-23 16:32:04 -07008033static int handle_invalid_op(struct kvm_vcpu *vcpu)
8034{
8035 kvm_queue_exception(vcpu, UD_VECTOR);
8036 return 1;
8037}
8038
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008039static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8040{
8041 return 1;
8042}
8043
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008044static int handle_monitor(struct kvm_vcpu *vcpu)
8045{
8046 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8047 return handle_nop(vcpu);
8048}
8049
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008050/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008051 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8052 * set the success or error code of an emulated VMX instruction, as specified
8053 * by Vol 2B, VMX Instruction Reference, "Conventions".
8054 */
8055static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8056{
8057 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8058 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8059 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8060}
8061
8062static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8063{
8064 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8065 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8066 X86_EFLAGS_SF | X86_EFLAGS_OF))
8067 | X86_EFLAGS_CF);
8068}
8069
Abel Gordon145c28d2013-04-18 14:36:55 +03008070static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008071 u32 vm_instruction_error)
8072{
8073 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8074 /*
8075 * failValid writes the error number to the current VMCS, which
8076 * can't be done there isn't a current VMCS.
8077 */
8078 nested_vmx_failInvalid(vcpu);
8079 return;
8080 }
8081 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8082 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8083 X86_EFLAGS_SF | X86_EFLAGS_OF))
8084 | X86_EFLAGS_ZF);
8085 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8086 /*
8087 * We don't need to force a shadow sync because
8088 * VM_INSTRUCTION_ERROR is not shadowed
8089 */
8090}
Abel Gordon145c28d2013-04-18 14:36:55 +03008091
Wincy Vanff651cb2014-12-11 08:52:58 +03008092static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8093{
8094 /* TODO: not to reset guest simply here. */
8095 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008096 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008097}
8098
Jan Kiszkaf4124502014-03-07 20:03:13 +01008099static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8100{
8101 struct vcpu_vmx *vmx =
8102 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8103
8104 vmx->nested.preemption_timer_expired = true;
8105 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8106 kvm_vcpu_kick(&vmx->vcpu);
8107
8108 return HRTIMER_NORESTART;
8109}
8110
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008111/*
Bandan Das19677e32014-05-06 02:19:15 -04008112 * Decode the memory-address operand of a vmx instruction, as recorded on an
8113 * exit caused by such an instruction (run by a guest hypervisor).
8114 * On success, returns 0. When the operand is invalid, returns 1 and throws
8115 * #UD or #GP.
8116 */
8117static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8118 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008119 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008120{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008121 gva_t off;
8122 bool exn;
8123 struct kvm_segment s;
8124
Bandan Das19677e32014-05-06 02:19:15 -04008125 /*
8126 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8127 * Execution", on an exit, vmx_instruction_info holds most of the
8128 * addressing components of the operand. Only the displacement part
8129 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8130 * For how an actual address is calculated from all these components,
8131 * refer to Vol. 1, "Operand Addressing".
8132 */
8133 int scaling = vmx_instruction_info & 3;
8134 int addr_size = (vmx_instruction_info >> 7) & 7;
8135 bool is_reg = vmx_instruction_info & (1u << 10);
8136 int seg_reg = (vmx_instruction_info >> 15) & 7;
8137 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8138 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8139 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8140 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8141
8142 if (is_reg) {
8143 kvm_queue_exception(vcpu, UD_VECTOR);
8144 return 1;
8145 }
8146
8147 /* Addr = segment_base + offset */
8148 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008149 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008150 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008151 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008152 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008153 off += kvm_register_read(vcpu, index_reg)<<scaling;
8154 vmx_get_segment(vcpu, &s, seg_reg);
8155 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008156
8157 if (addr_size == 1) /* 32 bit */
8158 *ret &= 0xffffffff;
8159
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008160 /* Checks for #GP/#SS exceptions. */
8161 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008162 if (is_long_mode(vcpu)) {
8163 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8164 * non-canonical form. This is the only check on the memory
8165 * destination for long mode!
8166 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008167 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008168 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008169 /* Protected mode: apply checks for segment validity in the
8170 * following order:
8171 * - segment type check (#GP(0) may be thrown)
8172 * - usability check (#GP(0)/#SS(0))
8173 * - limit check (#GP(0)/#SS(0))
8174 */
8175 if (wr)
8176 /* #GP(0) if the destination operand is located in a
8177 * read-only data segment or any code segment.
8178 */
8179 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8180 else
8181 /* #GP(0) if the source operand is located in an
8182 * execute-only code segment
8183 */
8184 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008185 if (exn) {
8186 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8187 return 1;
8188 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008189 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8190 */
8191 exn = (s.unusable != 0);
8192 /* Protected mode: #GP(0)/#SS(0) if the memory
8193 * operand is outside the segment limit.
8194 */
8195 exn = exn || (off + sizeof(u64) > s.limit);
8196 }
8197 if (exn) {
8198 kvm_queue_exception_e(vcpu,
8199 seg_reg == VCPU_SREG_SS ?
8200 SS_VECTOR : GP_VECTOR,
8201 0);
8202 return 1;
8203 }
8204
Bandan Das19677e32014-05-06 02:19:15 -04008205 return 0;
8206}
8207
Radim Krčmářcbf71272017-05-19 15:48:51 +02008208static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008209{
8210 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008211 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008212
8213 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008214 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008215 return 1;
8216
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008217 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008218 kvm_inject_page_fault(vcpu, &e);
8219 return 1;
8220 }
8221
Bandan Das3573e222014-05-06 02:19:16 -04008222 return 0;
8223}
8224
Liran Alonabfc52c2018-06-23 02:35:13 +03008225/*
8226 * Allocate a shadow VMCS and associate it with the currently loaded
8227 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8228 * VMCS is also VMCLEARed, so that it is ready for use.
8229 */
8230static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8231{
8232 struct vcpu_vmx *vmx = to_vmx(vcpu);
8233 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8234
8235 /*
8236 * We should allocate a shadow vmcs for vmcs01 only when L1
8237 * executes VMXON and free it when L1 executes VMXOFF.
8238 * As it is invalid to execute VMXON twice, we shouldn't reach
8239 * here when vmcs01 already have an allocated shadow vmcs.
8240 */
8241 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8242
8243 if (!loaded_vmcs->shadow_vmcs) {
8244 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8245 if (loaded_vmcs->shadow_vmcs)
8246 vmcs_clear(loaded_vmcs->shadow_vmcs);
8247 }
8248 return loaded_vmcs->shadow_vmcs;
8249}
8250
Jim Mattsone29acc52016-11-30 12:03:43 -08008251static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8252{
8253 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008254 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008255
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008256 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8257 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008258 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008259
8260 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8261 if (!vmx->nested.cached_vmcs12)
8262 goto out_cached_vmcs12;
8263
Liran Alon61ada742018-06-23 02:35:08 +03008264 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8265 if (!vmx->nested.cached_shadow_vmcs12)
8266 goto out_cached_shadow_vmcs12;
8267
Liran Alonabfc52c2018-06-23 02:35:13 +03008268 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8269 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008270
Jim Mattsone29acc52016-11-30 12:03:43 -08008271 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8272 HRTIMER_MODE_REL_PINNED);
8273 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8274
Roman Kagan63aff652018-07-19 21:59:07 +03008275 vmx->nested.vpid02 = allocate_vpid();
8276
Jim Mattsone29acc52016-11-30 12:03:43 -08008277 vmx->nested.vmxon = true;
8278 return 0;
8279
8280out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008281 kfree(vmx->nested.cached_shadow_vmcs12);
8282
8283out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008284 kfree(vmx->nested.cached_vmcs12);
8285
8286out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008287 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008288
Jim Mattsonde3a0022017-11-27 17:22:25 -06008289out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008290 return -ENOMEM;
8291}
8292
Bandan Das3573e222014-05-06 02:19:16 -04008293/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008294 * Emulate the VMXON instruction.
8295 * Currently, we just remember that VMX is active, and do not save or even
8296 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8297 * do not currently need to store anything in that guest-allocated memory
8298 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8299 * argument is different from the VMXON pointer (which the spec says they do).
8300 */
8301static int handle_vmon(struct kvm_vcpu *vcpu)
8302{
Jim Mattsone29acc52016-11-30 12:03:43 -08008303 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008304 gpa_t vmptr;
8305 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008306 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008307 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8308 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008309
Jim Mattson70f3aac2017-04-26 08:53:46 -07008310 /*
8311 * The Intel VMX Instruction Reference lists a bunch of bits that are
8312 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8313 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8314 * Otherwise, we should fail with #UD. But most faulting conditions
8315 * have already been checked by hardware, prior to the VM-exit for
8316 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8317 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008318 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008319 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008320 kvm_queue_exception(vcpu, UD_VECTOR);
8321 return 1;
8322 }
8323
Felix Wilhelm727ba742018-06-11 09:43:44 +02008324 /* CPL=0 must be checked manually. */
8325 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008326 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008327 return 1;
8328 }
8329
Abel Gordon145c28d2013-04-18 14:36:55 +03008330 if (vmx->nested.vmxon) {
8331 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008332 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008333 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008334
Haozhong Zhang3b840802016-06-22 14:59:54 +08008335 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008336 != VMXON_NEEDED_FEATURES) {
8337 kvm_inject_gp(vcpu, 0);
8338 return 1;
8339 }
8340
Radim Krčmářcbf71272017-05-19 15:48:51 +02008341 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008342 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008343
8344 /*
8345 * SDM 3: 24.11.5
8346 * The first 4 bytes of VMXON region contain the supported
8347 * VMCS revision identifier
8348 *
8349 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8350 * which replaces physical address width with 32
8351 */
8352 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8353 nested_vmx_failInvalid(vcpu);
8354 return kvm_skip_emulated_instruction(vcpu);
8355 }
8356
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008357 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8358 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008359 nested_vmx_failInvalid(vcpu);
8360 return kvm_skip_emulated_instruction(vcpu);
8361 }
8362 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8363 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008364 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008365 nested_vmx_failInvalid(vcpu);
8366 return kvm_skip_emulated_instruction(vcpu);
8367 }
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
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008376 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008377 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008378}
8379
8380/*
8381 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8382 * for running VMX instructions (except VMXON, whose prerequisites are
8383 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008384 * Note that many of these exceptions have priority over VM exits, so they
8385 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008386 */
8387static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8388{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008389 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008390 kvm_queue_exception(vcpu, UD_VECTOR);
8391 return 0;
8392 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008393
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008394 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008395 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008396 return 0;
8397 }
8398
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008399 return 1;
8400}
8401
David Matlack8ca44e82017-08-01 14:00:39 -07008402static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8403{
8404 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8405 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8406}
8407
Abel Gordone7953d72013-04-18 14:37:55 +03008408static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8409{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008410 if (vmx->nested.current_vmptr == -1ull)
8411 return;
8412
Abel Gordon012f83c2013-04-18 14:39:25 +03008413 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008414 /* copy to memory all shadowed fields in case
8415 they were modified */
8416 copy_shadow_to_vmcs12(vmx);
8417 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008418 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008419 }
Wincy Van705699a2015-02-03 23:58:17 +08008420 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008421
8422 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008423 kvm_vcpu_write_guest_page(&vmx->vcpu,
8424 vmx->nested.current_vmptr >> PAGE_SHIFT,
8425 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008426
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008427 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008428}
8429
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008430/*
8431 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8432 * just stops using VMX.
8433 */
8434static void free_nested(struct vcpu_vmx *vmx)
8435{
Wanpeng Lib7455822017-11-22 14:04:00 -08008436 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008437 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008438
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008439 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008440 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008441 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008442 vmx->nested.posted_intr_nv = -1;
8443 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008444 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008445 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008446 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8447 free_vmcs(vmx->vmcs01.shadow_vmcs);
8448 vmx->vmcs01.shadow_vmcs = NULL;
8449 }
David Matlack4f2777b2016-07-13 17:16:37 -07008450 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008451 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008452 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008453 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008454 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008455 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008456 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008457 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008458 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008459 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008460 }
Wincy Van705699a2015-02-03 23:58:17 +08008461 if (vmx->nested.pi_desc_page) {
8462 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008463 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008464 vmx->nested.pi_desc_page = NULL;
8465 vmx->nested.pi_desc = NULL;
8466 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008467
Jim Mattsonde3a0022017-11-27 17:22:25 -06008468 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008469}
8470
8471/* Emulate the VMXOFF instruction */
8472static int handle_vmoff(struct kvm_vcpu *vcpu)
8473{
8474 if (!nested_vmx_check_permission(vcpu))
8475 return 1;
8476 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008477 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008478 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008479}
8480
Nadav Har'El27d6c862011-05-25 23:06:59 +03008481/* Emulate the VMCLEAR instruction */
8482static int handle_vmclear(struct kvm_vcpu *vcpu)
8483{
8484 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008485 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008486 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008487
8488 if (!nested_vmx_check_permission(vcpu))
8489 return 1;
8490
Radim Krčmářcbf71272017-05-19 15:48:51 +02008491 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008492 return 1;
8493
Radim Krčmářcbf71272017-05-19 15:48:51 +02008494 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8495 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8496 return kvm_skip_emulated_instruction(vcpu);
8497 }
8498
8499 if (vmptr == vmx->nested.vmxon_ptr) {
8500 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8501 return kvm_skip_emulated_instruction(vcpu);
8502 }
8503
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008504 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008505 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008506
Jim Mattson587d7e722017-03-02 12:41:48 -08008507 kvm_vcpu_write_guest(vcpu,
8508 vmptr + offsetof(struct vmcs12, launch_state),
8509 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008510
Nadav Har'El27d6c862011-05-25 23:06:59 +03008511 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008512 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008513}
8514
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008515static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8516
8517/* Emulate the VMLAUNCH instruction */
8518static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8519{
8520 return nested_vmx_run(vcpu, true);
8521}
8522
8523/* Emulate the VMRESUME instruction */
8524static int handle_vmresume(struct kvm_vcpu *vcpu)
8525{
8526
8527 return nested_vmx_run(vcpu, false);
8528}
8529
Nadav Har'El49f705c2011-05-25 23:08:30 +03008530/*
8531 * Read a vmcs12 field. Since these can have varying lengths and we return
8532 * one type, we chose the biggest type (u64) and zero-extend the return value
8533 * to that size. Note that the caller, handle_vmread, might need to use only
8534 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8535 * 64-bit fields are to be returned).
8536 */
Liran Alone2536742018-06-23 02:35:02 +03008537static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008538 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008539{
8540 short offset = vmcs_field_to_offset(field);
8541 char *p;
8542
8543 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008544 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008545
Liran Alone2536742018-06-23 02:35:02 +03008546 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008547
Jim Mattsond37f4262017-12-22 12:12:16 -08008548 switch (vmcs_field_width(field)) {
8549 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008550 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008551 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008552 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008554 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008555 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008557 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008558 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008560 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008562 WARN_ON(1);
8563 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 }
8565}
8566
Abel Gordon20b97fe2013-04-18 14:36:25 +03008567
Liran Alone2536742018-06-23 02:35:02 +03008568static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008569 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008570 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008571 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008572 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008573 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008574
Jim Mattsond37f4262017-12-22 12:12:16 -08008575 switch (vmcs_field_width(field)) {
8576 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008577 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008578 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008579 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008580 *(u32 *)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_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008583 *(u64 *)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_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008588 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008589 WARN_ON(1);
8590 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591 }
8592
8593}
8594
Jim Mattsonf4160e42018-05-29 09:11:33 -07008595/*
8596 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8597 * they have been modified by the L1 guest. Note that the "read-only"
8598 * VM-exit information fields are actually writable if the vCPU is
8599 * configured to support "VMWRITE to any supported field in the VMCS."
8600 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008601static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8602{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008603 const u16 *fields[] = {
8604 shadow_read_write_fields,
8605 shadow_read_only_fields
8606 };
8607 const int max_fields[] = {
8608 max_shadow_read_write_fields,
8609 max_shadow_read_only_fields
8610 };
8611 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008612 unsigned long field;
8613 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008614 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008615
Jan Kiszka282da872014-10-08 18:05:39 +02008616 preempt_disable();
8617
Abel Gordon16f5b902013-04-18 14:38:25 +03008618 vmcs_load(shadow_vmcs);
8619
Jim Mattsonf4160e42018-05-29 09:11:33 -07008620 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8621 for (i = 0; i < max_fields[q]; i++) {
8622 field = fields[q][i];
8623 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008624 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008625 }
8626 /*
8627 * Skip the VM-exit information fields if they are read-only.
8628 */
8629 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8630 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008631 }
8632
8633 vmcs_clear(shadow_vmcs);
8634 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008635
8636 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008637}
8638
Abel Gordonc3114422013-04-18 14:38:55 +03008639static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8640{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008641 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008642 shadow_read_write_fields,
8643 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008644 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008645 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008646 max_shadow_read_write_fields,
8647 max_shadow_read_only_fields
8648 };
8649 int i, q;
8650 unsigned long field;
8651 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008652 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008653
8654 vmcs_load(shadow_vmcs);
8655
Mathias Krausec2bae892013-06-26 20:36:21 +02008656 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008657 for (i = 0; i < max_fields[q]; i++) {
8658 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008659 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008660 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008661 }
8662 }
8663
8664 vmcs_clear(shadow_vmcs);
8665 vmcs_load(vmx->loaded_vmcs->vmcs);
8666}
8667
Nadav Har'El49f705c2011-05-25 23:08:30 +03008668/*
8669 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8670 * used before) all generate the same failure when it is missing.
8671 */
8672static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8673{
8674 struct vcpu_vmx *vmx = to_vmx(vcpu);
8675 if (vmx->nested.current_vmptr == -1ull) {
8676 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008677 return 0;
8678 }
8679 return 1;
8680}
8681
8682static int handle_vmread(struct kvm_vcpu *vcpu)
8683{
8684 unsigned long field;
8685 u64 field_value;
8686 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8687 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8688 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008689 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008690
Kyle Hueyeb277562016-11-29 12:40:39 -08008691 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008692 return 1;
8693
Kyle Huey6affcbe2016-11-29 12:40:40 -08008694 if (!nested_vmx_check_vmcs12(vcpu))
8695 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008696
Liran Alon6d894f42018-06-23 02:35:09 +03008697 if (!is_guest_mode(vcpu))
8698 vmcs12 = get_vmcs12(vcpu);
8699 else {
8700 /*
8701 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8702 * to shadowed-field sets the ALU flags for VMfailInvalid.
8703 */
8704 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8705 nested_vmx_failInvalid(vcpu);
8706 return kvm_skip_emulated_instruction(vcpu);
8707 }
8708 vmcs12 = get_shadow_vmcs12(vcpu);
8709 }
8710
Nadav Har'El49f705c2011-05-25 23:08:30 +03008711 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008712 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008713 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008714 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008715 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008716 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008717 }
8718 /*
8719 * Now copy part of this value to register or memory, as requested.
8720 * Note that the number of bits actually copied is 32 or 64 depending
8721 * on the guest's mode (32 or 64 bit), not on the given field's length.
8722 */
8723 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008724 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008725 field_value);
8726 } else {
8727 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008728 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008729 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008730 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008731 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8732 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008733 }
8734
8735 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008736 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737}
8738
8739
8740static int handle_vmwrite(struct kvm_vcpu *vcpu)
8741{
8742 unsigned long field;
8743 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008744 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8746 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008747
Nadav Har'El49f705c2011-05-25 23:08:30 +03008748 /* The value to write might be 32 or 64 bits, depending on L1's long
8749 * mode, and eventually we need to write that into a field of several
8750 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008751 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008752 * bits into the vmcs12 field.
8753 */
8754 u64 field_value = 0;
8755 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008756 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008757
Kyle Hueyeb277562016-11-29 12:40:39 -08008758 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008759 return 1;
8760
Kyle Huey6affcbe2016-11-29 12:40:40 -08008761 if (!nested_vmx_check_vmcs12(vcpu))
8762 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008763
Nadav Har'El49f705c2011-05-25 23:08:30 +03008764 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008765 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008766 (((vmx_instruction_info) >> 3) & 0xf));
8767 else {
8768 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008769 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008771 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8772 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773 kvm_inject_page_fault(vcpu, &e);
8774 return 1;
8775 }
8776 }
8777
8778
Nadav Amit27e6fb52014-06-18 17:19:26 +03008779 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008780 /*
8781 * If the vCPU supports "VMWRITE to any supported field in the
8782 * VMCS," then the "read-only" fields are actually read/write.
8783 */
8784 if (vmcs_field_readonly(field) &&
8785 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008786 nested_vmx_failValid(vcpu,
8787 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008788 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008789 }
8790
Liran Alon6d894f42018-06-23 02:35:09 +03008791 if (!is_guest_mode(vcpu))
8792 vmcs12 = get_vmcs12(vcpu);
8793 else {
8794 /*
8795 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8796 * to shadowed-field sets the ALU flags for VMfailInvalid.
8797 */
8798 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8799 nested_vmx_failInvalid(vcpu);
8800 return kvm_skip_emulated_instruction(vcpu);
8801 }
8802 vmcs12 = get_shadow_vmcs12(vcpu);
8803
8804 }
8805
8806 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008807 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008808 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008809 }
8810
Liran Alon6d894f42018-06-23 02:35:09 +03008811 /*
8812 * Do not track vmcs12 dirty-state if in guest-mode
8813 * as we actually dirty shadow vmcs12 instead of vmcs12.
8814 */
8815 if (!is_guest_mode(vcpu)) {
8816 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008817#define SHADOW_FIELD_RW(x) case x:
8818#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008819 /*
8820 * The fields that can be updated by L1 without a vmexit are
8821 * always updated in the vmcs02, the others go down the slow
8822 * path of prepare_vmcs02.
8823 */
8824 break;
8825 default:
8826 vmx->nested.dirty_vmcs12 = true;
8827 break;
8828 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008829 }
8830
Nadav Har'El49f705c2011-05-25 23:08:30 +03008831 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008832 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008833}
8834
Jim Mattsona8bc2842016-11-30 12:03:44 -08008835static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8836{
8837 vmx->nested.current_vmptr = vmptr;
8838 if (enable_shadow_vmcs) {
8839 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8840 SECONDARY_EXEC_SHADOW_VMCS);
8841 vmcs_write64(VMCS_LINK_POINTER,
8842 __pa(vmx->vmcs01.shadow_vmcs));
8843 vmx->nested.sync_shadow_vmcs = true;
8844 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008845 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008846}
8847
Nadav Har'El63846662011-05-25 23:07:29 +03008848/* Emulate the VMPTRLD instruction */
8849static int handle_vmptrld(struct kvm_vcpu *vcpu)
8850{
8851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008852 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008853
8854 if (!nested_vmx_check_permission(vcpu))
8855 return 1;
8856
Radim Krčmářcbf71272017-05-19 15:48:51 +02008857 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008858 return 1;
8859
Radim Krčmářcbf71272017-05-19 15:48:51 +02008860 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8861 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8862 return kvm_skip_emulated_instruction(vcpu);
8863 }
8864
8865 if (vmptr == vmx->nested.vmxon_ptr) {
8866 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8867 return kvm_skip_emulated_instruction(vcpu);
8868 }
8869
Nadav Har'El63846662011-05-25 23:07:29 +03008870 if (vmx->nested.current_vmptr != vmptr) {
8871 struct vmcs12 *new_vmcs12;
8872 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008873 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8874 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008875 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008876 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008877 }
8878 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008879 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008880 (new_vmcs12->hdr.shadow_vmcs &&
8881 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008882 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008883 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008884 nested_vmx_failValid(vcpu,
8885 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008886 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008887 }
Nadav Har'El63846662011-05-25 23:07:29 +03008888
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008889 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008890 /*
8891 * Load VMCS12 from guest memory since it is not already
8892 * cached.
8893 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008894 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8895 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008896 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008897
Jim Mattsona8bc2842016-11-30 12:03:44 -08008898 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008899 }
8900
8901 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008902 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008903}
8904
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008905/* Emulate the VMPTRST instruction */
8906static int handle_vmptrst(struct kvm_vcpu *vcpu)
8907{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008908 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8909 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8910 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008911 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008912 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008913
8914 if (!nested_vmx_check_permission(vcpu))
8915 return 1;
8916
Sean Christopherson0a06d422018-07-19 10:31:00 -07008917 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008918 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008919 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008920 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8921 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008922 kvm_inject_page_fault(vcpu, &e);
8923 return 1;
8924 }
8925 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008926 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008927}
8928
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008929/* Emulate the INVEPT instruction */
8930static int handle_invept(struct kvm_vcpu *vcpu)
8931{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008932 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008933 u32 vmx_instruction_info, types;
8934 unsigned long type;
8935 gva_t gva;
8936 struct x86_exception e;
8937 struct {
8938 u64 eptp, gpa;
8939 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008940
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008941 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008942 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008943 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008944 kvm_queue_exception(vcpu, UD_VECTOR);
8945 return 1;
8946 }
8947
8948 if (!nested_vmx_check_permission(vcpu))
8949 return 1;
8950
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008951 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008952 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008953
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008954 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008955
Jim Mattson85c856b2016-10-26 08:38:38 -07008956 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957 nested_vmx_failValid(vcpu,
8958 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008959 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008960 }
8961
8962 /* According to the Intel VMX instruction reference, the memory
8963 * operand is read even if it isn't needed (e.g., for type==global)
8964 */
8965 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008966 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008967 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008968 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008969 kvm_inject_page_fault(vcpu, &e);
8970 return 1;
8971 }
8972
8973 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008974 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008975 /*
8976 * TODO: track mappings and invalidate
8977 * single context requests appropriately
8978 */
8979 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008980 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008981 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008982 nested_vmx_succeed(vcpu);
8983 break;
8984 default:
8985 BUG_ON(1);
8986 break;
8987 }
8988
Kyle Huey6affcbe2016-11-29 12:40:40 -08008989 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008990}
8991
Petr Matouseka642fc32014-09-23 20:22:30 +02008992static int handle_invvpid(struct kvm_vcpu *vcpu)
8993{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008994 struct vcpu_vmx *vmx = to_vmx(vcpu);
8995 u32 vmx_instruction_info;
8996 unsigned long type, types;
8997 gva_t gva;
8998 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008999 struct {
9000 u64 vpid;
9001 u64 gla;
9002 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009003
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009004 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009005 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009006 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009007 kvm_queue_exception(vcpu, UD_VECTOR);
9008 return 1;
9009 }
9010
9011 if (!nested_vmx_check_permission(vcpu))
9012 return 1;
9013
9014 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9015 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9016
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009017 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009018 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009019
Jim Mattson85c856b2016-10-26 08:38:38 -07009020 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009021 nested_vmx_failValid(vcpu,
9022 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009023 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009024 }
9025
9026 /* according to the intel vmx instruction reference, the memory
9027 * operand is read even if it isn't needed (e.g., for type==global)
9028 */
9029 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9030 vmx_instruction_info, false, &gva))
9031 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009032 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009033 kvm_inject_page_fault(vcpu, &e);
9034 return 1;
9035 }
Jim Mattson40352602017-06-28 09:37:37 -07009036 if (operand.vpid >> 16) {
9037 nested_vmx_failValid(vcpu,
9038 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9039 return kvm_skip_emulated_instruction(vcpu);
9040 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009041
9042 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009043 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009044 if (!operand.vpid ||
9045 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009046 nested_vmx_failValid(vcpu,
9047 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9048 return kvm_skip_emulated_instruction(vcpu);
9049 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009050 if (cpu_has_vmx_invvpid_individual_addr() &&
9051 vmx->nested.vpid02) {
9052 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9053 vmx->nested.vpid02, operand.gla);
9054 } else
9055 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9056 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009057 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009058 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009059 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009060 nested_vmx_failValid(vcpu,
9061 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009062 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009063 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009064 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009065 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009066 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03009067 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009068 break;
9069 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009070 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009071 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009072 }
9073
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009074 nested_vmx_succeed(vcpu);
9075
Kyle Huey6affcbe2016-11-29 12:40:40 -08009076 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009077}
9078
Junaid Shahideb4b2482018-06-27 14:59:14 -07009079static int handle_invpcid(struct kvm_vcpu *vcpu)
9080{
9081 u32 vmx_instruction_info;
9082 unsigned long type;
9083 bool pcid_enabled;
9084 gva_t gva;
9085 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009086 unsigned i;
9087 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009088 struct {
9089 u64 pcid;
9090 u64 gla;
9091 } operand;
9092
9093 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9094 kvm_queue_exception(vcpu, UD_VECTOR);
9095 return 1;
9096 }
9097
9098 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9099 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9100
9101 if (type > 3) {
9102 kvm_inject_gp(vcpu, 0);
9103 return 1;
9104 }
9105
9106 /* According to the Intel instruction reference, the memory operand
9107 * is read even if it isn't needed (e.g., for type==all)
9108 */
9109 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9110 vmx_instruction_info, false, &gva))
9111 return 1;
9112
9113 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9114 kvm_inject_page_fault(vcpu, &e);
9115 return 1;
9116 }
9117
9118 if (operand.pcid >> 12 != 0) {
9119 kvm_inject_gp(vcpu, 0);
9120 return 1;
9121 }
9122
9123 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9124
9125 switch (type) {
9126 case INVPCID_TYPE_INDIV_ADDR:
9127 if ((!pcid_enabled && (operand.pcid != 0)) ||
9128 is_noncanonical_address(operand.gla, vcpu)) {
9129 kvm_inject_gp(vcpu, 0);
9130 return 1;
9131 }
9132 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9133 return kvm_skip_emulated_instruction(vcpu);
9134
9135 case INVPCID_TYPE_SINGLE_CTXT:
9136 if (!pcid_enabled && (operand.pcid != 0)) {
9137 kvm_inject_gp(vcpu, 0);
9138 return 1;
9139 }
9140
9141 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9142 kvm_mmu_sync_roots(vcpu);
9143 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9144 }
9145
Junaid Shahidb94742c2018-06-27 14:59:20 -07009146 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9147 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9148 == operand.pcid)
9149 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009150
Junaid Shahidb94742c2018-06-27 14:59:20 -07009151 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009152 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009153 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009154 * given PCID, then nothing needs to be done here because a
9155 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009156 */
9157
9158 return kvm_skip_emulated_instruction(vcpu);
9159
9160 case INVPCID_TYPE_ALL_NON_GLOBAL:
9161 /*
9162 * Currently, KVM doesn't mark global entries in the shadow
9163 * page tables, so a non-global flush just degenerates to a
9164 * global flush. If needed, we could optimize this later by
9165 * keeping track of global entries in shadow page tables.
9166 */
9167
9168 /* fall-through */
9169 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9170 kvm_mmu_unload(vcpu);
9171 return kvm_skip_emulated_instruction(vcpu);
9172
9173 default:
9174 BUG(); /* We have already checked above that type <= 3 */
9175 }
9176}
9177
Kai Huang843e4332015-01-28 10:54:28 +08009178static int handle_pml_full(struct kvm_vcpu *vcpu)
9179{
9180 unsigned long exit_qualification;
9181
9182 trace_kvm_pml_full(vcpu->vcpu_id);
9183
9184 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9185
9186 /*
9187 * PML buffer FULL happened while executing iret from NMI,
9188 * "blocked by NMI" bit has to be set before next VM entry.
9189 */
9190 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009191 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009192 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9193 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9194 GUEST_INTR_STATE_NMI);
9195
9196 /*
9197 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9198 * here.., and there's no userspace involvement needed for PML.
9199 */
9200 return 1;
9201}
9202
Yunhong Jiang64672c92016-06-13 14:19:59 -07009203static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9204{
9205 kvm_lapic_expired_hv_timer(vcpu);
9206 return 1;
9207}
9208
Bandan Das41ab9372017-08-03 15:54:43 -04009209static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9210{
9211 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009212 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9213
9214 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009215 switch (address & VMX_EPTP_MT_MASK) {
9216 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009217 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009218 return false;
9219 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009220 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009221 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009222 return false;
9223 break;
9224 default:
9225 return false;
9226 }
9227
David Hildenbrandbb97a012017-08-10 23:15:28 +02009228 /* only 4 levels page-walk length are valid */
9229 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009230 return false;
9231
9232 /* Reserved bits should not be set */
9233 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9234 return false;
9235
9236 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009237 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009238 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009239 return false;
9240 }
9241
9242 return true;
9243}
9244
9245static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9246 struct vmcs12 *vmcs12)
9247{
9248 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9249 u64 address;
9250 bool accessed_dirty;
9251 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9252
9253 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9254 !nested_cpu_has_ept(vmcs12))
9255 return 1;
9256
9257 if (index >= VMFUNC_EPTP_ENTRIES)
9258 return 1;
9259
9260
9261 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9262 &address, index * 8, 8))
9263 return 1;
9264
David Hildenbrandbb97a012017-08-10 23:15:28 +02009265 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009266
9267 /*
9268 * If the (L2) guest does a vmfunc to the currently
9269 * active ept pointer, we don't have to do anything else
9270 */
9271 if (vmcs12->ept_pointer != address) {
9272 if (!valid_ept_address(vcpu, address))
9273 return 1;
9274
9275 kvm_mmu_unload(vcpu);
9276 mmu->ept_ad = accessed_dirty;
9277 mmu->base_role.ad_disabled = !accessed_dirty;
9278 vmcs12->ept_pointer = address;
9279 /*
9280 * TODO: Check what's the correct approach in case
9281 * mmu reload fails. Currently, we just let the next
9282 * reload potentially fail
9283 */
9284 kvm_mmu_reload(vcpu);
9285 }
9286
9287 return 0;
9288}
9289
Bandan Das2a499e42017-08-03 15:54:41 -04009290static int handle_vmfunc(struct kvm_vcpu *vcpu)
9291{
Bandan Das27c42a12017-08-03 15:54:42 -04009292 struct vcpu_vmx *vmx = to_vmx(vcpu);
9293 struct vmcs12 *vmcs12;
9294 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9295
9296 /*
9297 * VMFUNC is only supported for nested guests, but we always enable the
9298 * secondary control for simplicity; for non-nested mode, fake that we
9299 * didn't by injecting #UD.
9300 */
9301 if (!is_guest_mode(vcpu)) {
9302 kvm_queue_exception(vcpu, UD_VECTOR);
9303 return 1;
9304 }
9305
9306 vmcs12 = get_vmcs12(vcpu);
9307 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9308 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009309
9310 switch (function) {
9311 case 0:
9312 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9313 goto fail;
9314 break;
9315 default:
9316 goto fail;
9317 }
9318 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009319
9320fail:
9321 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9322 vmcs_read32(VM_EXIT_INTR_INFO),
9323 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009324 return 1;
9325}
9326
Sean Christopherson0b665d32018-08-14 09:33:34 -07009327static int handle_encls(struct kvm_vcpu *vcpu)
9328{
9329 /*
9330 * SGX virtualization is not yet supported. There is no software
9331 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9332 * to prevent the guest from executing ENCLS.
9333 */
9334 kvm_queue_exception(vcpu, UD_VECTOR);
9335 return 1;
9336}
9337
Nadav Har'El0140cae2011-05-25 23:06:28 +03009338/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009339 * The exit handlers return 1 if the exit was handled fully and guest execution
9340 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9341 * to be done to userspace and return 0.
9342 */
Mathias Krause772e0312012-08-30 01:30:19 +02009343static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009344 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9345 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009346 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009347 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009348 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009349 [EXIT_REASON_CR_ACCESS] = handle_cr,
9350 [EXIT_REASON_DR_ACCESS] = handle_dr,
9351 [EXIT_REASON_CPUID] = handle_cpuid,
9352 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9353 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9354 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9355 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009356 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009357 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009358 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009359 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009360 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009361 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009362 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009363 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009364 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009365 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009366 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009367 [EXIT_REASON_VMOFF] = handle_vmoff,
9368 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009369 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9370 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009371 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009372 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009373 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009374 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009375 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009376 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009377 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9378 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009379 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9380 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009381 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009382 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009383 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009384 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009385 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009386 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009387 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009388 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009389 [EXIT_REASON_XSAVES] = handle_xsaves,
9390 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009391 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009392 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009393 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009394 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009395 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009396};
9397
9398static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009399 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009400
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009401static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9402 struct vmcs12 *vmcs12)
9403{
9404 unsigned long exit_qualification;
9405 gpa_t bitmap, last_bitmap;
9406 unsigned int port;
9407 int size;
9408 u8 b;
9409
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009410 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009411 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009412
9413 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9414
9415 port = exit_qualification >> 16;
9416 size = (exit_qualification & 7) + 1;
9417
9418 last_bitmap = (gpa_t)-1;
9419 b = -1;
9420
9421 while (size > 0) {
9422 if (port < 0x8000)
9423 bitmap = vmcs12->io_bitmap_a;
9424 else if (port < 0x10000)
9425 bitmap = vmcs12->io_bitmap_b;
9426 else
Joe Perches1d804d02015-03-30 16:46:09 -07009427 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009428 bitmap += (port & 0x7fff) / 8;
9429
9430 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009431 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009432 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009433 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009434 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009435
9436 port++;
9437 size--;
9438 last_bitmap = bitmap;
9439 }
9440
Joe Perches1d804d02015-03-30 16:46:09 -07009441 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009442}
9443
Nadav Har'El644d7112011-05-25 23:12:35 +03009444/*
9445 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9446 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9447 * disinterest in the current event (read or write a specific MSR) by using an
9448 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9449 */
9450static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9451 struct vmcs12 *vmcs12, u32 exit_reason)
9452{
9453 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9454 gpa_t bitmap;
9455
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009456 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009457 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009458
9459 /*
9460 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9461 * for the four combinations of read/write and low/high MSR numbers.
9462 * First we need to figure out which of the four to use:
9463 */
9464 bitmap = vmcs12->msr_bitmap;
9465 if (exit_reason == EXIT_REASON_MSR_WRITE)
9466 bitmap += 2048;
9467 if (msr_index >= 0xc0000000) {
9468 msr_index -= 0xc0000000;
9469 bitmap += 1024;
9470 }
9471
9472 /* Then read the msr_index'th bit from this bitmap: */
9473 if (msr_index < 1024*8) {
9474 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009475 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009476 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009477 return 1 & (b >> (msr_index & 7));
9478 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009479 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009480}
9481
9482/*
9483 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9484 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9485 * intercept (via guest_host_mask etc.) the current event.
9486 */
9487static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9488 struct vmcs12 *vmcs12)
9489{
9490 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9491 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009492 int reg;
9493 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009494
9495 switch ((exit_qualification >> 4) & 3) {
9496 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009497 reg = (exit_qualification >> 8) & 15;
9498 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009499 switch (cr) {
9500 case 0:
9501 if (vmcs12->cr0_guest_host_mask &
9502 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009503 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009504 break;
9505 case 3:
9506 if ((vmcs12->cr3_target_count >= 1 &&
9507 vmcs12->cr3_target_value0 == val) ||
9508 (vmcs12->cr3_target_count >= 2 &&
9509 vmcs12->cr3_target_value1 == val) ||
9510 (vmcs12->cr3_target_count >= 3 &&
9511 vmcs12->cr3_target_value2 == val) ||
9512 (vmcs12->cr3_target_count >= 4 &&
9513 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009514 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009515 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009516 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009517 break;
9518 case 4:
9519 if (vmcs12->cr4_guest_host_mask &
9520 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009521 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009522 break;
9523 case 8:
9524 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009525 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009526 break;
9527 }
9528 break;
9529 case 2: /* clts */
9530 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9531 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009532 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009533 break;
9534 case 1: /* mov from cr */
9535 switch (cr) {
9536 case 3:
9537 if (vmcs12->cpu_based_vm_exec_control &
9538 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009539 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009540 break;
9541 case 8:
9542 if (vmcs12->cpu_based_vm_exec_control &
9543 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 break;
9546 }
9547 break;
9548 case 3: /* lmsw */
9549 /*
9550 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9551 * cr0. Other attempted changes are ignored, with no exit.
9552 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009553 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009554 if (vmcs12->cr0_guest_host_mask & 0xe &
9555 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009556 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009557 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9558 !(vmcs12->cr0_read_shadow & 0x1) &&
9559 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009560 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009561 break;
9562 }
Joe Perches1d804d02015-03-30 16:46:09 -07009563 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009564}
9565
Liran Alona7cde482018-06-23 02:35:10 +03009566static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9567 struct vmcs12 *vmcs12, gpa_t bitmap)
9568{
9569 u32 vmx_instruction_info;
9570 unsigned long field;
9571 u8 b;
9572
9573 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9574 return true;
9575
9576 /* Decode instruction info and find the field to access */
9577 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9578 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9579
9580 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9581 if (field >> 15)
9582 return true;
9583
9584 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9585 return true;
9586
9587 return 1 & (b >> (field & 7));
9588}
9589
Nadav Har'El644d7112011-05-25 23:12:35 +03009590/*
9591 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9592 * should handle it ourselves in L0 (and then continue L2). Only call this
9593 * when in is_guest_mode (L2).
9594 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009595static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009596{
Nadav Har'El644d7112011-05-25 23:12:35 +03009597 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9598 struct vcpu_vmx *vmx = to_vmx(vcpu);
9599 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9600
Jim Mattson4f350c62017-09-14 16:31:44 -07009601 if (vmx->nested.nested_run_pending)
9602 return false;
9603
9604 if (unlikely(vmx->fail)) {
9605 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9606 vmcs_read32(VM_INSTRUCTION_ERROR));
9607 return true;
9608 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009609
David Matlackc9f04402017-08-01 14:00:40 -07009610 /*
9611 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009612 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9613 * Page). The CPU may write to these pages via their host
9614 * physical address while L2 is running, bypassing any
9615 * address-translation-based dirty tracking (e.g. EPT write
9616 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009617 *
9618 * Mark them dirty on every exit from L2 to prevent them from
9619 * getting out of sync with dirty tracking.
9620 */
9621 nested_mark_vmcs12_pages_dirty(vcpu);
9622
Jim Mattson4f350c62017-09-14 16:31:44 -07009623 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9624 vmcs_readl(EXIT_QUALIFICATION),
9625 vmx->idt_vectoring_info,
9626 intr_info,
9627 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9628 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009629
9630 switch (exit_reason) {
9631 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009632 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009633 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009634 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009635 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009636 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009637 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009638 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009639 else if (is_debug(intr_info) &&
9640 vcpu->guest_debug &
9641 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9642 return false;
9643 else if (is_breakpoint(intr_info) &&
9644 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9645 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009646 return vmcs12->exception_bitmap &
9647 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9648 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009649 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009650 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009651 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009652 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009653 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009654 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009655 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009656 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009657 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009658 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009659 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 case EXIT_REASON_HLT:
9661 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9662 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009663 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_INVLPG:
9665 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9666 case EXIT_REASON_RDPMC:
9667 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009668 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009669 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009670 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009671 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009672 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009673 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009674 case EXIT_REASON_VMREAD:
9675 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9676 vmcs12->vmread_bitmap);
9677 case EXIT_REASON_VMWRITE:
9678 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9679 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009680 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9681 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009682 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009683 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009684 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009685 /*
9686 * VMX instructions trap unconditionally. This allows L1 to
9687 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9688 */
Joe Perches1d804d02015-03-30 16:46:09 -07009689 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009690 case EXIT_REASON_CR_ACCESS:
9691 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9692 case EXIT_REASON_DR_ACCESS:
9693 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9694 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009695 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009696 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9697 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009698 case EXIT_REASON_MSR_READ:
9699 case EXIT_REASON_MSR_WRITE:
9700 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9701 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009702 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009703 case EXIT_REASON_MWAIT_INSTRUCTION:
9704 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009705 case EXIT_REASON_MONITOR_TRAP_FLAG:
9706 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009707 case EXIT_REASON_MONITOR_INSTRUCTION:
9708 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9709 case EXIT_REASON_PAUSE_INSTRUCTION:
9710 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9711 nested_cpu_has2(vmcs12,
9712 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9713 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009714 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009715 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009716 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009717 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009718 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009719 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009720 /*
9721 * The controls for "virtualize APIC accesses," "APIC-
9722 * register virtualization," and "virtual-interrupt
9723 * delivery" only come from vmcs12.
9724 */
Joe Perches1d804d02015-03-30 16:46:09 -07009725 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009726 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009727 /*
9728 * L0 always deals with the EPT violation. If nested EPT is
9729 * used, and the nested mmu code discovers that the address is
9730 * missing in the guest EPT table (EPT12), the EPT violation
9731 * will be injected with nested_ept_inject_page_fault()
9732 */
Joe Perches1d804d02015-03-30 16:46:09 -07009733 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009734 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009735 /*
9736 * L2 never uses directly L1's EPT, but rather L0's own EPT
9737 * table (shadow on EPT) or a merged EPT table that L0 built
9738 * (EPT on EPT). So any problems with the structure of the
9739 * table is L0's fault.
9740 */
Joe Perches1d804d02015-03-30 16:46:09 -07009741 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009742 case EXIT_REASON_INVPCID:
9743 return
9744 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9745 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009746 case EXIT_REASON_WBINVD:
9747 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9748 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009749 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009750 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9751 /*
9752 * This should never happen, since it is not possible to
9753 * set XSS to a non-zero value---neither in L1 nor in L2.
9754 * If if it were, XSS would have to be checked against
9755 * the XSS exit bitmap in vmcs12.
9756 */
9757 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009758 case EXIT_REASON_PREEMPTION_TIMER:
9759 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009760 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009761 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009762 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009763 case EXIT_REASON_VMFUNC:
9764 /* VM functions are emulated through L2->L0 vmexits. */
9765 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009766 case EXIT_REASON_ENCLS:
9767 /* SGX is never exposed to L1 */
9768 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009769 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009770 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009771 }
9772}
9773
Paolo Bonzini7313c692017-07-27 10:31:25 +02009774static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9775{
9776 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9777
9778 /*
9779 * At this point, the exit interruption info in exit_intr_info
9780 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9781 * we need to query the in-kernel LAPIC.
9782 */
9783 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9784 if ((exit_intr_info &
9785 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9786 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9787 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9788 vmcs12->vm_exit_intr_error_code =
9789 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9790 }
9791
9792 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9793 vmcs_readl(EXIT_QUALIFICATION));
9794 return 1;
9795}
9796
Avi Kivity586f9602010-11-18 13:09:54 +02009797static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9798{
9799 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9800 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9801}
9802
Kai Huanga3eaa862015-11-04 13:46:05 +08009803static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009804{
Kai Huanga3eaa862015-11-04 13:46:05 +08009805 if (vmx->pml_pg) {
9806 __free_page(vmx->pml_pg);
9807 vmx->pml_pg = NULL;
9808 }
Kai Huang843e4332015-01-28 10:54:28 +08009809}
9810
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009811static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009812{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009813 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009814 u64 *pml_buf;
9815 u16 pml_idx;
9816
9817 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9818
9819 /* Do nothing if PML buffer is empty */
9820 if (pml_idx == (PML_ENTITY_NUM - 1))
9821 return;
9822
9823 /* PML index always points to next available PML buffer entity */
9824 if (pml_idx >= PML_ENTITY_NUM)
9825 pml_idx = 0;
9826 else
9827 pml_idx++;
9828
9829 pml_buf = page_address(vmx->pml_pg);
9830 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9831 u64 gpa;
9832
9833 gpa = pml_buf[pml_idx];
9834 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009835 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009836 }
9837
9838 /* reset PML index */
9839 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9840}
9841
9842/*
9843 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9844 * Called before reporting dirty_bitmap to userspace.
9845 */
9846static void kvm_flush_pml_buffers(struct kvm *kvm)
9847{
9848 int i;
9849 struct kvm_vcpu *vcpu;
9850 /*
9851 * We only need to kick vcpu out of guest mode here, as PML buffer
9852 * is flushed at beginning of all VMEXITs, and it's obvious that only
9853 * vcpus running in guest are possible to have unflushed GPAs in PML
9854 * buffer.
9855 */
9856 kvm_for_each_vcpu(i, vcpu, kvm)
9857 kvm_vcpu_kick(vcpu);
9858}
9859
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009860static void vmx_dump_sel(char *name, uint32_t sel)
9861{
9862 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009863 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009864 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9865 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9866 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9867}
9868
9869static void vmx_dump_dtsel(char *name, uint32_t limit)
9870{
9871 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9872 name, vmcs_read32(limit),
9873 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9874}
9875
9876static void dump_vmcs(void)
9877{
9878 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9879 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9880 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9881 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9882 u32 secondary_exec_control = 0;
9883 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009884 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009885 int i, n;
9886
9887 if (cpu_has_secondary_exec_ctrls())
9888 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9889
9890 pr_err("*** Guest State ***\n");
9891 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9892 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9893 vmcs_readl(CR0_GUEST_HOST_MASK));
9894 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9895 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9896 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9897 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9898 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9899 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009900 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9901 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9902 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9903 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009904 }
9905 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9906 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9907 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9908 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9909 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9910 vmcs_readl(GUEST_SYSENTER_ESP),
9911 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9912 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9913 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9914 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9915 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9916 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9917 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9918 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9919 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9920 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9921 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9922 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9923 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009924 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9925 efer, vmcs_read64(GUEST_IA32_PAT));
9926 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9927 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009928 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009929 if (cpu_has_load_perf_global_ctrl &&
9930 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009931 pr_err("PerfGlobCtl = 0x%016llx\n",
9932 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009933 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009934 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009935 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9936 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9937 vmcs_read32(GUEST_ACTIVITY_STATE));
9938 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9939 pr_err("InterruptStatus = %04x\n",
9940 vmcs_read16(GUEST_INTR_STATUS));
9941
9942 pr_err("*** Host State ***\n");
9943 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9944 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9945 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9946 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9947 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9948 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9949 vmcs_read16(HOST_TR_SELECTOR));
9950 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9951 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9952 vmcs_readl(HOST_TR_BASE));
9953 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9954 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9955 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9956 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9957 vmcs_readl(HOST_CR4));
9958 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9959 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9960 vmcs_read32(HOST_IA32_SYSENTER_CS),
9961 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9962 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009963 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9964 vmcs_read64(HOST_IA32_EFER),
9965 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009966 if (cpu_has_load_perf_global_ctrl &&
9967 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009968 pr_err("PerfGlobCtl = 0x%016llx\n",
9969 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009970
9971 pr_err("*** Control State ***\n");
9972 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9973 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9974 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9975 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9976 vmcs_read32(EXCEPTION_BITMAP),
9977 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9978 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9979 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9980 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9981 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9982 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9983 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9984 vmcs_read32(VM_EXIT_INTR_INFO),
9985 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9986 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9987 pr_err(" reason=%08x qualification=%016lx\n",
9988 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9989 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9990 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9991 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009992 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009993 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009994 pr_err("TSC Multiplier = 0x%016llx\n",
9995 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009996 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9997 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9998 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9999 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10000 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010001 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010002 n = vmcs_read32(CR3_TARGET_COUNT);
10003 for (i = 0; i + 1 < n; i += 4)
10004 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10005 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10006 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10007 if (i < n)
10008 pr_err("CR3 target%u=%016lx\n",
10009 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10010 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10011 pr_err("PLE Gap=%08x Window=%08x\n",
10012 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10013 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10014 pr_err("Virtual processor ID = 0x%04x\n",
10015 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10016}
10017
Avi Kivity6aa8b732006-12-10 02:21:36 -080010018/*
10019 * The guest has exited. See if we can fix it or if we need userspace
10020 * assistance.
10021 */
Avi Kivity851ba692009-08-24 11:10:17 +030010022static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010023{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010024 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010025 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010026 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010027
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010028 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10029
Kai Huang843e4332015-01-28 10:54:28 +080010030 /*
10031 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10032 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10033 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10034 * mode as if vcpus is in root mode, the PML buffer must has been
10035 * flushed already.
10036 */
10037 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010038 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010039
Mohammed Gamal80ced182009-09-01 12:48:18 +020010040 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010041 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010042 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010043
Paolo Bonzini7313c692017-07-27 10:31:25 +020010044 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10045 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010046
Mohammed Gamal51207022010-05-31 22:40:54 +030010047 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010048 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010049 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10050 vcpu->run->fail_entry.hardware_entry_failure_reason
10051 = exit_reason;
10052 return 0;
10053 }
10054
Avi Kivity29bd8a72007-09-10 17:27:03 +030010055 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010056 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10057 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010058 = vmcs_read32(VM_INSTRUCTION_ERROR);
10059 return 0;
10060 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010061
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010062 /*
10063 * Note:
10064 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10065 * delivery event since it indicates guest is accessing MMIO.
10066 * The vm-exit can be triggered again after return to guest that
10067 * will cause infinite loop.
10068 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010069 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010070 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010071 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010072 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010073 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10074 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10075 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010076 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010077 vcpu->run->internal.data[0] = vectoring_info;
10078 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010079 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10080 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10081 vcpu->run->internal.ndata++;
10082 vcpu->run->internal.data[3] =
10083 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10084 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010085 return 0;
10086 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010087
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010088 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010089 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10090 if (vmx_interrupt_allowed(vcpu)) {
10091 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10092 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10093 vcpu->arch.nmi_pending) {
10094 /*
10095 * This CPU don't support us in finding the end of an
10096 * NMI-blocked window if the guest runs with IRQs
10097 * disabled. So we pull the trigger after 1 s of
10098 * futile waiting, but inform the user about this.
10099 */
10100 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10101 "state on VCPU %d after 1 s timeout\n",
10102 __func__, vcpu->vcpu_id);
10103 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10104 }
10105 }
10106
Avi Kivity6aa8b732006-12-10 02:21:36 -080010107 if (exit_reason < kvm_vmx_max_exit_handlers
10108 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010109 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010110 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010111 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10112 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010113 kvm_queue_exception(vcpu, UD_VECTOR);
10114 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010115 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010116}
10117
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010118/*
10119 * Software based L1D cache flush which is used when microcode providing
10120 * the cache control MSR is not loaded.
10121 *
10122 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10123 * flush it is required to read in 64 KiB because the replacement algorithm
10124 * is not exactly LRU. This could be sized at runtime via topology
10125 * information but as all relevant affected CPUs have 32KiB L1D cache size
10126 * there is no point in doing so.
10127 */
10128#define L1D_CACHE_ORDER 4
10129static void *vmx_l1d_flush_pages;
10130
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010131static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010132{
10133 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010134
10135 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010136 * This code is only executed when the the flush mode is 'cond' or
10137 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010138 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010139 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010140 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010141
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010142 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010143 * Clear the per-vcpu flush bit, it gets set again
10144 * either from vcpu_run() or from one of the unsafe
10145 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010146 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010147 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010148 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010149
10150 /*
10151 * Clear the per-cpu flush bit, it gets set again from
10152 * the interrupt handlers.
10153 */
10154 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10155 kvm_clear_cpu_l1tf_flush_l1d();
10156
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010157 if (!flush_l1d)
10158 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010159 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010160
10161 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010162
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010163 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10164 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10165 return;
10166 }
10167
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010168 asm volatile(
10169 /* First ensure the pages are in the TLB */
10170 "xorl %%eax, %%eax\n"
10171 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010172 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010173 "addl $4096, %%eax\n\t"
10174 "cmpl %%eax, %[size]\n\t"
10175 "jne .Lpopulate_tlb\n\t"
10176 "xorl %%eax, %%eax\n\t"
10177 "cpuid\n\t"
10178 /* Now fill the cache */
10179 "xorl %%eax, %%eax\n"
10180 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010181 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010182 "addl $64, %%eax\n\t"
10183 "cmpl %%eax, %[size]\n\t"
10184 "jne .Lfill_cache\n\t"
10185 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010186 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010187 [size] "r" (size)
10188 : "eax", "ebx", "ecx", "edx");
10189}
10190
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010191static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010192{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10194
10195 if (is_guest_mode(vcpu) &&
10196 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10197 return;
10198
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010199 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010200 vmcs_write32(TPR_THRESHOLD, 0);
10201 return;
10202 }
10203
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010204 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010205}
10206
Jim Mattson8d860bb2018-05-09 16:56:05 -040010207static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010208{
10209 u32 sec_exec_control;
10210
Jim Mattson8d860bb2018-05-09 16:56:05 -040010211 if (!lapic_in_kernel(vcpu))
10212 return;
10213
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010214 /* Postpone execution until vmcs01 is the current VMCS. */
10215 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010216 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010217 return;
10218 }
10219
Paolo Bonzini35754c92015-07-29 12:05:37 +020010220 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +080010221 return;
10222
10223 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010224 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10225 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010226
Jim Mattson8d860bb2018-05-09 16:56:05 -040010227 switch (kvm_get_apic_mode(vcpu)) {
10228 case LAPIC_MODE_INVALID:
10229 WARN_ONCE(true, "Invalid local APIC state");
10230 case LAPIC_MODE_DISABLED:
10231 break;
10232 case LAPIC_MODE_XAPIC:
10233 if (flexpriority_enabled) {
10234 sec_exec_control |=
10235 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10236 vmx_flush_tlb(vcpu, true);
10237 }
10238 break;
10239 case LAPIC_MODE_X2APIC:
10240 if (cpu_has_vmx_virtualize_x2apic_mode())
10241 sec_exec_control |=
10242 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10243 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010244 }
10245 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10246
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010247 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010248}
10249
Tang Chen38b99172014-09-24 15:57:54 +080010250static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10251{
Jim Mattsonab5df312018-05-09 17:02:03 -040010252 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010253 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010254 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010255 }
Tang Chen38b99172014-09-24 15:57:54 +080010256}
10257
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010258static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010259{
10260 u16 status;
10261 u8 old;
10262
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010263 if (max_isr == -1)
10264 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010265
10266 status = vmcs_read16(GUEST_INTR_STATUS);
10267 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010268 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010269 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010270 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010271 vmcs_write16(GUEST_INTR_STATUS, status);
10272 }
10273}
10274
10275static void vmx_set_rvi(int vector)
10276{
10277 u16 status;
10278 u8 old;
10279
Wei Wang4114c272014-11-05 10:53:43 +080010280 if (vector == -1)
10281 vector = 0;
10282
Yang Zhangc7c9c562013-01-25 10:18:51 +080010283 status = vmcs_read16(GUEST_INTR_STATUS);
10284 old = (u8)status & 0xff;
10285 if ((u8)vector != old) {
10286 status &= ~0xff;
10287 status |= (u8)vector;
10288 vmcs_write16(GUEST_INTR_STATUS, status);
10289 }
10290}
10291
10292static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10293{
Liran Alon851c1a182017-12-24 18:12:56 +020010294 /*
10295 * When running L2, updating RVI is only relevant when
10296 * vmcs12 virtual-interrupt-delivery enabled.
10297 * However, it can be enabled only when L1 also
10298 * intercepts external-interrupts and in that case
10299 * we should not update vmcs02 RVI but instead intercept
10300 * interrupt. Therefore, do nothing when running L2.
10301 */
10302 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010303 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010304}
10305
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010306static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010307{
10308 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010309 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010310 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010311
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010312 WARN_ON(!vcpu->arch.apicv_active);
10313 if (pi_test_on(&vmx->pi_desc)) {
10314 pi_clear_on(&vmx->pi_desc);
10315 /*
10316 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10317 * But on x86 this is just a compiler barrier anyway.
10318 */
10319 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010320 max_irr_updated =
10321 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10322
10323 /*
10324 * If we are running L2 and L1 has a new pending interrupt
10325 * which can be injected, we should re-evaluate
10326 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010327 * If L1 intercepts external-interrupts, we should
10328 * exit from L2 to L1. Otherwise, interrupt should be
10329 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010330 */
Liran Alon851c1a182017-12-24 18:12:56 +020010331 if (is_guest_mode(vcpu) && max_irr_updated) {
10332 if (nested_exit_on_intr(vcpu))
10333 kvm_vcpu_exiting_guest_mode(vcpu);
10334 else
10335 kvm_make_request(KVM_REQ_EVENT, vcpu);
10336 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010337 } else {
10338 max_irr = kvm_lapic_find_highest_irr(vcpu);
10339 }
10340 vmx_hwapic_irr_update(vcpu, max_irr);
10341 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010342}
10343
Andrey Smetanin63086302015-11-10 15:36:32 +030010344static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010345{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010346 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010347 return;
10348
Yang Zhangc7c9c562013-01-25 10:18:51 +080010349 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10350 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10351 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10352 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10353}
10354
Paolo Bonzini967235d2016-12-19 14:03:45 +010010355static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10356{
10357 struct vcpu_vmx *vmx = to_vmx(vcpu);
10358
10359 pi_clear_on(&vmx->pi_desc);
10360 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10361}
10362
Avi Kivity51aa01d2010-07-20 14:31:20 +030010363static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010364{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010365 u32 exit_intr_info = 0;
10366 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010367
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010368 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10369 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010370 return;
10371
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010372 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10373 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10374 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010375
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010376 /* if exit due to PF check for async PF */
10377 if (is_page_fault(exit_intr_info))
10378 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10379
Andi Kleena0861c02009-06-08 17:37:09 +080010380 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010381 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10382 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010383 kvm_machine_check();
10384
Gleb Natapov20f65982009-05-11 13:35:55 +030010385 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010386 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010387 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010388 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010389 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010390 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010391}
Gleb Natapov20f65982009-05-11 13:35:55 +030010392
Yang Zhanga547c6d2013-04-11 19:25:10 +080010393static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10394{
10395 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10396
Yang Zhanga547c6d2013-04-11 19:25:10 +080010397 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10398 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10399 unsigned int vector;
10400 unsigned long entry;
10401 gate_desc *desc;
10402 struct vcpu_vmx *vmx = to_vmx(vcpu);
10403#ifdef CONFIG_X86_64
10404 unsigned long tmp;
10405#endif
10406
10407 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10408 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010409 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010410 asm volatile(
10411#ifdef CONFIG_X86_64
10412 "mov %%" _ASM_SP ", %[sp]\n\t"
10413 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10414 "push $%c[ss]\n\t"
10415 "push %[sp]\n\t"
10416#endif
10417 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010418 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010419 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010420 :
10421#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010422 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010423#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010424 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010425 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010426 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010427 [ss]"i"(__KERNEL_DS),
10428 [cs]"i"(__KERNEL_CS)
10429 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010430 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010431}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010432STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010433
Tom Lendackybc226f02018-05-10 22:06:39 +020010434static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010435{
Tom Lendackybc226f02018-05-10 22:06:39 +020010436 switch (index) {
10437 case MSR_IA32_SMBASE:
10438 /*
10439 * We cannot do SMM unless we can run the guest in big
10440 * real mode.
10441 */
10442 return enable_unrestricted_guest || emulate_invalid_guest_state;
10443 case MSR_AMD64_VIRT_SPEC_CTRL:
10444 /* This is AMD only. */
10445 return false;
10446 default:
10447 return true;
10448 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010449}
10450
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010451static bool vmx_mpx_supported(void)
10452{
10453 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10454 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10455}
10456
Wanpeng Li55412b22014-12-02 19:21:30 +080010457static bool vmx_xsaves_supported(void)
10458{
10459 return vmcs_config.cpu_based_2nd_exec_ctrl &
10460 SECONDARY_EXEC_XSAVES;
10461}
10462
Avi Kivity51aa01d2010-07-20 14:31:20 +030010463static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10464{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010465 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010466 bool unblock_nmi;
10467 u8 vector;
10468 bool idtv_info_valid;
10469
10470 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010471
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010472 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010473 if (vmx->loaded_vmcs->nmi_known_unmasked)
10474 return;
10475 /*
10476 * Can't use vmx->exit_intr_info since we're not sure what
10477 * the exit reason is.
10478 */
10479 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10480 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10481 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10482 /*
10483 * SDM 3: 27.7.1.2 (September 2008)
10484 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10485 * a guest IRET fault.
10486 * SDM 3: 23.2.2 (September 2008)
10487 * Bit 12 is undefined in any of the following cases:
10488 * If the VM exit sets the valid bit in the IDT-vectoring
10489 * information field.
10490 * If the VM exit is due to a double fault.
10491 */
10492 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10493 vector != DF_VECTOR && !idtv_info_valid)
10494 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10495 GUEST_INTR_STATE_NMI);
10496 else
10497 vmx->loaded_vmcs->nmi_known_unmasked =
10498 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10499 & GUEST_INTR_STATE_NMI);
10500 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10501 vmx->loaded_vmcs->vnmi_blocked_time +=
10502 ktime_to_ns(ktime_sub(ktime_get(),
10503 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010504}
10505
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010506static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010507 u32 idt_vectoring_info,
10508 int instr_len_field,
10509 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010510{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010511 u8 vector;
10512 int type;
10513 bool idtv_info_valid;
10514
10515 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010516
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010517 vcpu->arch.nmi_injected = false;
10518 kvm_clear_exception_queue(vcpu);
10519 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010520
10521 if (!idtv_info_valid)
10522 return;
10523
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010524 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010525
Avi Kivity668f6122008-07-02 09:28:55 +030010526 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10527 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010528
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010529 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010530 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010531 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010532 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010533 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010534 * Clear bit "block by NMI" before VM entry if a NMI
10535 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010536 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010537 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010538 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010539 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010540 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010541 /* fall through */
10542 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010543 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010544 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010545 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010546 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010547 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010548 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010549 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010550 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010551 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010552 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010553 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554 break;
10555 default:
10556 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010557 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010558}
10559
Avi Kivity83422e12010-07-20 14:43:23 +030010560static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10561{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010562 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010563 VM_EXIT_INSTRUCTION_LEN,
10564 IDT_VECTORING_ERROR_CODE);
10565}
10566
Avi Kivityb463a6f2010-07-20 15:06:17 +030010567static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10568{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010569 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010570 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10571 VM_ENTRY_INSTRUCTION_LEN,
10572 VM_ENTRY_EXCEPTION_ERROR_CODE);
10573
10574 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10575}
10576
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010577static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10578{
10579 int i, nr_msrs;
10580 struct perf_guest_switch_msr *msrs;
10581
10582 msrs = perf_guest_get_msrs(&nr_msrs);
10583
10584 if (!msrs)
10585 return;
10586
10587 for (i = 0; i < nr_msrs; i++)
10588 if (msrs[i].host == msrs[i].guest)
10589 clear_atomic_switch_msr(vmx, msrs[i].msr);
10590 else
10591 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010592 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010593}
10594
Jiang Biao33365e72016-11-03 15:03:37 +080010595static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010596{
10597 struct vcpu_vmx *vmx = to_vmx(vcpu);
10598 u64 tscl;
10599 u32 delta_tsc;
10600
10601 if (vmx->hv_deadline_tsc == -1)
10602 return;
10603
10604 tscl = rdtsc();
10605 if (vmx->hv_deadline_tsc > tscl)
10606 /* sure to be 32 bit only because checked on set_hv_timer */
10607 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10608 cpu_preemption_timer_multi);
10609 else
10610 delta_tsc = 0;
10611
10612 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10613}
10614
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010615static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010616{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010617 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010618 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010619
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010620 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010621 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010622 vmx->loaded_vmcs->soft_vnmi_blocked))
10623 vmx->loaded_vmcs->entry_time = ktime_get();
10624
Avi Kivity104f2262010-11-18 13:12:52 +020010625 /* Don't enter VMX if guest state is invalid, let the exit handler
10626 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010627 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010628 return;
10629
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010630 if (vmx->ple_window_dirty) {
10631 vmx->ple_window_dirty = false;
10632 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10633 }
10634
Abel Gordon012f83c2013-04-18 14:39:25 +030010635 if (vmx->nested.sync_shadow_vmcs) {
10636 copy_vmcs12_to_shadow(vmx);
10637 vmx->nested.sync_shadow_vmcs = false;
10638 }
10639
Avi Kivity104f2262010-11-18 13:12:52 +020010640 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10641 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10642 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10643 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10644
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010645 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010646 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010647 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010648 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010649 }
10650
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010651 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010652 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010653 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010654 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010655 }
10656
Avi Kivity104f2262010-11-18 13:12:52 +020010657 /* When single-stepping over STI and MOV SS, we must clear the
10658 * corresponding interruptibility bits in the guest state. Otherwise
10659 * vmentry fails as it then expects bit 14 (BS) in pending debug
10660 * exceptions being set, but that's not correct for the guest debugging
10661 * case. */
10662 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10663 vmx_set_interrupt_shadow(vcpu, 0);
10664
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010665 if (static_cpu_has(X86_FEATURE_PKU) &&
10666 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10667 vcpu->arch.pkru != vmx->host_pkru)
10668 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010669
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010670 atomic_switch_perf_msrs(vmx);
10671
Yunhong Jiang64672c92016-06-13 14:19:59 -070010672 vmx_arm_hv_timer(vcpu);
10673
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010674 /*
10675 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10676 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10677 * is no need to worry about the conditional branch over the wrmsr
10678 * being speculatively taken.
10679 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010680 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010681
Nadav Har'Eld462b812011-05-24 15:26:10 +030010682 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010683
10684 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10685 (unsigned long)&current_evmcs->host_rsp : 0;
10686
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010687 if (static_branch_unlikely(&vmx_l1d_should_flush))
10688 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010689
Avi Kivity104f2262010-11-18 13:12:52 +020010690 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010691 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010692 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10693 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10694 "push %%" _ASM_CX " \n\t"
10695 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010696 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010697 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010698 /* Avoid VMWRITE when Enlightened VMCS is in use */
10699 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10700 "jz 2f \n\t"
10701 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10702 "jmp 1f \n\t"
10703 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010704 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010705 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010706 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010707 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10708 "mov %%cr2, %%" _ASM_DX " \n\t"
10709 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010710 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010711 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010712 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010713 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010714 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010715 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010716 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10717 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10718 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10719 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10720 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10721 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010722#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010723 "mov %c[r8](%0), %%r8 \n\t"
10724 "mov %c[r9](%0), %%r9 \n\t"
10725 "mov %c[r10](%0), %%r10 \n\t"
10726 "mov %c[r11](%0), %%r11 \n\t"
10727 "mov %c[r12](%0), %%r12 \n\t"
10728 "mov %c[r13](%0), %%r13 \n\t"
10729 "mov %c[r14](%0), %%r14 \n\t"
10730 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010731#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010732 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010733
Avi Kivity6aa8b732006-12-10 02:21:36 -080010734 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010735 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010736 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010737 "jmp 2f \n\t"
10738 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10739 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010740 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010741 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010742 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010743 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010744 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10745 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10746 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10747 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10748 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10749 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10750 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010751#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010752 "mov %%r8, %c[r8](%0) \n\t"
10753 "mov %%r9, %c[r9](%0) \n\t"
10754 "mov %%r10, %c[r10](%0) \n\t"
10755 "mov %%r11, %c[r11](%0) \n\t"
10756 "mov %%r12, %c[r12](%0) \n\t"
10757 "mov %%r13, %c[r13](%0) \n\t"
10758 "mov %%r14, %c[r14](%0) \n\t"
10759 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010760 "xor %%r8d, %%r8d \n\t"
10761 "xor %%r9d, %%r9d \n\t"
10762 "xor %%r10d, %%r10d \n\t"
10763 "xor %%r11d, %%r11d \n\t"
10764 "xor %%r12d, %%r12d \n\t"
10765 "xor %%r13d, %%r13d \n\t"
10766 "xor %%r14d, %%r14d \n\t"
10767 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010768#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010769 "mov %%cr2, %%" _ASM_AX " \n\t"
10770 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010771
Jim Mattson0cb5b302018-01-03 14:31:38 -080010772 "xor %%eax, %%eax \n\t"
10773 "xor %%ebx, %%ebx \n\t"
10774 "xor %%esi, %%esi \n\t"
10775 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010776 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010777 ".pushsection .rodata \n\t"
10778 ".global vmx_return \n\t"
10779 "vmx_return: " _ASM_PTR " 2b \n\t"
10780 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010781 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010782 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010783 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010784 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010785 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10786 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10787 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10788 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10789 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10790 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10791 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010792#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010793 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10794 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10795 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10796 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10797 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10798 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10799 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10800 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010801#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010802 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10803 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010804 : "cc", "memory"
10805#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010806 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010807 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010808#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010809 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010810#endif
10811 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010812
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010813 /*
10814 * We do not use IBRS in the kernel. If this vCPU has used the
10815 * SPEC_CTRL MSR it may have left it on; save the value and
10816 * turn it off. This is much more efficient than blindly adding
10817 * it to the atomic save/restore list. Especially as the former
10818 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10819 *
10820 * For non-nested case:
10821 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10822 * save it.
10823 *
10824 * For nested case:
10825 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10826 * save it.
10827 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010828 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010829 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010830
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010831 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010832
David Woodhouse117cc7a2018-01-12 11:11:27 +000010833 /* Eliminate branch target predictions from guest mode */
10834 vmexit_fill_RSB();
10835
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010836 /* All fields are clean at this point */
10837 if (static_branch_unlikely(&enable_evmcs))
10838 current_evmcs->hv_clean_fields |=
10839 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10840
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010841 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010842 if (vmx->host_debugctlmsr)
10843 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010844
Avi Kivityaa67f602012-08-01 16:48:03 +030010845#ifndef CONFIG_X86_64
10846 /*
10847 * The sysexit path does not restore ds/es, so we must set them to
10848 * a reasonable value ourselves.
10849 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010850 * We can't defer this to vmx_prepare_switch_to_host() since that
10851 * function may be executed in interrupt context, which saves and
10852 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010853 */
10854 loadsegment(ds, __USER_DS);
10855 loadsegment(es, __USER_DS);
10856#endif
10857
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010858 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010859 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010860 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010861 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010862 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010863 vcpu->arch.regs_dirty = 0;
10864
Gleb Natapove0b890d2013-09-25 12:51:33 +030010865 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010866 * eager fpu is enabled if PKEY is supported and CR4 is switched
10867 * back on host, so it is safe to read guest PKRU from current
10868 * XSAVE.
10869 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010870 if (static_cpu_has(X86_FEATURE_PKU) &&
10871 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10872 vcpu->arch.pkru = __read_pkru();
10873 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010874 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010875 }
10876
Gleb Natapove0b890d2013-09-25 12:51:33 +030010877 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010878 vmx->idt_vectoring_info = 0;
10879
10880 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10881 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10882 return;
10883
10884 vmx->loaded_vmcs->launched = 1;
10885 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010886
Avi Kivity51aa01d2010-07-20 14:31:20 +030010887 vmx_complete_atomic_exit(vmx);
10888 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010889 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010890}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010891STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010892
Sean Christopherson434a1e92018-03-20 12:17:18 -070010893static struct kvm *vmx_vm_alloc(void)
10894{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010895 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010896 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010897}
10898
10899static void vmx_vm_free(struct kvm *kvm)
10900{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010901 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010902}
10903
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010904static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010905{
10906 struct vcpu_vmx *vmx = to_vmx(vcpu);
10907 int cpu;
10908
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010909 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010910 return;
10911
10912 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010913 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010914 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010915 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010916 put_cpu();
10917}
10918
Jim Mattson2f1fe812016-07-08 15:36:06 -070010919/*
10920 * Ensure that the current vmcs of the logical processor is the
10921 * vmcs01 of the vcpu before calling free_nested().
10922 */
10923static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10924{
10925 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010926
Christoffer Dallec7660c2017-12-04 21:35:23 +010010927 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010928 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010929 free_nested(vmx);
10930 vcpu_put(vcpu);
10931}
10932
Avi Kivity6aa8b732006-12-10 02:21:36 -080010933static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10934{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010935 struct vcpu_vmx *vmx = to_vmx(vcpu);
10936
Kai Huang843e4332015-01-28 10:54:28 +080010937 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010938 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010939 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010940 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010941 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010942 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010943 kfree(vmx->guest_msrs);
10944 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010945 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010946}
10947
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010948static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010949{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010950 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010951 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010952 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010953 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010954
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010955 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010956 return ERR_PTR(-ENOMEM);
10957
Wanpeng Li991e7a02015-09-16 17:30:05 +080010958 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010959
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010960 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10961 if (err)
10962 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010963
Peter Feiner4e595162016-07-07 14:49:58 -070010964 err = -ENOMEM;
10965
10966 /*
10967 * If PML is turned on, failure on enabling PML just results in failure
10968 * of creating the vcpu, therefore we can simplify PML logic (by
10969 * avoiding dealing with cases, such as enabling PML partially on vcpus
10970 * for the guest, etc.
10971 */
10972 if (enable_pml) {
10973 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10974 if (!vmx->pml_pg)
10975 goto uninit_vcpu;
10976 }
10977
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010978 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010979 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10980 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010981
Peter Feiner4e595162016-07-07 14:49:58 -070010982 if (!vmx->guest_msrs)
10983 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010984
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010985 err = alloc_loaded_vmcs(&vmx->vmcs01);
10986 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010987 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010988
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010989 msr_bitmap = vmx->vmcs01.msr_bitmap;
10990 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10991 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10992 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10993 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10994 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10995 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10996 vmx->msr_bitmap_mode = 0;
10997
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010998 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010999 cpu = get_cpu();
11000 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011001 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011002 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011003 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011004 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011005 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011006 err = alloc_apic_access_page(kvm);
11007 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011008 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011009 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011010
Sean Christophersone90008d2018-03-05 12:04:37 -080011011 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011012 err = init_rmode_identity_map(kvm);
11013 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011014 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011015 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011016
Roman Kagan63aff652018-07-19 21:59:07 +030011017 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011018 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11019 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011020
Wincy Van705699a2015-02-03 23:58:17 +080011021 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011022 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011023
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011024 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11025
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011026 /*
11027 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11028 * or POSTED_INTR_WAKEUP_VECTOR.
11029 */
11030 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11031 vmx->pi_desc.sn = 1;
11032
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011033 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011034
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011035free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011036 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011037free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011038 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011039free_pml:
11040 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011041uninit_vcpu:
11042 kvm_vcpu_uninit(&vmx->vcpu);
11043free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011044 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011045 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011046 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011047}
11048
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011049#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"
11050#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 -040011051
Wanpeng Lib31c1142018-03-12 04:53:04 -070011052static int vmx_vm_init(struct kvm *kvm)
11053{
Tianyu Lan877ad952018-07-19 08:40:23 +000011054 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11055
Wanpeng Lib31c1142018-03-12 04:53:04 -070011056 if (!ple_gap)
11057 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011058
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011059 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11060 switch (l1tf_mitigation) {
11061 case L1TF_MITIGATION_OFF:
11062 case L1TF_MITIGATION_FLUSH_NOWARN:
11063 /* 'I explicitly don't care' is set */
11064 break;
11065 case L1TF_MITIGATION_FLUSH:
11066 case L1TF_MITIGATION_FLUSH_NOSMT:
11067 case L1TF_MITIGATION_FULL:
11068 /*
11069 * Warn upon starting the first VM in a potentially
11070 * insecure environment.
11071 */
11072 if (cpu_smt_control == CPU_SMT_ENABLED)
11073 pr_warn_once(L1TF_MSG_SMT);
11074 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11075 pr_warn_once(L1TF_MSG_L1D);
11076 break;
11077 case L1TF_MITIGATION_FULL_FORCE:
11078 /* Flush is enforced */
11079 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011080 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011081 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011082 return 0;
11083}
11084
Yang, Sheng002c7f72007-07-31 14:23:01 +030011085static void __init vmx_check_processor_compat(void *rtn)
11086{
11087 struct vmcs_config vmcs_conf;
11088
11089 *(int *)rtn = 0;
11090 if (setup_vmcs_config(&vmcs_conf) < 0)
11091 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011092 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011093 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11094 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11095 smp_processor_id());
11096 *(int *)rtn = -EIO;
11097 }
11098}
11099
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011100static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011101{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011102 u8 cache;
11103 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011104
Sheng Yang522c68c2009-04-27 20:35:43 +080011105 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011106 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011107 * 2. EPT with VT-d:
11108 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011109 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011110 * b. VT-d with snooping control feature: snooping control feature of
11111 * VT-d engine can guarantee the cache correctness. Just set it
11112 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011113 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011114 * consistent with host MTRR
11115 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011116 if (is_mmio) {
11117 cache = MTRR_TYPE_UNCACHABLE;
11118 goto exit;
11119 }
11120
11121 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011122 ipat = VMX_EPT_IPAT_BIT;
11123 cache = MTRR_TYPE_WRBACK;
11124 goto exit;
11125 }
11126
11127 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11128 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011129 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011130 cache = MTRR_TYPE_WRBACK;
11131 else
11132 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011133 goto exit;
11134 }
11135
Xiao Guangrongff536042015-06-15 16:55:22 +080011136 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011137
11138exit:
11139 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011140}
11141
Sheng Yang17cc3932010-01-05 19:02:27 +080011142static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011143{
Sheng Yang878403b2010-01-05 19:02:29 +080011144 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11145 return PT_DIRECTORY_LEVEL;
11146 else
11147 /* For shadow and EPT supported 1GB page */
11148 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011149}
11150
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011151static void vmcs_set_secondary_exec_control(u32 new_ctl)
11152{
11153 /*
11154 * These bits in the secondary execution controls field
11155 * are dynamic, the others are mostly based on the hypervisor
11156 * architecture and the guest's CPUID. Do not touch the
11157 * dynamic bits.
11158 */
11159 u32 mask =
11160 SECONDARY_EXEC_SHADOW_VMCS |
11161 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011162 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11163 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011164
11165 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11166
11167 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11168 (new_ctl & ~mask) | (cur_ctl & mask));
11169}
11170
David Matlack8322ebb2016-11-29 18:14:09 -080011171/*
11172 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11173 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11174 */
11175static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11176{
11177 struct vcpu_vmx *vmx = to_vmx(vcpu);
11178 struct kvm_cpuid_entry2 *entry;
11179
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011180 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11181 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011182
11183#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11184 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011185 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011186} while (0)
11187
11188 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11189 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11190 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11191 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11192 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11193 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11194 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11195 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11196 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11197 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11198 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11199 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11200 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11201 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11202 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11203
11204 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11205 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11206 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11207 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11208 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011209 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011210
11211#undef cr4_fixed1_update
11212}
11213
Sheng Yang0e851882009-12-18 16:48:46 +080011214static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11215{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011216 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011217
Paolo Bonzini80154d72017-08-24 13:55:35 +020011218 if (cpu_has_secondary_exec_ctrls()) {
11219 vmx_compute_secondary_exec_control(vmx);
11220 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011221 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011222
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011223 if (nested_vmx_allowed(vcpu))
11224 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11225 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11226 else
11227 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11228 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011229
11230 if (nested_vmx_allowed(vcpu))
11231 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080011232}
11233
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011234static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11235{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011236 if (func == 1 && nested)
11237 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011238}
11239
Yang Zhang25d92082013-08-06 12:00:32 +030011240static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11241 struct x86_exception *fault)
11242{
Jan Kiszka533558b2014-01-04 18:47:20 +010011243 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011244 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011245 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011246 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011247
Bandan Dasc5f983f2017-05-05 15:25:14 -040011248 if (vmx->nested.pml_full) {
11249 exit_reason = EXIT_REASON_PML_FULL;
11250 vmx->nested.pml_full = false;
11251 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11252 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011253 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011254 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011255 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011256
11257 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011258 vmcs12->guest_physical_address = fault->address;
11259}
11260
Peter Feiner995f00a2017-06-30 17:26:32 -070011261static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11262{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011263 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011264}
11265
Nadav Har'El155a97a2013-08-05 11:07:16 +030011266/* Callbacks for nested_ept_init_mmu_context: */
11267
11268static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11269{
11270 /* return the page table to be shadowed - in our case, EPT12 */
11271 return get_vmcs12(vcpu)->ept_pointer;
11272}
11273
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011274static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011275{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011276 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011277 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011278 return 1;
11279
Paolo Bonziniad896af2013-10-02 16:56:14 +020011280 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011281 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011282 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011283 nested_ept_ad_enabled(vcpu),
11284 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011285 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11286 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11287 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11288
11289 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011290 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011291}
11292
11293static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11294{
11295 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11296}
11297
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011298static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11299 u16 error_code)
11300{
11301 bool inequality, bit;
11302
11303 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11304 inequality =
11305 (error_code & vmcs12->page_fault_error_code_mask) !=
11306 vmcs12->page_fault_error_code_match;
11307 return inequality ^ bit;
11308}
11309
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011310static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11311 struct x86_exception *fault)
11312{
11313 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11314
11315 WARN_ON(!is_guest_mode(vcpu));
11316
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011317 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11318 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011319 vmcs12->vm_exit_intr_error_code = fault->error_code;
11320 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11321 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11322 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11323 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011324 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011325 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011326 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011327}
11328
Paolo Bonzinic9923842017-12-13 14:16:30 +010011329static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11330 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011331
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011332static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011333{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011334 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011335 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011336 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011337 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011338
11339 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011340 /*
11341 * Translate L1 physical address to host physical
11342 * address for vmcs02. Keep the page pinned, so this
11343 * physical address remains valid. We keep a reference
11344 * to it so we can release it later.
11345 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011346 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011347 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011348 vmx->nested.apic_access_page = NULL;
11349 }
11350 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011351 /*
11352 * If translation failed, no matter: This feature asks
11353 * to exit when accessing the given address, and if it
11354 * can never be accessed, this feature won't do
11355 * anything anyway.
11356 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011357 if (!is_error_page(page)) {
11358 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011359 hpa = page_to_phys(vmx->nested.apic_access_page);
11360 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11361 } else {
11362 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11363 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11364 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011365 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011366
11367 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011368 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011369 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011370 vmx->nested.virtual_apic_page = NULL;
11371 }
11372 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011373
11374 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011375 * If translation failed, VM entry will fail because
11376 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11377 * Failing the vm entry is _not_ what the processor
11378 * does but it's basically the only possibility we
11379 * have. We could still enter the guest if CR8 load
11380 * exits are enabled, CR8 store exits are enabled, and
11381 * virtualize APIC access is disabled; in this case
11382 * the processor would never use the TPR shadow and we
11383 * could simply clear the bit from the execution
11384 * control. But such a configuration is useless, so
11385 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011386 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011387 if (!is_error_page(page)) {
11388 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011389 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11390 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11391 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011392 }
11393
Wincy Van705699a2015-02-03 23:58:17 +080011394 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011395 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11396 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011397 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011398 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011399 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011400 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11401 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011402 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011403 vmx->nested.pi_desc_page = page;
11404 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011405 vmx->nested.pi_desc =
11406 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11407 (unsigned long)(vmcs12->posted_intr_desc_addr &
11408 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011409 vmcs_write64(POSTED_INTR_DESC_ADDR,
11410 page_to_phys(vmx->nested.pi_desc_page) +
11411 (unsigned long)(vmcs12->posted_intr_desc_addr &
11412 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011413 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011414 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011415 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11416 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011417 else
11418 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11419 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011420}
11421
Jan Kiszkaf4124502014-03-07 20:03:13 +010011422static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11423{
11424 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11425 struct vcpu_vmx *vmx = to_vmx(vcpu);
11426
11427 if (vcpu->arch.virtual_tsc_khz == 0)
11428 return;
11429
11430 /* Make sure short timeouts reliably trigger an immediate vmexit.
11431 * hrtimer_start does not guarantee this. */
11432 if (preemption_timeout <= 1) {
11433 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11434 return;
11435 }
11436
11437 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11438 preemption_timeout *= 1000000;
11439 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11440 hrtimer_start(&vmx->nested.preemption_timer,
11441 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11442}
11443
Jim Mattson56a20512017-07-06 16:33:06 -070011444static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11445 struct vmcs12 *vmcs12)
11446{
11447 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11448 return 0;
11449
11450 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11451 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11452 return -EINVAL;
11453
11454 return 0;
11455}
11456
Wincy Van3af18d92015-02-03 23:49:31 +080011457static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11458 struct vmcs12 *vmcs12)
11459{
Wincy Van3af18d92015-02-03 23:49:31 +080011460 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11461 return 0;
11462
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011463 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011464 return -EINVAL;
11465
11466 return 0;
11467}
11468
Jim Mattson712b12d2017-08-24 13:24:47 -070011469static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11470 struct vmcs12 *vmcs12)
11471{
11472 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11473 return 0;
11474
11475 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11476 return -EINVAL;
11477
11478 return 0;
11479}
11480
Wincy Van3af18d92015-02-03 23:49:31 +080011481/*
11482 * Merge L0's and L1's MSR bitmap, return false to indicate that
11483 * we do not use the hardware.
11484 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011485static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11486 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011487{
Wincy Van82f0dd42015-02-03 23:57:18 +080011488 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011489 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011490 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011491 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011492 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011493 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011494 *
11495 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11496 * ensures that we do not accidentally generate an L02 MSR bitmap
11497 * from the L12 MSR bitmap that is too permissive.
11498 * 2. That L1 or L2s have actually used the MSR. This avoids
11499 * unnecessarily merging of the bitmap if the MSR is unused. This
11500 * works properly because we only update the L01 MSR bitmap lazily.
11501 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11502 * updated to reflect this when L1 (or its L2s) actually write to
11503 * the MSR.
11504 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011505 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11506 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011507
Paolo Bonzinic9923842017-12-13 14:16:30 +010011508 /* Nothing to do if the MSR bitmap is not in use. */
11509 if (!cpu_has_vmx_msr_bitmap() ||
11510 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11511 return false;
11512
Ashok Raj15d45072018-02-01 22:59:43 +010011513 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011514 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011515 return false;
11516
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011517 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11518 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011519 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011520
Radim Krčmářd048c092016-08-08 20:16:22 +020011521 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011522 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11523 /*
11524 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11525 * just lets the processor take the value from the virtual-APIC page;
11526 * take those 256 bits directly from the L1 bitmap.
11527 */
11528 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11529 unsigned word = msr / BITS_PER_LONG;
11530 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11531 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011532 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011533 } else {
11534 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11535 unsigned word = msr / BITS_PER_LONG;
11536 msr_bitmap_l0[word] = ~0;
11537 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11538 }
11539 }
11540
11541 nested_vmx_disable_intercept_for_msr(
11542 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011543 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011544 MSR_TYPE_W);
11545
11546 if (nested_cpu_has_vid(vmcs12)) {
11547 nested_vmx_disable_intercept_for_msr(
11548 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011549 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011550 MSR_TYPE_W);
11551 nested_vmx_disable_intercept_for_msr(
11552 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011553 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011554 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011555 }
Ashok Raj15d45072018-02-01 22:59:43 +010011556
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011557 if (spec_ctrl)
11558 nested_vmx_disable_intercept_for_msr(
11559 msr_bitmap_l1, msr_bitmap_l0,
11560 MSR_IA32_SPEC_CTRL,
11561 MSR_TYPE_R | MSR_TYPE_W);
11562
Ashok Raj15d45072018-02-01 22:59:43 +010011563 if (pred_cmd)
11564 nested_vmx_disable_intercept_for_msr(
11565 msr_bitmap_l1, msr_bitmap_l0,
11566 MSR_IA32_PRED_CMD,
11567 MSR_TYPE_W);
11568
Wincy Vanf2b93282015-02-03 23:56:03 +080011569 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011570 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011571
11572 return true;
11573}
11574
Liran Alon61ada742018-06-23 02:35:08 +030011575static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11576 struct vmcs12 *vmcs12)
11577{
11578 struct vmcs12 *shadow;
11579 struct page *page;
11580
11581 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11582 vmcs12->vmcs_link_pointer == -1ull)
11583 return;
11584
11585 shadow = get_shadow_vmcs12(vcpu);
11586 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11587
11588 memcpy(shadow, kmap(page), VMCS12_SIZE);
11589
11590 kunmap(page);
11591 kvm_release_page_clean(page);
11592}
11593
11594static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11595 struct vmcs12 *vmcs12)
11596{
11597 struct vcpu_vmx *vmx = to_vmx(vcpu);
11598
11599 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11600 vmcs12->vmcs_link_pointer == -1ull)
11601 return;
11602
11603 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11604 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11605}
11606
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011607static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11608 struct vmcs12 *vmcs12)
11609{
11610 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11611 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11612 return -EINVAL;
11613 else
11614 return 0;
11615}
11616
Wincy Vanf2b93282015-02-03 23:56:03 +080011617static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11618 struct vmcs12 *vmcs12)
11619{
Wincy Van82f0dd42015-02-03 23:57:18 +080011620 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011621 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011622 !nested_cpu_has_vid(vmcs12) &&
11623 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011624 return 0;
11625
11626 /*
11627 * If virtualize x2apic mode is enabled,
11628 * virtualize apic access must be disabled.
11629 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011630 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11631 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011632 return -EINVAL;
11633
Wincy Van608406e2015-02-03 23:57:51 +080011634 /*
11635 * If virtual interrupt delivery is enabled,
11636 * we must exit on external interrupts.
11637 */
11638 if (nested_cpu_has_vid(vmcs12) &&
11639 !nested_exit_on_intr(vcpu))
11640 return -EINVAL;
11641
Wincy Van705699a2015-02-03 23:58:17 +080011642 /*
11643 * bits 15:8 should be zero in posted_intr_nv,
11644 * the descriptor address has been already checked
11645 * in nested_get_vmcs12_pages.
11646 */
11647 if (nested_cpu_has_posted_intr(vmcs12) &&
11648 (!nested_cpu_has_vid(vmcs12) ||
11649 !nested_exit_intr_ack_set(vcpu) ||
11650 vmcs12->posted_intr_nv & 0xff00))
11651 return -EINVAL;
11652
Wincy Vanf2b93282015-02-03 23:56:03 +080011653 /* tpr shadow is needed by all apicv features. */
11654 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11655 return -EINVAL;
11656
11657 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011658}
11659
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011660static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11661 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011662 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011663{
Liran Alone2536742018-06-23 02:35:02 +030011664 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011665 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011666 u64 count, addr;
11667
Liran Alone2536742018-06-23 02:35:02 +030011668 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11669 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011670 WARN_ON(1);
11671 return -EINVAL;
11672 }
11673 if (count == 0)
11674 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011675 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011676 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11677 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011678 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011679 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11680 addr_field, maxphyaddr, count, addr);
11681 return -EINVAL;
11682 }
11683 return 0;
11684}
11685
11686static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11687 struct vmcs12 *vmcs12)
11688{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011689 if (vmcs12->vm_exit_msr_load_count == 0 &&
11690 vmcs12->vm_exit_msr_store_count == 0 &&
11691 vmcs12->vm_entry_msr_load_count == 0)
11692 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011693 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011694 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011695 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011696 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011697 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011698 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011699 return -EINVAL;
11700 return 0;
11701}
11702
Bandan Dasc5f983f2017-05-05 15:25:14 -040011703static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11704 struct vmcs12 *vmcs12)
11705{
11706 u64 address = vmcs12->pml_address;
11707 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11708
11709 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11710 if (!nested_cpu_has_ept(vmcs12) ||
11711 !IS_ALIGNED(address, 4096) ||
11712 address >> maxphyaddr)
11713 return -EINVAL;
11714 }
11715
11716 return 0;
11717}
11718
Liran Alona8a7c022018-06-23 02:35:06 +030011719static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11720 struct vmcs12 *vmcs12)
11721{
11722 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11723 return 0;
11724
11725 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11726 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11727 return -EINVAL;
11728
11729 return 0;
11730}
11731
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011732static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11733 struct vmx_msr_entry *e)
11734{
11735 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011736 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011737 return -EINVAL;
11738 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11739 e->index == MSR_IA32_UCODE_REV)
11740 return -EINVAL;
11741 if (e->reserved != 0)
11742 return -EINVAL;
11743 return 0;
11744}
11745
11746static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11747 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011748{
11749 if (e->index == MSR_FS_BASE ||
11750 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011751 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11752 nested_vmx_msr_check_common(vcpu, e))
11753 return -EINVAL;
11754 return 0;
11755}
11756
11757static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11758 struct vmx_msr_entry *e)
11759{
11760 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11761 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011762 return -EINVAL;
11763 return 0;
11764}
11765
11766/*
11767 * Load guest's/host's msr at nested entry/exit.
11768 * return 0 for success, entry index for failure.
11769 */
11770static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11771{
11772 u32 i;
11773 struct vmx_msr_entry e;
11774 struct msr_data msr;
11775
11776 msr.host_initiated = false;
11777 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011778 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11779 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011780 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011781 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11782 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011783 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011784 }
11785 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011786 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011787 "%s check failed (%u, 0x%x, 0x%x)\n",
11788 __func__, i, e.index, e.reserved);
11789 goto fail;
11790 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011791 msr.index = e.index;
11792 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011793 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011794 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011795 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11796 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011797 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011798 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011799 }
11800 return 0;
11801fail:
11802 return i + 1;
11803}
11804
11805static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11806{
11807 u32 i;
11808 struct vmx_msr_entry e;
11809
11810 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011811 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011812 if (kvm_vcpu_read_guest(vcpu,
11813 gpa + i * sizeof(e),
11814 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011815 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011816 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11817 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011818 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011819 }
11820 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011821 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011822 "%s check failed (%u, 0x%x, 0x%x)\n",
11823 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011824 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011825 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011826 msr_info.host_initiated = false;
11827 msr_info.index = e.index;
11828 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011829 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011830 "%s cannot read MSR (%u, 0x%x)\n",
11831 __func__, i, e.index);
11832 return -EINVAL;
11833 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011834 if (kvm_vcpu_write_guest(vcpu,
11835 gpa + i * sizeof(e) +
11836 offsetof(struct vmx_msr_entry, value),
11837 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011838 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011839 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011840 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011841 return -EINVAL;
11842 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011843 }
11844 return 0;
11845}
11846
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011847static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11848{
11849 unsigned long invalid_mask;
11850
11851 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11852 return (val & invalid_mask) == 0;
11853}
11854
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011855/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011856 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11857 * emulating VM entry into a guest with EPT enabled.
11858 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11859 * is assigned to entry_failure_code on failure.
11860 */
11861static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011862 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011863{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011864 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011865 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011866 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11867 return 1;
11868 }
11869
11870 /*
11871 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11872 * must not be dereferenced.
11873 */
11874 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11875 !nested_ept) {
11876 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11877 *entry_failure_code = ENTRY_FAIL_PDPTE;
11878 return 1;
11879 }
11880 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011881 }
11882
Junaid Shahid50c28f22018-06-27 14:59:11 -070011883 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011884 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011885
11886 vcpu->arch.cr3 = cr3;
11887 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11888
11889 kvm_init_mmu(vcpu, false);
11890
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011891 return 0;
11892}
11893
Jim Mattson6514dc32018-04-26 16:09:12 -070011894static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011895{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011896 struct vcpu_vmx *vmx = to_vmx(vcpu);
11897
11898 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11899 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11900 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11901 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11902 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11903 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11904 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11905 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11906 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11907 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11908 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11909 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11910 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11911 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11912 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11913 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11914 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11915 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11916 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11917 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11918 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11919 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11920 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11921 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11922 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11923 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11924 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11925 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11926 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11927 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11928 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011929
11930 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11931 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11932 vmcs12->guest_pending_dbg_exceptions);
11933 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11934 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11935
11936 if (nested_cpu_has_xsaves(vmcs12))
11937 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11938 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11939
11940 if (cpu_has_vmx_posted_intr())
11941 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11942
11943 /*
11944 * Whether page-faults are trapped is determined by a combination of
11945 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11946 * If enable_ept, L0 doesn't care about page faults and we should
11947 * set all of these to L1's desires. However, if !enable_ept, L0 does
11948 * care about (at least some) page faults, and because it is not easy
11949 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11950 * to exit on each and every L2 page fault. This is done by setting
11951 * MASK=MATCH=0 and (see below) EB.PF=1.
11952 * Note that below we don't need special code to set EB.PF beyond the
11953 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11954 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11955 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11956 */
11957 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11958 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11959 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11960 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11961
11962 /* All VMFUNCs are currently emulated through L0 vmexits. */
11963 if (cpu_has_vmx_vmfunc())
11964 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11965
11966 if (cpu_has_vmx_apicv()) {
11967 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11968 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11969 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11970 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11971 }
11972
11973 /*
11974 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11975 * Some constant fields are set here by vmx_set_constant_host_state().
11976 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011977 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
11978 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011979 */
11980 vmx_set_constant_host_state(vmx);
11981
11982 /*
11983 * Set the MSR load/store lists to match L0's settings.
11984 */
11985 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011986 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11987 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11988 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11989 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011990
11991 set_cr4_guest_host_mask(vmx);
11992
11993 if (vmx_mpx_supported())
11994 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11995
11996 if (enable_vpid) {
11997 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11998 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11999 else
12000 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12001 }
12002
12003 /*
12004 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12005 */
12006 if (enable_ept) {
12007 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12008 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12009 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12010 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12011 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012012
12013 if (cpu_has_vmx_msr_bitmap())
12014 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012015}
12016
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012017/*
12018 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12019 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012020 * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012021 * guest in a way that will both be appropriate to L1's requests, and our
12022 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12023 * function also has additional necessary side-effects, like setting various
12024 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012025 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12026 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012027 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012028static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012029 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012030{
12031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012032 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012033
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012034 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012035 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012036 vmx->nested.dirty_vmcs12 = false;
12037 }
12038
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012039 /*
12040 * First, the fields that are shadowed. This must be kept in sync
12041 * with vmx_shadow_fields.h.
12042 */
12043
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012044 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012045 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012046 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012047 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12048 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012049
Jim Mattson6514dc32018-04-26 16:09:12 -070012050 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012051 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012052 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12053 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12054 } else {
12055 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12056 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12057 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012058 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012059 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12060 vmcs12->vm_entry_intr_info_field);
12061 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12062 vmcs12->vm_entry_exception_error_code);
12063 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12064 vmcs12->vm_entry_instruction_len);
12065 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12066 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012067 vmx->loaded_vmcs->nmi_known_unmasked =
12068 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012069 } else {
12070 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12071 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012072 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012073
Jan Kiszkaf4124502014-03-07 20:03:13 +010012074 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012075
Paolo Bonzini93140062016-07-06 13:23:51 +020012076 /* Preemption timer setting is only taken from vmcs01. */
12077 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12078 exec_control |= vmcs_config.pin_based_exec_ctrl;
12079 if (vmx->hv_deadline_tsc == -1)
12080 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12081
12082 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012083 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012084 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12085 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012086 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012087 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012088 }
Wincy Van705699a2015-02-03 23:58:17 +080012089
Jan Kiszkaf4124502014-03-07 20:03:13 +010012090 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012091
Jan Kiszkaf4124502014-03-07 20:03:13 +010012092 vmx->nested.preemption_timer_expired = false;
12093 if (nested_cpu_has_preemption_timer(vmcs12))
12094 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012095
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012096 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012097 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012098
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012099 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012100 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012101 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012102 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012103 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012104 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012105 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12106 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012107 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012108 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12109 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12110 ~SECONDARY_EXEC_ENABLE_PML;
12111 exec_control |= vmcs12_exec_ctrl;
12112 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012113
Liran Alon32c7acf2018-06-23 02:35:11 +030012114 /* VMCS shadowing for L2 is emulated for now */
12115 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12116
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012117 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012118 vmcs_write16(GUEST_INTR_STATUS,
12119 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012120
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012121 /*
12122 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12123 * nested_get_vmcs12_pages will either fix it up or
12124 * remove the VM execution control.
12125 */
12126 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12127 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12128
Sean Christopherson0b665d32018-08-14 09:33:34 -070012129 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12130 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12131
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012132 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12133 }
12134
Jim Mattson83bafef2016-10-04 10:48:38 -070012135 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012136 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12137 * entry, but only if the current (host) sp changed from the value
12138 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12139 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12140 * here we just force the write to happen on entry.
12141 */
12142 vmx->host_rsp = 0;
12143
12144 exec_control = vmx_exec_control(vmx); /* L0's desires */
12145 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12146 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12147 exec_control &= ~CPU_BASED_TPR_SHADOW;
12148 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012149
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012150 /*
12151 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12152 * nested_get_vmcs12_pages can't fix it up, the illegal value
12153 * will result in a VM entry failure.
12154 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012155 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012156 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012157 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012158 } else {
12159#ifdef CONFIG_X86_64
12160 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12161 CPU_BASED_CR8_STORE_EXITING;
12162#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012163 }
12164
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012165 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012166 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12167 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012168 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012169 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12170 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12171
12172 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12173
12174 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12175 * bitwise-or of what L1 wants to trap for L2, and what we want to
12176 * trap. Note that CR0.TS also needs updating - we do this later.
12177 */
12178 update_exception_bitmap(vcpu);
12179 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12180 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12181
Nadav Har'El8049d652013-08-05 11:07:06 +030012182 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12183 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12184 * bits are further modified by vmx_set_efer() below.
12185 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012186 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012187
12188 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12189 * emulated by vmx_set_efer(), below.
12190 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012191 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012192 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12193 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012194 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12195
Jim Mattson6514dc32018-04-26 16:09:12 -070012196 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012197 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012198 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012199 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012200 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012201 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012202 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012203
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012204 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12205
Peter Feinerc95ba922016-08-17 09:36:47 -070012206 if (kvm_has_tsc_control)
12207 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012208
12209 if (enable_vpid) {
12210 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012211 * There is no direct mapping between vpid02 and vpid12, the
12212 * vpid02 is per-vCPU for L0 and reused while the value of
12213 * vpid12 is changed w/ one invvpid during nested vmentry.
12214 * The vpid12 is allocated by L1 for L2, so it will not
12215 * influence global bitmap(for vpid01 and vpid02 allocation)
12216 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012217 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012218 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012219 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12220 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012221 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012222 }
12223 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012224 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012225 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012226 }
12227
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012228 if (enable_pml) {
12229 /*
12230 * Conceptually we want to copy the PML address and index from
12231 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12232 * since we always flush the log on each vmexit, this happens
12233 * to be equivalent to simply resetting the fields in vmcs02.
12234 */
12235 ASSERT(vmx->pml_pg);
12236 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12237 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12238 }
12239
Nadav Har'El155a97a2013-08-05 11:07:16 +030012240 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012241 if (nested_ept_init_mmu_context(vcpu)) {
12242 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12243 return 1;
12244 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012245 } else if (nested_cpu_has2(vmcs12,
12246 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012247 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012248 }
12249
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012250 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012251 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12252 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012253 * The CR0_READ_SHADOW is what L2 should have expected to read given
12254 * the specifications by L1; It's not enough to take
12255 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12256 * have more bits than L1 expected.
12257 */
12258 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12259 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12260
12261 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12262 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12263
Jim Mattson6514dc32018-04-26 16:09:12 -070012264 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012265 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012266 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12267 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12268 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12269 else
12270 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12271 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12272 vmx_set_efer(vcpu, vcpu->arch.efer);
12273
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012274 /*
12275 * Guest state is invalid and unrestricted guest is disabled,
12276 * which means L1 attempted VMEntry to L2 with invalid state.
12277 * Fail the VMEntry.
12278 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012279 if (vmx->emulation_required) {
12280 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012281 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012282 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012283
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012284 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012285 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012286 entry_failure_code))
12287 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012288
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012289 if (!enable_ept)
12290 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12291
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012292 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12293 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012294 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012295}
12296
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012297static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12298{
12299 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12300 nested_cpu_has_virtual_nmis(vmcs12))
12301 return -EINVAL;
12302
12303 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12304 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12305 return -EINVAL;
12306
12307 return 0;
12308}
12309
Jim Mattsonca0bde22016-11-30 12:03:46 -080012310static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12311{
12312 struct vcpu_vmx *vmx = to_vmx(vcpu);
12313
12314 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12315 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12316 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12317
Jim Mattson56a20512017-07-06 16:33:06 -070012318 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12319 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12320
Jim Mattsonca0bde22016-11-30 12:03:46 -080012321 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12322 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12323
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012324 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12325 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12326
Jim Mattson712b12d2017-08-24 13:24:47 -070012327 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12328 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12329
Jim Mattsonca0bde22016-11-30 12:03:46 -080012330 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12331 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12332
12333 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12334 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12335
Bandan Dasc5f983f2017-05-05 15:25:14 -040012336 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12337 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12338
Liran Alona8a7c022018-06-23 02:35:06 +030012339 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12340 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12341
Jim Mattsonca0bde22016-11-30 12:03:46 -080012342 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012343 vmx->nested.msrs.procbased_ctls_low,
12344 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012345 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12346 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012347 vmx->nested.msrs.secondary_ctls_low,
12348 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012349 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012350 vmx->nested.msrs.pinbased_ctls_low,
12351 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012352 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012353 vmx->nested.msrs.exit_ctls_low,
12354 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012355 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012356 vmx->nested.msrs.entry_ctls_low,
12357 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012358 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12359
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012360 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012361 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12362
Bandan Das41ab9372017-08-03 15:54:43 -040012363 if (nested_cpu_has_vmfunc(vmcs12)) {
12364 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012365 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012366 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12367
12368 if (nested_cpu_has_eptp_switching(vmcs12)) {
12369 if (!nested_cpu_has_ept(vmcs12) ||
12370 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12371 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12372 }
12373 }
Bandan Das27c42a12017-08-03 15:54:42 -040012374
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012375 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12376 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12377
Jim Mattsonca0bde22016-11-30 12:03:46 -080012378 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12379 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12380 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12381 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12382
Marc Orr04473782018-06-20 17:21:29 -070012383 /*
12384 * From the Intel SDM, volume 3:
12385 * Fields relevant to VM-entry event injection must be set properly.
12386 * These fields are the VM-entry interruption-information field, the
12387 * VM-entry exception error code, and the VM-entry instruction length.
12388 */
12389 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12390 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12391 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12392 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12393 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12394 bool should_have_error_code;
12395 bool urg = nested_cpu_has2(vmcs12,
12396 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12397 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12398
12399 /* VM-entry interruption-info field: interruption type */
12400 if (intr_type == INTR_TYPE_RESERVED ||
12401 (intr_type == INTR_TYPE_OTHER_EVENT &&
12402 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12403 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12404
12405 /* VM-entry interruption-info field: vector */
12406 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12407 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12408 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12409 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12410
12411 /* VM-entry interruption-info field: deliver error code */
12412 should_have_error_code =
12413 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12414 x86_exception_has_error_code(vector);
12415 if (has_error_code != should_have_error_code)
12416 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12417
12418 /* VM-entry exception error code */
12419 if (has_error_code &&
12420 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12421 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12422
12423 /* VM-entry interruption-info field: reserved bits */
12424 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12425 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12426
12427 /* VM-entry instruction length */
12428 switch (intr_type) {
12429 case INTR_TYPE_SOFT_EXCEPTION:
12430 case INTR_TYPE_SOFT_INTR:
12431 case INTR_TYPE_PRIV_SW_EXCEPTION:
12432 if ((vmcs12->vm_entry_instruction_len > 15) ||
12433 (vmcs12->vm_entry_instruction_len == 0 &&
12434 !nested_cpu_has_zero_length_injection(vcpu)))
12435 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12436 }
12437 }
12438
Jim Mattsonca0bde22016-11-30 12:03:46 -080012439 return 0;
12440}
12441
Liran Alonf145d902018-06-23 02:35:07 +030012442static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12443 struct vmcs12 *vmcs12)
12444{
12445 int r;
12446 struct page *page;
12447 struct vmcs12 *shadow;
12448
12449 if (vmcs12->vmcs_link_pointer == -1ull)
12450 return 0;
12451
12452 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12453 return -EINVAL;
12454
12455 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12456 if (is_error_page(page))
12457 return -EINVAL;
12458
12459 r = 0;
12460 shadow = kmap(page);
12461 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12462 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12463 r = -EINVAL;
12464 kunmap(page);
12465 kvm_release_page_clean(page);
12466 return r;
12467}
12468
Jim Mattsonca0bde22016-11-30 12:03:46 -080012469static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12470 u32 *exit_qual)
12471{
12472 bool ia32e;
12473
12474 *exit_qual = ENTRY_FAIL_DEFAULT;
12475
12476 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12477 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12478 return 1;
12479
Liran Alonf145d902018-06-23 02:35:07 +030012480 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012481 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12482 return 1;
12483 }
12484
12485 /*
12486 * If the load IA32_EFER VM-entry control is 1, the following checks
12487 * are performed on the field for the IA32_EFER MSR:
12488 * - Bits reserved in the IA32_EFER MSR must be 0.
12489 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12490 * the IA-32e mode guest VM-exit control. It must also be identical
12491 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12492 * CR0.PG) is 1.
12493 */
12494 if (to_vmx(vcpu)->nested.nested_run_pending &&
12495 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12496 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12497 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12498 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12499 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12500 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12501 return 1;
12502 }
12503
12504 /*
12505 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12506 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12507 * the values of the LMA and LME bits in the field must each be that of
12508 * the host address-space size VM-exit control.
12509 */
12510 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12511 ia32e = (vmcs12->vm_exit_controls &
12512 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12513 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12514 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12515 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12516 return 1;
12517 }
12518
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012519 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12520 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12521 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12522 return 1;
12523
Jim Mattsonca0bde22016-11-30 12:03:46 -080012524 return 0;
12525}
12526
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012527/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012528 * If exit_qual is NULL, this is being called from state restore (either RSM
12529 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012530 */
12531static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012532{
12533 struct vcpu_vmx *vmx = to_vmx(vcpu);
12534 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012535 bool from_vmentry = !!exit_qual;
12536 u32 dummy_exit_qual;
12537 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012538
Jim Mattson858e25c2016-11-30 12:03:47 -080012539 enter_guest_mode(vcpu);
12540
12541 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12542 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12543
Jim Mattsonde3a0022017-11-27 17:22:25 -060012544 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012545 vmx_segment_cache_clear(vmx);
12546
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012547 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12548 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12549
12550 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012551 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012552 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012553
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012554 if (from_vmentry) {
12555 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012556
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012557 r = EXIT_REASON_MSR_LOAD_FAIL;
12558 *exit_qual = nested_vmx_load_msr(vcpu,
12559 vmcs12->vm_entry_msr_load_addr,
12560 vmcs12->vm_entry_msr_load_count);
12561 if (*exit_qual)
12562 goto fail;
12563 } else {
12564 /*
12565 * The MMU is not initialized to point at the right entities yet and
12566 * "get pages" would need to read data from the guest (i.e. we will
12567 * need to perform gpa to hpa translation). Request a call
12568 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12569 * have already been set at vmentry time and should not be reset.
12570 */
12571 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12572 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012573
Jim Mattson858e25c2016-11-30 12:03:47 -080012574 /*
12575 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12576 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12577 * returned as far as L1 is concerned. It will only return (and set
12578 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12579 */
12580 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012581
12582fail:
12583 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12584 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12585 leave_guest_mode(vcpu);
12586 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012587 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012588}
12589
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012590/*
12591 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12592 * for running an L2 nested guest.
12593 */
12594static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12595{
12596 struct vmcs12 *vmcs12;
12597 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012598 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012599 u32 exit_qual;
12600 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012601
Kyle Hueyeb277562016-11-29 12:40:39 -080012602 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012603 return 1;
12604
Kyle Hueyeb277562016-11-29 12:40:39 -080012605 if (!nested_vmx_check_vmcs12(vcpu))
12606 goto out;
12607
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012608 vmcs12 = get_vmcs12(vcpu);
12609
Liran Alona6192d42018-06-23 02:35:04 +030012610 /*
12611 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12612 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12613 * rather than RFLAGS.ZF, and no error number is stored to the
12614 * VM-instruction error field.
12615 */
12616 if (vmcs12->hdr.shadow_vmcs) {
12617 nested_vmx_failInvalid(vcpu);
12618 goto out;
12619 }
12620
Abel Gordon012f83c2013-04-18 14:39:25 +030012621 if (enable_shadow_vmcs)
12622 copy_shadow_to_vmcs12(vmx);
12623
Nadav Har'El7c177932011-05-25 23:12:04 +030012624 /*
12625 * The nested entry process starts with enforcing various prerequisites
12626 * on vmcs12 as required by the Intel SDM, and act appropriately when
12627 * they fail: As the SDM explains, some conditions should cause the
12628 * instruction to fail, while others will cause the instruction to seem
12629 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12630 * To speed up the normal (success) code path, we should avoid checking
12631 * for misconfigurations which will anyway be caught by the processor
12632 * when using the merged vmcs02.
12633 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012634 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12635 nested_vmx_failValid(vcpu,
12636 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12637 goto out;
12638 }
12639
Nadav Har'El7c177932011-05-25 23:12:04 +030012640 if (vmcs12->launch_state == launch) {
12641 nested_vmx_failValid(vcpu,
12642 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12643 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012644 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012645 }
12646
Jim Mattsonca0bde22016-11-30 12:03:46 -080012647 ret = check_vmentry_prereqs(vcpu, vmcs12);
12648 if (ret) {
12649 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012650 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012651 }
12652
Nadav Har'El7c177932011-05-25 23:12:04 +030012653 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012654 * After this point, the trap flag no longer triggers a singlestep trap
12655 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12656 * This is not 100% correct; for performance reasons, we delegate most
12657 * of the checks on host state to the processor. If those fail,
12658 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012659 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012660 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012661
Jim Mattsonca0bde22016-11-30 12:03:46 -080012662 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12663 if (ret) {
12664 nested_vmx_entry_failure(vcpu, vmcs12,
12665 EXIT_REASON_INVALID_STATE, exit_qual);
12666 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012667 }
12668
12669 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012670 * We're finally done with prerequisite checking, and can start with
12671 * the nested entry.
12672 */
12673
Jim Mattson6514dc32018-04-26 16:09:12 -070012674 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012675 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012676 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012677 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012678 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012679 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012680 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012681
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012682 /* Hide L1D cache contents from the nested guest. */
12683 vmx->vcpu.arch.l1tf_flush_l1d = true;
12684
Chao Gao135a06c2018-02-11 10:06:30 +080012685 /*
Liran Alon61ada742018-06-23 02:35:08 +030012686 * Must happen outside of enter_vmx_non_root_mode() as it will
12687 * also be used as part of restoring nVMX state for
12688 * snapshot restore (migration).
12689 *
12690 * In this flow, it is assumed that vmcs12 cache was
12691 * trasferred as part of captured nVMX state and should
12692 * therefore not be read from guest memory (which may not
12693 * exist on destination host yet).
12694 */
12695 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12696
12697 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012698 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12699 * by event injection, halt vcpu.
12700 */
12701 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012702 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12703 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012704 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012705 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012706 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012707
12708out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012709 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012710}
12711
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012712/*
12713 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12714 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12715 * This function returns the new value we should put in vmcs12.guest_cr0.
12716 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12717 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12718 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12719 * didn't trap the bit, because if L1 did, so would L0).
12720 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12721 * been modified by L2, and L1 knows it. So just leave the old value of
12722 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12723 * isn't relevant, because if L0 traps this bit it can set it to anything.
12724 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12725 * changed these bits, and therefore they need to be updated, but L0
12726 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12727 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12728 */
12729static inline unsigned long
12730vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12731{
12732 return
12733 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12734 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12735 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12736 vcpu->arch.cr0_guest_owned_bits));
12737}
12738
12739static inline unsigned long
12740vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12741{
12742 return
12743 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12744 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12745 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12746 vcpu->arch.cr4_guest_owned_bits));
12747}
12748
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012749static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12750 struct vmcs12 *vmcs12)
12751{
12752 u32 idt_vectoring;
12753 unsigned int nr;
12754
Wanpeng Li664f8e22017-08-24 03:35:09 -070012755 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012756 nr = vcpu->arch.exception.nr;
12757 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12758
12759 if (kvm_exception_is_soft(nr)) {
12760 vmcs12->vm_exit_instruction_len =
12761 vcpu->arch.event_exit_inst_len;
12762 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12763 } else
12764 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12765
12766 if (vcpu->arch.exception.has_error_code) {
12767 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12768 vmcs12->idt_vectoring_error_code =
12769 vcpu->arch.exception.error_code;
12770 }
12771
12772 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012773 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012774 vmcs12->idt_vectoring_info_field =
12775 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012776 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012777 nr = vcpu->arch.interrupt.nr;
12778 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12779
12780 if (vcpu->arch.interrupt.soft) {
12781 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12782 vmcs12->vm_entry_instruction_len =
12783 vcpu->arch.event_exit_inst_len;
12784 } else
12785 idt_vectoring |= INTR_TYPE_EXT_INTR;
12786
12787 vmcs12->idt_vectoring_info_field = idt_vectoring;
12788 }
12789}
12790
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012791static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12792{
12793 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012794 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012795 bool block_nested_events =
12796 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012797
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012798 if (vcpu->arch.exception.pending &&
12799 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012800 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012801 return -EBUSY;
12802 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012803 return 0;
12804 }
12805
Jan Kiszkaf4124502014-03-07 20:03:13 +010012806 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12807 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012808 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012809 return -EBUSY;
12810 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12811 return 0;
12812 }
12813
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012814 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012815 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012816 return -EBUSY;
12817 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12818 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12819 INTR_INFO_VALID_MASK, 0);
12820 /*
12821 * The NMI-triggered VM exit counts as injection:
12822 * clear this one and block further NMIs.
12823 */
12824 vcpu->arch.nmi_pending = 0;
12825 vmx_set_nmi_mask(vcpu, true);
12826 return 0;
12827 }
12828
12829 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12830 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012831 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012832 return -EBUSY;
12833 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012834 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012835 }
12836
David Hildenbrand6342c502017-01-25 11:58:58 +010012837 vmx_complete_nested_posted_interrupt(vcpu);
12838 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012839}
12840
Jan Kiszkaf4124502014-03-07 20:03:13 +010012841static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12842{
12843 ktime_t remaining =
12844 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12845 u64 value;
12846
12847 if (ktime_to_ns(remaining) <= 0)
12848 return 0;
12849
12850 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12851 do_div(value, 1000000);
12852 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12853}
12854
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012855/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012856 * Update the guest state fields of vmcs12 to reflect changes that
12857 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12858 * VM-entry controls is also updated, since this is really a guest
12859 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012860 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012861static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012862{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012863 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12864 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12865
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012866 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12867 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12868 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12869
12870 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12871 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12872 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12873 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12874 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12875 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12876 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12877 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12878 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12879 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12880 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12881 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12882 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12883 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12884 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12885 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12886 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12887 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12888 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12889 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12890 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12891 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12892 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12893 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12894 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12895 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12896 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12897 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12898 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12899 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12900 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12901 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12902 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12903 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12904 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12905 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12906
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012907 vmcs12->guest_interruptibility_info =
12908 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12909 vmcs12->guest_pending_dbg_exceptions =
12910 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012911 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12912 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12913 else
12914 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012915
Jan Kiszkaf4124502014-03-07 20:03:13 +010012916 if (nested_cpu_has_preemption_timer(vmcs12)) {
12917 if (vmcs12->vm_exit_controls &
12918 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12919 vmcs12->vmx_preemption_timer_value =
12920 vmx_get_preemption_timer_value(vcpu);
12921 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12922 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012923
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012924 /*
12925 * In some cases (usually, nested EPT), L2 is allowed to change its
12926 * own CR3 without exiting. If it has changed it, we must keep it.
12927 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12928 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12929 *
12930 * Additionally, restore L2's PDPTR to vmcs12.
12931 */
12932 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012933 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012934 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12935 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12936 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12937 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12938 }
12939
Jim Mattsond281e132017-06-01 12:44:46 -070012940 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012941
Wincy Van608406e2015-02-03 23:57:51 +080012942 if (nested_cpu_has_vid(vmcs12))
12943 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12944
Jan Kiszkac18911a2013-03-13 16:06:41 +010012945 vmcs12->vm_entry_controls =
12946 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012947 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012948
Jan Kiszka2996fca2014-06-16 13:59:43 +020012949 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12950 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12951 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12952 }
12953
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012954 /* TODO: These cannot have changed unless we have MSR bitmaps and
12955 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012956 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012957 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012958 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12959 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012960 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12961 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12962 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012963 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012964 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012965}
12966
12967/*
12968 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12969 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12970 * and this function updates it to reflect the changes to the guest state while
12971 * L2 was running (and perhaps made some exits which were handled directly by L0
12972 * without going back to L1), and to reflect the exit reason.
12973 * Note that we do not have to copy here all VMCS fields, just those that
12974 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12975 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12976 * which already writes to vmcs12 directly.
12977 */
12978static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12979 u32 exit_reason, u32 exit_intr_info,
12980 unsigned long exit_qualification)
12981{
12982 /* update guest state fields: */
12983 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012984
12985 /* update exit information fields: */
12986
Jan Kiszka533558b2014-01-04 18:47:20 +010012987 vmcs12->vm_exit_reason = exit_reason;
12988 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012989 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012990
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012991 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012992 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12993 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12994
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012995 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012996 vmcs12->launch_state = 1;
12997
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012998 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12999 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013000 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013001
13002 /*
13003 * Transfer the event that L0 or L1 may wanted to inject into
13004 * L2 to IDT_VECTORING_INFO_FIELD.
13005 */
13006 vmcs12_save_pending_event(vcpu, vmcs12);
13007 }
13008
13009 /*
13010 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13011 * preserved above and would only end up incorrectly in L1.
13012 */
13013 vcpu->arch.nmi_injected = false;
13014 kvm_clear_exception_queue(vcpu);
13015 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013016}
13017
Wanpeng Li5af41572017-11-05 16:54:49 -080013018static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
13019 struct vmcs12 *vmcs12)
13020{
13021 u32 entry_failure_code;
13022
13023 nested_ept_uninit_mmu_context(vcpu);
13024
13025 /*
13026 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13027 * couldn't have changed.
13028 */
13029 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13030 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13031
13032 if (!enable_ept)
13033 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13034}
13035
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013036/*
13037 * A part of what we need to when the nested L2 guest exits and we want to
13038 * run its L1 parent, is to reset L1's guest state to the host state specified
13039 * in vmcs12.
13040 * This function is to be called not only on normal nested exit, but also on
13041 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13042 * Failures During or After Loading Guest State").
13043 * This function should be called when the active VMCS is L1's (vmcs01).
13044 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013045static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13046 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013047{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013048 struct kvm_segment seg;
13049
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013050 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13051 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013052 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013053 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13054 else
13055 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13056 vmx_set_efer(vcpu, vcpu->arch.efer);
13057
13058 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13059 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013060 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013061 /*
13062 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013063 * actually changed, because vmx_set_cr0 refers to efer set above.
13064 *
13065 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13066 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013067 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013068 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013069 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013070
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013071 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013072 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013073 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013074
Wanpeng Li5af41572017-11-05 16:54:49 -080013075 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013076
Liran Alon6f1e03b2018-05-22 17:16:14 +030013077 /*
13078 * If vmcs01 don't use VPID, CPU flushes TLB on every
13079 * VMEntry/VMExit. Thus, no need to flush TLB.
13080 *
13081 * If vmcs12 uses VPID, TLB entries populated by L2 are
13082 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13083 * with vmx->vpid. Thus, no need to flush TLB.
13084 *
13085 * Therefore, flush TLB only in case vmcs01 uses VPID and
13086 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13087 * are both tagged with vmx->vpid.
13088 */
13089 if (enable_vpid &&
13090 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080013091 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013092 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013093
13094 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13095 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13096 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13097 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13098 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013099 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13100 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013101
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013102 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13103 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13104 vmcs_write64(GUEST_BNDCFGS, 0);
13105
Jan Kiszka44811c02013-08-04 17:17:27 +020013106 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013107 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013108 vcpu->arch.pat = vmcs12->host_ia32_pat;
13109 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013110 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13111 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13112 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013113
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013114 /* Set L1 segment info according to Intel SDM
13115 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13116 seg = (struct kvm_segment) {
13117 .base = 0,
13118 .limit = 0xFFFFFFFF,
13119 .selector = vmcs12->host_cs_selector,
13120 .type = 11,
13121 .present = 1,
13122 .s = 1,
13123 .g = 1
13124 };
13125 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13126 seg.l = 1;
13127 else
13128 seg.db = 1;
13129 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13130 seg = (struct kvm_segment) {
13131 .base = 0,
13132 .limit = 0xFFFFFFFF,
13133 .type = 3,
13134 .present = 1,
13135 .s = 1,
13136 .db = 1,
13137 .g = 1
13138 };
13139 seg.selector = vmcs12->host_ds_selector;
13140 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13141 seg.selector = vmcs12->host_es_selector;
13142 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13143 seg.selector = vmcs12->host_ss_selector;
13144 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13145 seg.selector = vmcs12->host_fs_selector;
13146 seg.base = vmcs12->host_fs_base;
13147 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13148 seg.selector = vmcs12->host_gs_selector;
13149 seg.base = vmcs12->host_gs_base;
13150 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13151 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013152 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013153 .limit = 0x67,
13154 .selector = vmcs12->host_tr_selector,
13155 .type = 11,
13156 .present = 1
13157 };
13158 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13159
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013160 kvm_set_dr(vcpu, 7, 0x400);
13161 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013162
Wincy Van3af18d92015-02-03 23:49:31 +080013163 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013164 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013165
Wincy Vanff651cb2014-12-11 08:52:58 +030013166 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13167 vmcs12->vm_exit_msr_load_count))
13168 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013169}
13170
13171/*
13172 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13173 * and modify vmcs12 to make it see what it would expect to see there if
13174 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13175 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013176static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13177 u32 exit_intr_info,
13178 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013179{
13180 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013181 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13182
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013183 /* trying to cancel vmlaunch/vmresume is a bug */
13184 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13185
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013186 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013187 * The only expected VM-instruction error is "VM entry with
13188 * invalid control field(s)." Anything else indicates a
13189 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013190 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013191 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13192 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13193
13194 leave_guest_mode(vcpu);
13195
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013196 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13197 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13198
Jim Mattson4f350c62017-09-14 16:31:44 -070013199 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013200 if (exit_reason == -1)
13201 sync_vmcs12(vcpu, vmcs12);
13202 else
13203 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13204 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013205
Liran Alon61ada742018-06-23 02:35:08 +030013206 /*
13207 * Must happen outside of sync_vmcs12() as it will
13208 * also be used to capture vmcs12 cache as part of
13209 * capturing nVMX state for snapshot (migration).
13210 *
13211 * Otherwise, this flush will dirty guest memory at a
13212 * point it is already assumed by user-space to be
13213 * immutable.
13214 */
13215 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13216
Jim Mattson4f350c62017-09-14 16:31:44 -070013217 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13218 vmcs12->vm_exit_msr_store_count))
13219 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013220 }
13221
Jim Mattson4f350c62017-09-14 16:31:44 -070013222 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013223 vm_entry_controls_reset_shadow(vmx);
13224 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013225 vmx_segment_cache_clear(vmx);
13226
Paolo Bonzini93140062016-07-06 13:23:51 +020013227 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013228 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13229 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013230 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini93140062016-07-06 13:23:51 +020013231 if (vmx->hv_deadline_tsc == -1)
13232 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13233 PIN_BASED_VMX_PREEMPTION_TIMER);
13234 else
13235 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13236 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070013237 if (kvm_has_tsc_control)
13238 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013239
Jim Mattson8d860bb2018-05-09 16:56:05 -040013240 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13241 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13242 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013243 } else if (!nested_cpu_has_ept(vmcs12) &&
13244 nested_cpu_has2(vmcs12,
13245 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013246 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013247 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013248
13249 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13250 vmx->host_rsp = 0;
13251
13252 /* Unpin physical memory we referred to in vmcs02 */
13253 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013254 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013255 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013256 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013257 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013258 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013259 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013260 }
Wincy Van705699a2015-02-03 23:58:17 +080013261 if (vmx->nested.pi_desc_page) {
13262 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013263 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013264 vmx->nested.pi_desc_page = NULL;
13265 vmx->nested.pi_desc = NULL;
13266 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013267
13268 /*
Tang Chen38b99172014-09-24 15:57:54 +080013269 * We are now running in L2, mmu_notifier will force to reload the
13270 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13271 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013272 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013273
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013274 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013275 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013276
13277 /* in case we halted in L2 */
13278 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013279
13280 if (likely(!vmx->fail)) {
13281 /*
13282 * TODO: SDM says that with acknowledge interrupt on
13283 * exit, bit 31 of the VM-exit interrupt information
13284 * (valid interrupt) is always set to 1 on
13285 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13286 * need kvm_cpu_has_interrupt(). See the commit
13287 * message for details.
13288 */
13289 if (nested_exit_intr_ack_set(vcpu) &&
13290 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13291 kvm_cpu_has_interrupt(vcpu)) {
13292 int irq = kvm_cpu_get_interrupt(vcpu);
13293 WARN_ON(irq < 0);
13294 vmcs12->vm_exit_intr_info = irq |
13295 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13296 }
13297
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013298 if (exit_reason != -1)
13299 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13300 vmcs12->exit_qualification,
13301 vmcs12->idt_vectoring_info_field,
13302 vmcs12->vm_exit_intr_info,
13303 vmcs12->vm_exit_intr_error_code,
13304 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013305
13306 load_vmcs12_host_state(vcpu, vmcs12);
13307
13308 return;
13309 }
13310
13311 /*
13312 * After an early L2 VM-entry failure, we're now back
13313 * in L1 which thinks it just finished a VMLAUNCH or
13314 * VMRESUME instruction, so we need to set the failure
13315 * flag and the VM-instruction error field of the VMCS
13316 * accordingly.
13317 */
13318 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013319
13320 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13321
Jim Mattson4f350c62017-09-14 16:31:44 -070013322 /*
13323 * The emulated instruction was already skipped in
13324 * nested_vmx_run, but the updated RIP was never
13325 * written back to the vmcs01.
13326 */
13327 skip_emulated_instruction(vcpu);
13328 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013329}
13330
Nadav Har'El7c177932011-05-25 23:12:04 +030013331/*
Jan Kiszka42124922014-01-04 18:47:19 +010013332 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13333 */
13334static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13335{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013336 if (is_guest_mode(vcpu)) {
13337 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013338 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013339 }
Jan Kiszka42124922014-01-04 18:47:19 +010013340 free_nested(to_vmx(vcpu));
13341}
13342
13343/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013344 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13345 * 23.7 "VM-entry failures during or after loading guest state" (this also
13346 * lists the acceptable exit-reason and exit-qualification parameters).
13347 * It should only be called before L2 actually succeeded to run, and when
13348 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13349 */
13350static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13351 struct vmcs12 *vmcs12,
13352 u32 reason, unsigned long qualification)
13353{
13354 load_vmcs12_host_state(vcpu, vmcs12);
13355 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13356 vmcs12->exit_qualification = qualification;
13357 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013358 if (enable_shadow_vmcs)
13359 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013360}
13361
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013362static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13363 struct x86_instruction_info *info,
13364 enum x86_intercept_stage stage)
13365{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013366 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13367 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13368
13369 /*
13370 * RDPID causes #UD if disabled through secondary execution controls.
13371 * Because it is marked as EmulateOnUD, we need to intercept it here.
13372 */
13373 if (info->intercept == x86_intercept_rdtscp &&
13374 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13375 ctxt->exception.vector = UD_VECTOR;
13376 ctxt->exception.error_code_valid = false;
13377 return X86EMUL_PROPAGATE_FAULT;
13378 }
13379
13380 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013381 return X86EMUL_CONTINUE;
13382}
13383
Yunhong Jiang64672c92016-06-13 14:19:59 -070013384#ifdef CONFIG_X86_64
13385/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13386static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13387 u64 divisor, u64 *result)
13388{
13389 u64 low = a << shift, high = a >> (64 - shift);
13390
13391 /* To avoid the overflow on divq */
13392 if (high >= divisor)
13393 return 1;
13394
13395 /* Low hold the result, high hold rem which is discarded */
13396 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13397 "rm" (divisor), "0" (low), "1" (high));
13398 *result = low;
13399
13400 return 0;
13401}
13402
13403static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13404{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013405 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013406 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013407
13408 if (kvm_mwait_in_guest(vcpu->kvm))
13409 return -EOPNOTSUPP;
13410
13411 vmx = to_vmx(vcpu);
13412 tscl = rdtsc();
13413 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13414 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013415 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13416
13417 if (delta_tsc > lapic_timer_advance_cycles)
13418 delta_tsc -= lapic_timer_advance_cycles;
13419 else
13420 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013421
13422 /* Convert to host delta tsc if tsc scaling is enabled */
13423 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13424 u64_shl_div_u64(delta_tsc,
13425 kvm_tsc_scaling_ratio_frac_bits,
13426 vcpu->arch.tsc_scaling_ratio,
13427 &delta_tsc))
13428 return -ERANGE;
13429
13430 /*
13431 * If the delta tsc can't fit in the 32 bit after the multi shift,
13432 * we can't use the preemption timer.
13433 * It's possible that it fits on later vmentries, but checking
13434 * on every vmentry is costly so we just use an hrtimer.
13435 */
13436 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13437 return -ERANGE;
13438
13439 vmx->hv_deadline_tsc = tscl + delta_tsc;
13440 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13441 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013442
13443 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013444}
13445
13446static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13447{
13448 struct vcpu_vmx *vmx = to_vmx(vcpu);
13449 vmx->hv_deadline_tsc = -1;
13450 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13451 PIN_BASED_VMX_PREEMPTION_TIMER);
13452}
13453#endif
13454
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013455static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013456{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013457 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013458 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013459}
13460
Kai Huang843e4332015-01-28 10:54:28 +080013461static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13462 struct kvm_memory_slot *slot)
13463{
13464 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13465 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13466}
13467
13468static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13469 struct kvm_memory_slot *slot)
13470{
13471 kvm_mmu_slot_set_dirty(kvm, slot);
13472}
13473
13474static void vmx_flush_log_dirty(struct kvm *kvm)
13475{
13476 kvm_flush_pml_buffers(kvm);
13477}
13478
Bandan Dasc5f983f2017-05-05 15:25:14 -040013479static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13480{
13481 struct vmcs12 *vmcs12;
13482 struct vcpu_vmx *vmx = to_vmx(vcpu);
13483 gpa_t gpa;
13484 struct page *page = NULL;
13485 u64 *pml_address;
13486
13487 if (is_guest_mode(vcpu)) {
13488 WARN_ON_ONCE(vmx->nested.pml_full);
13489
13490 /*
13491 * Check if PML is enabled for the nested guest.
13492 * Whether eptp bit 6 is set is already checked
13493 * as part of A/D emulation.
13494 */
13495 vmcs12 = get_vmcs12(vcpu);
13496 if (!nested_cpu_has_pml(vmcs12))
13497 return 0;
13498
Dan Carpenter47698862017-05-10 22:43:17 +030013499 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013500 vmx->nested.pml_full = true;
13501 return 1;
13502 }
13503
13504 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13505
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013506 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13507 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013508 return 0;
13509
13510 pml_address = kmap(page);
13511 pml_address[vmcs12->guest_pml_index--] = gpa;
13512 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013513 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013514 }
13515
13516 return 0;
13517}
13518
Kai Huang843e4332015-01-28 10:54:28 +080013519static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13520 struct kvm_memory_slot *memslot,
13521 gfn_t offset, unsigned long mask)
13522{
13523 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13524}
13525
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013526static void __pi_post_block(struct kvm_vcpu *vcpu)
13527{
13528 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13529 struct pi_desc old, new;
13530 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013531
13532 do {
13533 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013534 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13535 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013536
13537 dest = cpu_physical_id(vcpu->cpu);
13538
13539 if (x2apic_enabled())
13540 new.ndst = dest;
13541 else
13542 new.ndst = (dest << 8) & 0xFF00;
13543
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013544 /* set 'NV' to 'notification vector' */
13545 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013546 } while (cmpxchg64(&pi_desc->control, old.control,
13547 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013548
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013549 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13550 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013551 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013552 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013553 vcpu->pre_pcpu = -1;
13554 }
13555}
13556
Feng Wuefc64402015-09-18 22:29:51 +080013557/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013558 * This routine does the following things for vCPU which is going
13559 * to be blocked if VT-d PI is enabled.
13560 * - Store the vCPU to the wakeup list, so when interrupts happen
13561 * we can find the right vCPU to wake up.
13562 * - Change the Posted-interrupt descriptor as below:
13563 * 'NDST' <-- vcpu->pre_pcpu
13564 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13565 * - If 'ON' is set during this process, which means at least one
13566 * interrupt is posted for this vCPU, we cannot block it, in
13567 * this case, return 1, otherwise, return 0.
13568 *
13569 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013570static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013571{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013572 unsigned int dest;
13573 struct pi_desc old, new;
13574 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13575
13576 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013577 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13578 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013579 return 0;
13580
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013581 WARN_ON(irqs_disabled());
13582 local_irq_disable();
13583 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13584 vcpu->pre_pcpu = vcpu->cpu;
13585 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13586 list_add_tail(&vcpu->blocked_vcpu_list,
13587 &per_cpu(blocked_vcpu_on_cpu,
13588 vcpu->pre_pcpu));
13589 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13590 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013591
13592 do {
13593 old.control = new.control = pi_desc->control;
13594
Feng Wubf9f6ac2015-09-18 22:29:55 +080013595 WARN((pi_desc->sn == 1),
13596 "Warning: SN field of posted-interrupts "
13597 "is set before blocking\n");
13598
13599 /*
13600 * Since vCPU can be preempted during this process,
13601 * vcpu->cpu could be different with pre_pcpu, we
13602 * need to set pre_pcpu as the destination of wakeup
13603 * notification event, then we can find the right vCPU
13604 * to wakeup in wakeup handler if interrupts happen
13605 * when the vCPU is in blocked state.
13606 */
13607 dest = cpu_physical_id(vcpu->pre_pcpu);
13608
13609 if (x2apic_enabled())
13610 new.ndst = dest;
13611 else
13612 new.ndst = (dest << 8) & 0xFF00;
13613
13614 /* set 'NV' to 'wakeup vector' */
13615 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013616 } while (cmpxchg64(&pi_desc->control, old.control,
13617 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013618
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013619 /* We should not block the vCPU if an interrupt is posted for it. */
13620 if (pi_test_on(pi_desc) == 1)
13621 __pi_post_block(vcpu);
13622
13623 local_irq_enable();
13624 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013625}
13626
Yunhong Jiangbc225122016-06-13 14:19:58 -070013627static int vmx_pre_block(struct kvm_vcpu *vcpu)
13628{
13629 if (pi_pre_block(vcpu))
13630 return 1;
13631
Yunhong Jiang64672c92016-06-13 14:19:59 -070013632 if (kvm_lapic_hv_timer_in_use(vcpu))
13633 kvm_lapic_switch_to_sw_timer(vcpu);
13634
Yunhong Jiangbc225122016-06-13 14:19:58 -070013635 return 0;
13636}
13637
13638static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013639{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013640 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013641 return;
13642
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013643 WARN_ON(irqs_disabled());
13644 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013645 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013646 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013647}
13648
Yunhong Jiangbc225122016-06-13 14:19:58 -070013649static void vmx_post_block(struct kvm_vcpu *vcpu)
13650{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013651 if (kvm_x86_ops->set_hv_timer)
13652 kvm_lapic_switch_to_hv_timer(vcpu);
13653
Yunhong Jiangbc225122016-06-13 14:19:58 -070013654 pi_post_block(vcpu);
13655}
13656
Feng Wubf9f6ac2015-09-18 22:29:55 +080013657/*
Feng Wuefc64402015-09-18 22:29:51 +080013658 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13659 *
13660 * @kvm: kvm
13661 * @host_irq: host irq of the interrupt
13662 * @guest_irq: gsi of the interrupt
13663 * @set: set or unset PI
13664 * returns 0 on success, < 0 on failure
13665 */
13666static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13667 uint32_t guest_irq, bool set)
13668{
13669 struct kvm_kernel_irq_routing_entry *e;
13670 struct kvm_irq_routing_table *irq_rt;
13671 struct kvm_lapic_irq irq;
13672 struct kvm_vcpu *vcpu;
13673 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013674 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013675
13676 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013677 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13678 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013679 return 0;
13680
13681 idx = srcu_read_lock(&kvm->irq_srcu);
13682 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013683 if (guest_irq >= irq_rt->nr_rt_entries ||
13684 hlist_empty(&irq_rt->map[guest_irq])) {
13685 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13686 guest_irq, irq_rt->nr_rt_entries);
13687 goto out;
13688 }
Feng Wuefc64402015-09-18 22:29:51 +080013689
13690 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13691 if (e->type != KVM_IRQ_ROUTING_MSI)
13692 continue;
13693 /*
13694 * VT-d PI cannot support posting multicast/broadcast
13695 * interrupts to a vCPU, we still use interrupt remapping
13696 * for these kind of interrupts.
13697 *
13698 * For lowest-priority interrupts, we only support
13699 * those with single CPU as the destination, e.g. user
13700 * configures the interrupts via /proc/irq or uses
13701 * irqbalance to make the interrupts single-CPU.
13702 *
13703 * We will support full lowest-priority interrupt later.
13704 */
13705
Radim Krčmář371313132016-07-12 22:09:27 +020013706 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013707 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13708 /*
13709 * Make sure the IRTE is in remapped mode if
13710 * we don't handle it in posted mode.
13711 */
13712 ret = irq_set_vcpu_affinity(host_irq, NULL);
13713 if (ret < 0) {
13714 printk(KERN_INFO
13715 "failed to back to remapped mode, irq: %u\n",
13716 host_irq);
13717 goto out;
13718 }
13719
Feng Wuefc64402015-09-18 22:29:51 +080013720 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013721 }
Feng Wuefc64402015-09-18 22:29:51 +080013722
13723 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13724 vcpu_info.vector = irq.vector;
13725
hu huajun2698d822018-04-11 15:16:40 +080013726 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013727 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13728
13729 if (set)
13730 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013731 else
Feng Wuefc64402015-09-18 22:29:51 +080013732 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013733
13734 if (ret < 0) {
13735 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13736 __func__);
13737 goto out;
13738 }
13739 }
13740
13741 ret = 0;
13742out:
13743 srcu_read_unlock(&kvm->irq_srcu, idx);
13744 return ret;
13745}
13746
Ashok Rajc45dcc72016-06-22 14:59:56 +080013747static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13748{
13749 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13750 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13751 FEATURE_CONTROL_LMCE;
13752 else
13753 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13754 ~FEATURE_CONTROL_LMCE;
13755}
13756
Ladi Prosek72d7b372017-10-11 16:54:41 +020013757static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13758{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013759 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13760 if (to_vmx(vcpu)->nested.nested_run_pending)
13761 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013762 return 1;
13763}
13764
Ladi Prosek0234bf82017-10-11 16:54:40 +020013765static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13766{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013767 struct vcpu_vmx *vmx = to_vmx(vcpu);
13768
13769 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13770 if (vmx->nested.smm.guest_mode)
13771 nested_vmx_vmexit(vcpu, -1, 0, 0);
13772
13773 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13774 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013775 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013776 return 0;
13777}
13778
13779static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13780{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013781 struct vcpu_vmx *vmx = to_vmx(vcpu);
13782 int ret;
13783
13784 if (vmx->nested.smm.vmxon) {
13785 vmx->nested.vmxon = true;
13786 vmx->nested.smm.vmxon = false;
13787 }
13788
13789 if (vmx->nested.smm.guest_mode) {
13790 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013791 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013792 vcpu->arch.hflags |= HF_SMM_MASK;
13793 if (ret)
13794 return ret;
13795
13796 vmx->nested.smm.guest_mode = false;
13797 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013798 return 0;
13799}
13800
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013801static int enable_smi_window(struct kvm_vcpu *vcpu)
13802{
13803 return 0;
13804}
13805
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013806static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13807 struct kvm_nested_state __user *user_kvm_nested_state,
13808 u32 user_data_size)
13809{
13810 struct vcpu_vmx *vmx;
13811 struct vmcs12 *vmcs12;
13812 struct kvm_nested_state kvm_state = {
13813 .flags = 0,
13814 .format = 0,
13815 .size = sizeof(kvm_state),
13816 .vmx.vmxon_pa = -1ull,
13817 .vmx.vmcs_pa = -1ull,
13818 };
13819
13820 if (!vcpu)
13821 return kvm_state.size + 2 * VMCS12_SIZE;
13822
13823 vmx = to_vmx(vcpu);
13824 vmcs12 = get_vmcs12(vcpu);
13825 if (nested_vmx_allowed(vcpu) &&
13826 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13827 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13828 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13829
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013830 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013831 kvm_state.size += VMCS12_SIZE;
13832
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013833 if (is_guest_mode(vcpu) &&
13834 nested_cpu_has_shadow_vmcs(vmcs12) &&
13835 vmcs12->vmcs_link_pointer != -1ull)
13836 kvm_state.size += VMCS12_SIZE;
13837 }
13838
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013839 if (vmx->nested.smm.vmxon)
13840 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13841
13842 if (vmx->nested.smm.guest_mode)
13843 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13844
13845 if (is_guest_mode(vcpu)) {
13846 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13847
13848 if (vmx->nested.nested_run_pending)
13849 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13850 }
13851 }
13852
13853 if (user_data_size < kvm_state.size)
13854 goto out;
13855
13856 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13857 return -EFAULT;
13858
13859 if (vmx->nested.current_vmptr == -1ull)
13860 goto out;
13861
13862 /*
13863 * When running L2, the authoritative vmcs12 state is in the
13864 * vmcs02. When running L1, the authoritative vmcs12 state is
13865 * in the shadow vmcs linked to vmcs01, unless
13866 * sync_shadow_vmcs is set, in which case, the authoritative
13867 * vmcs12 state is in the vmcs12 already.
13868 */
13869 if (is_guest_mode(vcpu))
13870 sync_vmcs12(vcpu, vmcs12);
13871 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13872 copy_shadow_to_vmcs12(vmx);
13873
13874 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13875 return -EFAULT;
13876
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013877 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13878 vmcs12->vmcs_link_pointer != -1ull) {
13879 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13880 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13881 return -EFAULT;
13882 }
13883
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013884out:
13885 return kvm_state.size;
13886}
13887
13888static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13889 struct kvm_nested_state __user *user_kvm_nested_state,
13890 struct kvm_nested_state *kvm_state)
13891{
13892 struct vcpu_vmx *vmx = to_vmx(vcpu);
13893 struct vmcs12 *vmcs12;
13894 u32 exit_qual;
13895 int ret;
13896
13897 if (kvm_state->format != 0)
13898 return -EINVAL;
13899
13900 if (!nested_vmx_allowed(vcpu))
13901 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13902
13903 if (kvm_state->vmx.vmxon_pa == -1ull) {
13904 if (kvm_state->vmx.smm.flags)
13905 return -EINVAL;
13906
13907 if (kvm_state->vmx.vmcs_pa != -1ull)
13908 return -EINVAL;
13909
13910 vmx_leave_nested(vcpu);
13911 return 0;
13912 }
13913
13914 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13915 return -EINVAL;
13916
13917 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13918 return -EINVAL;
13919
13920 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13921 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13922 return -EINVAL;
13923
13924 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13925 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13926 return -EINVAL;
13927
13928 if (kvm_state->vmx.smm.flags &
13929 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13930 return -EINVAL;
13931
13932 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13933 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13934 return -EINVAL;
13935
13936 vmx_leave_nested(vcpu);
13937 if (kvm_state->vmx.vmxon_pa == -1ull)
13938 return 0;
13939
13940 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13941 ret = enter_vmx_operation(vcpu);
13942 if (ret)
13943 return ret;
13944
13945 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13946
13947 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13948 vmx->nested.smm.vmxon = true;
13949 vmx->nested.vmxon = false;
13950
13951 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13952 vmx->nested.smm.guest_mode = true;
13953 }
13954
13955 vmcs12 = get_vmcs12(vcpu);
13956 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13957 return -EFAULT;
13958
Liran Alon392b2f22018-06-23 02:35:01 +030013959 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013960 return -EINVAL;
13961
13962 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13963 return 0;
13964
13965 vmx->nested.nested_run_pending =
13966 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13967
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013968 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13969 vmcs12->vmcs_link_pointer != -1ull) {
13970 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13971 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13972 return -EINVAL;
13973
13974 if (copy_from_user(shadow_vmcs12,
13975 user_kvm_nested_state->data + VMCS12_SIZE,
13976 sizeof(*vmcs12)))
13977 return -EFAULT;
13978
13979 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13980 !shadow_vmcs12->hdr.shadow_vmcs)
13981 return -EINVAL;
13982 }
13983
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013984 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13985 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13986 return -EINVAL;
13987
13988 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13989 vmx->nested.nested_run_pending = 1;
13990
13991 vmx->nested.dirty_vmcs12 = true;
13992 ret = enter_vmx_non_root_mode(vcpu, NULL);
13993 if (ret)
13994 return -EINVAL;
13995
13996 return 0;
13997}
13998
Kees Cook404f6aa2016-08-08 16:29:06 -070013999static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014000 .cpu_has_kvm_support = cpu_has_kvm_support,
14001 .disabled_by_bios = vmx_disabled_by_bios,
14002 .hardware_setup = hardware_setup,
14003 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014004 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014005 .hardware_enable = hardware_enable,
14006 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014007 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014008 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014009
Wanpeng Lib31c1142018-03-12 04:53:04 -070014010 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014011 .vm_alloc = vmx_vm_alloc,
14012 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014013
Avi Kivity6aa8b732006-12-10 02:21:36 -080014014 .vcpu_create = vmx_create_vcpu,
14015 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014016 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014017
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014018 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014019 .vcpu_load = vmx_vcpu_load,
14020 .vcpu_put = vmx_vcpu_put,
14021
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014022 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014023 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014024 .get_msr = vmx_get_msr,
14025 .set_msr = vmx_set_msr,
14026 .get_segment_base = vmx_get_segment_base,
14027 .get_segment = vmx_get_segment,
14028 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014029 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014030 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014031 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014032 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014033 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014034 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014035 .set_cr3 = vmx_set_cr3,
14036 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014037 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014038 .get_idt = vmx_get_idt,
14039 .set_idt = vmx_set_idt,
14040 .get_gdt = vmx_get_gdt,
14041 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014042 .get_dr6 = vmx_get_dr6,
14043 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014044 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014045 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014046 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014047 .get_rflags = vmx_get_rflags,
14048 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014049
Avi Kivity6aa8b732006-12-10 02:21:36 -080014050 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014051 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014052
Avi Kivity6aa8b732006-12-10 02:21:36 -080014053 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014054 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014055 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014056 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14057 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014058 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014059 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014060 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014061 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014062 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014063 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014064 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014065 .get_nmi_mask = vmx_get_nmi_mask,
14066 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014067 .enable_nmi_window = enable_nmi_window,
14068 .enable_irq_window = enable_irq_window,
14069 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014070 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014071 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014072 .get_enable_apicv = vmx_get_enable_apicv,
14073 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014074 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014075 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014076 .hwapic_irr_update = vmx_hwapic_irr_update,
14077 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080014078 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14079 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014080
Izik Eiduscbc94022007-10-25 00:29:55 +020014081 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014082 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014083 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014084 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014085
Avi Kivity586f9602010-11-18 13:09:54 +020014086 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014087
Sheng Yang17cc3932010-01-05 19:02:27 +080014088 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014089
14090 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014091
14092 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014093 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014094
14095 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014096
14097 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014098
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014099 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014100 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014101
14102 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014103
14104 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014105 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014106 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014107 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014108 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014109
14110 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014111
14112 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014113
14114 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14115 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14116 .flush_log_dirty = vmx_flush_log_dirty,
14117 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014118 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014119
Feng Wubf9f6ac2015-09-18 22:29:55 +080014120 .pre_block = vmx_pre_block,
14121 .post_block = vmx_post_block,
14122
Wei Huang25462f72015-06-19 15:45:05 +020014123 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014124
14125 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014126
14127#ifdef CONFIG_X86_64
14128 .set_hv_timer = vmx_set_hv_timer,
14129 .cancel_hv_timer = vmx_cancel_hv_timer,
14130#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014131
14132 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014133
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014134 .get_nested_state = vmx_get_nested_state,
14135 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014136 .get_vmcs12_pages = nested_get_vmcs12_pages,
14137
Ladi Prosek72d7b372017-10-11 16:54:41 +020014138 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014139 .pre_enter_smm = vmx_pre_enter_smm,
14140 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014141 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014142};
14143
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014144static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014145{
14146 if (vmx_l1d_flush_pages) {
14147 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14148 vmx_l1d_flush_pages = NULL;
14149 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014150 /* Restore state so sysfs ignores VMX */
14151 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014152}
14153
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014154static void vmx_exit(void)
14155{
14156#ifdef CONFIG_KEXEC_CORE
14157 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14158 synchronize_rcu();
14159#endif
14160
14161 kvm_exit();
14162
14163#if IS_ENABLED(CONFIG_HYPERV)
14164 if (static_branch_unlikely(&enable_evmcs)) {
14165 int cpu;
14166 struct hv_vp_assist_page *vp_ap;
14167 /*
14168 * Reset everything to support using non-enlightened VMCS
14169 * access later (e.g. when we reload the module with
14170 * enlightened_vmcs=0)
14171 */
14172 for_each_online_cpu(cpu) {
14173 vp_ap = hv_get_vp_assist_page(cpu);
14174
14175 if (!vp_ap)
14176 continue;
14177
14178 vp_ap->current_nested_vmcs = 0;
14179 vp_ap->enlighten_vmentry = 0;
14180 }
14181
14182 static_branch_disable(&enable_evmcs);
14183 }
14184#endif
14185 vmx_cleanup_l1d_flush();
14186}
14187module_exit(vmx_exit);
14188
Avi Kivity6aa8b732006-12-10 02:21:36 -080014189static int __init vmx_init(void)
14190{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014191 int r;
14192
14193#if IS_ENABLED(CONFIG_HYPERV)
14194 /*
14195 * Enlightened VMCS usage should be recommended and the host needs
14196 * to support eVMCS v1 or above. We can also disable eVMCS support
14197 * with module parameter.
14198 */
14199 if (enlightened_vmcs &&
14200 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14201 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14202 KVM_EVMCS_VERSION) {
14203 int cpu;
14204
14205 /* Check that we have assist pages on all online CPUs */
14206 for_each_online_cpu(cpu) {
14207 if (!hv_get_vp_assist_page(cpu)) {
14208 enlightened_vmcs = false;
14209 break;
14210 }
14211 }
14212
14213 if (enlightened_vmcs) {
14214 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14215 static_branch_enable(&enable_evmcs);
14216 }
14217 } else {
14218 enlightened_vmcs = false;
14219 }
14220#endif
14221
14222 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014223 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014224 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014225 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014226
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014227 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014228 * Must be called after kvm_init() so enable_ept is properly set
14229 * up. Hand the parameter mitigation value in which was stored in
14230 * the pre module init parser. If no parameter was given, it will
14231 * contain 'auto' which will be turned into the default 'cond'
14232 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014233 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014234 if (boot_cpu_has(X86_BUG_L1TF)) {
14235 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14236 if (r) {
14237 vmx_exit();
14238 return r;
14239 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014240 }
14241
Dave Young2965faa2015-09-09 15:38:55 -070014242#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014243 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14244 crash_vmclear_local_loaded_vmcss);
14245#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014246 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014247
He, Qingfdef3ad2007-04-30 09:45:24 +030014248 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014249}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014250module_init(vmx_init);