blob: 1519f030fd73198a2dbac4a6a050cf79b587c2cf [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
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 Kiszkaf41245002014-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
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200222 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
223 u64 msr;
224
225 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 }
231
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 Kiszkaf41245002014-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
Yunhong Jiang64672c92016-06-13 14:19:59 -07001687/*
1688 * Comment's format: document - errata name - stepping - processor name.
1689 * Refer from
1690 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1691 */
1692static u32 vmx_preemption_cpu_tfms[] = {
1693/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16940x000206E6,
1695/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1696/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1697/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
16980x00020652,
1699/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17000x00020655,
1701/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1702/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1703/*
1704 * 320767.pdf - AAP86 - B1 -
1705 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1706 */
17070x000106E5,
1708/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17090x000106A0,
1710/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17110x000106A1,
1712/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17130x000106A4,
1714 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1715 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1716 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17170x000106A5,
1718};
1719
1720static inline bool cpu_has_broken_vmx_preemption_timer(void)
1721{
1722 u32 eax = cpuid_eax(0x00000001), i;
1723
1724 /* Clear the reserved bits */
1725 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001726 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001727 if (eax == vmx_preemption_cpu_tfms[i])
1728 return true;
1729
1730 return false;
1731}
1732
1733static inline bool cpu_has_vmx_preemption_timer(void)
1734{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001735 return vmcs_config.pin_based_exec_ctrl &
1736 PIN_BASED_VMX_PREEMPTION_TIMER;
1737}
1738
Yang Zhang01e439b2013-04-11 19:25:12 +08001739static inline bool cpu_has_vmx_posted_intr(void)
1740{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001741 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1742 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001743}
1744
1745static inline bool cpu_has_vmx_apicv(void)
1746{
1747 return cpu_has_vmx_apic_register_virt() &&
1748 cpu_has_vmx_virtual_intr_delivery() &&
1749 cpu_has_vmx_posted_intr();
1750}
1751
Sheng Yang04547152009-04-01 15:52:31 +08001752static inline bool cpu_has_vmx_flexpriority(void)
1753{
1754 return cpu_has_vmx_tpr_shadow() &&
1755 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001756}
1757
Marcelo Tosattie7997942009-06-11 12:07:40 -03001758static inline bool cpu_has_vmx_ept_execute_only(void)
1759{
Gui Jianfeng31299942010-03-15 17:29:09 +08001760 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001761}
1762
Marcelo Tosattie7997942009-06-11 12:07:40 -03001763static inline bool cpu_has_vmx_ept_2m_page(void)
1764{
Gui Jianfeng31299942010-03-15 17:29:09 +08001765 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001766}
1767
Sheng Yang878403b2010-01-05 19:02:29 +08001768static inline bool cpu_has_vmx_ept_1g_page(void)
1769{
Gui Jianfeng31299942010-03-15 17:29:09 +08001770 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001771}
1772
Sheng Yang4bc9b982010-06-02 14:05:24 +08001773static inline bool cpu_has_vmx_ept_4levels(void)
1774{
1775 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1776}
1777
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001778static inline bool cpu_has_vmx_ept_mt_wb(void)
1779{
1780 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1781}
1782
Yu Zhang855feb62017-08-24 20:27:55 +08001783static inline bool cpu_has_vmx_ept_5levels(void)
1784{
1785 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1786}
1787
Xudong Hao83c3a332012-05-28 19:33:35 +08001788static inline bool cpu_has_vmx_ept_ad_bits(void)
1789{
1790 return vmx_capability.ept & VMX_EPT_AD_BIT;
1791}
1792
Gui Jianfeng31299942010-03-15 17:29:09 +08001793static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001794{
Gui Jianfeng31299942010-03-15 17:29:09 +08001795 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001796}
1797
Gui Jianfeng31299942010-03-15 17:29:09 +08001798static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001799{
Gui Jianfeng31299942010-03-15 17:29:09 +08001800 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001801}
1802
Liran Aloncd9a4912018-05-22 17:16:15 +03001803static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1804{
1805 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1806}
1807
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001808static inline bool cpu_has_vmx_invvpid_single(void)
1809{
1810 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1811}
1812
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001813static inline bool cpu_has_vmx_invvpid_global(void)
1814{
1815 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1816}
1817
Wanpeng Li08d839c2017-03-23 05:30:08 -07001818static inline bool cpu_has_vmx_invvpid(void)
1819{
1820 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1821}
1822
Gui Jianfeng31299942010-03-15 17:29:09 +08001823static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001824{
Sheng Yang04547152009-04-01 15:52:31 +08001825 return vmcs_config.cpu_based_2nd_exec_ctrl &
1826 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001827}
1828
Gui Jianfeng31299942010-03-15 17:29:09 +08001829static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001830{
1831 return vmcs_config.cpu_based_2nd_exec_ctrl &
1832 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1833}
1834
Gui Jianfeng31299942010-03-15 17:29:09 +08001835static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001836{
1837 return vmcs_config.cpu_based_2nd_exec_ctrl &
1838 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1839}
1840
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001841static inline bool cpu_has_vmx_basic_inout(void)
1842{
1843 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1844}
1845
Paolo Bonzini35754c92015-07-29 12:05:37 +02001846static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001847{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001848 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001849}
1850
Gui Jianfeng31299942010-03-15 17:29:09 +08001851static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001852{
Sheng Yang04547152009-04-01 15:52:31 +08001853 return vmcs_config.cpu_based_2nd_exec_ctrl &
1854 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001855}
1856
Gui Jianfeng31299942010-03-15 17:29:09 +08001857static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001858{
1859 return vmcs_config.cpu_based_2nd_exec_ctrl &
1860 SECONDARY_EXEC_RDTSCP;
1861}
1862
Mao, Junjiead756a12012-07-02 01:18:48 +00001863static inline bool cpu_has_vmx_invpcid(void)
1864{
1865 return vmcs_config.cpu_based_2nd_exec_ctrl &
1866 SECONDARY_EXEC_ENABLE_INVPCID;
1867}
1868
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001869static inline bool cpu_has_virtual_nmis(void)
1870{
1871 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1872}
1873
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001874static inline bool cpu_has_vmx_wbinvd_exit(void)
1875{
1876 return vmcs_config.cpu_based_2nd_exec_ctrl &
1877 SECONDARY_EXEC_WBINVD_EXITING;
1878}
1879
Abel Gordonabc4fc52013-04-18 14:35:25 +03001880static inline bool cpu_has_vmx_shadow_vmcs(void)
1881{
1882 u64 vmx_msr;
1883 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1884 /* check if the cpu supports writing r/o exit information fields */
1885 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1886 return false;
1887
1888 return vmcs_config.cpu_based_2nd_exec_ctrl &
1889 SECONDARY_EXEC_SHADOW_VMCS;
1890}
1891
Kai Huang843e4332015-01-28 10:54:28 +08001892static inline bool cpu_has_vmx_pml(void)
1893{
1894 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1895}
1896
Haozhong Zhang64903d62015-10-20 15:39:09 +08001897static inline bool cpu_has_vmx_tsc_scaling(void)
1898{
1899 return vmcs_config.cpu_based_2nd_exec_ctrl &
1900 SECONDARY_EXEC_TSC_SCALING;
1901}
1902
Bandan Das2a499e42017-08-03 15:54:41 -04001903static inline bool cpu_has_vmx_vmfunc(void)
1904{
1905 return vmcs_config.cpu_based_2nd_exec_ctrl &
1906 SECONDARY_EXEC_ENABLE_VMFUNC;
1907}
1908
Sean Christopherson64f7a112018-04-30 10:01:06 -07001909static bool vmx_umip_emulated(void)
1910{
1911 return vmcs_config.cpu_based_2nd_exec_ctrl &
1912 SECONDARY_EXEC_DESC;
1913}
1914
Sheng Yang04547152009-04-01 15:52:31 +08001915static inline bool report_flexpriority(void)
1916{
1917 return flexpriority_enabled;
1918}
1919
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001920static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1921{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001922 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001923}
1924
Jim Mattsonf4160e42018-05-29 09:11:33 -07001925/*
1926 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1927 * to modify any valid field of the VMCS, or are the VM-exit
1928 * information fields read-only?
1929 */
1930static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1931{
1932 return to_vmx(vcpu)->nested.msrs.misc_low &
1933 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1934}
1935
Marc Orr04473782018-06-20 17:21:29 -07001936static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1937{
1938 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1939}
1940
1941static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1942{
1943 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1944 CPU_BASED_MONITOR_TRAP_FLAG;
1945}
1946
Liran Alonfa97d7d2018-07-18 14:07:59 +02001947static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1948{
1949 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1950 SECONDARY_EXEC_SHADOW_VMCS;
1951}
1952
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001953static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1954{
1955 return vmcs12->cpu_based_vm_exec_control & bit;
1956}
1957
1958static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1959{
1960 return (vmcs12->cpu_based_vm_exec_control &
1961 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1962 (vmcs12->secondary_vm_exec_control & bit);
1963}
1964
Jan Kiszkaf41245002014-03-07 20:03:13 +01001965static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1966{
1967 return vmcs12->pin_based_vm_exec_control &
1968 PIN_BASED_VMX_PREEMPTION_TIMER;
1969}
1970
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001971static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1972{
1973 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1974}
1975
1976static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1977{
1978 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1979}
1980
Nadav Har'El155a97a2013-08-05 11:07:16 +03001981static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1982{
1983 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1984}
1985
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001986static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1987{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001988 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001989}
1990
Bandan Dasc5f983f2017-05-05 15:25:14 -04001991static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1992{
1993 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1994}
1995
Wincy Vanf2b93282015-02-03 23:56:03 +08001996static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1997{
1998 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1999}
2000
Wanpeng Li5c614b32015-10-13 09:18:36 -07002001static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2002{
2003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2004}
2005
Wincy Van82f0dd42015-02-03 23:57:18 +08002006static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2007{
2008 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2009}
2010
Wincy Van608406e2015-02-03 23:57:51 +08002011static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2012{
2013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2014}
2015
Wincy Van705699a2015-02-03 23:58:17 +08002016static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2017{
2018 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2019}
2020
Bandan Das27c42a12017-08-03 15:54:42 -04002021static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2022{
2023 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2024}
2025
Bandan Das41ab9372017-08-03 15:54:43 -04002026static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2027{
2028 return nested_cpu_has_vmfunc(vmcs12) &&
2029 (vmcs12->vm_function_control &
2030 VMX_VMFUNC_EPTP_SWITCHING);
2031}
2032
Liran Alonf792d272018-06-23 02:35:05 +03002033static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2034{
2035 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2036}
2037
Jim Mattsonef85b672016-12-12 11:01:37 -08002038static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002039{
2040 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002041 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002042}
2043
Jan Kiszka533558b2014-01-04 18:47:20 +01002044static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2045 u32 exit_intr_info,
2046 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002047static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2048 struct vmcs12 *vmcs12,
2049 u32 reason, unsigned long qualification);
2050
Rusty Russell8b9cf982007-07-30 16:31:43 +10002051static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002052{
2053 int i;
2054
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002055 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002056 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002057 return i;
2058 return -1;
2059}
2060
Sheng Yang2384d2b2008-01-17 15:14:33 +08002061static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2062{
2063 struct {
2064 u64 vpid : 16;
2065 u64 rsvd : 48;
2066 u64 gva;
2067 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002068 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002069
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002070 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2071 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2072 : "memory");
2073 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002074}
2075
Sheng Yang14394422008-04-28 12:24:45 +08002076static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2077{
2078 struct {
2079 u64 eptp, gpa;
2080 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002081 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002082
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002083 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2084 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2085 : "memory");
2086 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002087}
2088
Avi Kivity26bb0982009-09-07 11:14:12 +03002089static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002090{
2091 int i;
2092
Rusty Russell8b9cf982007-07-30 16:31:43 +10002093 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002094 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002095 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002096 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002097}
2098
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099static void vmcs_clear(struct vmcs *vmcs)
2100{
2101 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002102 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002104 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2105 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2106 : "memory");
2107 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2109 vmcs, phys_addr);
2110}
2111
Nadav Har'Eld462b812011-05-24 15:26:10 +03002112static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2113{
2114 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002115 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2116 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002117 loaded_vmcs->cpu = -1;
2118 loaded_vmcs->launched = 0;
2119}
2120
Dongxiao Xu7725b892010-05-11 18:29:38 +08002121static void vmcs_load(struct vmcs *vmcs)
2122{
2123 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002124 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002125
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002126 if (static_branch_unlikely(&enable_evmcs))
2127 return evmcs_load(phys_addr);
2128
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002129 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2130 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2131 : "memory");
2132 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002133 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002134 vmcs, phys_addr);
2135}
2136
Dave Young2965faa2015-09-09 15:38:55 -07002137#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002138/*
2139 * This bitmap is used to indicate whether the vmclear
2140 * operation is enabled on all cpus. All disabled by
2141 * default.
2142 */
2143static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2144
2145static inline void crash_enable_local_vmclear(int cpu)
2146{
2147 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2148}
2149
2150static inline void crash_disable_local_vmclear(int cpu)
2151{
2152 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2153}
2154
2155static inline int crash_local_vmclear_enabled(int cpu)
2156{
2157 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2158}
2159
2160static void crash_vmclear_local_loaded_vmcss(void)
2161{
2162 int cpu = raw_smp_processor_id();
2163 struct loaded_vmcs *v;
2164
2165 if (!crash_local_vmclear_enabled(cpu))
2166 return;
2167
2168 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2169 loaded_vmcss_on_cpu_link)
2170 vmcs_clear(v->vmcs);
2171}
2172#else
2173static inline void crash_enable_local_vmclear(int cpu) { }
2174static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002175#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002176
Nadav Har'Eld462b812011-05-24 15:26:10 +03002177static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002178{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002179 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002180 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181
Nadav Har'Eld462b812011-05-24 15:26:10 +03002182 if (loaded_vmcs->cpu != cpu)
2183 return; /* vcpu migration can race with cpu offline */
2184 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002186 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002187 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002188
2189 /*
2190 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2191 * is before setting loaded_vmcs->vcpu to -1 which is done in
2192 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2193 * then adds the vmcs into percpu list before it is deleted.
2194 */
2195 smp_wmb();
2196
Nadav Har'Eld462b812011-05-24 15:26:10 +03002197 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002198 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199}
2200
Nadav Har'Eld462b812011-05-24 15:26:10 +03002201static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002202{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002203 int cpu = loaded_vmcs->cpu;
2204
2205 if (cpu != -1)
2206 smp_call_function_single(cpu,
2207 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002208}
2209
Junaid Shahidfaff8752018-06-29 13:10:05 -07002210static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2211{
2212 if (vpid == 0)
2213 return true;
2214
2215 if (cpu_has_vmx_invvpid_individual_addr()) {
2216 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2217 return true;
2218 }
2219
2220 return false;
2221}
2222
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002223static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002224{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002225 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002226 return;
2227
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002228 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002229 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002230}
2231
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002232static inline void vpid_sync_vcpu_global(void)
2233{
2234 if (cpu_has_vmx_invvpid_global())
2235 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2236}
2237
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002238static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002239{
2240 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002241 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002242 else
2243 vpid_sync_vcpu_global();
2244}
2245
Sheng Yang14394422008-04-28 12:24:45 +08002246static inline void ept_sync_global(void)
2247{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002248 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002249}
2250
2251static inline void ept_sync_context(u64 eptp)
2252{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002253 if (cpu_has_vmx_invept_context())
2254 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2255 else
2256 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002257}
2258
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002259static __always_inline void vmcs_check16(unsigned long field)
2260{
2261 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2262 "16-bit accessor invalid for 64-bit field");
2263 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2264 "16-bit accessor invalid for 64-bit high field");
2265 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2266 "16-bit accessor invalid for 32-bit high field");
2267 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2268 "16-bit accessor invalid for natural width field");
2269}
2270
2271static __always_inline void vmcs_check32(unsigned long field)
2272{
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2274 "32-bit accessor invalid for 16-bit field");
2275 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2276 "32-bit accessor invalid for natural width field");
2277}
2278
2279static __always_inline void vmcs_check64(unsigned long field)
2280{
2281 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2282 "64-bit accessor invalid for 16-bit field");
2283 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2284 "64-bit accessor invalid for 64-bit high field");
2285 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2286 "64-bit accessor invalid for 32-bit field");
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2288 "64-bit accessor invalid for natural width field");
2289}
2290
2291static __always_inline void vmcs_checkl(unsigned long field)
2292{
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2294 "Natural width accessor invalid for 16-bit field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2296 "Natural width accessor invalid for 64-bit field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2298 "Natural width accessor invalid for 64-bit high field");
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2300 "Natural width accessor invalid for 32-bit field");
2301}
2302
2303static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304{
Avi Kivity5e520e62011-05-15 10:13:12 -04002305 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306
Avi Kivity5e520e62011-05-15 10:13:12 -04002307 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2308 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309 return value;
2310}
2311
Avi Kivity96304212011-05-15 10:13:13 -04002312static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002314 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002315 if (static_branch_unlikely(&enable_evmcs))
2316 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002317 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318}
2319
Avi Kivity96304212011-05-15 10:13:13 -04002320static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002322 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002323 if (static_branch_unlikely(&enable_evmcs))
2324 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002325 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002326}
2327
Avi Kivity96304212011-05-15 10:13:13 -04002328static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002330 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002331 if (static_branch_unlikely(&enable_evmcs))
2332 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002333#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002334 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002336 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002337#endif
2338}
2339
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340static __always_inline unsigned long vmcs_readl(unsigned long field)
2341{
2342 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002343 if (static_branch_unlikely(&enable_evmcs))
2344 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002345 return __vmcs_readl(field);
2346}
2347
Avi Kivitye52de1b2007-01-05 16:36:56 -08002348static noinline void vmwrite_error(unsigned long field, unsigned long value)
2349{
2350 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2351 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2352 dump_stack();
2353}
2354
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002357 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002358
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002359 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2360 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002361 if (unlikely(error))
2362 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002363}
2364
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002365static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002366{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002367 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002368 if (static_branch_unlikely(&enable_evmcs))
2369 return evmcs_write16(field, value);
2370
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002371 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372}
2373
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002374static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002376 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002377 if (static_branch_unlikely(&enable_evmcs))
2378 return evmcs_write32(field, value);
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381}
2382
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002383static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002385 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002386 if (static_branch_unlikely(&enable_evmcs))
2387 return evmcs_write64(field, value);
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002390#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002392 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393#endif
2394}
2395
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002396static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002397{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002399 if (static_branch_unlikely(&enable_evmcs))
2400 return evmcs_write64(field, value);
2401
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002402 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002403}
2404
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002405static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002406{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2408 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002409 if (static_branch_unlikely(&enable_evmcs))
2410 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2411
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002412 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2413}
2414
2415static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2416{
2417 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2418 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002419 if (static_branch_unlikely(&enable_evmcs))
2420 return evmcs_write32(field, evmcs_read32(field) | mask);
2421
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002423}
2424
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002425static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2426{
2427 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2428}
2429
Gleb Natapov2961e8762013-11-25 15:37:13 +02002430static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2431{
2432 vmcs_write32(VM_ENTRY_CONTROLS, val);
2433 vmx->vm_entry_controls_shadow = val;
2434}
2435
2436static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2437{
2438 if (vmx->vm_entry_controls_shadow != val)
2439 vm_entry_controls_init(vmx, val);
2440}
2441
2442static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2443{
2444 return vmx->vm_entry_controls_shadow;
2445}
2446
2447
2448static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2449{
2450 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2451}
2452
2453static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2454{
2455 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2456}
2457
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002458static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2459{
2460 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2461}
2462
Gleb Natapov2961e8762013-11-25 15:37:13 +02002463static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2464{
2465 vmcs_write32(VM_EXIT_CONTROLS, val);
2466 vmx->vm_exit_controls_shadow = val;
2467}
2468
2469static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2470{
2471 if (vmx->vm_exit_controls_shadow != val)
2472 vm_exit_controls_init(vmx, val);
2473}
2474
2475static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2476{
2477 return vmx->vm_exit_controls_shadow;
2478}
2479
2480
2481static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2482{
2483 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2484}
2485
2486static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2487{
2488 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2489}
2490
Avi Kivity2fb92db2011-04-27 19:42:18 +03002491static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2492{
2493 vmx->segment_cache.bitmask = 0;
2494}
2495
2496static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2497 unsigned field)
2498{
2499 bool ret;
2500 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2501
2502 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2503 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2504 vmx->segment_cache.bitmask = 0;
2505 }
2506 ret = vmx->segment_cache.bitmask & mask;
2507 vmx->segment_cache.bitmask |= mask;
2508 return ret;
2509}
2510
2511static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2512{
2513 u16 *p = &vmx->segment_cache.seg[seg].selector;
2514
2515 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2516 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2517 return *p;
2518}
2519
2520static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2521{
2522 ulong *p = &vmx->segment_cache.seg[seg].base;
2523
2524 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2525 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2526 return *p;
2527}
2528
2529static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2530{
2531 u32 *p = &vmx->segment_cache.seg[seg].limit;
2532
2533 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2534 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2535 return *p;
2536}
2537
2538static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2539{
2540 u32 *p = &vmx->segment_cache.seg[seg].ar;
2541
2542 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2543 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2544 return *p;
2545}
2546
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002547static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2548{
2549 u32 eb;
2550
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002551 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002552 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002553 /*
2554 * Guest access to VMware backdoor ports could legitimately
2555 * trigger #GP because of TSS I/O permission bitmap.
2556 * We intercept those #GP and allow access to them anyway
2557 * as VMware does.
2558 */
2559 if (enable_vmware_backdoor)
2560 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002561 if ((vcpu->guest_debug &
2562 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2563 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2564 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002565 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002566 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002567 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002568 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002569
2570 /* When we are running a nested L2 guest and L1 specified for it a
2571 * certain exception bitmap, we must trap the same exceptions and pass
2572 * them to L1. When running L2, we will only handle the exceptions
2573 * specified above if L1 did not want them.
2574 */
2575 if (is_guest_mode(vcpu))
2576 eb |= get_vmcs12(vcpu)->exception_bitmap;
2577
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002578 vmcs_write32(EXCEPTION_BITMAP, eb);
2579}
2580
Ashok Raj15d45072018-02-01 22:59:43 +01002581/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002582 * Check if MSR is intercepted for currently loaded MSR bitmap.
2583 */
2584static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2585{
2586 unsigned long *msr_bitmap;
2587 int f = sizeof(unsigned long);
2588
2589 if (!cpu_has_vmx_msr_bitmap())
2590 return true;
2591
2592 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2593
2594 if (msr <= 0x1fff) {
2595 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2596 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2597 msr &= 0x1fff;
2598 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2599 }
2600
2601 return true;
2602}
2603
2604/*
Ashok Raj15d45072018-02-01 22:59:43 +01002605 * Check if MSR is intercepted for L01 MSR bitmap.
2606 */
2607static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2608{
2609 unsigned long *msr_bitmap;
2610 int f = sizeof(unsigned long);
2611
2612 if (!cpu_has_vmx_msr_bitmap())
2613 return true;
2614
2615 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2616
2617 if (msr <= 0x1fff) {
2618 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2619 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2620 msr &= 0x1fff;
2621 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2622 }
2623
2624 return true;
2625}
2626
Gleb Natapov2961e8762013-11-25 15:37:13 +02002627static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2628 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002629{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002630 vm_entry_controls_clearbit(vmx, entry);
2631 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002632}
2633
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002634static int find_msr(struct vmx_msrs *m, unsigned int msr)
2635{
2636 unsigned int i;
2637
2638 for (i = 0; i < m->nr; ++i) {
2639 if (m->val[i].index == msr)
2640 return i;
2641 }
2642 return -ENOENT;
2643}
2644
Avi Kivity61d2ef22010-04-28 16:40:38 +03002645static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2646{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002647 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002648 struct msr_autoload *m = &vmx->msr_autoload;
2649
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002650 switch (msr) {
2651 case MSR_EFER:
2652 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002653 clear_atomic_switch_msr_special(vmx,
2654 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002655 VM_EXIT_LOAD_IA32_EFER);
2656 return;
2657 }
2658 break;
2659 case MSR_CORE_PERF_GLOBAL_CTRL:
2660 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002661 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002662 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2663 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2664 return;
2665 }
2666 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002667 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002668 i = find_msr(&m->guest, msr);
2669 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002670 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002671 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002672 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002673 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002674
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002675skip_guest:
2676 i = find_msr(&m->host, msr);
2677 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002678 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002679
2680 --m->host.nr;
2681 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002682 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002683}
2684
Gleb Natapov2961e8762013-11-25 15:37:13 +02002685static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2686 unsigned long entry, unsigned long exit,
2687 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2688 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002689{
2690 vmcs_write64(guest_val_vmcs, guest_val);
2691 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002692 vm_entry_controls_setbit(vmx, entry);
2693 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002694}
2695
Avi Kivity61d2ef22010-04-28 16:40:38 +03002696static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002697 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002698{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002699 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002700 struct msr_autoload *m = &vmx->msr_autoload;
2701
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002702 switch (msr) {
2703 case MSR_EFER:
2704 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002705 add_atomic_switch_msr_special(vmx,
2706 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002707 VM_EXIT_LOAD_IA32_EFER,
2708 GUEST_IA32_EFER,
2709 HOST_IA32_EFER,
2710 guest_val, host_val);
2711 return;
2712 }
2713 break;
2714 case MSR_CORE_PERF_GLOBAL_CTRL:
2715 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002716 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002717 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2718 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2719 GUEST_IA32_PERF_GLOBAL_CTRL,
2720 HOST_IA32_PERF_GLOBAL_CTRL,
2721 guest_val, host_val);
2722 return;
2723 }
2724 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002725 case MSR_IA32_PEBS_ENABLE:
2726 /* PEBS needs a quiescent period after being disabled (to write
2727 * a record). Disabling PEBS through VMX MSR swapping doesn't
2728 * provide that period, so a CPU could write host's record into
2729 * guest's memory.
2730 */
2731 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002732 }
2733
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002734 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002735 if (!entry_only)
2736 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002737
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002738 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002739 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002740 "Can't add msr %x\n", msr);
2741 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002742 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002743 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002744 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002745 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002746 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002747 m->guest.val[i].index = msr;
2748 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002749
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002750 if (entry_only)
2751 return;
2752
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 if (j < 0) {
2754 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002755 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002756 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002757 m->host.val[j].index = msr;
2758 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002759}
2760
Avi Kivity92c0d902009-10-29 11:00:16 +02002761static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002762{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002763 u64 guest_efer = vmx->vcpu.arch.efer;
2764 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002765
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002766 if (!enable_ept) {
2767 /*
2768 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2769 * host CPUID is more efficient than testing guest CPUID
2770 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2771 */
2772 if (boot_cpu_has(X86_FEATURE_SMEP))
2773 guest_efer |= EFER_NX;
2774 else if (!(guest_efer & EFER_NX))
2775 ignore_bits |= EFER_NX;
2776 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002777
Avi Kivity51c6cf62007-08-29 03:48:05 +03002778 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002779 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002780 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002781 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002782#ifdef CONFIG_X86_64
2783 ignore_bits |= EFER_LMA | EFER_LME;
2784 /* SCE is meaningful only in long mode on Intel */
2785 if (guest_efer & EFER_LMA)
2786 ignore_bits &= ~(u64)EFER_SCE;
2787#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002788
2789 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002790
2791 /*
2792 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2793 * On CPUs that support "load IA32_EFER", always switch EFER
2794 * atomically, since it's faster than switching it manually.
2795 */
2796 if (cpu_has_load_ia32_efer ||
2797 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002798 if (!(guest_efer & EFER_LMA))
2799 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002800 if (guest_efer != host_efer)
2801 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002802 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002803 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002804 } else {
2805 guest_efer &= ~ignore_bits;
2806 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002807
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002808 vmx->guest_msrs[efer_offset].data = guest_efer;
2809 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2810
2811 return true;
2812 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002813}
2814
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002815#ifdef CONFIG_X86_32
2816/*
2817 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2818 * VMCS rather than the segment table. KVM uses this helper to figure
2819 * out the current bases to poke them into the VMCS before entry.
2820 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002821static unsigned long segment_base(u16 selector)
2822{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002823 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002824 unsigned long v;
2825
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002826 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002827 return 0;
2828
Thomas Garnier45fc8752017-03-14 10:05:08 -07002829 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002830
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002831 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002832 u16 ldt_selector = kvm_read_ldt();
2833
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002834 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002835 return 0;
2836
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002839 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002840 return v;
2841}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002842#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002843
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002844static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002845{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002846 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002847 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002848#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002849 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002850#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002851 unsigned long fs_base, gs_base;
2852 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002853 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002854
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002855 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002856 return;
2857
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002858 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002859 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002860
Avi Kivity33ed6322007-05-02 16:54:03 +03002861 /*
2862 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2863 * allow segment selectors with cpl > 0 or ti == 1.
2864 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002865 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002866
2867#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002868 savesegment(ds, host_state->ds_sel);
2869 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002870
2871 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002872 if (likely(is_64bit_mm(current->mm))) {
2873 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002874 fs_sel = current->thread.fsindex;
2875 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002876 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002877 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002878 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002879 savesegment(fs, fs_sel);
2880 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002881 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002882 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002883 }
2884
Avi Kivityc8770e72010-11-11 12:37:26 +02002885 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002886 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002887#else
Sean Christophersone368b872018-07-23 12:32:41 -07002888 savesegment(fs, fs_sel);
2889 savesegment(gs, gs_sel);
2890 fs_base = segment_base(fs_sel);
2891 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002892#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002893
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002894 if (unlikely(fs_sel != host_state->fs_sel)) {
2895 if (!(fs_sel & 7))
2896 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2897 else
2898 vmcs_write16(HOST_FS_SELECTOR, 0);
2899 host_state->fs_sel = fs_sel;
2900 }
2901 if (unlikely(gs_sel != host_state->gs_sel)) {
2902 if (!(gs_sel & 7))
2903 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2904 else
2905 vmcs_write16(HOST_GS_SELECTOR, 0);
2906 host_state->gs_sel = gs_sel;
2907 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002908 if (unlikely(fs_base != host_state->fs_base)) {
2909 vmcs_writel(HOST_FS_BASE, fs_base);
2910 host_state->fs_base = fs_base;
2911 }
2912 if (unlikely(gs_base != host_state->gs_base)) {
2913 vmcs_writel(HOST_GS_BASE, gs_base);
2914 host_state->gs_base = gs_base;
2915 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002916
Avi Kivity26bb0982009-09-07 11:14:12 +03002917 for (i = 0; i < vmx->save_nmsrs; ++i)
2918 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002919 vmx->guest_msrs[i].data,
2920 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002921}
2922
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002923static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002924{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002925 struct vmcs_host_state *host_state;
2926
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002927 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002928 return;
2929
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002930 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002931 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002932
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002933 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002934 vmx->loaded_cpu_state = NULL;
2935
Avi Kivityc8770e72010-11-11 12:37:26 +02002936#ifdef CONFIG_X86_64
2937 if (is_long_mode(&vmx->vcpu))
2938 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2939#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002940 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2941 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002942#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002943 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002944#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002945 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002946#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002947 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002948 if (host_state->fs_sel & 7)
2949 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002950#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002951 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2952 loadsegment(ds, host_state->ds_sel);
2953 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002954 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002955#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002956 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002957#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002958 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002959#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002960 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002961}
2962
Sean Christopherson678e3152018-07-23 12:32:43 -07002963#ifdef CONFIG_X86_64
2964static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002965{
Sean Christopherson678e3152018-07-23 12:32:43 -07002966 if (is_long_mode(&vmx->vcpu)) {
2967 preempt_disable();
2968 if (vmx->loaded_cpu_state)
2969 rdmsrl(MSR_KERNEL_GS_BASE,
2970 vmx->msr_guest_kernel_gs_base);
2971 preempt_enable();
2972 }
2973 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002974}
2975
Sean Christopherson678e3152018-07-23 12:32:43 -07002976static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2977{
2978 if (is_long_mode(&vmx->vcpu)) {
2979 preempt_disable();
2980 if (vmx->loaded_cpu_state)
2981 wrmsrl(MSR_KERNEL_GS_BASE, data);
2982 preempt_enable();
2983 }
2984 vmx->msr_guest_kernel_gs_base = data;
2985}
2986#endif
2987
Feng Wu28b835d2015-09-18 22:29:54 +08002988static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2989{
2990 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2991 struct pi_desc old, new;
2992 unsigned int dest;
2993
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002994 /*
2995 * In case of hot-plug or hot-unplug, we may have to undo
2996 * vmx_vcpu_pi_put even if there is no assigned device. And we
2997 * always keep PI.NDST up to date for simplicity: it makes the
2998 * code easier, and CPU migration is not a fast path.
2999 */
3000 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003001 return;
3002
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003003 /*
3004 * First handle the simple case where no cmpxchg is necessary; just
3005 * allow posting non-urgent interrupts.
3006 *
3007 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3008 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3009 * expects the VCPU to be on the blocked_vcpu_list that matches
3010 * PI.NDST.
3011 */
3012 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3013 vcpu->cpu == cpu) {
3014 pi_clear_sn(pi_desc);
3015 return;
3016 }
3017
3018 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003019 do {
3020 old.control = new.control = pi_desc->control;
3021
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003022 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003023
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003024 if (x2apic_enabled())
3025 new.ndst = dest;
3026 else
3027 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003028
Feng Wu28b835d2015-09-18 22:29:54 +08003029 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003030 } while (cmpxchg64(&pi_desc->control, old.control,
3031 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003032}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003033
Peter Feinerc95ba922016-08-17 09:36:47 -07003034static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3035{
3036 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3037 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3038}
3039
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040/*
3041 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3042 * vcpu mutex is already taken.
3043 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003044static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003045{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003046 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003047 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003049 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003050 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003051 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003052 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003053
3054 /*
3055 * Read loaded_vmcs->cpu should be before fetching
3056 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3057 * See the comments in __loaded_vmcs_clear().
3058 */
3059 smp_rmb();
3060
Nadav Har'Eld462b812011-05-24 15:26:10 +03003061 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3062 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003063 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003064 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003065 }
3066
3067 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3068 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3069 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003070 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003071 }
3072
3073 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003074 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003075 unsigned long sysenter_esp;
3076
3077 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003078
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079 /*
3080 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003081 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003083 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003084 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003085 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003087 /*
3088 * VM exits change the host TR limit to 0x67 after a VM
3089 * exit. This is okay, since 0x67 covers everything except
3090 * the IO bitmap and have have code to handle the IO bitmap
3091 * being lost after a VM exit.
3092 */
3093 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3094
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3096 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003097
Nadav Har'Eld462b812011-05-24 15:26:10 +03003098 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 }
Feng Wu28b835d2015-09-18 22:29:54 +08003100
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003101 /* Setup TSC multiplier */
3102 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003103 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3104 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003105
Feng Wu28b835d2015-09-18 22:29:54 +08003106 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003107 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003108 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003109}
3110
3111static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3112{
3113 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3114
3115 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003116 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3117 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003118 return;
3119
3120 /* Set SN when the vCPU is preempted */
3121 if (vcpu->preempted)
3122 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003123}
3124
3125static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3126{
Feng Wu28b835d2015-09-18 22:29:54 +08003127 vmx_vcpu_pi_put(vcpu);
3128
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003129 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130}
3131
Wanpeng Lif244dee2017-07-20 01:11:54 -07003132static bool emulation_required(struct kvm_vcpu *vcpu)
3133{
3134 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3135}
3136
Avi Kivityedcafe32009-12-30 18:07:40 +02003137static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3138
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003139/*
3140 * Return the cr0 value that a nested guest would read. This is a combination
3141 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3142 * its hypervisor (cr0_read_shadow).
3143 */
3144static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3145{
3146 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3147 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3148}
3149static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3150{
3151 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3152 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3153}
3154
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3156{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003157 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003158
Avi Kivity6de12732011-03-07 12:51:22 +02003159 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3160 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3161 rflags = vmcs_readl(GUEST_RFLAGS);
3162 if (to_vmx(vcpu)->rmode.vm86_active) {
3163 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3164 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3165 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3166 }
3167 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003168 }
Avi Kivity6de12732011-03-07 12:51:22 +02003169 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170}
3171
3172static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3173{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003174 unsigned long old_rflags = vmx_get_rflags(vcpu);
3175
Avi Kivity6de12732011-03-07 12:51:22 +02003176 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3177 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003178 if (to_vmx(vcpu)->rmode.vm86_active) {
3179 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003180 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003181 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003183
3184 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3185 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186}
3187
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003188static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003189{
3190 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3191 int ret = 0;
3192
3193 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003194 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003195 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003196 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003197
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003198 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003199}
3200
3201static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3202{
3203 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3204 u32 interruptibility = interruptibility_old;
3205
3206 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3207
Jan Kiszka48005f62010-02-19 19:38:07 +01003208 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003210 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003211 interruptibility |= GUEST_INTR_STATE_STI;
3212
3213 if ((interruptibility != interruptibility_old))
3214 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3215}
3216
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3218{
3219 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003221 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003223 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224
Glauber Costa2809f5d2009-05-12 16:21:05 -04003225 /* skipping an emulated instruction also counts */
3226 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227}
3228
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003229static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3230 unsigned long exit_qual)
3231{
3232 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3233 unsigned int nr = vcpu->arch.exception.nr;
3234 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3235
3236 if (vcpu->arch.exception.has_error_code) {
3237 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3238 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3239 }
3240
3241 if (kvm_exception_is_soft(nr))
3242 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3243 else
3244 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3245
3246 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3247 vmx_get_nmi_mask(vcpu))
3248 intr_info |= INTR_INFO_UNBLOCK_NMI;
3249
3250 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3251}
3252
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003253/*
3254 * KVM wants to inject page-faults which it got to the guest. This function
3255 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003256 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003257static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003258{
3259 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003260 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003261
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003262 if (nr == PF_VECTOR) {
3263 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003264 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003265 return 1;
3266 }
3267 /*
3268 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3269 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3270 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3271 * can be written only when inject_pending_event runs. This should be
3272 * conditional on a new capability---if the capability is disabled,
3273 * kvm_multiple_exception would write the ancillary information to
3274 * CR2 or DR6, for backwards ABI-compatibility.
3275 */
3276 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3277 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003278 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003279 return 1;
3280 }
3281 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003282 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003283 if (nr == DB_VECTOR)
3284 *exit_qual = vcpu->arch.dr6;
3285 else
3286 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003287 return 1;
3288 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003289 }
3290
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003291 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003292}
3293
Wanpeng Licaa057a2018-03-12 04:53:03 -07003294static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3295{
3296 /*
3297 * Ensure that we clear the HLT state in the VMCS. We don't need to
3298 * explicitly skip the instruction because if the HLT state is set,
3299 * then the instruction is already executing and RIP has already been
3300 * advanced.
3301 */
3302 if (kvm_hlt_in_guest(vcpu->kvm) &&
3303 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3304 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3305}
3306
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003307static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003308{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003309 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003310 unsigned nr = vcpu->arch.exception.nr;
3311 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003312 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003313 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003314
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003315 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003316 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003317 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3318 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003319
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003320 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003321 int inc_eip = 0;
3322 if (kvm_exception_is_soft(nr))
3323 inc_eip = vcpu->arch.event_exit_inst_len;
3324 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003325 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003326 return;
3327 }
3328
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003329 WARN_ON_ONCE(vmx->emulation_required);
3330
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003331 if (kvm_exception_is_soft(nr)) {
3332 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3333 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003334 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3335 } else
3336 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3337
3338 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003339
3340 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003341}
3342
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003343static bool vmx_rdtscp_supported(void)
3344{
3345 return cpu_has_vmx_rdtscp();
3346}
3347
Mao, Junjiead756a12012-07-02 01:18:48 +00003348static bool vmx_invpcid_supported(void)
3349{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003350 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003351}
3352
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353/*
Eddie Donga75beee2007-05-17 18:55:15 +03003354 * Swap MSR entry in host/guest MSR entry array.
3355 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003356static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003357{
Avi Kivity26bb0982009-09-07 11:14:12 +03003358 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003359
3360 tmp = vmx->guest_msrs[to];
3361 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3362 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003363}
3364
3365/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003366 * Set up the vmcs to automatically save and restore system
3367 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3368 * mode, as fiddling with msrs is very expensive.
3369 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003370static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003371{
Avi Kivity26bb0982009-09-07 11:14:12 +03003372 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003373
Eddie Donga75beee2007-05-17 18:55:15 +03003374 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003375#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003376 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003377 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003378 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003379 move_msr_up(vmx, index, save_nmsrs++);
3380 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003381 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003382 move_msr_up(vmx, index, save_nmsrs++);
3383 index = __find_msr_index(vmx, MSR_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003386 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003387 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003388 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003389 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003390 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003391 * if efer.sce is enabled.
3392 */
Brian Gerst8c065852010-07-17 09:03:26 -04003393 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003394 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003395 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003396 }
Eddie Donga75beee2007-05-17 18:55:15 +03003397#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003398 index = __find_msr_index(vmx, MSR_EFER);
3399 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003400 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003401
Avi Kivity26bb0982009-09-07 11:14:12 +03003402 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003403
Yang Zhang8d146952013-01-25 10:18:50 +08003404 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003405 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003406}
3407
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003408static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003410 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003412 if (is_guest_mode(vcpu) &&
3413 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3414 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3415
3416 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417}
3418
3419/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003420 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003422static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003424 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003425 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003426 * We're here if L1 chose not to trap WRMSR to TSC. According
3427 * to the spec, this should set L1's TSC; The offset that L1
3428 * set for L2 remains unchanged, and still needs to be added
3429 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003430 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003431 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003432 /* recalculate vmcs02.TSC_OFFSET: */
3433 vmcs12 = get_vmcs12(vcpu);
3434 vmcs_write64(TSC_OFFSET, offset +
3435 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3436 vmcs12->tsc_offset : 0));
3437 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003438 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3439 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003440 vmcs_write64(TSC_OFFSET, offset);
3441 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442}
3443
Nadav Har'El801d3422011-05-25 23:02:23 +03003444/*
3445 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3446 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3447 * all guests if the "nested" module option is off, and can also be disabled
3448 * for a single guest by disabling its VMX cpuid bit.
3449 */
3450static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3451{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003452 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003453}
3454
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003456 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3457 * returned for the various VMX controls MSRs when nested VMX is enabled.
3458 * The same values should also be used to verify that vmcs12 control fields are
3459 * valid during nested entry from L1 to L2.
3460 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3461 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3462 * bit in the high half is on if the corresponding bit in the control field
3463 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003464 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003465static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466{
Paolo Bonzini13893092018-02-26 13:40:09 +01003467 if (!nested) {
3468 memset(msrs, 0, sizeof(*msrs));
3469 return;
3470 }
3471
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003472 /*
3473 * Note that as a general rule, the high half of the MSRs (bits in
3474 * the control fields which may be 1) should be initialized by the
3475 * intersection of the underlying hardware's MSR (i.e., features which
3476 * can be supported) and the list of features we want to expose -
3477 * because they are known to be properly supported in our code.
3478 * Also, usually, the low half of the MSRs (bits which must be 1) can
3479 * be set to 0, meaning that L1 may turn off any of these bits. The
3480 * reason is that if one of these bits is necessary, it will appear
3481 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3482 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003483 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003484 * These rules have exceptions below.
3485 */
3486
3487 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003488 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003489 msrs->pinbased_ctls_low,
3490 msrs->pinbased_ctls_high);
3491 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003492 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003493 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003494 PIN_BASED_EXT_INTR_MASK |
3495 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003496 PIN_BASED_VIRTUAL_NMIS |
3497 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003498 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003499 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003500 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003501
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003502 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003503 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003504 msrs->exit_ctls_low,
3505 msrs->exit_ctls_high);
3506 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003507 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003508
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003509 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003510#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003511 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003512#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003513 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003514 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003515 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003516 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003517 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3518
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003519 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003520 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003521
Jan Kiszka2996fca2014-06-16 13:59:43 +02003522 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003523 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003524
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003525 /* entry controls */
3526 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003527 msrs->entry_ctls_low,
3528 msrs->entry_ctls_high);
3529 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003530 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003531 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003532#ifdef CONFIG_X86_64
3533 VM_ENTRY_IA32E_MODE |
3534#endif
3535 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003536 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003537 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003538 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003540
Jan Kiszka2996fca2014-06-16 13:59:43 +02003541 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003542 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003543
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003544 /* cpu-based controls */
3545 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->procbased_ctls_low,
3547 msrs->procbased_ctls_high);
3548 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003549 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003550 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003551 CPU_BASED_VIRTUAL_INTR_PENDING |
3552 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003553 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3554 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3555 CPU_BASED_CR3_STORE_EXITING |
3556#ifdef CONFIG_X86_64
3557 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3558#endif
3559 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003560 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3561 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3562 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3563 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003564 /*
3565 * We can allow some features even when not supported by the
3566 * hardware. For example, L1 can specify an MSR bitmap - and we
3567 * can use it to avoid exits to L1 - even when L0 runs L2
3568 * without MSR bitmaps.
3569 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003570 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003571 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003572 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003573
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003574 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003575 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003576 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3577
Paolo Bonzini80154d72017-08-24 13:55:35 +02003578 /*
3579 * secondary cpu-based controls. Do not include those that
3580 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3581 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003582 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003583 msrs->secondary_ctls_low,
3584 msrs->secondary_ctls_high);
3585 msrs->secondary_ctls_low = 0;
3586 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003587 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003588 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003589 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003590 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003591 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003592 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003593 /*
3594 * We can emulate "VMCS shadowing," even if the hardware
3595 * doesn't support it.
3596 */
3597 msrs->secondary_ctls_high |=
3598 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003599
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003600 if (enable_ept) {
3601 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003602 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003603 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003604 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003605 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003606 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003608 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003609 msrs->ept_caps &= vmx_capability.ept;
3610 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003611 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3612 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003613 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003614 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003615 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003616 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003617 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003618 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003619
Bandan Das27c42a12017-08-03 15:54:42 -04003620 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003622 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003623 /*
3624 * Advertise EPTP switching unconditionally
3625 * since we emulate it
3626 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003627 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003629 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003630 }
3631
Paolo Bonzinief697a72016-03-18 16:58:38 +01003632 /*
3633 * Old versions of KVM use the single-context version without
3634 * checking for support, so declare that it is supported even
3635 * though it is treated as global context. The alternative is
3636 * not failing the single-context invvpid, and it is worse.
3637 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003638 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003639 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003640 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003641 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003642 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003643 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003644
Radim Krčmář0790ec12015-03-17 14:02:32 +01003645 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003647 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3648
Jan Kiszkac18911a2013-03-13 16:06:41 +01003649 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003650 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->misc_low,
3652 msrs->misc_high);
3653 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3654 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003655 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003656 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003657 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003659
3660 /*
3661 * This MSR reports some information about VMX support. We
3662 * should return information about the VMX we emulate for the
3663 * guest, and the VMCS structure we give it - not about the
3664 * VMX support of the underlying hardware.
3665 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003666 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003667 VMCS12_REVISION |
3668 VMX_BASIC_TRUE_CTLS |
3669 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3670 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3671
3672 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003673 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003674
3675 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003676 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003677 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3678 * We picked the standard core2 setting.
3679 */
3680#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3681#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3683 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003684
3685 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3687 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003688
3689 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003690 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003691}
3692
David Matlack38991522016-11-29 18:14:08 -08003693/*
3694 * if fixed0[i] == 1: val[i] must be 1
3695 * if fixed1[i] == 0: val[i] must be 0
3696 */
3697static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3698{
3699 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700}
3701
3702static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3703{
David Matlack38991522016-11-29 18:14:08 -08003704 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003705}
3706
3707static inline u64 vmx_control_msr(u32 low, u32 high)
3708{
3709 return low | ((u64)high << 32);
3710}
3711
David Matlack62cc6b9d2016-11-29 18:14:07 -08003712static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3713{
3714 superset &= mask;
3715 subset &= mask;
3716
3717 return (superset | subset) == superset;
3718}
3719
3720static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3721{
3722 const u64 feature_and_reserved =
3723 /* feature (except bit 48; see below) */
3724 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3725 /* reserved */
3726 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003727 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003728
3729 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3730 return -EINVAL;
3731
3732 /*
3733 * KVM does not emulate a version of VMX that constrains physical
3734 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3735 */
3736 if (data & BIT_ULL(48))
3737 return -EINVAL;
3738
3739 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3740 vmx_basic_vmcs_revision_id(data))
3741 return -EINVAL;
3742
3743 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3744 return -EINVAL;
3745
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003746 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003747 return 0;
3748}
3749
3750static int
3751vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3752{
3753 u64 supported;
3754 u32 *lowp, *highp;
3755
3756 switch (msr_index) {
3757 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3759 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003760 break;
3761 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003762 lowp = &vmx->nested.msrs.procbased_ctls_low;
3763 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003764 break;
3765 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003766 lowp = &vmx->nested.msrs.exit_ctls_low;
3767 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003768 break;
3769 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.entry_ctls_low;
3771 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.secondary_ctls_low;
3775 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 default:
3778 BUG();
3779 }
3780
3781 supported = vmx_control_msr(*lowp, *highp);
3782
3783 /* Check must-be-1 bits are still 1. */
3784 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3785 return -EINVAL;
3786
3787 /* Check must-be-0 bits are still 0. */
3788 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3789 return -EINVAL;
3790
3791 *lowp = data;
3792 *highp = data >> 32;
3793 return 0;
3794}
3795
3796static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3797{
3798 const u64 feature_and_reserved_bits =
3799 /* feature */
3800 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3801 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3802 /* reserved */
3803 GENMASK_ULL(13, 9) | BIT_ULL(31);
3804 u64 vmx_misc;
3805
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003806 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3807 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003808
3809 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3810 return -EINVAL;
3811
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003812 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003813 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3814 vmx_misc_preemption_timer_rate(data) !=
3815 vmx_misc_preemption_timer_rate(vmx_misc))
3816 return -EINVAL;
3817
3818 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3819 return -EINVAL;
3820
3821 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3822 return -EINVAL;
3823
3824 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3825 return -EINVAL;
3826
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003827 vmx->nested.msrs.misc_low = data;
3828 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003829
3830 /*
3831 * If L1 has read-only VM-exit information fields, use the
3832 * less permissive vmx_vmwrite_bitmap to specify write
3833 * permissions for the shadow VMCS.
3834 */
3835 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3836 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3837
David Matlack62cc6b9d2016-11-29 18:14:07 -08003838 return 0;
3839}
3840
3841static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3842{
3843 u64 vmx_ept_vpid_cap;
3844
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003845 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3846 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003847
3848 /* Every bit is either reserved or a feature bit. */
3849 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3850 return -EINVAL;
3851
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003852 vmx->nested.msrs.ept_caps = data;
3853 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003854 return 0;
3855}
3856
3857static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3858{
3859 u64 *msr;
3860
3861 switch (msr_index) {
3862 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003863 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003864 break;
3865 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003866 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003867 break;
3868 default:
3869 BUG();
3870 }
3871
3872 /*
3873 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3874 * must be 1 in the restored value.
3875 */
3876 if (!is_bitwise_subset(data, *msr, -1ULL))
3877 return -EINVAL;
3878
3879 *msr = data;
3880 return 0;
3881}
3882
3883/*
3884 * Called when userspace is restoring VMX MSRs.
3885 *
3886 * Returns 0 on success, non-0 otherwise.
3887 */
3888static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3889{
3890 struct vcpu_vmx *vmx = to_vmx(vcpu);
3891
Jim Mattsona943ac52018-05-29 09:11:32 -07003892 /*
3893 * Don't allow changes to the VMX capability MSRs while the vCPU
3894 * is in VMX operation.
3895 */
3896 if (vmx->nested.vmxon)
3897 return -EBUSY;
3898
David Matlack62cc6b9d2016-11-29 18:14:07 -08003899 switch (msr_index) {
3900 case MSR_IA32_VMX_BASIC:
3901 return vmx_restore_vmx_basic(vmx, data);
3902 case MSR_IA32_VMX_PINBASED_CTLS:
3903 case MSR_IA32_VMX_PROCBASED_CTLS:
3904 case MSR_IA32_VMX_EXIT_CTLS:
3905 case MSR_IA32_VMX_ENTRY_CTLS:
3906 /*
3907 * The "non-true" VMX capability MSRs are generated from the
3908 * "true" MSRs, so we do not support restoring them directly.
3909 *
3910 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3911 * should restore the "true" MSRs with the must-be-1 bits
3912 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3913 * DEFAULT SETTINGS".
3914 */
3915 return -EINVAL;
3916 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3917 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3918 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3919 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3920 case MSR_IA32_VMX_PROCBASED_CTLS2:
3921 return vmx_restore_control_msr(vmx, msr_index, data);
3922 case MSR_IA32_VMX_MISC:
3923 return vmx_restore_vmx_misc(vmx, data);
3924 case MSR_IA32_VMX_CR0_FIXED0:
3925 case MSR_IA32_VMX_CR4_FIXED0:
3926 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3927 case MSR_IA32_VMX_CR0_FIXED1:
3928 case MSR_IA32_VMX_CR4_FIXED1:
3929 /*
3930 * These MSRs are generated based on the vCPU's CPUID, so we
3931 * do not support restoring them directly.
3932 */
3933 return -EINVAL;
3934 case MSR_IA32_VMX_EPT_VPID_CAP:
3935 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3936 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003937 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003938 return 0;
3939 default:
3940 /*
3941 * The rest of the VMX capability MSRs do not support restore.
3942 */
3943 return -EINVAL;
3944 }
3945}
3946
Jan Kiszkacae50132014-01-04 18:47:22 +01003947/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003948static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003949{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003950 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003951 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003952 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003953 break;
3954 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3955 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003956 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003957 msrs->pinbased_ctls_low,
3958 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003959 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3960 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003961 break;
3962 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3963 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003964 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003965 msrs->procbased_ctls_low,
3966 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003967 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3968 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003969 break;
3970 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3971 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003972 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003973 msrs->exit_ctls_low,
3974 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003975 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3976 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003977 break;
3978 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3979 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003980 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003981 msrs->entry_ctls_low,
3982 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003983 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3984 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003985 break;
3986 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003987 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003988 msrs->misc_low,
3989 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003990 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003992 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003993 break;
3994 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003995 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003996 break;
3997 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003998 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
4000 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004007 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004008 msrs->secondary_ctls_low,
4009 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004010 break;
4011 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004012 *pdata = msrs->ept_caps |
4013 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004015 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004017 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004018 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004020 }
4021
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 return 0;
4023}
4024
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004025static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4026 uint64_t val)
4027{
4028 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4029
4030 return !(val & ~valid_bits);
4031}
4032
Tom Lendacky801e4592018-02-21 13:39:51 -06004033static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4034{
Paolo Bonzini13893092018-02-26 13:40:09 +01004035 switch (msr->index) {
4036 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4037 if (!nested)
4038 return 1;
4039 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4040 default:
4041 return 1;
4042 }
4043
4044 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004045}
4046
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004047/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048 * Reads an msr value (of 'msr_index') into 'pdata'.
4049 * Returns 0 on success, non-0 otherwise.
4050 * Assumes vcpu_load() was already called.
4051 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004052static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004054 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004055 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004057 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004058#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004060 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061 break;
4062 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004063 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004065 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004066 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004067 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004068#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004069 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004070 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004071 case MSR_IA32_SPEC_CTRL:
4072 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004073 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4074 return 1;
4075
4076 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4077 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004078 case MSR_IA32_ARCH_CAPABILITIES:
4079 if (!msr_info->host_initiated &&
4080 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4081 return 1;
4082 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4083 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004085 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 break;
4087 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004088 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 break;
4090 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004091 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004093 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004094 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004095 (!msr_info->host_initiated &&
4096 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004097 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004098 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004099 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004100 case MSR_IA32_MCG_EXT_CTL:
4101 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004102 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004103 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004104 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004105 msr_info->data = vcpu->arch.mcg_ext_ctl;
4106 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004107 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004108 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004109 break;
4110 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4111 if (!nested_vmx_allowed(vcpu))
4112 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004113 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4114 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004115 case MSR_IA32_XSS:
4116 if (!vmx_xsaves_supported())
4117 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004118 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004119 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004120 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004121 if (!msr_info->host_initiated &&
4122 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004123 return 1;
4124 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004126 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004127 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004128 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004129 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004130 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004131 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004132 }
4133
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 return 0;
4135}
4136
Jan Kiszkacae50132014-01-04 18:47:22 +01004137static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4138
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139/*
4140 * Writes msr value into into the appropriate "register".
4141 * Returns 0 on success, non-0 otherwise.
4142 * Assumes vcpu_load() was already called.
4143 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004144static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004145{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004146 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004147 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004148 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004149 u32 msr_index = msr_info->index;
4150 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004151
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004153 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004154 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004155 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004156#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004158 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 vmcs_writel(GUEST_FS_BASE, data);
4160 break;
4161 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004162 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 vmcs_writel(GUEST_GS_BASE, data);
4164 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004165 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004166 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004167 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168#endif
4169 case MSR_IA32_SYSENTER_CS:
4170 vmcs_write32(GUEST_SYSENTER_CS, data);
4171 break;
4172 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004173 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 break;
4175 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004176 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004178 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004179 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004180 (!msr_info->host_initiated &&
4181 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004182 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004183 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004184 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004186 vmcs_write64(GUEST_BNDCFGS, data);
4187 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004188 case MSR_IA32_SPEC_CTRL:
4189 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004190 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4191 return 1;
4192
4193 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004194 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004195 return 1;
4196
4197 vmx->spec_ctrl = data;
4198
4199 if (!data)
4200 break;
4201
4202 /*
4203 * For non-nested:
4204 * When it's written (to non-zero) for the first time, pass
4205 * it through.
4206 *
4207 * For nested:
4208 * The handling of the MSR bitmap for L2 guests is done in
4209 * nested_vmx_merge_msr_bitmap. We should not touch the
4210 * vmcs02.msr_bitmap here since it gets completely overwritten
4211 * in the merging. We update the vmcs01 here for L1 as well
4212 * since it will end up touching the MSR anyway now.
4213 */
4214 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4215 MSR_IA32_SPEC_CTRL,
4216 MSR_TYPE_RW);
4217 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004218 case MSR_IA32_PRED_CMD:
4219 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004220 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4221 return 1;
4222
4223 if (data & ~PRED_CMD_IBPB)
4224 return 1;
4225
4226 if (!data)
4227 break;
4228
4229 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4230
4231 /*
4232 * For non-nested:
4233 * When it's written (to non-zero) for the first time, pass
4234 * it through.
4235 *
4236 * For nested:
4237 * The handling of the MSR bitmap for L2 guests is done in
4238 * nested_vmx_merge_msr_bitmap. We should not touch the
4239 * vmcs02.msr_bitmap here since it gets completely overwritten
4240 * in the merging.
4241 */
4242 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4243 MSR_TYPE_W);
4244 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004245 case MSR_IA32_ARCH_CAPABILITIES:
4246 if (!msr_info->host_initiated)
4247 return 1;
4248 vmx->arch_capabilities = data;
4249 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004250 case MSR_IA32_CR_PAT:
4251 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004252 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4253 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004254 vmcs_write64(GUEST_IA32_PAT, data);
4255 vcpu->arch.pat = data;
4256 break;
4257 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004258 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004259 break;
Will Auldba904632012-11-29 12:42:50 -08004260 case MSR_IA32_TSC_ADJUST:
4261 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004262 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004263 case MSR_IA32_MCG_EXT_CTL:
4264 if ((!msr_info->host_initiated &&
4265 !(to_vmx(vcpu)->msr_ia32_feature_control &
4266 FEATURE_CONTROL_LMCE)) ||
4267 (data & ~MCG_EXT_CTL_LMCE_EN))
4268 return 1;
4269 vcpu->arch.mcg_ext_ctl = data;
4270 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004271 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004272 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004273 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004274 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4275 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004276 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004277 if (msr_info->host_initiated && data == 0)
4278 vmx_leave_nested(vcpu);
4279 break;
4280 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004281 if (!msr_info->host_initiated)
4282 return 1; /* they are read-only */
4283 if (!nested_vmx_allowed(vcpu))
4284 return 1;
4285 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004286 case MSR_IA32_XSS:
4287 if (!vmx_xsaves_supported())
4288 return 1;
4289 /*
4290 * The only supported bit as of Skylake is bit 8, but
4291 * it is not supported on KVM.
4292 */
4293 if (data != 0)
4294 return 1;
4295 vcpu->arch.ia32_xss = data;
4296 if (vcpu->arch.ia32_xss != host_xss)
4297 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004298 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004299 else
4300 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4301 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004302 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004303 if (!msr_info->host_initiated &&
4304 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004305 return 1;
4306 /* Check reserved bit, higher 32 bits should be zero */
4307 if ((data >> 32) != 0)
4308 return 1;
4309 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004311 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004312 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004313 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004314 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004315 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4316 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004317 ret = kvm_set_shared_msr(msr->index, msr->data,
4318 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004319 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004320 if (ret)
4321 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004322 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004323 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004324 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004325 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326 }
4327
Eddie Dong2cc51562007-05-21 07:28:09 +03004328 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329}
4330
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004331static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004333 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4334 switch (reg) {
4335 case VCPU_REGS_RSP:
4336 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4337 break;
4338 case VCPU_REGS_RIP:
4339 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4340 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004341 case VCPU_EXREG_PDPTR:
4342 if (enable_ept)
4343 ept_save_pdptrs(vcpu);
4344 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 default:
4346 break;
4347 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348}
4349
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350static __init int cpu_has_kvm_support(void)
4351{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004352 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004353}
4354
4355static __init int vmx_disabled_by_bios(void)
4356{
4357 u64 msr;
4358
4359 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004360 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004361 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004362 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4363 && tboot_enabled())
4364 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004365 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004366 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004367 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004368 && !tboot_enabled()) {
4369 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004370 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004371 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004372 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 /* launched w/o TXT and VMX disabled */
4374 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4375 && !tboot_enabled())
4376 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004377 }
4378
4379 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004380}
4381
Dongxiao Xu7725b892010-05-11 18:29:38 +08004382static void kvm_cpu_vmxon(u64 addr)
4383{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004384 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004385 intel_pt_handle_vmx(1);
4386
Dongxiao Xu7725b892010-05-11 18:29:38 +08004387 asm volatile (ASM_VMX_VMXON_RAX
4388 : : "a"(&addr), "m"(addr)
4389 : "memory", "cc");
4390}
4391
Radim Krčmář13a34e02014-08-28 15:13:03 +02004392static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393{
4394 int cpu = raw_smp_processor_id();
4395 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004396 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004398 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004399 return -EBUSY;
4400
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004401 /*
4402 * This can happen if we hot-added a CPU but failed to allocate
4403 * VP assist page for it.
4404 */
4405 if (static_branch_unlikely(&enable_evmcs) &&
4406 !hv_get_vp_assist_page(cpu))
4407 return -EFAULT;
4408
Nadav Har'Eld462b812011-05-24 15:26:10 +03004409 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004410 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4411 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004412
4413 /*
4414 * Now we can enable the vmclear operation in kdump
4415 * since the loaded_vmcss_on_cpu list on this cpu
4416 * has been initialized.
4417 *
4418 * Though the cpu is not in VMX operation now, there
4419 * is no problem to enable the vmclear operation
4420 * for the loaded_vmcss_on_cpu list is empty!
4421 */
4422 crash_enable_local_vmclear(cpu);
4423
Avi Kivity6aa8b732006-12-10 02:21:36 -08004424 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004425
4426 test_bits = FEATURE_CONTROL_LOCKED;
4427 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4428 if (tboot_enabled())
4429 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4430
4431 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004432 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004433 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4434 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004435 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004436 if (enable_ept)
4437 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004438
4439 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004440}
4441
Nadav Har'Eld462b812011-05-24 15:26:10 +03004442static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004443{
4444 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004445 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004446
Nadav Har'Eld462b812011-05-24 15:26:10 +03004447 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4448 loaded_vmcss_on_cpu_link)
4449 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004450}
4451
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004452
4453/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4454 * tricks.
4455 */
4456static void kvm_cpu_vmxoff(void)
4457{
4458 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004459
4460 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004461 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004462}
4463
Radim Krčmář13a34e02014-08-28 15:13:03 +02004464static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004466 vmclear_local_loaded_vmcss();
4467 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468}
4469
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004470static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004471 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472{
4473 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004474 u32 ctl = ctl_min | ctl_opt;
4475
4476 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4477
4478 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4479 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4480
4481 /* Ensure minimum (required) set of control bits are supported. */
4482 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004483 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004484
4485 *result = ctl;
4486 return 0;
4487}
4488
Avi Kivity110312c2010-12-21 12:54:20 +02004489static __init bool allow_1_setting(u32 msr, u32 ctl)
4490{
4491 u32 vmx_msr_low, vmx_msr_high;
4492
4493 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4494 return vmx_msr_high & ctl;
4495}
4496
Yang, Sheng002c7f72007-07-31 14:23:01 +03004497static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004498{
4499 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004500 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004501 u32 _pin_based_exec_control = 0;
4502 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004503 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504 u32 _vmexit_control = 0;
4505 u32 _vmentry_control = 0;
4506
Paolo Bonzini13893092018-02-26 13:40:09 +01004507 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304508 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004509#ifdef CONFIG_X86_64
4510 CPU_BASED_CR8_LOAD_EXITING |
4511 CPU_BASED_CR8_STORE_EXITING |
4512#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004513 CPU_BASED_CR3_LOAD_EXITING |
4514 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004515 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004517 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004518 CPU_BASED_MWAIT_EXITING |
4519 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004520 CPU_BASED_INVLPG_EXITING |
4521 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004522
Sheng Yangf78e0e22007-10-29 09:40:42 +08004523 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004524 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004525 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4527 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004528 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004529#ifdef CONFIG_X86_64
4530 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4531 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4532 ~CPU_BASED_CR8_STORE_EXITING;
4533#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004534 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004535 min2 = 0;
4536 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004537 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004538 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004539 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004540 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004541 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004542 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004543 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004544 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004545 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004546 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004547 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004548 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004549 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004550 SECONDARY_EXEC_RDSEED_EXITING |
4551 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004552 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004553 SECONDARY_EXEC_TSC_SCALING |
4554 SECONDARY_EXEC_ENABLE_VMFUNC;
Sheng Yangd56f5462008-04-25 10:13:16 +08004555 if (adjust_vmx_controls(min2, opt2,
4556 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004557 &_cpu_based_2nd_exec_control) < 0)
4558 return -EIO;
4559 }
4560#ifndef CONFIG_X86_64
4561 if (!(_cpu_based_2nd_exec_control &
4562 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4563 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4564#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004565
4566 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4567 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004568 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004569 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4570 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004571
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004572 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4573 &vmx_capability.ept, &vmx_capability.vpid);
4574
Sheng Yangd56f5462008-04-25 10:13:16 +08004575 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004576 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4577 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004578 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4579 CPU_BASED_CR3_STORE_EXITING |
4580 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004581 } else if (vmx_capability.ept) {
4582 vmx_capability.ept = 0;
4583 pr_warn_once("EPT CAP should not exist if not support "
4584 "1-setting enable EPT VM-execution control\n");
4585 }
4586 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4587 vmx_capability.vpid) {
4588 vmx_capability.vpid = 0;
4589 pr_warn_once("VPID CAP should not exist if not support "
4590 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004591 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004592
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004593 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004594#ifdef CONFIG_X86_64
4595 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4596#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004597 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004598 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004599 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4600 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004601 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004602
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004603 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4604 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4605 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004606 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4607 &_pin_based_exec_control) < 0)
4608 return -EIO;
4609
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004610 if (cpu_has_broken_vmx_preemption_timer())
4611 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004612 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004613 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004614 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4615
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004616 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004617 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004618 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4619 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004620 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004622 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004623
4624 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4625 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004626 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004627
4628#ifdef CONFIG_X86_64
4629 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4630 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004631 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004632#endif
4633
4634 /* Require Write-Back (WB) memory type for VMCS accesses. */
4635 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004636 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637
Yang, Sheng002c7f72007-07-31 14:23:01 +03004638 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004639 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004640 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004641
Liran Alon2307af12018-06-29 22:59:04 +03004642 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004643
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4645 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004646 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004647 vmcs_conf->vmexit_ctrl = _vmexit_control;
4648 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004649
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004650 if (static_branch_unlikely(&enable_evmcs))
4651 evmcs_sanitize_exec_ctrls(vmcs_conf);
4652
Avi Kivity110312c2010-12-21 12:54:20 +02004653 cpu_has_load_ia32_efer =
4654 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4655 VM_ENTRY_LOAD_IA32_EFER)
4656 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4657 VM_EXIT_LOAD_IA32_EFER);
4658
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004659 cpu_has_load_perf_global_ctrl =
4660 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4661 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4662 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4663 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4664
4665 /*
4666 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004667 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004668 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4669 *
4670 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4671 *
4672 * AAK155 (model 26)
4673 * AAP115 (model 30)
4674 * AAT100 (model 37)
4675 * BC86,AAY89,BD102 (model 44)
4676 * BA97 (model 46)
4677 *
4678 */
4679 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4680 switch (boot_cpu_data.x86_model) {
4681 case 26:
4682 case 30:
4683 case 37:
4684 case 44:
4685 case 46:
4686 cpu_has_load_perf_global_ctrl = false;
4687 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4688 "does not work properly. Using workaround\n");
4689 break;
4690 default:
4691 break;
4692 }
4693 }
4694
Borislav Petkov782511b2016-04-04 22:25:03 +02004695 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004696 rdmsrl(MSR_IA32_XSS, host_xss);
4697
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004698 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004699}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700
Liran Alon491a6032018-06-23 02:35:12 +03004701static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702{
4703 int node = cpu_to_node(cpu);
4704 struct page *pages;
4705 struct vmcs *vmcs;
4706
Vlastimil Babka96db8002015-09-08 15:03:50 -07004707 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004708 if (!pages)
4709 return NULL;
4710 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004711 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004712
4713 /* KVM supports Enlightened VMCS v1 only */
4714 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004715 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004716 else
Liran Alon392b2f22018-06-23 02:35:01 +03004717 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004718
Liran Alon491a6032018-06-23 02:35:12 +03004719 if (shadow)
4720 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 return vmcs;
4722}
4723
Avi Kivity6aa8b732006-12-10 02:21:36 -08004724static void free_vmcs(struct vmcs *vmcs)
4725{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004726 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727}
4728
Nadav Har'Eld462b812011-05-24 15:26:10 +03004729/*
4730 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4731 */
4732static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4733{
4734 if (!loaded_vmcs->vmcs)
4735 return;
4736 loaded_vmcs_clear(loaded_vmcs);
4737 free_vmcs(loaded_vmcs->vmcs);
4738 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004739 if (loaded_vmcs->msr_bitmap)
4740 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004741 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004742}
4743
Liran Alon491a6032018-06-23 02:35:12 +03004744static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004745{
Liran Alon491a6032018-06-23 02:35:12 +03004746 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004747}
4748
4749static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4750{
Liran Alon491a6032018-06-23 02:35:12 +03004751 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004752 if (!loaded_vmcs->vmcs)
4753 return -ENOMEM;
4754
4755 loaded_vmcs->shadow_vmcs = NULL;
4756 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004757
4758 if (cpu_has_vmx_msr_bitmap()) {
4759 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4760 if (!loaded_vmcs->msr_bitmap)
4761 goto out_vmcs;
4762 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004763
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004764 if (IS_ENABLED(CONFIG_HYPERV) &&
4765 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004766 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4767 struct hv_enlightened_vmcs *evmcs =
4768 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4769
4770 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4771 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004772 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004773
4774 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4775
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004776 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004777
4778out_vmcs:
4779 free_loaded_vmcs(loaded_vmcs);
4780 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004781}
4782
Sam Ravnborg39959582007-06-01 00:47:13 -07004783static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784{
4785 int cpu;
4786
Zachary Amsden3230bb42009-09-29 11:38:37 -10004787 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004789 per_cpu(vmxarea, cpu) = NULL;
4790 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791}
4792
Jim Mattsond37f4262017-12-22 12:12:16 -08004793enum vmcs_field_width {
4794 VMCS_FIELD_WIDTH_U16 = 0,
4795 VMCS_FIELD_WIDTH_U64 = 1,
4796 VMCS_FIELD_WIDTH_U32 = 2,
4797 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004798};
4799
Jim Mattsond37f4262017-12-22 12:12:16 -08004800static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004801{
4802 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004803 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004804 return (field >> 13) & 0x3 ;
4805}
4806
4807static inline int vmcs_field_readonly(unsigned long field)
4808{
4809 return (((field >> 10) & 0x3) == 1);
4810}
4811
Bandan Dasfe2b2012014-04-21 15:20:14 -04004812static void init_vmcs_shadow_fields(void)
4813{
4814 int i, j;
4815
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004816 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4817 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004818 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004819 (i + 1 == max_shadow_read_only_fields ||
4820 shadow_read_only_fields[i + 1] != field + 1))
4821 pr_err("Missing field from shadow_read_only_field %x\n",
4822 field + 1);
4823
4824 clear_bit(field, vmx_vmread_bitmap);
4825#ifdef CONFIG_X86_64
4826 if (field & 1)
4827 continue;
4828#endif
4829 if (j < i)
4830 shadow_read_only_fields[j] = field;
4831 j++;
4832 }
4833 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004834
4835 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004836 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004837 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004838 (i + 1 == max_shadow_read_write_fields ||
4839 shadow_read_write_fields[i + 1] != field + 1))
4840 pr_err("Missing field from shadow_read_write_field %x\n",
4841 field + 1);
4842
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004843 /*
4844 * PML and the preemption timer can be emulated, but the
4845 * processor cannot vmwrite to fields that don't exist
4846 * on bare metal.
4847 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004848 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004849 case GUEST_PML_INDEX:
4850 if (!cpu_has_vmx_pml())
4851 continue;
4852 break;
4853 case VMX_PREEMPTION_TIMER_VALUE:
4854 if (!cpu_has_vmx_preemption_timer())
4855 continue;
4856 break;
4857 case GUEST_INTR_STATUS:
4858 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004859 continue;
4860 break;
4861 default:
4862 break;
4863 }
4864
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004865 clear_bit(field, vmx_vmwrite_bitmap);
4866 clear_bit(field, vmx_vmread_bitmap);
4867#ifdef CONFIG_X86_64
4868 if (field & 1)
4869 continue;
4870#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004871 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004872 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004873 j++;
4874 }
4875 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004876}
4877
Avi Kivity6aa8b732006-12-10 02:21:36 -08004878static __init int alloc_kvm_area(void)
4879{
4880 int cpu;
4881
Zachary Amsden3230bb42009-09-29 11:38:37 -10004882 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883 struct vmcs *vmcs;
4884
Liran Alon491a6032018-06-23 02:35:12 +03004885 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004886 if (!vmcs) {
4887 free_kvm_area();
4888 return -ENOMEM;
4889 }
4890
Liran Alon2307af12018-06-29 22:59:04 +03004891 /*
4892 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4893 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4894 * revision_id reported by MSR_IA32_VMX_BASIC.
4895 *
4896 * However, even though not explictly documented by
4897 * TLFS, VMXArea passed as VMXON argument should
4898 * still be marked with revision_id reported by
4899 * physical CPU.
4900 */
4901 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004902 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004903
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 per_cpu(vmxarea, cpu) = vmcs;
4905 }
4906 return 0;
4907}
4908
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004909static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004910 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004912 if (!emulate_invalid_guest_state) {
4913 /*
4914 * CS and SS RPL should be equal during guest entry according
4915 * to VMX spec, but in reality it is not always so. Since vcpu
4916 * is in the middle of the transition from real mode to
4917 * protected mode it is safe to assume that RPL 0 is a good
4918 * default value.
4919 */
4920 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004921 save->selector &= ~SEGMENT_RPL_MASK;
4922 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004923 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004925 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926}
4927
4928static void enter_pmode(struct kvm_vcpu *vcpu)
4929{
4930 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004931 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004932
Gleb Natapovd99e4152012-12-20 16:57:45 +02004933 /*
4934 * Update real mode segment cache. It may be not up-to-date if sement
4935 * register was written while vcpu was in a guest mode.
4936 */
4937 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4938 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4939 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4940 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4943
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004944 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004945
Avi Kivity2fb92db2011-04-27 19:42:18 +03004946 vmx_segment_cache_clear(vmx);
4947
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004948 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949
4950 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004951 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4952 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953 vmcs_writel(GUEST_RFLAGS, flags);
4954
Rusty Russell66aee912007-07-17 23:34:16 +10004955 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4956 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957
4958 update_exception_bitmap(vcpu);
4959
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004960 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4961 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4962 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4963 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4964 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4965 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966}
4967
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004968static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004969{
Mathias Krause772e0312012-08-30 01:30:19 +02004970 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004971 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972
Gleb Natapovd99e4152012-12-20 16:57:45 +02004973 var.dpl = 0x3;
4974 if (seg == VCPU_SREG_CS)
4975 var.type = 0x3;
4976
4977 if (!emulate_invalid_guest_state) {
4978 var.selector = var.base >> 4;
4979 var.base = var.base & 0xffff0;
4980 var.limit = 0xffff;
4981 var.g = 0;
4982 var.db = 0;
4983 var.present = 1;
4984 var.s = 1;
4985 var.l = 0;
4986 var.unusable = 0;
4987 var.type = 0x3;
4988 var.avl = 0;
4989 if (save->base & 0xf)
4990 printk_once(KERN_WARNING "kvm: segment base is not "
4991 "paragraph aligned when entering "
4992 "protected mode (seg=%d)", seg);
4993 }
4994
4995 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004996 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004997 vmcs_write32(sf->limit, var.limit);
4998 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004999}
5000
5001static void enter_rmode(struct kvm_vcpu *vcpu)
5002{
5003 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005004 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005005 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005007 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5008 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5009 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5010 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5011 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005014
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005015 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016
Gleb Natapov776e58e2011-03-13 12:34:27 +02005017 /*
5018 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005019 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005020 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005021 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005022 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5023 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005024
Avi Kivity2fb92db2011-04-27 19:42:18 +03005025 vmx_segment_cache_clear(vmx);
5026
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005027 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5030
5031 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005032 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005034 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035
5036 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005037 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038 update_exception_bitmap(vcpu);
5039
Gleb Natapovd99e4152012-12-20 16:57:45 +02005040 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5041 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5042 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5043 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5044 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5045 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005046
Eddie Dong8668a3c2007-10-10 14:26:45 +08005047 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048}
5049
Amit Shah401d10d2009-02-20 22:53:37 +05305050static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5051{
5052 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005053 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5054
5055 if (!msr)
5056 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305057
Avi Kivity44ea2b12009-09-06 15:55:37 +03005058 /*
Sean Christopherson678e3152018-07-23 12:32:43 -07005059 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
5060 * 64-bit mode as a 64-bit kernel may frequently access the
5061 * MSR. This means we need to manually save/restore the MSR
5062 * when switching between guest and host state, but only if
5063 * the guest is in 64-bit mode. Sync our cached value if the
5064 * guest is transitioning to 32-bit mode and the CPU contains
5065 * guest state, i.e. the cache is stale.
Avi Kivity44ea2b12009-09-06 15:55:37 +03005066 */
Sean Christopherson678e3152018-07-23 12:32:43 -07005067#ifdef CONFIG_X86_64
5068 if (!(efer & EFER_LMA))
5069 (void)vmx_read_guest_kernel_gs_base(vmx);
5070#endif
Avi Kivityf6801df2010-01-21 15:31:50 +02005071 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305072 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005073 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305074 msr->data = efer;
5075 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005076 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305077
5078 msr->data = efer & ~EFER_LME;
5079 }
5080 setup_msrs(vmx);
5081}
5082
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005083#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084
5085static void enter_lmode(struct kvm_vcpu *vcpu)
5086{
5087 u32 guest_tr_ar;
5088
Avi Kivity2fb92db2011-04-27 19:42:18 +03005089 vmx_segment_cache_clear(to_vmx(vcpu));
5090
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005092 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005093 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5094 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005096 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5097 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098 }
Avi Kivityda38f432010-07-06 11:30:49 +03005099 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100}
5101
5102static void exit_lmode(struct kvm_vcpu *vcpu)
5103{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005104 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005105 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106}
5107
5108#endif
5109
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005110static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5111 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005112{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005113 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005114 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5115 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005116 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005117 } else {
5118 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005119 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005120}
5121
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005122static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005123{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005124 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005125}
5126
Junaid Shahidfaff8752018-06-29 13:10:05 -07005127static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5128{
5129 int vpid = to_vmx(vcpu)->vpid;
5130
5131 if (!vpid_sync_vcpu_addr(vpid, addr))
5132 vpid_sync_context(vpid);
5133
5134 /*
5135 * If VPIDs are not supported or enabled, then the above is a no-op.
5136 * But we don't really need a TLB flush in that case anyway, because
5137 * each VM entry/exit includes an implicit flush when VPID is 0.
5138 */
5139}
5140
Avi Kivitye8467fd2009-12-29 18:43:06 +02005141static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5142{
5143 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5144
5145 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5146 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5147}
5148
Avi Kivityaff48ba2010-12-05 18:56:11 +02005149static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5150{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005151 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005152 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5153 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5154}
5155
Anthony Liguori25c4c272007-04-27 09:29:21 +03005156static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005157{
Avi Kivityfc78f512009-12-07 12:16:48 +02005158 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5159
5160 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5161 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005162}
5163
Sheng Yang14394422008-04-28 12:24:45 +08005164static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5165{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005166 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5167
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005168 if (!test_bit(VCPU_EXREG_PDPTR,
5169 (unsigned long *)&vcpu->arch.regs_dirty))
5170 return;
5171
Sheng Yang14394422008-04-28 12:24:45 +08005172 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005173 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5174 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5175 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5176 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005177 }
5178}
5179
Avi Kivity8f5d5492009-05-31 18:41:29 +03005180static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5181{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005182 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5183
Avi Kivity8f5d5492009-05-31 18:41:29 +03005184 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005185 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5186 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5187 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5188 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005189 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005190
5191 __set_bit(VCPU_EXREG_PDPTR,
5192 (unsigned long *)&vcpu->arch.regs_avail);
5193 __set_bit(VCPU_EXREG_PDPTR,
5194 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005195}
5196
David Matlack38991522016-11-29 18:14:08 -08005197static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5198{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005199 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5200 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005201 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5202
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005203 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005204 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5205 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5206 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5207
5208 return fixed_bits_valid(val, fixed0, fixed1);
5209}
5210
5211static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5212{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005213 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5214 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005215
5216 return fixed_bits_valid(val, fixed0, fixed1);
5217}
5218
5219static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5220{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005221 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5222 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005223
5224 return fixed_bits_valid(val, fixed0, fixed1);
5225}
5226
5227/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5228#define nested_guest_cr4_valid nested_cr4_valid
5229#define nested_host_cr4_valid nested_cr4_valid
5230
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005231static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005232
5233static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5234 unsigned long cr0,
5235 struct kvm_vcpu *vcpu)
5236{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005237 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5238 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005239 if (!(cr0 & X86_CR0_PG)) {
5240 /* From paging/starting to nonpaging */
5241 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005242 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005243 (CPU_BASED_CR3_LOAD_EXITING |
5244 CPU_BASED_CR3_STORE_EXITING));
5245 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005246 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005247 } else if (!is_paging(vcpu)) {
5248 /* From nonpaging to paging */
5249 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005250 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005251 ~(CPU_BASED_CR3_LOAD_EXITING |
5252 CPU_BASED_CR3_STORE_EXITING));
5253 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005254 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005255 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005256
5257 if (!(cr0 & X86_CR0_WP))
5258 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005259}
5260
Avi Kivity6aa8b732006-12-10 02:21:36 -08005261static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5262{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005263 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005264 unsigned long hw_cr0;
5265
Gleb Natapov50378782013-02-04 16:00:28 +02005266 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005267 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005268 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005269 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005270 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005271
Gleb Natapov218e7632013-01-21 15:36:45 +02005272 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5273 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274
Gleb Natapov218e7632013-01-21 15:36:45 +02005275 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5276 enter_rmode(vcpu);
5277 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005279#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005280 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005281 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005282 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005283 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005284 exit_lmode(vcpu);
5285 }
5286#endif
5287
Sean Christophersonb4d18512018-03-05 12:04:40 -08005288 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005289 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5290
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005292 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005293 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005294
5295 /* depends on vcpu->arch.cr0 to be set to a new value */
5296 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297}
5298
Yu Zhang855feb62017-08-24 20:27:55 +08005299static int get_ept_level(struct kvm_vcpu *vcpu)
5300{
5301 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5302 return 5;
5303 return 4;
5304}
5305
Peter Feiner995f00a2017-06-30 17:26:32 -07005306static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005307{
Yu Zhang855feb62017-08-24 20:27:55 +08005308 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005309
Yu Zhang855feb62017-08-24 20:27:55 +08005310 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005311
Peter Feiner995f00a2017-06-30 17:26:32 -07005312 if (enable_ept_ad_bits &&
5313 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005314 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005315 eptp |= (root_hpa & PAGE_MASK);
5316
5317 return eptp;
5318}
5319
Avi Kivity6aa8b732006-12-10 02:21:36 -08005320static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5321{
Tianyu Lan877ad952018-07-19 08:40:23 +00005322 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005323 unsigned long guest_cr3;
5324 u64 eptp;
5325
5326 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005327 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005328 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005329 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005330
5331 if (kvm_x86_ops->tlb_remote_flush) {
5332 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5333 to_vmx(vcpu)->ept_pointer = eptp;
5334 to_kvm_vmx(kvm)->ept_pointers_match
5335 = EPT_POINTERS_CHECK;
5336 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5337 }
5338
Sean Christophersone90008d2018-03-05 12:04:37 -08005339 if (enable_unrestricted_guest || is_paging(vcpu) ||
5340 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005341 guest_cr3 = kvm_read_cr3(vcpu);
5342 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005343 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005344 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005345 }
5346
Sheng Yang14394422008-04-28 12:24:45 +08005347 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348}
5349
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005350static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005351{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005352 /*
5353 * Pass through host's Machine Check Enable value to hw_cr4, which
5354 * is in force while we are in guest mode. Do not let guests control
5355 * this bit, even if host CR4.MCE == 0.
5356 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005357 unsigned long hw_cr4;
5358
5359 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5360 if (enable_unrestricted_guest)
5361 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5362 else if (to_vmx(vcpu)->rmode.vm86_active)
5363 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5364 else
5365 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005366
Sean Christopherson64f7a112018-04-30 10:01:06 -07005367 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5368 if (cr4 & X86_CR4_UMIP) {
5369 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005370 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005371 hw_cr4 &= ~X86_CR4_UMIP;
5372 } else if (!is_guest_mode(vcpu) ||
5373 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5374 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5375 SECONDARY_EXEC_DESC);
5376 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005377
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005378 if (cr4 & X86_CR4_VMXE) {
5379 /*
5380 * To use VMXON (and later other VMX instructions), a guest
5381 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5382 * So basically the check on whether to allow nested VMX
5383 * is here.
5384 */
5385 if (!nested_vmx_allowed(vcpu))
5386 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005387 }
David Matlack38991522016-11-29 18:14:08 -08005388
5389 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005390 return 1;
5391
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005392 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005393
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005394 if (!enable_unrestricted_guest) {
5395 if (enable_ept) {
5396 if (!is_paging(vcpu)) {
5397 hw_cr4 &= ~X86_CR4_PAE;
5398 hw_cr4 |= X86_CR4_PSE;
5399 } else if (!(cr4 & X86_CR4_PAE)) {
5400 hw_cr4 &= ~X86_CR4_PAE;
5401 }
5402 }
5403
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005404 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005405 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5406 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5407 * to be manually disabled when guest switches to non-paging
5408 * mode.
5409 *
5410 * If !enable_unrestricted_guest, the CPU is always running
5411 * with CR0.PG=1 and CR4 needs to be modified.
5412 * If enable_unrestricted_guest, the CPU automatically
5413 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005414 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005415 if (!is_paging(vcpu))
5416 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5417 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005418
Sheng Yang14394422008-04-28 12:24:45 +08005419 vmcs_writel(CR4_READ_SHADOW, cr4);
5420 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005421 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005422}
5423
Avi Kivity6aa8b732006-12-10 02:21:36 -08005424static void vmx_get_segment(struct kvm_vcpu *vcpu,
5425 struct kvm_segment *var, int seg)
5426{
Avi Kivitya9179492011-01-03 14:28:52 +02005427 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005428 u32 ar;
5429
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005430 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005431 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005432 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005433 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005434 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005435 var->base = vmx_read_guest_seg_base(vmx, seg);
5436 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5437 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005438 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005439 var->base = vmx_read_guest_seg_base(vmx, seg);
5440 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5441 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5442 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005443 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005444 var->type = ar & 15;
5445 var->s = (ar >> 4) & 1;
5446 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005447 /*
5448 * Some userspaces do not preserve unusable property. Since usable
5449 * segment has to be present according to VMX spec we can use present
5450 * property to amend userspace bug by making unusable segment always
5451 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5452 * segment as unusable.
5453 */
5454 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005455 var->avl = (ar >> 12) & 1;
5456 var->l = (ar >> 13) & 1;
5457 var->db = (ar >> 14) & 1;
5458 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005459}
5460
Avi Kivitya9179492011-01-03 14:28:52 +02005461static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5462{
Avi Kivitya9179492011-01-03 14:28:52 +02005463 struct kvm_segment s;
5464
5465 if (to_vmx(vcpu)->rmode.vm86_active) {
5466 vmx_get_segment(vcpu, &s, seg);
5467 return s.base;
5468 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005469 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005470}
5471
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005472static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005473{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005474 struct vcpu_vmx *vmx = to_vmx(vcpu);
5475
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005476 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005477 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005478 else {
5479 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005480 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005481 }
Avi Kivity69c73022011-03-07 15:26:44 +02005482}
5483
Avi Kivity653e3102007-05-07 10:55:37 +03005484static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005485{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486 u32 ar;
5487
Avi Kivityf0495f92012-06-07 17:06:10 +03005488 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489 ar = 1 << 16;
5490 else {
5491 ar = var->type & 15;
5492 ar |= (var->s & 1) << 4;
5493 ar |= (var->dpl & 3) << 5;
5494 ar |= (var->present & 1) << 7;
5495 ar |= (var->avl & 1) << 12;
5496 ar |= (var->l & 1) << 13;
5497 ar |= (var->db & 1) << 14;
5498 ar |= (var->g & 1) << 15;
5499 }
Avi Kivity653e3102007-05-07 10:55:37 +03005500
5501 return ar;
5502}
5503
5504static void vmx_set_segment(struct kvm_vcpu *vcpu,
5505 struct kvm_segment *var, int seg)
5506{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005507 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005508 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005509
Avi Kivity2fb92db2011-04-27 19:42:18 +03005510 vmx_segment_cache_clear(vmx);
5511
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005512 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5513 vmx->rmode.segs[seg] = *var;
5514 if (seg == VCPU_SREG_TR)
5515 vmcs_write16(sf->selector, var->selector);
5516 else if (var->s)
5517 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005518 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005519 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005520
Avi Kivity653e3102007-05-07 10:55:37 +03005521 vmcs_writel(sf->base, var->base);
5522 vmcs_write32(sf->limit, var->limit);
5523 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005524
5525 /*
5526 * Fix the "Accessed" bit in AR field of segment registers for older
5527 * qemu binaries.
5528 * IA32 arch specifies that at the time of processor reset the
5529 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005530 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005531 * state vmexit when "unrestricted guest" mode is turned on.
5532 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5533 * tree. Newer qemu binaries with that qemu fix would not need this
5534 * kvm hack.
5535 */
5536 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005537 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005538
Gleb Natapovf924d662012-12-12 19:10:55 +02005539 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005540
5541out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005542 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543}
5544
Avi Kivity6aa8b732006-12-10 02:21:36 -08005545static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5546{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005547 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005548
5549 *db = (ar >> 14) & 1;
5550 *l = (ar >> 13) & 1;
5551}
5552
Gleb Natapov89a27f42010-02-16 10:51:48 +02005553static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005554{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005555 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5556 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005557}
5558
Gleb Natapov89a27f42010-02-16 10:51:48 +02005559static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005560{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005561 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5562 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005563}
5564
Gleb Natapov89a27f42010-02-16 10:51:48 +02005565static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005566{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005567 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5568 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005569}
5570
Gleb Natapov89a27f42010-02-16 10:51:48 +02005571static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005572{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005573 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5574 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005575}
5576
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005577static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5578{
5579 struct kvm_segment var;
5580 u32 ar;
5581
5582 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005583 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005584 if (seg == VCPU_SREG_CS)
5585 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005586 ar = vmx_segment_access_rights(&var);
5587
5588 if (var.base != (var.selector << 4))
5589 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005590 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005591 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005592 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005593 return false;
5594
5595 return true;
5596}
5597
5598static bool code_segment_valid(struct kvm_vcpu *vcpu)
5599{
5600 struct kvm_segment cs;
5601 unsigned int cs_rpl;
5602
5603 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005604 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005605
Avi Kivity1872a3f2009-01-04 23:26:52 +02005606 if (cs.unusable)
5607 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005608 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005609 return false;
5610 if (!cs.s)
5611 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005612 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005613 if (cs.dpl > cs_rpl)
5614 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005615 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005616 if (cs.dpl != cs_rpl)
5617 return false;
5618 }
5619 if (!cs.present)
5620 return false;
5621
5622 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5623 return true;
5624}
5625
5626static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5627{
5628 struct kvm_segment ss;
5629 unsigned int ss_rpl;
5630
5631 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005632 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005633
Avi Kivity1872a3f2009-01-04 23:26:52 +02005634 if (ss.unusable)
5635 return true;
5636 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005637 return false;
5638 if (!ss.s)
5639 return false;
5640 if (ss.dpl != ss_rpl) /* DPL != RPL */
5641 return false;
5642 if (!ss.present)
5643 return false;
5644
5645 return true;
5646}
5647
5648static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5649{
5650 struct kvm_segment var;
5651 unsigned int rpl;
5652
5653 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005654 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005655
Avi Kivity1872a3f2009-01-04 23:26:52 +02005656 if (var.unusable)
5657 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005658 if (!var.s)
5659 return false;
5660 if (!var.present)
5661 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005662 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005663 if (var.dpl < rpl) /* DPL < RPL */
5664 return false;
5665 }
5666
5667 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5668 * rights flags
5669 */
5670 return true;
5671}
5672
5673static bool tr_valid(struct kvm_vcpu *vcpu)
5674{
5675 struct kvm_segment tr;
5676
5677 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5678
Avi Kivity1872a3f2009-01-04 23:26:52 +02005679 if (tr.unusable)
5680 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005681 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005682 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005683 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005684 return false;
5685 if (!tr.present)
5686 return false;
5687
5688 return true;
5689}
5690
5691static bool ldtr_valid(struct kvm_vcpu *vcpu)
5692{
5693 struct kvm_segment ldtr;
5694
5695 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5696
Avi Kivity1872a3f2009-01-04 23:26:52 +02005697 if (ldtr.unusable)
5698 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005699 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005700 return false;
5701 if (ldtr.type != 2)
5702 return false;
5703 if (!ldtr.present)
5704 return false;
5705
5706 return true;
5707}
5708
5709static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5710{
5711 struct kvm_segment cs, ss;
5712
5713 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5714 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5715
Nadav Amitb32a9912015-03-29 16:33:04 +03005716 return ((cs.selector & SEGMENT_RPL_MASK) ==
5717 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005718}
5719
5720/*
5721 * Check if guest state is valid. Returns true if valid, false if
5722 * not.
5723 * We assume that registers are always usable
5724 */
5725static bool guest_state_valid(struct kvm_vcpu *vcpu)
5726{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005727 if (enable_unrestricted_guest)
5728 return true;
5729
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005730 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005731 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005732 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5733 return false;
5734 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5735 return false;
5736 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5737 return false;
5738 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5739 return false;
5740 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5741 return false;
5742 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5743 return false;
5744 } else {
5745 /* protected mode guest state checks */
5746 if (!cs_ss_rpl_check(vcpu))
5747 return false;
5748 if (!code_segment_valid(vcpu))
5749 return false;
5750 if (!stack_segment_valid(vcpu))
5751 return false;
5752 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5753 return false;
5754 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5755 return false;
5756 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5757 return false;
5758 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5759 return false;
5760 if (!tr_valid(vcpu))
5761 return false;
5762 if (!ldtr_valid(vcpu))
5763 return false;
5764 }
5765 /* TODO:
5766 * - Add checks on RIP
5767 * - Add checks on RFLAGS
5768 */
5769
5770 return true;
5771}
5772
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005773static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5774{
5775 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5776}
5777
Mike Dayd77c26f2007-10-08 09:02:08 -04005778static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005779{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005780 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005781 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005782 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005783
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005784 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005785 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005786 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5787 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005788 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005789 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005790 r = kvm_write_guest_page(kvm, fn++, &data,
5791 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005792 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005793 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005794 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5795 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005796 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005797 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5798 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005799 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005800 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005801 r = kvm_write_guest_page(kvm, fn, &data,
5802 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5803 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005804out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005805 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005806 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005807}
5808
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005809static int init_rmode_identity_map(struct kvm *kvm)
5810{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005811 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005812 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005813 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005814 u32 tmp;
5815
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005816 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005817 mutex_lock(&kvm->slots_lock);
5818
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005819 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005820 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005821
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005822 if (!kvm_vmx->ept_identity_map_addr)
5823 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5824 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005825
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005826 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005827 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005828 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005829 goto out2;
5830
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005831 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005832 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5833 if (r < 0)
5834 goto out;
5835 /* Set up identity-mapping pagetable for EPT in real mode */
5836 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5837 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5838 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5839 r = kvm_write_guest_page(kvm, identity_map_pfn,
5840 &tmp, i * sizeof(tmp), sizeof(tmp));
5841 if (r < 0)
5842 goto out;
5843 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005844 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005845
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005846out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005847 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005848
5849out2:
5850 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005851 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005852}
5853
Avi Kivity6aa8b732006-12-10 02:21:36 -08005854static void seg_setup(int seg)
5855{
Mathias Krause772e0312012-08-30 01:30:19 +02005856 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005857 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005858
5859 vmcs_write16(sf->selector, 0);
5860 vmcs_writel(sf->base, 0);
5861 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005862 ar = 0x93;
5863 if (seg == VCPU_SREG_CS)
5864 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005865
5866 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867}
5868
Sheng Yangf78e0e22007-10-29 09:40:42 +08005869static int alloc_apic_access_page(struct kvm *kvm)
5870{
Xiao Guangrong44841412012-09-07 14:14:20 +08005871 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005872 int r = 0;
5873
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005874 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005875 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005876 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005877 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5878 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005879 if (r)
5880 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005881
Tang Chen73a6d942014-09-11 13:38:00 +08005882 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005883 if (is_error_page(page)) {
5884 r = -EFAULT;
5885 goto out;
5886 }
5887
Tang Chenc24ae0d2014-09-24 15:57:58 +08005888 /*
5889 * Do not pin the page in memory, so that memory hot-unplug
5890 * is able to migrate it.
5891 */
5892 put_page(page);
5893 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005894out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005895 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005896 return r;
5897}
5898
Wanpeng Li991e7a02015-09-16 17:30:05 +08005899static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005900{
5901 int vpid;
5902
Avi Kivity919818a2009-03-23 18:01:29 +02005903 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005904 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005905 spin_lock(&vmx_vpid_lock);
5906 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005907 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005908 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005909 else
5910 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005911 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005912 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005913}
5914
Wanpeng Li991e7a02015-09-16 17:30:05 +08005915static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005916{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005917 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005918 return;
5919 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005920 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005921 spin_unlock(&vmx_vpid_lock);
5922}
5923
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005924static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5925 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005926{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005927 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005928
5929 if (!cpu_has_vmx_msr_bitmap())
5930 return;
5931
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005932 if (static_branch_unlikely(&enable_evmcs))
5933 evmcs_touch_msr_bitmap();
5934
Sheng Yang25c5f222008-03-28 13:18:56 +08005935 /*
5936 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5937 * have the write-low and read-high bitmap offsets the wrong way round.
5938 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5939 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005940 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005941 if (type & MSR_TYPE_R)
5942 /* read-low */
5943 __clear_bit(msr, msr_bitmap + 0x000 / f);
5944
5945 if (type & MSR_TYPE_W)
5946 /* write-low */
5947 __clear_bit(msr, msr_bitmap + 0x800 / f);
5948
Sheng Yang25c5f222008-03-28 13:18:56 +08005949 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5950 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005951 if (type & MSR_TYPE_R)
5952 /* read-high */
5953 __clear_bit(msr, msr_bitmap + 0x400 / f);
5954
5955 if (type & MSR_TYPE_W)
5956 /* write-high */
5957 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5958
5959 }
5960}
5961
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005962static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5963 u32 msr, int type)
5964{
5965 int f = sizeof(unsigned long);
5966
5967 if (!cpu_has_vmx_msr_bitmap())
5968 return;
5969
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005970 if (static_branch_unlikely(&enable_evmcs))
5971 evmcs_touch_msr_bitmap();
5972
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005973 /*
5974 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5975 * have the write-low and read-high bitmap offsets the wrong way round.
5976 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5977 */
5978 if (msr <= 0x1fff) {
5979 if (type & MSR_TYPE_R)
5980 /* read-low */
5981 __set_bit(msr, msr_bitmap + 0x000 / f);
5982
5983 if (type & MSR_TYPE_W)
5984 /* write-low */
5985 __set_bit(msr, msr_bitmap + 0x800 / f);
5986
5987 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5988 msr &= 0x1fff;
5989 if (type & MSR_TYPE_R)
5990 /* read-high */
5991 __set_bit(msr, msr_bitmap + 0x400 / f);
5992
5993 if (type & MSR_TYPE_W)
5994 /* write-high */
5995 __set_bit(msr, msr_bitmap + 0xc00 / f);
5996
5997 }
5998}
5999
6000static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6001 u32 msr, int type, bool value)
6002{
6003 if (value)
6004 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6005 else
6006 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6007}
6008
Wincy Vanf2b93282015-02-03 23:56:03 +08006009/*
6010 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6011 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6012 */
6013static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6014 unsigned long *msr_bitmap_nested,
6015 u32 msr, int type)
6016{
6017 int f = sizeof(unsigned long);
6018
Wincy Vanf2b93282015-02-03 23:56:03 +08006019 /*
6020 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6021 * have the write-low and read-high bitmap offsets the wrong way round.
6022 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6023 */
6024 if (msr <= 0x1fff) {
6025 if (type & MSR_TYPE_R &&
6026 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6027 /* read-low */
6028 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6029
6030 if (type & MSR_TYPE_W &&
6031 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6032 /* write-low */
6033 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6034
6035 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6036 msr &= 0x1fff;
6037 if (type & MSR_TYPE_R &&
6038 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6039 /* read-high */
6040 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6041
6042 if (type & MSR_TYPE_W &&
6043 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6044 /* write-high */
6045 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6046
6047 }
6048}
6049
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006050static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006051{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006052 u8 mode = 0;
6053
6054 if (cpu_has_secondary_exec_ctrls() &&
6055 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6056 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6057 mode |= MSR_BITMAP_MODE_X2APIC;
6058 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6059 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6060 }
6061
6062 if (is_long_mode(vcpu))
6063 mode |= MSR_BITMAP_MODE_LM;
6064
6065 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006066}
6067
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006068#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6069
6070static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6071 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006072{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006073 int msr;
6074
6075 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6076 unsigned word = msr / BITS_PER_LONG;
6077 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6078 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006079 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006080
6081 if (mode & MSR_BITMAP_MODE_X2APIC) {
6082 /*
6083 * TPR reads and writes can be virtualized even if virtual interrupt
6084 * delivery is not in use.
6085 */
6086 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6087 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6088 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6089 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6090 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6091 }
6092 }
6093}
6094
6095static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6096{
6097 struct vcpu_vmx *vmx = to_vmx(vcpu);
6098 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6099 u8 mode = vmx_msr_bitmap_mode(vcpu);
6100 u8 changed = mode ^ vmx->msr_bitmap_mode;
6101
6102 if (!changed)
6103 return;
6104
6105 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
6106 !(mode & MSR_BITMAP_MODE_LM));
6107
6108 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6109 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6110
6111 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006112}
6113
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006114static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006115{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006116 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006117}
6118
David Matlackc9f04402017-08-01 14:00:40 -07006119static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6120{
6121 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6122 gfn_t gfn;
6123
6124 /*
6125 * Don't need to mark the APIC access page dirty; it is never
6126 * written to by the CPU during APIC virtualization.
6127 */
6128
6129 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6130 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6131 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6132 }
6133
6134 if (nested_cpu_has_posted_intr(vmcs12)) {
6135 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6136 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6137 }
6138}
6139
6140
David Hildenbrand6342c502017-01-25 11:58:58 +01006141static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006142{
6143 struct vcpu_vmx *vmx = to_vmx(vcpu);
6144 int max_irr;
6145 void *vapic_page;
6146 u16 status;
6147
David Matlackc9f04402017-08-01 14:00:40 -07006148 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6149 return;
Wincy Van705699a2015-02-03 23:58:17 +08006150
David Matlackc9f04402017-08-01 14:00:40 -07006151 vmx->nested.pi_pending = false;
6152 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6153 return;
Wincy Van705699a2015-02-03 23:58:17 +08006154
David Matlackc9f04402017-08-01 14:00:40 -07006155 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6156 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006157 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006158 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6159 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006160 kunmap(vmx->nested.virtual_apic_page);
6161
6162 status = vmcs_read16(GUEST_INTR_STATUS);
6163 if ((u8)max_irr > ((u8)status & 0xff)) {
6164 status &= ~0xff;
6165 status |= (u8)max_irr;
6166 vmcs_write16(GUEST_INTR_STATUS, status);
6167 }
6168 }
David Matlackc9f04402017-08-01 14:00:40 -07006169
6170 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006171}
6172
Wincy Van06a55242017-04-28 13:13:59 +08006173static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6174 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006175{
6176#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006177 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6178
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006179 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006180 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006181 * The vector of interrupt to be delivered to vcpu had
6182 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006183 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006184 * Following cases will be reached in this block, and
6185 * we always send a notification event in all cases as
6186 * explained below.
6187 *
6188 * Case 1: vcpu keeps in non-root mode. Sending a
6189 * notification event posts the interrupt to vcpu.
6190 *
6191 * Case 2: vcpu exits to root mode and is still
6192 * runnable. PIR will be synced to vIRR before the
6193 * next vcpu entry. Sending a notification event in
6194 * this case has no effect, as vcpu is not in root
6195 * mode.
6196 *
6197 * Case 3: vcpu exits to root mode and is blocked.
6198 * vcpu_block() has already synced PIR to vIRR and
6199 * never blocks vcpu if vIRR is not cleared. Therefore,
6200 * a blocked vcpu here does not wait for any requested
6201 * interrupts in PIR, and sending a notification event
6202 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006203 */
Feng Wu28b835d2015-09-18 22:29:54 +08006204
Wincy Van06a55242017-04-28 13:13:59 +08006205 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006206 return true;
6207 }
6208#endif
6209 return false;
6210}
6211
Wincy Van705699a2015-02-03 23:58:17 +08006212static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6213 int vector)
6214{
6215 struct vcpu_vmx *vmx = to_vmx(vcpu);
6216
6217 if (is_guest_mode(vcpu) &&
6218 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006219 /*
6220 * If a posted intr is not recognized by hardware,
6221 * we will accomplish it in the next vmentry.
6222 */
6223 vmx->nested.pi_pending = true;
6224 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006225 /* the PIR and ON have been set by L1. */
6226 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6227 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006228 return 0;
6229 }
6230 return -1;
6231}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006232/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006233 * Send interrupt to vcpu via posted interrupt way.
6234 * 1. If target vcpu is running(non-root mode), send posted interrupt
6235 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6236 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6237 * interrupt from PIR in next vmentry.
6238 */
6239static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6240{
6241 struct vcpu_vmx *vmx = to_vmx(vcpu);
6242 int r;
6243
Wincy Van705699a2015-02-03 23:58:17 +08006244 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6245 if (!r)
6246 return;
6247
Yang Zhanga20ed542013-04-11 19:25:15 +08006248 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6249 return;
6250
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006251 /* If a previous notification has sent the IPI, nothing to do. */
6252 if (pi_test_and_set_on(&vmx->pi_desc))
6253 return;
6254
Wincy Van06a55242017-04-28 13:13:59 +08006255 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006256 kvm_vcpu_kick(vcpu);
6257}
6258
Avi Kivity6aa8b732006-12-10 02:21:36 -08006259/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006260 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6261 * will not change in the lifetime of the guest.
6262 * Note that host-state that does change is set elsewhere. E.g., host-state
6263 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6264 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006265static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006266{
6267 u32 low32, high32;
6268 unsigned long tmpl;
6269 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006270 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006271
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006272 cr0 = read_cr0();
6273 WARN_ON(cr0 & X86_CR0_TS);
6274 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006275
6276 /*
6277 * Save the most likely value for this task's CR3 in the VMCS.
6278 * We can't use __get_current_cr3_fast() because we're not atomic.
6279 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006280 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006281 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006282 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006283
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006284 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006285 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006286 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006287 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006288
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006289 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006290#ifdef CONFIG_X86_64
6291 /*
6292 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006293 * vmx_prepare_switch_to_host(), in case userspace uses
6294 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006295 */
6296 vmcs_write16(HOST_DS_SELECTOR, 0);
6297 vmcs_write16(HOST_ES_SELECTOR, 0);
6298#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006299 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6300 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006301#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006302 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6303 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6304
Juergen Gross87930012017-09-04 12:25:27 +02006305 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006306 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006307 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006308
Avi Kivity83287ea422012-09-16 15:10:57 +03006309 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006310
6311 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6312 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6313 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6314 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6315
6316 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6317 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6318 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6319 }
6320}
6321
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006322static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6323{
6324 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6325 if (enable_ept)
6326 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006327 if (is_guest_mode(&vmx->vcpu))
6328 vmx->vcpu.arch.cr4_guest_owned_bits &=
6329 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006330 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6331}
6332
Yang Zhang01e439b2013-04-11 19:25:12 +08006333static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6334{
6335 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6336
Andrey Smetanind62caab2015-11-10 15:36:33 +03006337 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006338 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006339
6340 if (!enable_vnmi)
6341 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6342
Yunhong Jiang64672c92016-06-13 14:19:59 -07006343 /* Enable the preemption timer dynamically */
6344 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006345 return pin_based_exec_ctrl;
6346}
6347
Andrey Smetanind62caab2015-11-10 15:36:33 +03006348static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6349{
6350 struct vcpu_vmx *vmx = to_vmx(vcpu);
6351
6352 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006353 if (cpu_has_secondary_exec_ctrls()) {
6354 if (kvm_vcpu_apicv_active(vcpu))
6355 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6356 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6357 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6358 else
6359 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6360 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6361 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6362 }
6363
6364 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006365 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006366}
6367
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006368static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6369{
6370 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006371
6372 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6373 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6374
Paolo Bonzini35754c92015-07-29 12:05:37 +02006375 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006376 exec_control &= ~CPU_BASED_TPR_SHADOW;
6377#ifdef CONFIG_X86_64
6378 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6379 CPU_BASED_CR8_LOAD_EXITING;
6380#endif
6381 }
6382 if (!enable_ept)
6383 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6384 CPU_BASED_CR3_LOAD_EXITING |
6385 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006386 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6387 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6388 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006389 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6390 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006391 return exec_control;
6392}
6393
Jim Mattson45ec3682017-08-23 16:32:04 -07006394static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006395{
Jim Mattson45ec3682017-08-23 16:32:04 -07006396 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006397 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006398}
6399
Jim Mattson75f4fc82017-08-23 16:32:03 -07006400static bool vmx_rdseed_supported(void)
6401{
6402 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006403 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006404}
6405
Paolo Bonzini80154d72017-08-24 13:55:35 +02006406static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006407{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006408 struct kvm_vcpu *vcpu = &vmx->vcpu;
6409
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006410 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006411
Paolo Bonzini80154d72017-08-24 13:55:35 +02006412 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006413 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6414 if (vmx->vpid == 0)
6415 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6416 if (!enable_ept) {
6417 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6418 enable_unrestricted_guest = 0;
6419 }
6420 if (!enable_unrestricted_guest)
6421 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006422 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006424 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006425 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6426 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006427 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006428
6429 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6430 * in vmx_set_cr4. */
6431 exec_control &= ~SECONDARY_EXEC_DESC;
6432
Abel Gordonabc4fc52013-04-18 14:35:25 +03006433 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6434 (handle_vmptrld).
6435 We can NOT enable shadow_vmcs here because we don't have yet
6436 a current VMCS12
6437 */
6438 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006439
6440 if (!enable_pml)
6441 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006442
Paolo Bonzini3db13482017-08-24 14:48:03 +02006443 if (vmx_xsaves_supported()) {
6444 /* Exposing XSAVES only when XSAVE is exposed */
6445 bool xsaves_enabled =
6446 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6447 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6448
6449 if (!xsaves_enabled)
6450 exec_control &= ~SECONDARY_EXEC_XSAVES;
6451
6452 if (nested) {
6453 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006454 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006455 SECONDARY_EXEC_XSAVES;
6456 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006457 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006458 ~SECONDARY_EXEC_XSAVES;
6459 }
6460 }
6461
Paolo Bonzini80154d72017-08-24 13:55:35 +02006462 if (vmx_rdtscp_supported()) {
6463 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6464 if (!rdtscp_enabled)
6465 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6466
6467 if (nested) {
6468 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006469 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006470 SECONDARY_EXEC_RDTSCP;
6471 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006472 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006473 ~SECONDARY_EXEC_RDTSCP;
6474 }
6475 }
6476
6477 if (vmx_invpcid_supported()) {
6478 /* Exposing INVPCID only when PCID is exposed */
6479 bool invpcid_enabled =
6480 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6481 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6482
6483 if (!invpcid_enabled) {
6484 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6485 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6486 }
6487
6488 if (nested) {
6489 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006490 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006491 SECONDARY_EXEC_ENABLE_INVPCID;
6492 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006493 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006494 ~SECONDARY_EXEC_ENABLE_INVPCID;
6495 }
6496 }
6497
Jim Mattson45ec3682017-08-23 16:32:04 -07006498 if (vmx_rdrand_supported()) {
6499 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6500 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006501 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006502
6503 if (nested) {
6504 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006505 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006506 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006507 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006508 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006509 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006510 }
6511 }
6512
Jim Mattson75f4fc82017-08-23 16:32:03 -07006513 if (vmx_rdseed_supported()) {
6514 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6515 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006516 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006517
6518 if (nested) {
6519 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006520 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006521 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006522 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006523 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006524 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006525 }
6526 }
6527
Paolo Bonzini80154d72017-08-24 13:55:35 +02006528 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006529}
6530
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006531static void ept_set_mmio_spte_mask(void)
6532{
6533 /*
6534 * EPT Misconfigurations can be generated if the value of bits 2:0
6535 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006536 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006537 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6538 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006539}
6540
Wanpeng Lif53cd632014-12-02 19:14:58 +08006541#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006542/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006543 * Sets up the vmcs for emulated real mode.
6544 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006545static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006546{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006547 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006548
Abel Gordon4607c2d2013-04-18 14:35:55 +03006549 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006550 /*
6551 * At vCPU creation, "VMWRITE to any supported field
6552 * in the VMCS" is supported, so use the more
6553 * permissive vmx_vmread_bitmap to specify both read
6554 * and write permissions for the shadow VMCS.
6555 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006556 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006557 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006558 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006559 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006560 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006561
Avi Kivity6aa8b732006-12-10 02:21:36 -08006562 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6563
Avi Kivity6aa8b732006-12-10 02:21:36 -08006564 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006565 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006566 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006567
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006568 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006569
Dan Williamsdfa169b2016-06-02 11:17:24 -07006570 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006571 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006572 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006573 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006574 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006575
Andrey Smetanind62caab2015-11-10 15:36:33 +03006576 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006577 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6578 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6579 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6580 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6581
6582 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006583
Li RongQing0bcf2612015-12-03 13:29:34 +08006584 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006585 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006586 }
6587
Wanpeng Lib31c1142018-03-12 04:53:04 -07006588 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006589 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006590 vmx->ple_window = ple_window;
6591 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006592 }
6593
Xiao Guangrongc3707952011-07-12 03:28:04 +08006594 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6595 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006596 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6597
Avi Kivity9581d442010-10-19 16:46:55 +02006598 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6599 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006600 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006601 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6602 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006603
Bandan Das2a499e42017-08-03 15:54:41 -04006604 if (cpu_has_vmx_vmfunc())
6605 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6606
Eddie Dong2cc51562007-05-21 07:28:09 +03006607 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6608 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006609 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006610 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006611 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006612
Radim Krčmář74545702015-04-27 15:11:25 +02006613 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6614 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006615
Paolo Bonzini03916db2014-07-24 14:21:57 +02006616 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006617 u32 index = vmx_msr_index[i];
6618 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006619 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006620
6621 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6622 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006623 if (wrmsr_safe(index, data_low, data_high) < 0)
6624 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006625 vmx->guest_msrs[j].index = i;
6626 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006627 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006628 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006629 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006630
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006631 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006632
6633 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006634
6635 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006636 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006637
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006638 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6639 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6640
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006641 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006642
Wanpeng Lif53cd632014-12-02 19:14:58 +08006643 if (vmx_xsaves_supported())
6644 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6645
Peter Feiner4e595162016-07-07 14:49:58 -07006646 if (enable_pml) {
6647 ASSERT(vmx->pml_pg);
6648 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6649 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6650 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006651}
6652
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006653static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006654{
6655 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006656 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006657 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006658
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006659 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006660 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006661
Wanpeng Li518e7b92018-02-28 14:03:31 +08006662 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006663 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006664 kvm_set_cr8(vcpu, 0);
6665
6666 if (!init_event) {
6667 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6668 MSR_IA32_APICBASE_ENABLE;
6669 if (kvm_vcpu_is_reset_bsp(vcpu))
6670 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6671 apic_base_msr.host_initiated = true;
6672 kvm_set_apic_base(vcpu, &apic_base_msr);
6673 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006674
Avi Kivity2fb92db2011-04-27 19:42:18 +03006675 vmx_segment_cache_clear(vmx);
6676
Avi Kivity5706be02008-08-20 15:07:31 +03006677 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006678 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006679 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006680
6681 seg_setup(VCPU_SREG_DS);
6682 seg_setup(VCPU_SREG_ES);
6683 seg_setup(VCPU_SREG_FS);
6684 seg_setup(VCPU_SREG_GS);
6685 seg_setup(VCPU_SREG_SS);
6686
6687 vmcs_write16(GUEST_TR_SELECTOR, 0);
6688 vmcs_writel(GUEST_TR_BASE, 0);
6689 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6690 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6691
6692 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6693 vmcs_writel(GUEST_LDTR_BASE, 0);
6694 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6695 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6696
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006697 if (!init_event) {
6698 vmcs_write32(GUEST_SYSENTER_CS, 0);
6699 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6700 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6701 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6702 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006703
Wanpeng Lic37c2872017-11-20 14:52:21 -08006704 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006705 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006706
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006707 vmcs_writel(GUEST_GDTR_BASE, 0);
6708 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6709
6710 vmcs_writel(GUEST_IDTR_BASE, 0);
6711 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6712
Anthony Liguori443381a2010-12-06 10:53:38 -06006713 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006714 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006715 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006716 if (kvm_mpx_supported())
6717 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006718
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006719 setup_msrs(vmx);
6720
Avi Kivity6aa8b732006-12-10 02:21:36 -08006721 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6722
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006723 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006724 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006725 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006726 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006727 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006728 vmcs_write32(TPR_THRESHOLD, 0);
6729 }
6730
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006731 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006732
Sheng Yang2384d2b2008-01-17 15:14:33 +08006733 if (vmx->vpid != 0)
6734 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6735
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006736 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006737 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006738 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006739 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006740 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006741
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006742 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006744 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006745 if (init_event)
6746 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006747}
6748
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006749/*
6750 * In nested virtualization, check if L1 asked to exit on external interrupts.
6751 * For most existing hypervisors, this will always return true.
6752 */
6753static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6754{
6755 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6756 PIN_BASED_EXT_INTR_MASK;
6757}
6758
Bandan Das77b0f5d2014-04-19 18:17:45 -04006759/*
6760 * In nested virtualization, check if L1 has set
6761 * VM_EXIT_ACK_INTR_ON_EXIT
6762 */
6763static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6764{
6765 return get_vmcs12(vcpu)->vm_exit_controls &
6766 VM_EXIT_ACK_INTR_ON_EXIT;
6767}
6768
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006769static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6770{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006771 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006772}
6773
Jan Kiszkac9a79532014-03-07 20:03:15 +01006774static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006775{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006776 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6777 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006778}
6779
Jan Kiszkac9a79532014-03-07 20:03:15 +01006780static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006781{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006782 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006783 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006784 enable_irq_window(vcpu);
6785 return;
6786 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006787
Paolo Bonzini47c01522016-12-19 11:44:07 +01006788 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6789 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006790}
6791
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006792static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006793{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006795 uint32_t intr;
6796 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006797
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006798 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006799
Avi Kivityfa89a812008-09-01 15:57:51 +03006800 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006801 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006802 int inc_eip = 0;
6803 if (vcpu->arch.interrupt.soft)
6804 inc_eip = vcpu->arch.event_exit_inst_len;
6805 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006806 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006807 return;
6808 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006809 intr = irq | INTR_INFO_VALID_MASK;
6810 if (vcpu->arch.interrupt.soft) {
6811 intr |= INTR_TYPE_SOFT_INTR;
6812 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6813 vmx->vcpu.arch.event_exit_inst_len);
6814 } else
6815 intr |= INTR_TYPE_EXT_INTR;
6816 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006817
6818 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006819}
6820
Sheng Yangf08864b2008-05-15 18:23:25 +08006821static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6822{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006823 struct vcpu_vmx *vmx = to_vmx(vcpu);
6824
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006825 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006826 /*
6827 * Tracking the NMI-blocked state in software is built upon
6828 * finding the next open IRQ window. This, in turn, depends on
6829 * well-behaving guests: They have to keep IRQs disabled at
6830 * least as long as the NMI handler runs. Otherwise we may
6831 * cause NMI nesting, maybe breaking the guest. But as this is
6832 * highly unlikely, we can live with the residual risk.
6833 */
6834 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6835 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6836 }
6837
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006838 ++vcpu->stat.nmi_injections;
6839 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006840
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006841 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006842 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006843 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006844 return;
6845 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006846
Sheng Yangf08864b2008-05-15 18:23:25 +08006847 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6848 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006849
6850 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006851}
6852
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006853static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6854{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006855 struct vcpu_vmx *vmx = to_vmx(vcpu);
6856 bool masked;
6857
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006858 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006859 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006860 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006861 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006862 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6863 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6864 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006865}
6866
6867static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6868{
6869 struct vcpu_vmx *vmx = to_vmx(vcpu);
6870
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006871 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006872 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6873 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6874 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6875 }
6876 } else {
6877 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6878 if (masked)
6879 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6880 GUEST_INTR_STATE_NMI);
6881 else
6882 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6883 GUEST_INTR_STATE_NMI);
6884 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006885}
6886
Jan Kiszka2505dc92013-04-14 12:12:47 +02006887static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6888{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006889 if (to_vmx(vcpu)->nested.nested_run_pending)
6890 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006891
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006892 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006893 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6894 return 0;
6895
Jan Kiszka2505dc92013-04-14 12:12:47 +02006896 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6897 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6898 | GUEST_INTR_STATE_NMI));
6899}
6900
Gleb Natapov78646122009-03-23 12:12:11 +02006901static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6902{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006903 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6904 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006905 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6906 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006907}
6908
Izik Eiduscbc94022007-10-25 00:29:55 +02006909static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6910{
6911 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006912
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006913 if (enable_unrestricted_guest)
6914 return 0;
6915
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006916 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6917 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006918 if (ret)
6919 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006920 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006921 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006922}
6923
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006924static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6925{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006926 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006927 return 0;
6928}
6929
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006930static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006931{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006932 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006933 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006934 /*
6935 * Update instruction length as we may reinject the exception
6936 * from user space while in guest debugging mode.
6937 */
6938 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6939 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006940 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006941 return false;
6942 /* fall through */
6943 case DB_VECTOR:
6944 if (vcpu->guest_debug &
6945 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6946 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006947 /* fall through */
6948 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006949 case OF_VECTOR:
6950 case BR_VECTOR:
6951 case UD_VECTOR:
6952 case DF_VECTOR:
6953 case SS_VECTOR:
6954 case GP_VECTOR:
6955 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006956 return true;
6957 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006958 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006959 return false;
6960}
6961
6962static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6963 int vec, u32 err_code)
6964{
6965 /*
6966 * Instruction with address size override prefix opcode 0x67
6967 * Cause the #SS fault with 0 error code in VM86 mode.
6968 */
6969 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
6970 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
6971 if (vcpu->arch.halt_request) {
6972 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006973 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006974 }
6975 return 1;
6976 }
6977 return 0;
6978 }
6979
6980 /*
6981 * Forward all other exceptions that are valid in real mode.
6982 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6983 * the required debugging infrastructure rework.
6984 */
6985 kvm_queue_exception(vcpu, vec);
6986 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006987}
6988
Andi Kleena0861c02009-06-08 17:37:09 +08006989/*
6990 * Trigger machine check on the host. We assume all the MSRs are already set up
6991 * by the CPU and that we still run on the same CPU as the MCE occurred on.
6992 * We pass a fake environment to the machine check handler because we want
6993 * the guest to be always treated like user space, no matter what context
6994 * it used internally.
6995 */
6996static void kvm_machine_check(void)
6997{
6998#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
6999 struct pt_regs regs = {
7000 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7001 .flags = X86_EFLAGS_IF,
7002 };
7003
7004 do_machine_check(&regs, 0);
7005#endif
7006}
7007
Avi Kivity851ba692009-08-24 11:10:17 +03007008static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007009{
7010 /* already handled by vcpu_run */
7011 return 1;
7012}
7013
Avi Kivity851ba692009-08-24 11:10:17 +03007014static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007015{
Avi Kivity1155f762007-11-22 11:30:47 +02007016 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007017 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007018 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007019 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007020 u32 vect_info;
7021 enum emulation_result er;
7022
Avi Kivity1155f762007-11-22 11:30:47 +02007023 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007024 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007025
Andi Kleena0861c02009-06-08 17:37:09 +08007026 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007027 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007028
Jim Mattsonef85b672016-12-12 11:01:37 -08007029 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007030 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007031
Wanpeng Li082d06e2018-04-03 16:28:48 -07007032 if (is_invalid_opcode(intr_info))
7033 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007034
Avi Kivity6aa8b732006-12-10 02:21:36 -08007035 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007036 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007037 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007038
Liran Alon9e869482018-03-12 13:12:51 +02007039 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7040 WARN_ON_ONCE(!enable_vmware_backdoor);
7041 er = emulate_instruction(vcpu,
7042 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7043 if (er == EMULATE_USER_EXIT)
7044 return 0;
7045 else if (er != EMULATE_DONE)
7046 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7047 return 1;
7048 }
7049
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007050 /*
7051 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7052 * MMIO, it is better to report an internal error.
7053 * See the comments in vmx_handle_exit.
7054 */
7055 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7056 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7057 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7058 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007059 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007060 vcpu->run->internal.data[0] = vect_info;
7061 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007062 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007063 return 0;
7064 }
7065
Avi Kivity6aa8b732006-12-10 02:21:36 -08007066 if (is_page_fault(intr_info)) {
7067 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007068 /* EPT won't cause page fault directly */
7069 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007070 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007071 }
7072
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007073 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007074
7075 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7076 return handle_rmode_exception(vcpu, ex_no, error_code);
7077
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007078 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007079 case AC_VECTOR:
7080 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7081 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007082 case DB_VECTOR:
7083 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7084 if (!(vcpu->guest_debug &
7085 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007086 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007087 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007088 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007089 skip_emulated_instruction(vcpu);
7090
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007091 kvm_queue_exception(vcpu, DB_VECTOR);
7092 return 1;
7093 }
7094 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7095 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7096 /* fall through */
7097 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007098 /*
7099 * Update instruction length as we may reinject #BP from
7100 * user space while in guest debugging mode. Reading it for
7101 * #DB as well causes no harm, it is not used in that case.
7102 */
7103 vmx->vcpu.arch.event_exit_inst_len =
7104 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007105 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007106 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007107 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7108 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007109 break;
7110 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007111 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7112 kvm_run->ex.exception = ex_no;
7113 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007114 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007115 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 return 0;
7117}
7118
Avi Kivity851ba692009-08-24 11:10:17 +03007119static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007121 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007122 return 1;
7123}
7124
Avi Kivity851ba692009-08-24 11:10:17 +03007125static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007126{
Avi Kivity851ba692009-08-24 11:10:17 +03007127 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007128 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007129 return 0;
7130}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131
Avi Kivity851ba692009-08-24 11:10:17 +03007132static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133{
He, Qingbfdaab02007-09-12 14:18:28 +08007134 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007135 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007136 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007137
He, Qingbfdaab02007-09-12 14:18:28 +08007138 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007139 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007140
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007141 ++vcpu->stat.io_exits;
7142
Sean Christopherson432baf62018-03-08 08:57:26 -08007143 if (string)
Andre Przywara51d8b662010-12-21 11:12:02 +01007144 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007145
7146 port = exit_qualification >> 16;
7147 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007148 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007149
Sean Christophersondca7f122018-03-08 08:57:27 -08007150 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007151}
7152
Ingo Molnar102d8322007-02-19 14:37:47 +02007153static void
7154vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7155{
7156 /*
7157 * Patch in the VMCALL instruction:
7158 */
7159 hypercall[0] = 0x0f;
7160 hypercall[1] = 0x01;
7161 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007162}
7163
Guo Chao0fa06072012-06-28 15:16:19 +08007164/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007165static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7166{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007167 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007168 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7169 unsigned long orig_val = val;
7170
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007171 /*
7172 * We get here when L2 changed cr0 in a way that did not change
7173 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007174 * but did change L0 shadowed bits. So we first calculate the
7175 * effective cr0 value that L1 would like to write into the
7176 * hardware. It consists of the L2-owned bits from the new
7177 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007178 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007179 val = (val & ~vmcs12->cr0_guest_host_mask) |
7180 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7181
David Matlack38991522016-11-29 18:14:08 -08007182 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007183 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007184
7185 if (kvm_set_cr0(vcpu, val))
7186 return 1;
7187 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007188 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007189 } else {
7190 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007191 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007192 return 1;
David Matlack38991522016-11-29 18:14:08 -08007193
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007194 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007196}
7197
7198static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7199{
7200 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007201 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7202 unsigned long orig_val = val;
7203
7204 /* analogously to handle_set_cr0 */
7205 val = (val & ~vmcs12->cr4_guest_host_mask) |
7206 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7207 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007208 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007209 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 return 0;
7211 } else
7212 return kvm_set_cr4(vcpu, val);
7213}
7214
Paolo Bonzini0367f202016-07-12 10:44:55 +02007215static int handle_desc(struct kvm_vcpu *vcpu)
7216{
7217 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
7218 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
7219}
7220
Avi Kivity851ba692009-08-24 11:10:17 +03007221static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007222{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007223 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007224 int cr;
7225 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007226 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007227 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007228
He, Qingbfdaab02007-09-12 14:18:28 +08007229 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007230 cr = exit_qualification & 15;
7231 reg = (exit_qualification >> 8) & 15;
7232 switch ((exit_qualification >> 4) & 3) {
7233 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007234 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007235 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007236 switch (cr) {
7237 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007238 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007239 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007241 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007242 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007243 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007245 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007246 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007247 case 8: {
7248 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007249 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007250 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007251 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007252 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007253 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007254 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007255 return ret;
7256 /*
7257 * TODO: we might be squashing a
7258 * KVM_GUESTDBG_SINGLESTEP-triggered
7259 * KVM_EXIT_DEBUG here.
7260 */
Avi Kivity851ba692009-08-24 11:10:17 +03007261 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007262 return 0;
7263 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007264 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007265 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007266 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007267 WARN_ONCE(1, "Guest should always own CR0.TS");
7268 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007269 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271 case 1: /*mov from cr*/
7272 switch (cr) {
7273 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007274 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007275 val = kvm_read_cr3(vcpu);
7276 kvm_register_write(vcpu, reg, val);
7277 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007278 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007280 val = kvm_get_cr8(vcpu);
7281 kvm_register_write(vcpu, reg, val);
7282 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007283 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284 }
7285 break;
7286 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007287 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007288 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007289 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007290
Kyle Huey6affcbe2016-11-29 12:40:40 -08007291 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 default:
7293 break;
7294 }
Avi Kivity851ba692009-08-24 11:10:17 +03007295 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007296 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 (int)(exit_qualification >> 4) & 3, cr);
7298 return 0;
7299}
7300
Avi Kivity851ba692009-08-24 11:10:17 +03007301static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007302{
He, Qingbfdaab02007-09-12 14:18:28 +08007303 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007304 int dr, dr7, reg;
7305
7306 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7307 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7308
7309 /* First, if DR does not exist, trigger UD */
7310 if (!kvm_require_dr(vcpu, dr))
7311 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312
Jan Kiszkaf2483412010-01-20 18:20:20 +01007313 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007314 if (!kvm_require_cpl(vcpu, 0))
7315 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007316 dr7 = vmcs_readl(GUEST_DR7);
7317 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007318 /*
7319 * As the vm-exit takes precedence over the debug trap, we
7320 * need to emulate the latter, either for the host or the
7321 * guest debugging itself.
7322 */
7323 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007324 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007325 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007326 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007327 vcpu->run->debug.arch.exception = DB_VECTOR;
7328 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007329 return 0;
7330 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007331 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007332 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007333 kvm_queue_exception(vcpu, DB_VECTOR);
7334 return 1;
7335 }
7336 }
7337
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007338 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007339 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7340 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007341
7342 /*
7343 * No more DR vmexits; force a reload of the debug registers
7344 * and reenter on this instruction. The next vmexit will
7345 * retrieve the full state of the debug registers.
7346 */
7347 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7348 return 1;
7349 }
7350
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007351 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7352 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007353 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007354
7355 if (kvm_get_dr(vcpu, dr, &val))
7356 return 1;
7357 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007358 } else
Nadav Amit57773922014-06-18 17:19:23 +03007359 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007360 return 1;
7361
Kyle Huey6affcbe2016-11-29 12:40:40 -08007362 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007363}
7364
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007365static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7366{
7367 return vcpu->arch.dr6;
7368}
7369
7370static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7371{
7372}
7373
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007374static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7375{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007376 get_debugreg(vcpu->arch.db[0], 0);
7377 get_debugreg(vcpu->arch.db[1], 1);
7378 get_debugreg(vcpu->arch.db[2], 2);
7379 get_debugreg(vcpu->arch.db[3], 3);
7380 get_debugreg(vcpu->arch.dr6, 6);
7381 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7382
7383 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007384 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007385}
7386
Gleb Natapov020df072010-04-13 10:05:23 +03007387static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7388{
7389 vmcs_writel(GUEST_DR7, val);
7390}
7391
Avi Kivity851ba692009-08-24 11:10:17 +03007392static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007393{
Kyle Huey6a908b62016-11-29 12:40:37 -08007394 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007395}
7396
Avi Kivity851ba692009-08-24 11:10:17 +03007397static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007398{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007399 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007400 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007401
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007402 msr_info.index = ecx;
7403 msr_info.host_initiated = false;
7404 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007405 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007406 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007407 return 1;
7408 }
7409
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007410 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007411
Avi Kivity6aa8b732006-12-10 02:21:36 -08007412 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007413 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7414 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007415 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007416}
7417
Avi Kivity851ba692009-08-24 11:10:17 +03007418static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007419{
Will Auld8fe8ab42012-11-29 12:42:12 -08007420 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007421 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7422 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7423 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007424
Will Auld8fe8ab42012-11-29 12:42:12 -08007425 msr.data = data;
7426 msr.index = ecx;
7427 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007428 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007429 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007430 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007431 return 1;
7432 }
7433
Avi Kivity59200272010-01-25 19:47:02 +02007434 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007435 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007436}
7437
Avi Kivity851ba692009-08-24 11:10:17 +03007438static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007439{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007440 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007441 return 1;
7442}
7443
Avi Kivity851ba692009-08-24 11:10:17 +03007444static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007445{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007446 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7447 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007448
Avi Kivity3842d132010-07-27 12:30:24 +03007449 kvm_make_request(KVM_REQ_EVENT, vcpu);
7450
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007451 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007452 return 1;
7453}
7454
Avi Kivity851ba692009-08-24 11:10:17 +03007455static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456{
Avi Kivityd3bef152007-06-05 15:53:05 +03007457 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458}
7459
Avi Kivity851ba692009-08-24 11:10:17 +03007460static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007461{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007462 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007463}
7464
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007465static int handle_invd(struct kvm_vcpu *vcpu)
7466{
Andre Przywara51d8b662010-12-21 11:12:02 +01007467 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007468}
7469
Avi Kivity851ba692009-08-24 11:10:17 +03007470static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007471{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007472 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007473
7474 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007475 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007476}
7477
Avi Kivityfee84b02011-11-10 14:57:25 +02007478static int handle_rdpmc(struct kvm_vcpu *vcpu)
7479{
7480 int err;
7481
7482 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007483 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007484}
7485
Avi Kivity851ba692009-08-24 11:10:17 +03007486static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007487{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007488 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007489}
7490
Dexuan Cui2acf9232010-06-10 11:27:12 +08007491static int handle_xsetbv(struct kvm_vcpu *vcpu)
7492{
7493 u64 new_bv = kvm_read_edx_eax(vcpu);
7494 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7495
7496 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007497 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007498 return 1;
7499}
7500
Wanpeng Lif53cd632014-12-02 19:14:58 +08007501static int handle_xsaves(struct kvm_vcpu *vcpu)
7502{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007503 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007504 WARN(1, "this should never happen\n");
7505 return 1;
7506}
7507
7508static int handle_xrstors(struct kvm_vcpu *vcpu)
7509{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007510 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007511 WARN(1, "this should never happen\n");
7512 return 1;
7513}
7514
Avi Kivity851ba692009-08-24 11:10:17 +03007515static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007516{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007517 if (likely(fasteoi)) {
7518 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7519 int access_type, offset;
7520
7521 access_type = exit_qualification & APIC_ACCESS_TYPE;
7522 offset = exit_qualification & APIC_ACCESS_OFFSET;
7523 /*
7524 * Sane guest uses MOV to write EOI, with written value
7525 * not cared. So make a short-circuit here by avoiding
7526 * heavy instruction emulation.
7527 */
7528 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7529 (offset == APIC_EOI)) {
7530 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007531 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007532 }
7533 }
Andre Przywara51d8b662010-12-21 11:12:02 +01007534 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007535}
7536
Yang Zhangc7c9c562013-01-25 10:18:51 +08007537static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7538{
7539 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7540 int vector = exit_qualification & 0xff;
7541
7542 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7543 kvm_apic_set_eoi_accelerated(vcpu, vector);
7544 return 1;
7545}
7546
Yang Zhang83d4c282013-01-25 10:18:49 +08007547static int handle_apic_write(struct kvm_vcpu *vcpu)
7548{
7549 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7550 u32 offset = exit_qualification & 0xfff;
7551
7552 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7553 kvm_apic_write_nodecode(vcpu, offset);
7554 return 1;
7555}
7556
Avi Kivity851ba692009-08-24 11:10:17 +03007557static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007558{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007559 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007560 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007561 bool has_error_code = false;
7562 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007563 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007564 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007565
7566 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007567 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007568 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007569
7570 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7571
7572 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007573 if (reason == TASK_SWITCH_GATE && idt_v) {
7574 switch (type) {
7575 case INTR_TYPE_NMI_INTR:
7576 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007577 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007578 break;
7579 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007580 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007581 kvm_clear_interrupt_queue(vcpu);
7582 break;
7583 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007584 if (vmx->idt_vectoring_info &
7585 VECTORING_INFO_DELIVER_CODE_MASK) {
7586 has_error_code = true;
7587 error_code =
7588 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7589 }
7590 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007591 case INTR_TYPE_SOFT_EXCEPTION:
7592 kvm_clear_exception_queue(vcpu);
7593 break;
7594 default:
7595 break;
7596 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007597 }
Izik Eidus37817f22008-03-24 23:14:53 +02007598 tss_selector = exit_qualification;
7599
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007600 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7601 type != INTR_TYPE_EXT_INTR &&
7602 type != INTR_TYPE_NMI_INTR))
7603 skip_emulated_instruction(vcpu);
7604
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007605 if (kvm_task_switch(vcpu, tss_selector,
7606 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7607 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007608 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7609 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7610 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007611 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007612 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007613
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007614 /*
7615 * TODO: What about debug traps on tss switch?
7616 * Are we supposed to inject them and update dr6?
7617 */
7618
7619 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007620}
7621
Avi Kivity851ba692009-08-24 11:10:17 +03007622static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007623{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007624 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007625 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007626 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007627
Sheng Yangf9c617f2009-03-25 10:08:52 +08007628 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007629
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007630 /*
7631 * EPT violation happened while executing iret from NMI,
7632 * "blocked by NMI" bit has to be set before next VM entry.
7633 * There are errata that may cause this bit to not be set:
7634 * AAK134, BY25.
7635 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007636 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007637 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007638 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007639 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7640
Sheng Yang14394422008-04-28 12:24:45 +08007641 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007642 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007643
Junaid Shahid27959a42016-12-06 16:46:10 -08007644 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007645 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007646 ? PFERR_USER_MASK : 0;
7647 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007648 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007649 ? PFERR_WRITE_MASK : 0;
7650 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007651 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007652 ? PFERR_FETCH_MASK : 0;
7653 /* ept page table entry is present? */
7654 error_code |= (exit_qualification &
7655 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7656 EPT_VIOLATION_EXECUTABLE))
7657 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007658
Paolo Bonzinieebed242016-11-28 14:39:58 +01007659 error_code |= (exit_qualification & 0x100) != 0 ?
7660 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007661
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007662 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007663 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007664}
7665
Avi Kivity851ba692009-08-24 11:10:17 +03007666static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007667{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007668 gpa_t gpa;
7669
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007670 /*
7671 * A nested guest cannot optimize MMIO vmexits, because we have an
7672 * nGPA here instead of the required GPA.
7673 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007674 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007675 if (!is_guest_mode(vcpu) &&
7676 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007677 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007678 /*
7679 * Doing kvm_skip_emulated_instruction() depends on undefined
7680 * behavior: Intel's manual doesn't mandate
7681 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7682 * occurs and while on real hardware it was observed to be set,
7683 * other hypervisors (namely Hyper-V) don't set it, we end up
7684 * advancing IP with some random value. Disable fast mmio when
7685 * running nested and keep it for real hardware in hope that
7686 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7687 */
7688 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7689 return kvm_skip_emulated_instruction(vcpu);
7690 else
7691 return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP,
7692 NULL, 0) == EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007693 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007694
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007695 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007696}
7697
Avi Kivity851ba692009-08-24 11:10:17 +03007698static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007699{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007700 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007701 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7702 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007703 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007704 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007705
7706 return 1;
7707}
7708
Mohammed Gamal80ced182009-09-01 12:48:18 +02007709static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007710{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007711 struct vcpu_vmx *vmx = to_vmx(vcpu);
7712 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007713 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007714 u32 cpu_exec_ctrl;
7715 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007716 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007717
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007718 /*
7719 * We should never reach the point where we are emulating L2
7720 * due to invalid guest state as that means we incorrectly
7721 * allowed a nested VMEntry with an invalid vmcs12.
7722 */
7723 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7724
Avi Kivity49e9d552010-09-19 14:34:08 +02007725 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7726 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007727
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007728 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007729 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007730 return handle_interrupt_window(&vmx->vcpu);
7731
Radim Krčmář72875d82017-04-26 22:32:19 +02007732 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007733 return 1;
7734
Liran Alon9b8ae632017-11-05 16:56:34 +02007735 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007736
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007737 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007738 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007739 ret = 0;
7740 goto out;
7741 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007742
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007743 if (err != EMULATE_DONE)
7744 goto emulation_error;
7745
7746 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7747 vcpu->arch.exception.pending)
7748 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007749
Gleb Natapov8d76c492013-05-08 18:38:44 +03007750 if (vcpu->arch.halt_request) {
7751 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007752 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007753 goto out;
7754 }
7755
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007756 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007757 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007758 if (need_resched())
7759 schedule();
7760 }
7761
Mohammed Gamal80ced182009-09-01 12:48:18 +02007762out:
7763 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007764
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007765emulation_error:
7766 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7767 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7768 vcpu->run->internal.ndata = 0;
7769 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007770}
7771
7772static void grow_ple_window(struct kvm_vcpu *vcpu)
7773{
7774 struct vcpu_vmx *vmx = to_vmx(vcpu);
7775 int old = vmx->ple_window;
7776
Babu Mogerc8e88712018-03-16 16:37:24 -04007777 vmx->ple_window = __grow_ple_window(old, ple_window,
7778 ple_window_grow,
7779 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007780
7781 if (vmx->ple_window != old)
7782 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007783
7784 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007785}
7786
7787static void shrink_ple_window(struct kvm_vcpu *vcpu)
7788{
7789 struct vcpu_vmx *vmx = to_vmx(vcpu);
7790 int old = vmx->ple_window;
7791
Babu Mogerc8e88712018-03-16 16:37:24 -04007792 vmx->ple_window = __shrink_ple_window(old, ple_window,
7793 ple_window_shrink,
7794 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007795
7796 if (vmx->ple_window != old)
7797 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007798
7799 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007800}
7801
7802/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007803 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7804 */
7805static void wakeup_handler(void)
7806{
7807 struct kvm_vcpu *vcpu;
7808 int cpu = smp_processor_id();
7809
7810 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7811 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7812 blocked_vcpu_list) {
7813 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7814
7815 if (pi_test_on(pi_desc) == 1)
7816 kvm_vcpu_kick(vcpu);
7817 }
7818 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7819}
7820
Peng Haoe01bca22018-04-07 05:47:32 +08007821static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007822{
7823 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7824 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7825 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7826 0ull, VMX_EPT_EXECUTABLE_MASK,
7827 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007828 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007829
7830 ept_set_mmio_spte_mask();
7831 kvm_enable_tdp();
7832}
7833
Tiejun Chenf2c76482014-10-28 10:14:47 +08007834static __init int hardware_setup(void)
7835{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007836 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007837 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007838
7839 rdmsrl_safe(MSR_EFER, &host_efer);
7840
7841 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7842 kvm_define_shared_msr(i, vmx_msr_index[i]);
7843
Radim Krčmář23611332016-09-29 22:41:33 +02007844 for (i = 0; i < VMX_BITMAP_NR; i++) {
7845 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7846 if (!vmx_bitmap[i])
7847 goto out;
7848 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007849
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007850 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7851 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7852
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007853 if (setup_vmcs_config(&vmcs_config) < 0) {
7854 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007855 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007856 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007857
7858 if (boot_cpu_has(X86_FEATURE_NX))
7859 kvm_enable_efer_bits(EFER_NX);
7860
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007861 if (boot_cpu_has(X86_FEATURE_MPX)) {
7862 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7863 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7864 }
7865
Wanpeng Li08d839c2017-03-23 05:30:08 -07007866 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7867 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007868 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007869
Tiejun Chenf2c76482014-10-28 10:14:47 +08007870 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007871 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007872 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007873 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007874 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007875
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007876 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007877 enable_ept_ad_bits = 0;
7878
Wanpeng Li8ad81822017-10-09 15:51:53 -07007879 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007880 enable_unrestricted_guest = 0;
7881
Paolo Bonziniad15a292015-01-30 16:18:49 +01007882 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007883 flexpriority_enabled = 0;
7884
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007885 if (!cpu_has_virtual_nmis())
7886 enable_vnmi = 0;
7887
Paolo Bonziniad15a292015-01-30 16:18:49 +01007888 /*
7889 * set_apic_access_page_addr() is used to reload apic access
7890 * page upon invalidation. No need to do anything if not
7891 * using the APIC_ACCESS_ADDR VMCS field.
7892 */
7893 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007894 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007895
7896 if (!cpu_has_vmx_tpr_shadow())
7897 kvm_x86_ops->update_cr8_intercept = NULL;
7898
7899 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7900 kvm_disable_largepages();
7901
Tianyu Lan877ad952018-07-19 08:40:23 +00007902#if IS_ENABLED(CONFIG_HYPERV)
7903 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7904 && enable_ept)
7905 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7906#endif
7907
Wanpeng Li0f107682017-09-28 18:06:24 -07007908 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007909 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007910 ple_window = 0;
7911 ple_window_grow = 0;
7912 ple_window_max = 0;
7913 ple_window_shrink = 0;
7914 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007915
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007916 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007918 kvm_x86_ops->sync_pir_to_irr = NULL;
7919 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007920
Haozhong Zhang64903d62015-10-20 15:39:09 +08007921 if (cpu_has_vmx_tsc_scaling()) {
7922 kvm_has_tsc_control = true;
7923 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7924 kvm_tsc_scaling_ratio_frac_bits = 48;
7925 }
7926
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007927 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7928
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007929 if (enable_ept)
7930 vmx_enable_tdp();
7931 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007932 kvm_disable_tdp();
7933
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007934 if (!nested) {
7935 kvm_x86_ops->get_nested_state = NULL;
7936 kvm_x86_ops->set_nested_state = NULL;
7937 }
7938
Kai Huang843e4332015-01-28 10:54:28 +08007939 /*
7940 * Only enable PML when hardware supports PML feature, and both EPT
7941 * and EPT A/D bit features are enabled -- PML depends on them to work.
7942 */
7943 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7944 enable_pml = 0;
7945
7946 if (!enable_pml) {
7947 kvm_x86_ops->slot_enable_log_dirty = NULL;
7948 kvm_x86_ops->slot_disable_log_dirty = NULL;
7949 kvm_x86_ops->flush_log_dirty = NULL;
7950 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7951 }
7952
Yunhong Jiang64672c92016-06-13 14:19:59 -07007953 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7954 u64 vmx_msr;
7955
7956 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7957 cpu_preemption_timer_multi =
7958 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7959 } else {
7960 kvm_x86_ops->set_hv_timer = NULL;
7961 kvm_x86_ops->cancel_hv_timer = NULL;
7962 }
7963
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007964 if (!cpu_has_vmx_shadow_vmcs())
7965 enable_shadow_vmcs = 0;
7966 if (enable_shadow_vmcs)
7967 init_vmcs_shadow_fields();
7968
Feng Wubf9f6ac2015-09-18 22:29:55 +08007969 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007970 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007971
Ashok Rajc45dcc72016-06-22 14:59:56 +08007972 kvm_mce_cap_supported |= MCG_LMCE_P;
7973
Tiejun Chenf2c76482014-10-28 10:14:47 +08007974 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007975
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007976out:
Radim Krčmář23611332016-09-29 22:41:33 +02007977 for (i = 0; i < VMX_BITMAP_NR; i++)
7978 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007979
7980 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007981}
7982
7983static __exit void hardware_unsetup(void)
7984{
Radim Krčmář23611332016-09-29 22:41:33 +02007985 int i;
7986
7987 for (i = 0; i < VMX_BITMAP_NR; i++)
7988 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007989
Tiejun Chenf2c76482014-10-28 10:14:47 +08007990 free_kvm_area();
7991}
7992
Avi Kivity6aa8b732006-12-10 02:21:36 -08007993/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007994 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
7995 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
7996 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03007997static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007998{
Wanpeng Lib31c1142018-03-12 04:53:04 -07007999 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008000 grow_ple_window(vcpu);
8001
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008002 /*
8003 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8004 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8005 * never set PAUSE_EXITING and just set PLE if supported,
8006 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8007 */
8008 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008009 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008010}
8011
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008012static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008013{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008014 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008015}
8016
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008017static int handle_mwait(struct kvm_vcpu *vcpu)
8018{
8019 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8020 return handle_nop(vcpu);
8021}
8022
Jim Mattson45ec3682017-08-23 16:32:04 -07008023static int handle_invalid_op(struct kvm_vcpu *vcpu)
8024{
8025 kvm_queue_exception(vcpu, UD_VECTOR);
8026 return 1;
8027}
8028
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008029static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8030{
8031 return 1;
8032}
8033
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008034static int handle_monitor(struct kvm_vcpu *vcpu)
8035{
8036 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8037 return handle_nop(vcpu);
8038}
8039
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008040/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008041 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8042 * set the success or error code of an emulated VMX instruction, as specified
8043 * by Vol 2B, VMX Instruction Reference, "Conventions".
8044 */
8045static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8046{
8047 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8048 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8049 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8050}
8051
8052static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8053{
8054 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8055 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8056 X86_EFLAGS_SF | X86_EFLAGS_OF))
8057 | X86_EFLAGS_CF);
8058}
8059
Abel Gordon145c28d2013-04-18 14:36:55 +03008060static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008061 u32 vm_instruction_error)
8062{
8063 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8064 /*
8065 * failValid writes the error number to the current VMCS, which
8066 * can't be done there isn't a current VMCS.
8067 */
8068 nested_vmx_failInvalid(vcpu);
8069 return;
8070 }
8071 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8072 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8073 X86_EFLAGS_SF | X86_EFLAGS_OF))
8074 | X86_EFLAGS_ZF);
8075 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8076 /*
8077 * We don't need to force a shadow sync because
8078 * VM_INSTRUCTION_ERROR is not shadowed
8079 */
8080}
Abel Gordon145c28d2013-04-18 14:36:55 +03008081
Wincy Vanff651cb2014-12-11 08:52:58 +03008082static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8083{
8084 /* TODO: not to reset guest simply here. */
8085 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008086 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008087}
8088
Jan Kiszkaf41245002014-03-07 20:03:13 +01008089static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8090{
8091 struct vcpu_vmx *vmx =
8092 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8093
8094 vmx->nested.preemption_timer_expired = true;
8095 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8096 kvm_vcpu_kick(&vmx->vcpu);
8097
8098 return HRTIMER_NORESTART;
8099}
8100
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008101/*
Bandan Das19677e32014-05-06 02:19:15 -04008102 * Decode the memory-address operand of a vmx instruction, as recorded on an
8103 * exit caused by such an instruction (run by a guest hypervisor).
8104 * On success, returns 0. When the operand is invalid, returns 1 and throws
8105 * #UD or #GP.
8106 */
8107static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8108 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008109 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008110{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008111 gva_t off;
8112 bool exn;
8113 struct kvm_segment s;
8114
Bandan Das19677e32014-05-06 02:19:15 -04008115 /*
8116 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8117 * Execution", on an exit, vmx_instruction_info holds most of the
8118 * addressing components of the operand. Only the displacement part
8119 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8120 * For how an actual address is calculated from all these components,
8121 * refer to Vol. 1, "Operand Addressing".
8122 */
8123 int scaling = vmx_instruction_info & 3;
8124 int addr_size = (vmx_instruction_info >> 7) & 7;
8125 bool is_reg = vmx_instruction_info & (1u << 10);
8126 int seg_reg = (vmx_instruction_info >> 15) & 7;
8127 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8128 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8129 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8130 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8131
8132 if (is_reg) {
8133 kvm_queue_exception(vcpu, UD_VECTOR);
8134 return 1;
8135 }
8136
8137 /* Addr = segment_base + offset */
8138 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008139 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008140 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008141 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008142 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008143 off += kvm_register_read(vcpu, index_reg)<<scaling;
8144 vmx_get_segment(vcpu, &s, seg_reg);
8145 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008146
8147 if (addr_size == 1) /* 32 bit */
8148 *ret &= 0xffffffff;
8149
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008150 /* Checks for #GP/#SS exceptions. */
8151 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008152 if (is_long_mode(vcpu)) {
8153 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8154 * non-canonical form. This is the only check on the memory
8155 * destination for long mode!
8156 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008157 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008158 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008159 /* Protected mode: apply checks for segment validity in the
8160 * following order:
8161 * - segment type check (#GP(0) may be thrown)
8162 * - usability check (#GP(0)/#SS(0))
8163 * - limit check (#GP(0)/#SS(0))
8164 */
8165 if (wr)
8166 /* #GP(0) if the destination operand is located in a
8167 * read-only data segment or any code segment.
8168 */
8169 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8170 else
8171 /* #GP(0) if the source operand is located in an
8172 * execute-only code segment
8173 */
8174 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008175 if (exn) {
8176 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8177 return 1;
8178 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008179 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8180 */
8181 exn = (s.unusable != 0);
8182 /* Protected mode: #GP(0)/#SS(0) if the memory
8183 * operand is outside the segment limit.
8184 */
8185 exn = exn || (off + sizeof(u64) > s.limit);
8186 }
8187 if (exn) {
8188 kvm_queue_exception_e(vcpu,
8189 seg_reg == VCPU_SREG_SS ?
8190 SS_VECTOR : GP_VECTOR,
8191 0);
8192 return 1;
8193 }
8194
Bandan Das19677e32014-05-06 02:19:15 -04008195 return 0;
8196}
8197
Radim Krčmářcbf71272017-05-19 15:48:51 +02008198static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008199{
8200 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008201 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008202
8203 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008204 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008205 return 1;
8206
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008207 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008208 kvm_inject_page_fault(vcpu, &e);
8209 return 1;
8210 }
8211
Bandan Das3573e222014-05-06 02:19:16 -04008212 return 0;
8213}
8214
Liran Alonabfc52c2018-06-23 02:35:13 +03008215/*
8216 * Allocate a shadow VMCS and associate it with the currently loaded
8217 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8218 * VMCS is also VMCLEARed, so that it is ready for use.
8219 */
8220static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8221{
8222 struct vcpu_vmx *vmx = to_vmx(vcpu);
8223 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8224
8225 /*
8226 * We should allocate a shadow vmcs for vmcs01 only when L1
8227 * executes VMXON and free it when L1 executes VMXOFF.
8228 * As it is invalid to execute VMXON twice, we shouldn't reach
8229 * here when vmcs01 already have an allocated shadow vmcs.
8230 */
8231 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8232
8233 if (!loaded_vmcs->shadow_vmcs) {
8234 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8235 if (loaded_vmcs->shadow_vmcs)
8236 vmcs_clear(loaded_vmcs->shadow_vmcs);
8237 }
8238 return loaded_vmcs->shadow_vmcs;
8239}
8240
Jim Mattsone29acc52016-11-30 12:03:43 -08008241static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8242{
8243 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008244 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008245
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008246 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8247 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008248 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008249
8250 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8251 if (!vmx->nested.cached_vmcs12)
8252 goto out_cached_vmcs12;
8253
Liran Alon61ada742018-06-23 02:35:08 +03008254 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8255 if (!vmx->nested.cached_shadow_vmcs12)
8256 goto out_cached_shadow_vmcs12;
8257
Liran Alonabfc52c2018-06-23 02:35:13 +03008258 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8259 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008260
Jim Mattsone29acc52016-11-30 12:03:43 -08008261 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8262 HRTIMER_MODE_REL_PINNED);
8263 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8264
Roman Kagan63aff652018-07-19 21:59:07 +03008265 vmx->nested.vpid02 = allocate_vpid();
8266
Jim Mattsone29acc52016-11-30 12:03:43 -08008267 vmx->nested.vmxon = true;
8268 return 0;
8269
8270out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008271 kfree(vmx->nested.cached_shadow_vmcs12);
8272
8273out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008274 kfree(vmx->nested.cached_vmcs12);
8275
8276out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008277 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008278
Jim Mattsonde3a0022017-11-27 17:22:25 -06008279out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008280 return -ENOMEM;
8281}
8282
Bandan Das3573e222014-05-06 02:19:16 -04008283/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008284 * Emulate the VMXON instruction.
8285 * Currently, we just remember that VMX is active, and do not save or even
8286 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8287 * do not currently need to store anything in that guest-allocated memory
8288 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8289 * argument is different from the VMXON pointer (which the spec says they do).
8290 */
8291static int handle_vmon(struct kvm_vcpu *vcpu)
8292{
Jim Mattsone29acc52016-11-30 12:03:43 -08008293 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008294 gpa_t vmptr;
8295 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008296 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008297 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8298 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008299
Jim Mattson70f3aac2017-04-26 08:53:46 -07008300 /*
8301 * The Intel VMX Instruction Reference lists a bunch of bits that are
8302 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8303 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8304 * Otherwise, we should fail with #UD. But most faulting conditions
8305 * have already been checked by hardware, prior to the VM-exit for
8306 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8307 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008308 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008309 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008310 kvm_queue_exception(vcpu, UD_VECTOR);
8311 return 1;
8312 }
8313
Felix Wilhelm727ba742018-06-11 09:43:44 +02008314 /* CPL=0 must be checked manually. */
8315 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008316 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008317 return 1;
8318 }
8319
Abel Gordon145c28d2013-04-18 14:36:55 +03008320 if (vmx->nested.vmxon) {
8321 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008322 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008323 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008324
Haozhong Zhang3b840802016-06-22 14:59:54 +08008325 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008326 != VMXON_NEEDED_FEATURES) {
8327 kvm_inject_gp(vcpu, 0);
8328 return 1;
8329 }
8330
Radim Krčmářcbf71272017-05-19 15:48:51 +02008331 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008332 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008333
8334 /*
8335 * SDM 3: 24.11.5
8336 * The first 4 bytes of VMXON region contain the supported
8337 * VMCS revision identifier
8338 *
8339 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8340 * which replaces physical address width with 32
8341 */
8342 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8343 nested_vmx_failInvalid(vcpu);
8344 return kvm_skip_emulated_instruction(vcpu);
8345 }
8346
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008347 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8348 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008349 nested_vmx_failInvalid(vcpu);
8350 return kvm_skip_emulated_instruction(vcpu);
8351 }
8352 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8353 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008354 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008355 nested_vmx_failInvalid(vcpu);
8356 return kvm_skip_emulated_instruction(vcpu);
8357 }
8358 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008359 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008360
8361 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008362 ret = enter_vmx_operation(vcpu);
8363 if (ret)
8364 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008365
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008366 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008367 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008368}
8369
8370/*
8371 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8372 * for running VMX instructions (except VMXON, whose prerequisites are
8373 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008374 * Note that many of these exceptions have priority over VM exits, so they
8375 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008376 */
8377static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8378{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008379 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008380 kvm_queue_exception(vcpu, UD_VECTOR);
8381 return 0;
8382 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008383
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008384 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008385 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008386 return 0;
8387 }
8388
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008389 return 1;
8390}
8391
David Matlack8ca44e82017-08-01 14:00:39 -07008392static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8393{
8394 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8395 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8396}
8397
Abel Gordone7953d72013-04-18 14:37:55 +03008398static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8399{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008400 if (vmx->nested.current_vmptr == -1ull)
8401 return;
8402
Abel Gordon012f83c2013-04-18 14:39:25 +03008403 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008404 /* copy to memory all shadowed fields in case
8405 they were modified */
8406 copy_shadow_to_vmcs12(vmx);
8407 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008408 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008409 }
Wincy Van705699a2015-02-03 23:58:17 +08008410 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008411
8412 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008413 kvm_vcpu_write_guest_page(&vmx->vcpu,
8414 vmx->nested.current_vmptr >> PAGE_SHIFT,
8415 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008416
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008417 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008418}
8419
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008420/*
8421 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8422 * just stops using VMX.
8423 */
8424static void free_nested(struct vcpu_vmx *vmx)
8425{
Wanpeng Lib7455822017-11-22 14:04:00 -08008426 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008427 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008428
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008429 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008430 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008431 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008432 vmx->nested.posted_intr_nv = -1;
8433 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008434 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008435 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008436 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8437 free_vmcs(vmx->vmcs01.shadow_vmcs);
8438 vmx->vmcs01.shadow_vmcs = NULL;
8439 }
David Matlack4f2777b2016-07-13 17:16:37 -07008440 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008441 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008442 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008443 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008444 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008445 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008446 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008447 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008448 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008449 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008450 }
Wincy Van705699a2015-02-03 23:58:17 +08008451 if (vmx->nested.pi_desc_page) {
8452 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008453 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008454 vmx->nested.pi_desc_page = NULL;
8455 vmx->nested.pi_desc = NULL;
8456 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008457
Jim Mattsonde3a0022017-11-27 17:22:25 -06008458 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008459}
8460
8461/* Emulate the VMXOFF instruction */
8462static int handle_vmoff(struct kvm_vcpu *vcpu)
8463{
8464 if (!nested_vmx_check_permission(vcpu))
8465 return 1;
8466 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008467 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008468 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008469}
8470
Nadav Har'El27d6c862011-05-25 23:06:59 +03008471/* Emulate the VMCLEAR instruction */
8472static int handle_vmclear(struct kvm_vcpu *vcpu)
8473{
8474 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008475 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008476 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008477
8478 if (!nested_vmx_check_permission(vcpu))
8479 return 1;
8480
Radim Krčmářcbf71272017-05-19 15:48:51 +02008481 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008482 return 1;
8483
Radim Krčmářcbf71272017-05-19 15:48:51 +02008484 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8485 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8486 return kvm_skip_emulated_instruction(vcpu);
8487 }
8488
8489 if (vmptr == vmx->nested.vmxon_ptr) {
8490 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8491 return kvm_skip_emulated_instruction(vcpu);
8492 }
8493
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008494 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008495 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008496
Jim Mattson587d7e722017-03-02 12:41:48 -08008497 kvm_vcpu_write_guest(vcpu,
8498 vmptr + offsetof(struct vmcs12, launch_state),
8499 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008500
Nadav Har'El27d6c862011-05-25 23:06:59 +03008501 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008502 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008503}
8504
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008505static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8506
8507/* Emulate the VMLAUNCH instruction */
8508static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8509{
8510 return nested_vmx_run(vcpu, true);
8511}
8512
8513/* Emulate the VMRESUME instruction */
8514static int handle_vmresume(struct kvm_vcpu *vcpu)
8515{
8516
8517 return nested_vmx_run(vcpu, false);
8518}
8519
Nadav Har'El49f705c2011-05-25 23:08:30 +03008520/*
8521 * Read a vmcs12 field. Since these can have varying lengths and we return
8522 * one type, we chose the biggest type (u64) and zero-extend the return value
8523 * to that size. Note that the caller, handle_vmread, might need to use only
8524 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8525 * 64-bit fields are to be returned).
8526 */
Liran Alone2536742018-06-23 02:35:02 +03008527static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008528 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008529{
8530 short offset = vmcs_field_to_offset(field);
8531 char *p;
8532
8533 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008534 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008535
Liran Alone2536742018-06-23 02:35:02 +03008536 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008537
Jim Mattsond37f4262017-12-22 12:12:16 -08008538 switch (vmcs_field_width(field)) {
8539 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008540 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008541 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008542 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008544 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008545 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008546 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008547 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008548 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008549 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008550 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008552 WARN_ON(1);
8553 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008554 }
8555}
8556
Abel Gordon20b97fe2013-04-18 14:36:25 +03008557
Liran Alone2536742018-06-23 02:35:02 +03008558static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008559 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008560 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008561 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008562 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008563 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008564
Jim Mattsond37f4262017-12-22 12:12:16 -08008565 switch (vmcs_field_width(field)) {
8566 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008567 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008569 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008570 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008571 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008572 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008573 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008575 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008576 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008579 WARN_ON(1);
8580 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008581 }
8582
8583}
8584
Jim Mattsonf4160e42018-05-29 09:11:33 -07008585/*
8586 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8587 * they have been modified by the L1 guest. Note that the "read-only"
8588 * VM-exit information fields are actually writable if the vCPU is
8589 * configured to support "VMWRITE to any supported field in the VMCS."
8590 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008591static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8592{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008593 const u16 *fields[] = {
8594 shadow_read_write_fields,
8595 shadow_read_only_fields
8596 };
8597 const int max_fields[] = {
8598 max_shadow_read_write_fields,
8599 max_shadow_read_only_fields
8600 };
8601 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008602 unsigned long field;
8603 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008604 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008605
Jan Kiszka282da872014-10-08 18:05:39 +02008606 preempt_disable();
8607
Abel Gordon16f5b902013-04-18 14:38:25 +03008608 vmcs_load(shadow_vmcs);
8609
Jim Mattsonf4160e42018-05-29 09:11:33 -07008610 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8611 for (i = 0; i < max_fields[q]; i++) {
8612 field = fields[q][i];
8613 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008614 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008615 }
8616 /*
8617 * Skip the VM-exit information fields if they are read-only.
8618 */
8619 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8620 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008621 }
8622
8623 vmcs_clear(shadow_vmcs);
8624 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008625
8626 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008627}
8628
Abel Gordonc3114422013-04-18 14:38:55 +03008629static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8630{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008631 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008632 shadow_read_write_fields,
8633 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008634 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008635 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008636 max_shadow_read_write_fields,
8637 max_shadow_read_only_fields
8638 };
8639 int i, q;
8640 unsigned long field;
8641 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008642 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008643
8644 vmcs_load(shadow_vmcs);
8645
Mathias Krausec2bae892013-06-26 20:36:21 +02008646 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008647 for (i = 0; i < max_fields[q]; i++) {
8648 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008649 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008650 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008651 }
8652 }
8653
8654 vmcs_clear(shadow_vmcs);
8655 vmcs_load(vmx->loaded_vmcs->vmcs);
8656}
8657
Nadav Har'El49f705c2011-05-25 23:08:30 +03008658/*
8659 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8660 * used before) all generate the same failure when it is missing.
8661 */
8662static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8663{
8664 struct vcpu_vmx *vmx = to_vmx(vcpu);
8665 if (vmx->nested.current_vmptr == -1ull) {
8666 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008667 return 0;
8668 }
8669 return 1;
8670}
8671
8672static int handle_vmread(struct kvm_vcpu *vcpu)
8673{
8674 unsigned long field;
8675 u64 field_value;
8676 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8677 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8678 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008679 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008680
Kyle Hueyeb277562016-11-29 12:40:39 -08008681 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008682 return 1;
8683
Kyle Huey6affcbe2016-11-29 12:40:40 -08008684 if (!nested_vmx_check_vmcs12(vcpu))
8685 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008686
Liran Alon6d894f42018-06-23 02:35:09 +03008687 if (!is_guest_mode(vcpu))
8688 vmcs12 = get_vmcs12(vcpu);
8689 else {
8690 /*
8691 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8692 * to shadowed-field sets the ALU flags for VMfailInvalid.
8693 */
8694 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8695 nested_vmx_failInvalid(vcpu);
8696 return kvm_skip_emulated_instruction(vcpu);
8697 }
8698 vmcs12 = get_shadow_vmcs12(vcpu);
8699 }
8700
Nadav Har'El49f705c2011-05-25 23:08:30 +03008701 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008702 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008703 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008704 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008705 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008706 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008707 }
8708 /*
8709 * Now copy part of this value to register or memory, as requested.
8710 * Note that the number of bits actually copied is 32 or 64 depending
8711 * on the guest's mode (32 or 64 bit), not on the given field's length.
8712 */
8713 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008714 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008715 field_value);
8716 } else {
8717 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008718 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008720 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008721 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8722 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 }
8724
8725 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008726 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008727}
8728
8729
8730static int handle_vmwrite(struct kvm_vcpu *vcpu)
8731{
8732 unsigned long field;
8733 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008734 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008735 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8736 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008737
Nadav Har'El49f705c2011-05-25 23:08:30 +03008738 /* The value to write might be 32 or 64 bits, depending on L1's long
8739 * mode, and eventually we need to write that into a field of several
8740 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008741 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008742 * bits into the vmcs12 field.
8743 */
8744 u64 field_value = 0;
8745 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008746 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008747
Kyle Hueyeb277562016-11-29 12:40:39 -08008748 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749 return 1;
8750
Kyle Huey6affcbe2016-11-29 12:40:40 -08008751 if (!nested_vmx_check_vmcs12(vcpu))
8752 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008753
Nadav Har'El49f705c2011-05-25 23:08:30 +03008754 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008755 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 (((vmx_instruction_info) >> 3) & 0xf));
8757 else {
8758 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008759 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008760 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008761 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8762 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763 kvm_inject_page_fault(vcpu, &e);
8764 return 1;
8765 }
8766 }
8767
8768
Nadav Amit27e6fb52014-06-18 17:19:26 +03008769 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008770 /*
8771 * If the vCPU supports "VMWRITE to any supported field in the
8772 * VMCS," then the "read-only" fields are actually read/write.
8773 */
8774 if (vmcs_field_readonly(field) &&
8775 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008776 nested_vmx_failValid(vcpu,
8777 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008778 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008779 }
8780
Liran Alon6d894f42018-06-23 02:35:09 +03008781 if (!is_guest_mode(vcpu))
8782 vmcs12 = get_vmcs12(vcpu);
8783 else {
8784 /*
8785 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8786 * to shadowed-field sets the ALU flags for VMfailInvalid.
8787 */
8788 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8789 nested_vmx_failInvalid(vcpu);
8790 return kvm_skip_emulated_instruction(vcpu);
8791 }
8792 vmcs12 = get_shadow_vmcs12(vcpu);
8793
8794 }
8795
8796 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008797 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008798 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008799 }
8800
Liran Alon6d894f42018-06-23 02:35:09 +03008801 /*
8802 * Do not track vmcs12 dirty-state if in guest-mode
8803 * as we actually dirty shadow vmcs12 instead of vmcs12.
8804 */
8805 if (!is_guest_mode(vcpu)) {
8806 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008807#define SHADOW_FIELD_RW(x) case x:
8808#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008809 /*
8810 * The fields that can be updated by L1 without a vmexit are
8811 * always updated in the vmcs02, the others go down the slow
8812 * path of prepare_vmcs02.
8813 */
8814 break;
8815 default:
8816 vmx->nested.dirty_vmcs12 = true;
8817 break;
8818 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008819 }
8820
Nadav Har'El49f705c2011-05-25 23:08:30 +03008821 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008822 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008823}
8824
Jim Mattsona8bc2842016-11-30 12:03:44 -08008825static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8826{
8827 vmx->nested.current_vmptr = vmptr;
8828 if (enable_shadow_vmcs) {
8829 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8830 SECONDARY_EXEC_SHADOW_VMCS);
8831 vmcs_write64(VMCS_LINK_POINTER,
8832 __pa(vmx->vmcs01.shadow_vmcs));
8833 vmx->nested.sync_shadow_vmcs = true;
8834 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008835 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008836}
8837
Nadav Har'El63846662011-05-25 23:07:29 +03008838/* Emulate the VMPTRLD instruction */
8839static int handle_vmptrld(struct kvm_vcpu *vcpu)
8840{
8841 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008842 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008843
8844 if (!nested_vmx_check_permission(vcpu))
8845 return 1;
8846
Radim Krčmářcbf71272017-05-19 15:48:51 +02008847 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008848 return 1;
8849
Radim Krčmářcbf71272017-05-19 15:48:51 +02008850 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8851 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8852 return kvm_skip_emulated_instruction(vcpu);
8853 }
8854
8855 if (vmptr == vmx->nested.vmxon_ptr) {
8856 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8857 return kvm_skip_emulated_instruction(vcpu);
8858 }
8859
Nadav Har'El63846662011-05-25 23:07:29 +03008860 if (vmx->nested.current_vmptr != vmptr) {
8861 struct vmcs12 *new_vmcs12;
8862 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008863 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8864 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008865 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008866 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008867 }
8868 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008869 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008870 (new_vmcs12->hdr.shadow_vmcs &&
8871 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008872 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008873 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008874 nested_vmx_failValid(vcpu,
8875 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008876 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008877 }
Nadav Har'El63846662011-05-25 23:07:29 +03008878
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008879 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008880 /*
8881 * Load VMCS12 from guest memory since it is not already
8882 * cached.
8883 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008884 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8885 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008886 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008887
Jim Mattsona8bc2842016-11-30 12:03:44 -08008888 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008889 }
8890
8891 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008892 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008893}
8894
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008895/* Emulate the VMPTRST instruction */
8896static int handle_vmptrst(struct kvm_vcpu *vcpu)
8897{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008898 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8899 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8900 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008901 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008902 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008903
8904 if (!nested_vmx_check_permission(vcpu))
8905 return 1;
8906
Sean Christopherson0a06d422018-07-19 10:31:00 -07008907 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008908 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008909 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008910 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8911 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008912 kvm_inject_page_fault(vcpu, &e);
8913 return 1;
8914 }
8915 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008916 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917}
8918
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008919/* Emulate the INVEPT instruction */
8920static int handle_invept(struct kvm_vcpu *vcpu)
8921{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008922 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008923 u32 vmx_instruction_info, types;
8924 unsigned long type;
8925 gva_t gva;
8926 struct x86_exception e;
8927 struct {
8928 u64 eptp, gpa;
8929 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008930
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008931 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008932 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008933 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008934 kvm_queue_exception(vcpu, UD_VECTOR);
8935 return 1;
8936 }
8937
8938 if (!nested_vmx_check_permission(vcpu))
8939 return 1;
8940
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008941 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008942 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008943
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008944 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008945
Jim Mattson85c856b2016-10-26 08:38:38 -07008946 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008947 nested_vmx_failValid(vcpu,
8948 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008949 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008950 }
8951
8952 /* According to the Intel VMX instruction reference, the memory
8953 * operand is read even if it isn't needed (e.g., for type==global)
8954 */
8955 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008956 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008958 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959 kvm_inject_page_fault(vcpu, &e);
8960 return 1;
8961 }
8962
8963 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008965 /*
8966 * TODO: track mappings and invalidate
8967 * single context requests appropriately
8968 */
8969 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008970 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008971 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008972 nested_vmx_succeed(vcpu);
8973 break;
8974 default:
8975 BUG_ON(1);
8976 break;
8977 }
8978
Kyle Huey6affcbe2016-11-29 12:40:40 -08008979 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008980}
8981
Petr Matouseka642fc32014-09-23 20:22:30 +02008982static int handle_invvpid(struct kvm_vcpu *vcpu)
8983{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008984 struct vcpu_vmx *vmx = to_vmx(vcpu);
8985 u32 vmx_instruction_info;
8986 unsigned long type, types;
8987 gva_t gva;
8988 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07008989 struct {
8990 u64 vpid;
8991 u64 gla;
8992 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008993
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008994 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008995 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008996 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008997 kvm_queue_exception(vcpu, UD_VECTOR);
8998 return 1;
8999 }
9000
9001 if (!nested_vmx_check_permission(vcpu))
9002 return 1;
9003
9004 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9005 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9006
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009007 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009008 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009009
Jim Mattson85c856b2016-10-26 08:38:38 -07009010 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009011 nested_vmx_failValid(vcpu,
9012 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009013 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009014 }
9015
9016 /* according to the intel vmx instruction reference, the memory
9017 * operand is read even if it isn't needed (e.g., for type==global)
9018 */
9019 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9020 vmx_instruction_info, false, &gva))
9021 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009022 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009023 kvm_inject_page_fault(vcpu, &e);
9024 return 1;
9025 }
Jim Mattson40352602017-06-28 09:37:37 -07009026 if (operand.vpid >> 16) {
9027 nested_vmx_failValid(vcpu,
9028 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9029 return kvm_skip_emulated_instruction(vcpu);
9030 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009031
9032 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009033 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009034 if (!operand.vpid ||
9035 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009036 nested_vmx_failValid(vcpu,
9037 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9038 return kvm_skip_emulated_instruction(vcpu);
9039 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009040 if (cpu_has_vmx_invvpid_individual_addr() &&
9041 vmx->nested.vpid02) {
9042 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9043 vmx->nested.vpid02, operand.gla);
9044 } else
9045 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9046 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009047 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009048 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009049 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009050 nested_vmx_failValid(vcpu,
9051 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009052 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009053 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009054 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009055 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009056 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03009057 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009058 break;
9059 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009060 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009061 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009062 }
9063
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009064 nested_vmx_succeed(vcpu);
9065
Kyle Huey6affcbe2016-11-29 12:40:40 -08009066 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009067}
9068
Junaid Shahideb4b2482018-06-27 14:59:14 -07009069static int handle_invpcid(struct kvm_vcpu *vcpu)
9070{
9071 u32 vmx_instruction_info;
9072 unsigned long type;
9073 bool pcid_enabled;
9074 gva_t gva;
9075 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009076 unsigned i;
9077 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009078 struct {
9079 u64 pcid;
9080 u64 gla;
9081 } operand;
9082
9083 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9084 kvm_queue_exception(vcpu, UD_VECTOR);
9085 return 1;
9086 }
9087
9088 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9089 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9090
9091 if (type > 3) {
9092 kvm_inject_gp(vcpu, 0);
9093 return 1;
9094 }
9095
9096 /* According to the Intel instruction reference, the memory operand
9097 * is read even if it isn't needed (e.g., for type==all)
9098 */
9099 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9100 vmx_instruction_info, false, &gva))
9101 return 1;
9102
9103 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9104 kvm_inject_page_fault(vcpu, &e);
9105 return 1;
9106 }
9107
9108 if (operand.pcid >> 12 != 0) {
9109 kvm_inject_gp(vcpu, 0);
9110 return 1;
9111 }
9112
9113 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9114
9115 switch (type) {
9116 case INVPCID_TYPE_INDIV_ADDR:
9117 if ((!pcid_enabled && (operand.pcid != 0)) ||
9118 is_noncanonical_address(operand.gla, vcpu)) {
9119 kvm_inject_gp(vcpu, 0);
9120 return 1;
9121 }
9122 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9123 return kvm_skip_emulated_instruction(vcpu);
9124
9125 case INVPCID_TYPE_SINGLE_CTXT:
9126 if (!pcid_enabled && (operand.pcid != 0)) {
9127 kvm_inject_gp(vcpu, 0);
9128 return 1;
9129 }
9130
9131 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9132 kvm_mmu_sync_roots(vcpu);
9133 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9134 }
9135
Junaid Shahidb94742c2018-06-27 14:59:20 -07009136 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9137 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9138 == operand.pcid)
9139 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009140
Junaid Shahidb94742c2018-06-27 14:59:20 -07009141 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009142 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009143 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009144 * given PCID, then nothing needs to be done here because a
9145 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009146 */
9147
9148 return kvm_skip_emulated_instruction(vcpu);
9149
9150 case INVPCID_TYPE_ALL_NON_GLOBAL:
9151 /*
9152 * Currently, KVM doesn't mark global entries in the shadow
9153 * page tables, so a non-global flush just degenerates to a
9154 * global flush. If needed, we could optimize this later by
9155 * keeping track of global entries in shadow page tables.
9156 */
9157
9158 /* fall-through */
9159 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9160 kvm_mmu_unload(vcpu);
9161 return kvm_skip_emulated_instruction(vcpu);
9162
9163 default:
9164 BUG(); /* We have already checked above that type <= 3 */
9165 }
9166}
9167
Kai Huang843e4332015-01-28 10:54:28 +08009168static int handle_pml_full(struct kvm_vcpu *vcpu)
9169{
9170 unsigned long exit_qualification;
9171
9172 trace_kvm_pml_full(vcpu->vcpu_id);
9173
9174 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9175
9176 /*
9177 * PML buffer FULL happened while executing iret from NMI,
9178 * "blocked by NMI" bit has to be set before next VM entry.
9179 */
9180 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009181 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009182 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9183 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9184 GUEST_INTR_STATE_NMI);
9185
9186 /*
9187 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9188 * here.., and there's no userspace involvement needed for PML.
9189 */
9190 return 1;
9191}
9192
Yunhong Jiang64672c92016-06-13 14:19:59 -07009193static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9194{
9195 kvm_lapic_expired_hv_timer(vcpu);
9196 return 1;
9197}
9198
Bandan Das41ab9372017-08-03 15:54:43 -04009199static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9200{
9201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009202 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9203
9204 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009205 switch (address & VMX_EPTP_MT_MASK) {
9206 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009207 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009208 return false;
9209 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009210 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009211 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009212 return false;
9213 break;
9214 default:
9215 return false;
9216 }
9217
David Hildenbrandbb97a012017-08-10 23:15:28 +02009218 /* only 4 levels page-walk length are valid */
9219 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009220 return false;
9221
9222 /* Reserved bits should not be set */
9223 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9224 return false;
9225
9226 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009227 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009228 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009229 return false;
9230 }
9231
9232 return true;
9233}
9234
9235static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9236 struct vmcs12 *vmcs12)
9237{
9238 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9239 u64 address;
9240 bool accessed_dirty;
9241 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9242
9243 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9244 !nested_cpu_has_ept(vmcs12))
9245 return 1;
9246
9247 if (index >= VMFUNC_EPTP_ENTRIES)
9248 return 1;
9249
9250
9251 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9252 &address, index * 8, 8))
9253 return 1;
9254
David Hildenbrandbb97a012017-08-10 23:15:28 +02009255 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009256
9257 /*
9258 * If the (L2) guest does a vmfunc to the currently
9259 * active ept pointer, we don't have to do anything else
9260 */
9261 if (vmcs12->ept_pointer != address) {
9262 if (!valid_ept_address(vcpu, address))
9263 return 1;
9264
9265 kvm_mmu_unload(vcpu);
9266 mmu->ept_ad = accessed_dirty;
9267 mmu->base_role.ad_disabled = !accessed_dirty;
9268 vmcs12->ept_pointer = address;
9269 /*
9270 * TODO: Check what's the correct approach in case
9271 * mmu reload fails. Currently, we just let the next
9272 * reload potentially fail
9273 */
9274 kvm_mmu_reload(vcpu);
9275 }
9276
9277 return 0;
9278}
9279
Bandan Das2a499e42017-08-03 15:54:41 -04009280static int handle_vmfunc(struct kvm_vcpu *vcpu)
9281{
Bandan Das27c42a12017-08-03 15:54:42 -04009282 struct vcpu_vmx *vmx = to_vmx(vcpu);
9283 struct vmcs12 *vmcs12;
9284 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9285
9286 /*
9287 * VMFUNC is only supported for nested guests, but we always enable the
9288 * secondary control for simplicity; for non-nested mode, fake that we
9289 * didn't by injecting #UD.
9290 */
9291 if (!is_guest_mode(vcpu)) {
9292 kvm_queue_exception(vcpu, UD_VECTOR);
9293 return 1;
9294 }
9295
9296 vmcs12 = get_vmcs12(vcpu);
9297 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9298 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009299
9300 switch (function) {
9301 case 0:
9302 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9303 goto fail;
9304 break;
9305 default:
9306 goto fail;
9307 }
9308 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009309
9310fail:
9311 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9312 vmcs_read32(VM_EXIT_INTR_INFO),
9313 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009314 return 1;
9315}
9316
Nadav Har'El0140cae2011-05-25 23:06:28 +03009317/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009318 * The exit handlers return 1 if the exit was handled fully and guest execution
9319 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9320 * to be done to userspace and return 0.
9321 */
Mathias Krause772e0312012-08-30 01:30:19 +02009322static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009323 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9324 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009325 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009326 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009327 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009328 [EXIT_REASON_CR_ACCESS] = handle_cr,
9329 [EXIT_REASON_DR_ACCESS] = handle_dr,
9330 [EXIT_REASON_CPUID] = handle_cpuid,
9331 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9332 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9333 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9334 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009335 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009336 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009337 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009338 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009339 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009340 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009341 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009342 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009343 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009344 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009345 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009346 [EXIT_REASON_VMOFF] = handle_vmoff,
9347 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009348 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9349 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009350 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009351 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009352 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009353 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009354 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009355 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009356 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9357 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009358 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9359 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009360 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009361 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009362 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009363 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009364 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009365 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009366 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009367 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009368 [EXIT_REASON_XSAVES] = handle_xsaves,
9369 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009370 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009371 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009372 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009373 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009374};
9375
9376static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009377 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009378
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009379static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9380 struct vmcs12 *vmcs12)
9381{
9382 unsigned long exit_qualification;
9383 gpa_t bitmap, last_bitmap;
9384 unsigned int port;
9385 int size;
9386 u8 b;
9387
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009388 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009389 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009390
9391 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9392
9393 port = exit_qualification >> 16;
9394 size = (exit_qualification & 7) + 1;
9395
9396 last_bitmap = (gpa_t)-1;
9397 b = -1;
9398
9399 while (size > 0) {
9400 if (port < 0x8000)
9401 bitmap = vmcs12->io_bitmap_a;
9402 else if (port < 0x10000)
9403 bitmap = vmcs12->io_bitmap_b;
9404 else
Joe Perches1d804d02015-03-30 16:46:09 -07009405 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009406 bitmap += (port & 0x7fff) / 8;
9407
9408 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009409 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009410 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009411 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009412 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009413
9414 port++;
9415 size--;
9416 last_bitmap = bitmap;
9417 }
9418
Joe Perches1d804d02015-03-30 16:46:09 -07009419 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009420}
9421
Nadav Har'El644d7112011-05-25 23:12:35 +03009422/*
9423 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9424 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9425 * disinterest in the current event (read or write a specific MSR) by using an
9426 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9427 */
9428static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9429 struct vmcs12 *vmcs12, u32 exit_reason)
9430{
9431 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9432 gpa_t bitmap;
9433
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009434 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009435 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009436
9437 /*
9438 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9439 * for the four combinations of read/write and low/high MSR numbers.
9440 * First we need to figure out which of the four to use:
9441 */
9442 bitmap = vmcs12->msr_bitmap;
9443 if (exit_reason == EXIT_REASON_MSR_WRITE)
9444 bitmap += 2048;
9445 if (msr_index >= 0xc0000000) {
9446 msr_index -= 0xc0000000;
9447 bitmap += 1024;
9448 }
9449
9450 /* Then read the msr_index'th bit from this bitmap: */
9451 if (msr_index < 1024*8) {
9452 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009453 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009454 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009455 return 1 & (b >> (msr_index & 7));
9456 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009457 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009458}
9459
9460/*
9461 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9462 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9463 * intercept (via guest_host_mask etc.) the current event.
9464 */
9465static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9466 struct vmcs12 *vmcs12)
9467{
9468 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9469 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009470 int reg;
9471 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009472
9473 switch ((exit_qualification >> 4) & 3) {
9474 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009475 reg = (exit_qualification >> 8) & 15;
9476 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009477 switch (cr) {
9478 case 0:
9479 if (vmcs12->cr0_guest_host_mask &
9480 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009481 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009482 break;
9483 case 3:
9484 if ((vmcs12->cr3_target_count >= 1 &&
9485 vmcs12->cr3_target_value0 == val) ||
9486 (vmcs12->cr3_target_count >= 2 &&
9487 vmcs12->cr3_target_value1 == val) ||
9488 (vmcs12->cr3_target_count >= 3 &&
9489 vmcs12->cr3_target_value2 == val) ||
9490 (vmcs12->cr3_target_count >= 4 &&
9491 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009492 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009493 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009494 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009495 break;
9496 case 4:
9497 if (vmcs12->cr4_guest_host_mask &
9498 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009499 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009500 break;
9501 case 8:
9502 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009503 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009504 break;
9505 }
9506 break;
9507 case 2: /* clts */
9508 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9509 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009510 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009511 break;
9512 case 1: /* mov from cr */
9513 switch (cr) {
9514 case 3:
9515 if (vmcs12->cpu_based_vm_exec_control &
9516 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009517 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009518 break;
9519 case 8:
9520 if (vmcs12->cpu_based_vm_exec_control &
9521 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009522 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009523 break;
9524 }
9525 break;
9526 case 3: /* lmsw */
9527 /*
9528 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9529 * cr0. Other attempted changes are ignored, with no exit.
9530 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009531 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009532 if (vmcs12->cr0_guest_host_mask & 0xe &
9533 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009534 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009535 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9536 !(vmcs12->cr0_read_shadow & 0x1) &&
9537 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009538 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009539 break;
9540 }
Joe Perches1d804d02015-03-30 16:46:09 -07009541 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009542}
9543
Liran Alona7cde482018-06-23 02:35:10 +03009544static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9545 struct vmcs12 *vmcs12, gpa_t bitmap)
9546{
9547 u32 vmx_instruction_info;
9548 unsigned long field;
9549 u8 b;
9550
9551 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9552 return true;
9553
9554 /* Decode instruction info and find the field to access */
9555 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9556 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9557
9558 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9559 if (field >> 15)
9560 return true;
9561
9562 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9563 return true;
9564
9565 return 1 & (b >> (field & 7));
9566}
9567
Nadav Har'El644d7112011-05-25 23:12:35 +03009568/*
9569 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9570 * should handle it ourselves in L0 (and then continue L2). Only call this
9571 * when in is_guest_mode (L2).
9572 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009573static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009574{
Nadav Har'El644d7112011-05-25 23:12:35 +03009575 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9576 struct vcpu_vmx *vmx = to_vmx(vcpu);
9577 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9578
Jim Mattson4f350c62017-09-14 16:31:44 -07009579 if (vmx->nested.nested_run_pending)
9580 return false;
9581
9582 if (unlikely(vmx->fail)) {
9583 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9584 vmcs_read32(VM_INSTRUCTION_ERROR));
9585 return true;
9586 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009587
David Matlackc9f04402017-08-01 14:00:40 -07009588 /*
9589 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009590 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9591 * Page). The CPU may write to these pages via their host
9592 * physical address while L2 is running, bypassing any
9593 * address-translation-based dirty tracking (e.g. EPT write
9594 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009595 *
9596 * Mark them dirty on every exit from L2 to prevent them from
9597 * getting out of sync with dirty tracking.
9598 */
9599 nested_mark_vmcs12_pages_dirty(vcpu);
9600
Jim Mattson4f350c62017-09-14 16:31:44 -07009601 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9602 vmcs_readl(EXIT_QUALIFICATION),
9603 vmx->idt_vectoring_info,
9604 intr_info,
9605 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9606 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009607
9608 switch (exit_reason) {
9609 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009610 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009611 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009612 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009613 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009614 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009615 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009616 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009617 else if (is_debug(intr_info) &&
9618 vcpu->guest_debug &
9619 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9620 return false;
9621 else if (is_breakpoint(intr_info) &&
9622 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9623 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009624 return vmcs12->exception_bitmap &
9625 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9626 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009627 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009628 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009629 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009630 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009631 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009632 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009633 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009634 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009635 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009636 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009637 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009638 case EXIT_REASON_HLT:
9639 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9640 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009641 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009642 case EXIT_REASON_INVLPG:
9643 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9644 case EXIT_REASON_RDPMC:
9645 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009646 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009647 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009648 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009649 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009650 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009651 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009652 case EXIT_REASON_VMREAD:
9653 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9654 vmcs12->vmread_bitmap);
9655 case EXIT_REASON_VMWRITE:
9656 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9657 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009658 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9659 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009660 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009661 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009662 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009663 /*
9664 * VMX instructions trap unconditionally. This allows L1 to
9665 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9666 */
Joe Perches1d804d02015-03-30 16:46:09 -07009667 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009668 case EXIT_REASON_CR_ACCESS:
9669 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9670 case EXIT_REASON_DR_ACCESS:
9671 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9672 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009673 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009674 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9675 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009676 case EXIT_REASON_MSR_READ:
9677 case EXIT_REASON_MSR_WRITE:
9678 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9679 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009680 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009681 case EXIT_REASON_MWAIT_INSTRUCTION:
9682 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009683 case EXIT_REASON_MONITOR_TRAP_FLAG:
9684 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009685 case EXIT_REASON_MONITOR_INSTRUCTION:
9686 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9687 case EXIT_REASON_PAUSE_INSTRUCTION:
9688 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9689 nested_cpu_has2(vmcs12,
9690 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9691 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009692 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009693 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009694 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009695 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009696 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009697 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009698 /*
9699 * The controls for "virtualize APIC accesses," "APIC-
9700 * register virtualization," and "virtual-interrupt
9701 * delivery" only come from vmcs12.
9702 */
Joe Perches1d804d02015-03-30 16:46:09 -07009703 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009704 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009705 /*
9706 * L0 always deals with the EPT violation. If nested EPT is
9707 * used, and the nested mmu code discovers that the address is
9708 * missing in the guest EPT table (EPT12), the EPT violation
9709 * will be injected with nested_ept_inject_page_fault()
9710 */
Joe Perches1d804d02015-03-30 16:46:09 -07009711 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009712 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009713 /*
9714 * L2 never uses directly L1's EPT, but rather L0's own EPT
9715 * table (shadow on EPT) or a merged EPT table that L0 built
9716 * (EPT on EPT). So any problems with the structure of the
9717 * table is L0's fault.
9718 */
Joe Perches1d804d02015-03-30 16:46:09 -07009719 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009720 case EXIT_REASON_INVPCID:
9721 return
9722 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9723 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009724 case EXIT_REASON_WBINVD:
9725 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9726 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009727 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009728 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9729 /*
9730 * This should never happen, since it is not possible to
9731 * set XSS to a non-zero value---neither in L1 nor in L2.
9732 * If if it were, XSS would have to be checked against
9733 * the XSS exit bitmap in vmcs12.
9734 */
9735 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009736 case EXIT_REASON_PREEMPTION_TIMER:
9737 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009738 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009739 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009740 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009741 case EXIT_REASON_VMFUNC:
9742 /* VM functions are emulated through L2->L0 vmexits. */
9743 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009744 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009745 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009746 }
9747}
9748
Paolo Bonzini7313c692017-07-27 10:31:25 +02009749static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9750{
9751 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9752
9753 /*
9754 * At this point, the exit interruption info in exit_intr_info
9755 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9756 * we need to query the in-kernel LAPIC.
9757 */
9758 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9759 if ((exit_intr_info &
9760 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9761 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9762 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9763 vmcs12->vm_exit_intr_error_code =
9764 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9765 }
9766
9767 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9768 vmcs_readl(EXIT_QUALIFICATION));
9769 return 1;
9770}
9771
Avi Kivity586f9602010-11-18 13:09:54 +02009772static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9773{
9774 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9775 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9776}
9777
Kai Huanga3eaa862015-11-04 13:46:05 +08009778static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009779{
Kai Huanga3eaa862015-11-04 13:46:05 +08009780 if (vmx->pml_pg) {
9781 __free_page(vmx->pml_pg);
9782 vmx->pml_pg = NULL;
9783 }
Kai Huang843e4332015-01-28 10:54:28 +08009784}
9785
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009786static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009787{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009788 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009789 u64 *pml_buf;
9790 u16 pml_idx;
9791
9792 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9793
9794 /* Do nothing if PML buffer is empty */
9795 if (pml_idx == (PML_ENTITY_NUM - 1))
9796 return;
9797
9798 /* PML index always points to next available PML buffer entity */
9799 if (pml_idx >= PML_ENTITY_NUM)
9800 pml_idx = 0;
9801 else
9802 pml_idx++;
9803
9804 pml_buf = page_address(vmx->pml_pg);
9805 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9806 u64 gpa;
9807
9808 gpa = pml_buf[pml_idx];
9809 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009810 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009811 }
9812
9813 /* reset PML index */
9814 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9815}
9816
9817/*
9818 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9819 * Called before reporting dirty_bitmap to userspace.
9820 */
9821static void kvm_flush_pml_buffers(struct kvm *kvm)
9822{
9823 int i;
9824 struct kvm_vcpu *vcpu;
9825 /*
9826 * We only need to kick vcpu out of guest mode here, as PML buffer
9827 * is flushed at beginning of all VMEXITs, and it's obvious that only
9828 * vcpus running in guest are possible to have unflushed GPAs in PML
9829 * buffer.
9830 */
9831 kvm_for_each_vcpu(i, vcpu, kvm)
9832 kvm_vcpu_kick(vcpu);
9833}
9834
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009835static void vmx_dump_sel(char *name, uint32_t sel)
9836{
9837 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009838 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009839 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9840 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9841 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9842}
9843
9844static void vmx_dump_dtsel(char *name, uint32_t limit)
9845{
9846 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9847 name, vmcs_read32(limit),
9848 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9849}
9850
9851static void dump_vmcs(void)
9852{
9853 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9854 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9855 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9856 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9857 u32 secondary_exec_control = 0;
9858 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009859 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009860 int i, n;
9861
9862 if (cpu_has_secondary_exec_ctrls())
9863 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9864
9865 pr_err("*** Guest State ***\n");
9866 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9867 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9868 vmcs_readl(CR0_GUEST_HOST_MASK));
9869 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9870 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9871 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9872 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9873 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9874 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009875 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9876 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9877 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9878 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009879 }
9880 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9881 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9882 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9883 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9884 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9885 vmcs_readl(GUEST_SYSENTER_ESP),
9886 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9887 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9888 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9889 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9890 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9891 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9892 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9893 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9894 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9895 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9896 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9897 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9898 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009899 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9900 efer, vmcs_read64(GUEST_IA32_PAT));
9901 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9902 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009903 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009904 if (cpu_has_load_perf_global_ctrl &&
9905 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009906 pr_err("PerfGlobCtl = 0x%016llx\n",
9907 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009908 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009909 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009910 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9911 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9912 vmcs_read32(GUEST_ACTIVITY_STATE));
9913 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9914 pr_err("InterruptStatus = %04x\n",
9915 vmcs_read16(GUEST_INTR_STATUS));
9916
9917 pr_err("*** Host State ***\n");
9918 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9919 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9920 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9921 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9922 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9923 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9924 vmcs_read16(HOST_TR_SELECTOR));
9925 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9926 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9927 vmcs_readl(HOST_TR_BASE));
9928 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9929 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9930 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9931 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9932 vmcs_readl(HOST_CR4));
9933 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9934 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9935 vmcs_read32(HOST_IA32_SYSENTER_CS),
9936 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9937 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009938 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9939 vmcs_read64(HOST_IA32_EFER),
9940 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009941 if (cpu_has_load_perf_global_ctrl &&
9942 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009943 pr_err("PerfGlobCtl = 0x%016llx\n",
9944 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009945
9946 pr_err("*** Control State ***\n");
9947 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9948 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9949 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9950 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9951 vmcs_read32(EXCEPTION_BITMAP),
9952 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9953 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9954 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9955 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9956 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9957 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9958 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9959 vmcs_read32(VM_EXIT_INTR_INFO),
9960 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9961 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9962 pr_err(" reason=%08x qualification=%016lx\n",
9963 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9964 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9965 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9966 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009967 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009968 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009969 pr_err("TSC Multiplier = 0x%016llx\n",
9970 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009971 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
9972 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
9973 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
9974 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
9975 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009976 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009977 n = vmcs_read32(CR3_TARGET_COUNT);
9978 for (i = 0; i + 1 < n; i += 4)
9979 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
9980 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
9981 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
9982 if (i < n)
9983 pr_err("CR3 target%u=%016lx\n",
9984 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
9985 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
9986 pr_err("PLE Gap=%08x Window=%08x\n",
9987 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
9988 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
9989 pr_err("Virtual processor ID = 0x%04x\n",
9990 vmcs_read16(VIRTUAL_PROCESSOR_ID));
9991}
9992
Avi Kivity6aa8b732006-12-10 02:21:36 -08009993/*
9994 * The guest has exited. See if we can fix it or if we need userspace
9995 * assistance.
9996 */
Avi Kivity851ba692009-08-24 11:10:17 +03009997static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009998{
Avi Kivity29bd8a72007-09-10 17:27:03 +03009999 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010000 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010001 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010002
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010003 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10004
Kai Huang843e4332015-01-28 10:54:28 +080010005 /*
10006 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10007 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10008 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10009 * mode as if vcpus is in root mode, the PML buffer must has been
10010 * flushed already.
10011 */
10012 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010013 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010014
Mohammed Gamal80ced182009-09-01 12:48:18 +020010015 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010016 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010017 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010018
Paolo Bonzini7313c692017-07-27 10:31:25 +020010019 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10020 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010021
Mohammed Gamal51207022010-05-31 22:40:54 +030010022 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010023 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010024 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10025 vcpu->run->fail_entry.hardware_entry_failure_reason
10026 = exit_reason;
10027 return 0;
10028 }
10029
Avi Kivity29bd8a72007-09-10 17:27:03 +030010030 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010031 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10032 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010033 = vmcs_read32(VM_INSTRUCTION_ERROR);
10034 return 0;
10035 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010036
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010037 /*
10038 * Note:
10039 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10040 * delivery event since it indicates guest is accessing MMIO.
10041 * The vm-exit can be triggered again after return to guest that
10042 * will cause infinite loop.
10043 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010044 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010045 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010046 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010047 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010048 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10049 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10050 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010051 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010052 vcpu->run->internal.data[0] = vectoring_info;
10053 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010054 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10055 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10056 vcpu->run->internal.ndata++;
10057 vcpu->run->internal.data[3] =
10058 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10059 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010060 return 0;
10061 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010062
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010063 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010064 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10065 if (vmx_interrupt_allowed(vcpu)) {
10066 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10067 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10068 vcpu->arch.nmi_pending) {
10069 /*
10070 * This CPU don't support us in finding the end of an
10071 * NMI-blocked window if the guest runs with IRQs
10072 * disabled. So we pull the trigger after 1 s of
10073 * futile waiting, but inform the user about this.
10074 */
10075 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10076 "state on VCPU %d after 1 s timeout\n",
10077 __func__, vcpu->vcpu_id);
10078 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10079 }
10080 }
10081
Avi Kivity6aa8b732006-12-10 02:21:36 -080010082 if (exit_reason < kvm_vmx_max_exit_handlers
10083 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010084 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010085 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010086 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10087 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010088 kvm_queue_exception(vcpu, UD_VECTOR);
10089 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010090 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010091}
10092
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010093/*
10094 * Software based L1D cache flush which is used when microcode providing
10095 * the cache control MSR is not loaded.
10096 *
10097 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10098 * flush it is required to read in 64 KiB because the replacement algorithm
10099 * is not exactly LRU. This could be sized at runtime via topology
10100 * information but as all relevant affected CPUs have 32KiB L1D cache size
10101 * there is no point in doing so.
10102 */
10103#define L1D_CACHE_ORDER 4
10104static void *vmx_l1d_flush_pages;
10105
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010106static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010107{
10108 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010109
10110 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010111 * This code is only executed when the the flush mode is 'cond' or
10112 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010113 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010114 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010115 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010116
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010117 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010118 * Clear the per-vcpu flush bit, it gets set again
10119 * either from vcpu_run() or from one of the unsafe
10120 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010121 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010122 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010123 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010124
10125 /*
10126 * Clear the per-cpu flush bit, it gets set again from
10127 * the interrupt handlers.
10128 */
10129 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10130 kvm_clear_cpu_l1tf_flush_l1d();
10131
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010132 if (!flush_l1d)
10133 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010134 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010135
10136 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010137
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010138 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10139 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10140 return;
10141 }
10142
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010143 asm volatile(
10144 /* First ensure the pages are in the TLB */
10145 "xorl %%eax, %%eax\n"
10146 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010147 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010148 "addl $4096, %%eax\n\t"
10149 "cmpl %%eax, %[size]\n\t"
10150 "jne .Lpopulate_tlb\n\t"
10151 "xorl %%eax, %%eax\n\t"
10152 "cpuid\n\t"
10153 /* Now fill the cache */
10154 "xorl %%eax, %%eax\n"
10155 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010156 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010157 "addl $64, %%eax\n\t"
10158 "cmpl %%eax, %[size]\n\t"
10159 "jne .Lfill_cache\n\t"
10160 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010161 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010162 [size] "r" (size)
10163 : "eax", "ebx", "ecx", "edx");
10164}
10165
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010166static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010167{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010168 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10169
10170 if (is_guest_mode(vcpu) &&
10171 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10172 return;
10173
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010174 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010175 vmcs_write32(TPR_THRESHOLD, 0);
10176 return;
10177 }
10178
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010179 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010180}
10181
Jim Mattson8d860bb2018-05-09 16:56:05 -040010182static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010183{
10184 u32 sec_exec_control;
10185
Jim Mattson8d860bb2018-05-09 16:56:05 -040010186 if (!lapic_in_kernel(vcpu))
10187 return;
10188
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010189 /* Postpone execution until vmcs01 is the current VMCS. */
10190 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010191 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010192 return;
10193 }
10194
Paolo Bonzini35754c92015-07-29 12:05:37 +020010195 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +080010196 return;
10197
10198 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010199 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10200 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010201
Jim Mattson8d860bb2018-05-09 16:56:05 -040010202 switch (kvm_get_apic_mode(vcpu)) {
10203 case LAPIC_MODE_INVALID:
10204 WARN_ONCE(true, "Invalid local APIC state");
10205 case LAPIC_MODE_DISABLED:
10206 break;
10207 case LAPIC_MODE_XAPIC:
10208 if (flexpriority_enabled) {
10209 sec_exec_control |=
10210 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10211 vmx_flush_tlb(vcpu, true);
10212 }
10213 break;
10214 case LAPIC_MODE_X2APIC:
10215 if (cpu_has_vmx_virtualize_x2apic_mode())
10216 sec_exec_control |=
10217 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10218 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010219 }
10220 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10221
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010222 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010223}
10224
Tang Chen38b99172014-09-24 15:57:54 +080010225static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10226{
Jim Mattsonab5df312018-05-09 17:02:03 -040010227 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010228 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010229 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010230 }
Tang Chen38b99172014-09-24 15:57:54 +080010231}
10232
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010233static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010234{
10235 u16 status;
10236 u8 old;
10237
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010238 if (max_isr == -1)
10239 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010240
10241 status = vmcs_read16(GUEST_INTR_STATUS);
10242 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010243 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010244 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010245 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010246 vmcs_write16(GUEST_INTR_STATUS, status);
10247 }
10248}
10249
10250static void vmx_set_rvi(int vector)
10251{
10252 u16 status;
10253 u8 old;
10254
Wei Wang4114c272014-11-05 10:53:43 +080010255 if (vector == -1)
10256 vector = 0;
10257
Yang Zhangc7c9c562013-01-25 10:18:51 +080010258 status = vmcs_read16(GUEST_INTR_STATUS);
10259 old = (u8)status & 0xff;
10260 if ((u8)vector != old) {
10261 status &= ~0xff;
10262 status |= (u8)vector;
10263 vmcs_write16(GUEST_INTR_STATUS, status);
10264 }
10265}
10266
10267static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10268{
Liran Alon851c1a182017-12-24 18:12:56 +020010269 /*
10270 * When running L2, updating RVI is only relevant when
10271 * vmcs12 virtual-interrupt-delivery enabled.
10272 * However, it can be enabled only when L1 also
10273 * intercepts external-interrupts and in that case
10274 * we should not update vmcs02 RVI but instead intercept
10275 * interrupt. Therefore, do nothing when running L2.
10276 */
10277 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010278 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010279}
10280
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010281static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010282{
10283 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010284 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010285 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010286
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010287 WARN_ON(!vcpu->arch.apicv_active);
10288 if (pi_test_on(&vmx->pi_desc)) {
10289 pi_clear_on(&vmx->pi_desc);
10290 /*
10291 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10292 * But on x86 this is just a compiler barrier anyway.
10293 */
10294 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010295 max_irr_updated =
10296 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10297
10298 /*
10299 * If we are running L2 and L1 has a new pending interrupt
10300 * which can be injected, we should re-evaluate
10301 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010302 * If L1 intercepts external-interrupts, we should
10303 * exit from L2 to L1. Otherwise, interrupt should be
10304 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010305 */
Liran Alon851c1a182017-12-24 18:12:56 +020010306 if (is_guest_mode(vcpu) && max_irr_updated) {
10307 if (nested_exit_on_intr(vcpu))
10308 kvm_vcpu_exiting_guest_mode(vcpu);
10309 else
10310 kvm_make_request(KVM_REQ_EVENT, vcpu);
10311 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010312 } else {
10313 max_irr = kvm_lapic_find_highest_irr(vcpu);
10314 }
10315 vmx_hwapic_irr_update(vcpu, max_irr);
10316 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010317}
10318
Andrey Smetanin63086302015-11-10 15:36:32 +030010319static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010320{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010321 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010322 return;
10323
Yang Zhangc7c9c562013-01-25 10:18:51 +080010324 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10325 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10326 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10327 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10328}
10329
Paolo Bonzini967235d2016-12-19 14:03:45 +010010330static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10331{
10332 struct vcpu_vmx *vmx = to_vmx(vcpu);
10333
10334 pi_clear_on(&vmx->pi_desc);
10335 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10336}
10337
Avi Kivity51aa01d2010-07-20 14:31:20 +030010338static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010339{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010340 u32 exit_intr_info = 0;
10341 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010342
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010343 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10344 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010345 return;
10346
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010347 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10348 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10349 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010350
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010351 /* if exit due to PF check for async PF */
10352 if (is_page_fault(exit_intr_info))
10353 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10354
Andi Kleena0861c02009-06-08 17:37:09 +080010355 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010356 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10357 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010358 kvm_machine_check();
10359
Gleb Natapov20f65982009-05-11 13:35:55 +030010360 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010361 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010362 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010363 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010364 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010365 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010366}
Gleb Natapov20f65982009-05-11 13:35:55 +030010367
Yang Zhanga547c6d2013-04-11 19:25:10 +080010368static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10369{
10370 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10371
Yang Zhanga547c6d2013-04-11 19:25:10 +080010372 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10373 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10374 unsigned int vector;
10375 unsigned long entry;
10376 gate_desc *desc;
10377 struct vcpu_vmx *vmx = to_vmx(vcpu);
10378#ifdef CONFIG_X86_64
10379 unsigned long tmp;
10380#endif
10381
10382 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10383 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010384 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010385 asm volatile(
10386#ifdef CONFIG_X86_64
10387 "mov %%" _ASM_SP ", %[sp]\n\t"
10388 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10389 "push $%c[ss]\n\t"
10390 "push %[sp]\n\t"
10391#endif
10392 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010393 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010394 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010395 :
10396#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010397 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010398#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010399 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010400 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010401 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010402 [ss]"i"(__KERNEL_DS),
10403 [cs]"i"(__KERNEL_CS)
10404 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010405 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010406}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010407STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010408
Tom Lendackybc226f02018-05-10 22:06:39 +020010409static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010410{
Tom Lendackybc226f02018-05-10 22:06:39 +020010411 switch (index) {
10412 case MSR_IA32_SMBASE:
10413 /*
10414 * We cannot do SMM unless we can run the guest in big
10415 * real mode.
10416 */
10417 return enable_unrestricted_guest || emulate_invalid_guest_state;
10418 case MSR_AMD64_VIRT_SPEC_CTRL:
10419 /* This is AMD only. */
10420 return false;
10421 default:
10422 return true;
10423 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010424}
10425
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010426static bool vmx_mpx_supported(void)
10427{
10428 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10429 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10430}
10431
Wanpeng Li55412b22014-12-02 19:21:30 +080010432static bool vmx_xsaves_supported(void)
10433{
10434 return vmcs_config.cpu_based_2nd_exec_ctrl &
10435 SECONDARY_EXEC_XSAVES;
10436}
10437
Avi Kivity51aa01d2010-07-20 14:31:20 +030010438static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10439{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010440 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010441 bool unblock_nmi;
10442 u8 vector;
10443 bool idtv_info_valid;
10444
10445 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010446
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010447 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010448 if (vmx->loaded_vmcs->nmi_known_unmasked)
10449 return;
10450 /*
10451 * Can't use vmx->exit_intr_info since we're not sure what
10452 * the exit reason is.
10453 */
10454 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10455 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10456 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10457 /*
10458 * SDM 3: 27.7.1.2 (September 2008)
10459 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10460 * a guest IRET fault.
10461 * SDM 3: 23.2.2 (September 2008)
10462 * Bit 12 is undefined in any of the following cases:
10463 * If the VM exit sets the valid bit in the IDT-vectoring
10464 * information field.
10465 * If the VM exit is due to a double fault.
10466 */
10467 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10468 vector != DF_VECTOR && !idtv_info_valid)
10469 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10470 GUEST_INTR_STATE_NMI);
10471 else
10472 vmx->loaded_vmcs->nmi_known_unmasked =
10473 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10474 & GUEST_INTR_STATE_NMI);
10475 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10476 vmx->loaded_vmcs->vnmi_blocked_time +=
10477 ktime_to_ns(ktime_sub(ktime_get(),
10478 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010479}
10480
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010481static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010482 u32 idt_vectoring_info,
10483 int instr_len_field,
10484 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010485{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010486 u8 vector;
10487 int type;
10488 bool idtv_info_valid;
10489
10490 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010491
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010492 vcpu->arch.nmi_injected = false;
10493 kvm_clear_exception_queue(vcpu);
10494 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010495
10496 if (!idtv_info_valid)
10497 return;
10498
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010499 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010500
Avi Kivity668f6122008-07-02 09:28:55 +030010501 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10502 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010503
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010504 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010505 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010506 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010507 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010508 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010509 * Clear bit "block by NMI" before VM entry if a NMI
10510 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010511 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010512 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010513 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010514 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010515 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010516 /* fall through */
10517 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010518 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010519 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010520 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010521 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010522 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010523 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010524 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010525 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010526 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010527 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010528 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010529 break;
10530 default:
10531 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010532 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010533}
10534
Avi Kivity83422e12010-07-20 14:43:23 +030010535static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10536{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010537 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010538 VM_EXIT_INSTRUCTION_LEN,
10539 IDT_VECTORING_ERROR_CODE);
10540}
10541
Avi Kivityb463a6f2010-07-20 15:06:17 +030010542static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10543{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010544 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010545 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10546 VM_ENTRY_INSTRUCTION_LEN,
10547 VM_ENTRY_EXCEPTION_ERROR_CODE);
10548
10549 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10550}
10551
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010552static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10553{
10554 int i, nr_msrs;
10555 struct perf_guest_switch_msr *msrs;
10556
10557 msrs = perf_guest_get_msrs(&nr_msrs);
10558
10559 if (!msrs)
10560 return;
10561
10562 for (i = 0; i < nr_msrs; i++)
10563 if (msrs[i].host == msrs[i].guest)
10564 clear_atomic_switch_msr(vmx, msrs[i].msr);
10565 else
10566 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010567 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010568}
10569
Jiang Biao33365e72016-11-03 15:03:37 +080010570static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010571{
10572 struct vcpu_vmx *vmx = to_vmx(vcpu);
10573 u64 tscl;
10574 u32 delta_tsc;
10575
10576 if (vmx->hv_deadline_tsc == -1)
10577 return;
10578
10579 tscl = rdtsc();
10580 if (vmx->hv_deadline_tsc > tscl)
10581 /* sure to be 32 bit only because checked on set_hv_timer */
10582 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10583 cpu_preemption_timer_multi);
10584 else
10585 delta_tsc = 0;
10586
10587 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10588}
10589
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010590static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010591{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010592 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010593 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010594
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010595 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010596 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010597 vmx->loaded_vmcs->soft_vnmi_blocked))
10598 vmx->loaded_vmcs->entry_time = ktime_get();
10599
Avi Kivity104f2262010-11-18 13:12:52 +020010600 /* Don't enter VMX if guest state is invalid, let the exit handler
10601 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010602 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010603 return;
10604
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010605 if (vmx->ple_window_dirty) {
10606 vmx->ple_window_dirty = false;
10607 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10608 }
10609
Abel Gordon012f83c2013-04-18 14:39:25 +030010610 if (vmx->nested.sync_shadow_vmcs) {
10611 copy_vmcs12_to_shadow(vmx);
10612 vmx->nested.sync_shadow_vmcs = false;
10613 }
10614
Avi Kivity104f2262010-11-18 13:12:52 +020010615 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10616 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10617 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10618 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10619
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010620 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010621 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010622 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010623 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010624 }
10625
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010626 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010627 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010628 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010629 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010630 }
10631
Avi Kivity104f2262010-11-18 13:12:52 +020010632 /* When single-stepping over STI and MOV SS, we must clear the
10633 * corresponding interruptibility bits in the guest state. Otherwise
10634 * vmentry fails as it then expects bit 14 (BS) in pending debug
10635 * exceptions being set, but that's not correct for the guest debugging
10636 * case. */
10637 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10638 vmx_set_interrupt_shadow(vcpu, 0);
10639
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010640 if (static_cpu_has(X86_FEATURE_PKU) &&
10641 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10642 vcpu->arch.pkru != vmx->host_pkru)
10643 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010644
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010645 atomic_switch_perf_msrs(vmx);
10646
Yunhong Jiang64672c92016-06-13 14:19:59 -070010647 vmx_arm_hv_timer(vcpu);
10648
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010649 /*
10650 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10651 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10652 * is no need to worry about the conditional branch over the wrmsr
10653 * being speculatively taken.
10654 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010655 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010656
Nadav Har'Eld462b812011-05-24 15:26:10 +030010657 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010658
10659 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10660 (unsigned long)&current_evmcs->host_rsp : 0;
10661
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010662 if (static_branch_unlikely(&vmx_l1d_should_flush))
10663 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010664
Avi Kivity104f2262010-11-18 13:12:52 +020010665 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010666 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010667 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10668 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10669 "push %%" _ASM_CX " \n\t"
10670 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010671 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010672 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010673 /* Avoid VMWRITE when Enlightened VMCS is in use */
10674 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10675 "jz 2f \n\t"
10676 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10677 "jmp 1f \n\t"
10678 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010679 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010680 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010681 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010682 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10683 "mov %%cr2, %%" _ASM_DX " \n\t"
10684 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010685 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010686 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010687 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010688 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010689 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010690 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010691 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10692 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10693 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10694 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10695 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10696 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010697#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010698 "mov %c[r8](%0), %%r8 \n\t"
10699 "mov %c[r9](%0), %%r9 \n\t"
10700 "mov %c[r10](%0), %%r10 \n\t"
10701 "mov %c[r11](%0), %%r11 \n\t"
10702 "mov %c[r12](%0), %%r12 \n\t"
10703 "mov %c[r13](%0), %%r13 \n\t"
10704 "mov %c[r14](%0), %%r14 \n\t"
10705 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010706#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010707 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010708
Avi Kivity6aa8b732006-12-10 02:21:36 -080010709 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010710 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010711 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010712 "jmp 2f \n\t"
10713 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10714 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010715 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010716 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010717 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010718 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010719 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10720 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10721 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10722 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10723 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10724 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10725 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010726#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010727 "mov %%r8, %c[r8](%0) \n\t"
10728 "mov %%r9, %c[r9](%0) \n\t"
10729 "mov %%r10, %c[r10](%0) \n\t"
10730 "mov %%r11, %c[r11](%0) \n\t"
10731 "mov %%r12, %c[r12](%0) \n\t"
10732 "mov %%r13, %c[r13](%0) \n\t"
10733 "mov %%r14, %c[r14](%0) \n\t"
10734 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010735 "xor %%r8d, %%r8d \n\t"
10736 "xor %%r9d, %%r9d \n\t"
10737 "xor %%r10d, %%r10d \n\t"
10738 "xor %%r11d, %%r11d \n\t"
10739 "xor %%r12d, %%r12d \n\t"
10740 "xor %%r13d, %%r13d \n\t"
10741 "xor %%r14d, %%r14d \n\t"
10742 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010743#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010744 "mov %%cr2, %%" _ASM_AX " \n\t"
10745 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010746
Jim Mattson0cb5b302018-01-03 14:31:38 -080010747 "xor %%eax, %%eax \n\t"
10748 "xor %%ebx, %%ebx \n\t"
10749 "xor %%esi, %%esi \n\t"
10750 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010751 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010752 ".pushsection .rodata \n\t"
10753 ".global vmx_return \n\t"
10754 "vmx_return: " _ASM_PTR " 2b \n\t"
10755 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010756 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010757 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010758 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010759 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010760 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10761 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10762 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10763 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10764 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10765 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10766 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010767#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010768 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10769 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10770 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10771 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10772 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10773 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10774 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10775 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010776#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010777 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10778 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010779 : "cc", "memory"
10780#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010781 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010782 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010783#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010784 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010785#endif
10786 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010787
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010788 /*
10789 * We do not use IBRS in the kernel. If this vCPU has used the
10790 * SPEC_CTRL MSR it may have left it on; save the value and
10791 * turn it off. This is much more efficient than blindly adding
10792 * it to the atomic save/restore list. Especially as the former
10793 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10794 *
10795 * For non-nested case:
10796 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10797 * save it.
10798 *
10799 * For nested case:
10800 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10801 * save it.
10802 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010803 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010804 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010805
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010806 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010807
David Woodhouse117cc7a2018-01-12 11:11:27 +000010808 /* Eliminate branch target predictions from guest mode */
10809 vmexit_fill_RSB();
10810
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010811 /* All fields are clean at this point */
10812 if (static_branch_unlikely(&enable_evmcs))
10813 current_evmcs->hv_clean_fields |=
10814 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10815
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010816 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010817 if (vmx->host_debugctlmsr)
10818 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010819
Avi Kivityaa67f602012-08-01 16:48:03 +030010820#ifndef CONFIG_X86_64
10821 /*
10822 * The sysexit path does not restore ds/es, so we must set them to
10823 * a reasonable value ourselves.
10824 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010825 * We can't defer this to vmx_prepare_switch_to_host() since that
10826 * function may be executed in interrupt context, which saves and
10827 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010828 */
10829 loadsegment(ds, __USER_DS);
10830 loadsegment(es, __USER_DS);
10831#endif
10832
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010833 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010834 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010835 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010836 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010837 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010838 vcpu->arch.regs_dirty = 0;
10839
Gleb Natapove0b890d2013-09-25 12:51:33 +030010840 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010841 * eager fpu is enabled if PKEY is supported and CR4 is switched
10842 * back on host, so it is safe to read guest PKRU from current
10843 * XSAVE.
10844 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010845 if (static_cpu_has(X86_FEATURE_PKU) &&
10846 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10847 vcpu->arch.pkru = __read_pkru();
10848 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010849 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010850 }
10851
Gleb Natapove0b890d2013-09-25 12:51:33 +030010852 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010853 vmx->idt_vectoring_info = 0;
10854
10855 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10856 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10857 return;
10858
10859 vmx->loaded_vmcs->launched = 1;
10860 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010861
Avi Kivity51aa01d2010-07-20 14:31:20 +030010862 vmx_complete_atomic_exit(vmx);
10863 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010864 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010865}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010866STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010867
Sean Christopherson434a1e92018-03-20 12:17:18 -070010868static struct kvm *vmx_vm_alloc(void)
10869{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010870 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010871 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010872}
10873
10874static void vmx_vm_free(struct kvm *kvm)
10875{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010876 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010877}
10878
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010879static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010880{
10881 struct vcpu_vmx *vmx = to_vmx(vcpu);
10882 int cpu;
10883
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010884 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010885 return;
10886
10887 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010888 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010889 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010890 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010891 put_cpu();
10892}
10893
Jim Mattson2f1fe812016-07-08 15:36:06 -070010894/*
10895 * Ensure that the current vmcs of the logical processor is the
10896 * vmcs01 of the vcpu before calling free_nested().
10897 */
10898static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10899{
10900 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010901
Christoffer Dallec7660c2017-12-04 21:35:23 +010010902 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010903 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010904 free_nested(vmx);
10905 vcpu_put(vcpu);
10906}
10907
Avi Kivity6aa8b732006-12-10 02:21:36 -080010908static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10909{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010910 struct vcpu_vmx *vmx = to_vmx(vcpu);
10911
Kai Huang843e4332015-01-28 10:54:28 +080010912 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010913 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010914 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010915 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010916 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010917 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010918 kfree(vmx->guest_msrs);
10919 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010920 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010921}
10922
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010923static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010924{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010925 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010926 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010927 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010928 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010929
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010930 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010931 return ERR_PTR(-ENOMEM);
10932
Wanpeng Li991e7a02015-09-16 17:30:05 +080010933 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010934
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010935 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10936 if (err)
10937 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010938
Peter Feiner4e595162016-07-07 14:49:58 -070010939 err = -ENOMEM;
10940
10941 /*
10942 * If PML is turned on, failure on enabling PML just results in failure
10943 * of creating the vcpu, therefore we can simplify PML logic (by
10944 * avoiding dealing with cases, such as enabling PML partially on vcpus
10945 * for the guest, etc.
10946 */
10947 if (enable_pml) {
10948 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10949 if (!vmx->pml_pg)
10950 goto uninit_vcpu;
10951 }
10952
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010953 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010954 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10955 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010956
Peter Feiner4e595162016-07-07 14:49:58 -070010957 if (!vmx->guest_msrs)
10958 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010959
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010960 err = alloc_loaded_vmcs(&vmx->vmcs01);
10961 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010962 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010963
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010964 msr_bitmap = vmx->vmcs01.msr_bitmap;
10965 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10966 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10967 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10968 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10969 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10970 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10971 vmx->msr_bitmap_mode = 0;
10972
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010973 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030010974 cpu = get_cpu();
10975 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100010976 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020010977 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010978 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030010979 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020010980 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020010981 err = alloc_apic_access_page(kvm);
10982 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020010983 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020010984 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080010985
Sean Christophersone90008d2018-03-05 12:04:37 -080010986 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080010987 err = init_rmode_identity_map(kvm);
10988 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020010989 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080010990 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080010991
Roman Kagan63aff652018-07-19 21:59:07 +030010992 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010010993 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
10994 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080010995
Wincy Van705699a2015-02-03 23:58:17 +080010996 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010997 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030010998
Haozhong Zhang37e4c992016-06-22 14:59:55 +080010999 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11000
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011001 /*
11002 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11003 * or POSTED_INTR_WAKEUP_VECTOR.
11004 */
11005 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11006 vmx->pi_desc.sn = 1;
11007
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011008 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011009
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011010free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011011 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011012free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011013 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011014free_pml:
11015 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011016uninit_vcpu:
11017 kvm_vcpu_uninit(&vmx->vcpu);
11018free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011019 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011020 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011021 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011022}
11023
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011024#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"
11025#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 -040011026
Wanpeng Lib31c1142018-03-12 04:53:04 -070011027static int vmx_vm_init(struct kvm *kvm)
11028{
Tianyu Lan877ad952018-07-19 08:40:23 +000011029 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11030
Wanpeng Lib31c1142018-03-12 04:53:04 -070011031 if (!ple_gap)
11032 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011033
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011034 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11035 switch (l1tf_mitigation) {
11036 case L1TF_MITIGATION_OFF:
11037 case L1TF_MITIGATION_FLUSH_NOWARN:
11038 /* 'I explicitly don't care' is set */
11039 break;
11040 case L1TF_MITIGATION_FLUSH:
11041 case L1TF_MITIGATION_FLUSH_NOSMT:
11042 case L1TF_MITIGATION_FULL:
11043 /*
11044 * Warn upon starting the first VM in a potentially
11045 * insecure environment.
11046 */
11047 if (cpu_smt_control == CPU_SMT_ENABLED)
11048 pr_warn_once(L1TF_MSG_SMT);
11049 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11050 pr_warn_once(L1TF_MSG_L1D);
11051 break;
11052 case L1TF_MITIGATION_FULL_FORCE:
11053 /* Flush is enforced */
11054 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011055 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011056 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011057 return 0;
11058}
11059
Yang, Sheng002c7f72007-07-31 14:23:01 +030011060static void __init vmx_check_processor_compat(void *rtn)
11061{
11062 struct vmcs_config vmcs_conf;
11063
11064 *(int *)rtn = 0;
11065 if (setup_vmcs_config(&vmcs_conf) < 0)
11066 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011067 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011068 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11069 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11070 smp_processor_id());
11071 *(int *)rtn = -EIO;
11072 }
11073}
11074
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011075static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011076{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011077 u8 cache;
11078 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011079
Sheng Yang522c68c2009-04-27 20:35:43 +080011080 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011081 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011082 * 2. EPT with VT-d:
11083 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011084 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011085 * b. VT-d with snooping control feature: snooping control feature of
11086 * VT-d engine can guarantee the cache correctness. Just set it
11087 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011088 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011089 * consistent with host MTRR
11090 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011091 if (is_mmio) {
11092 cache = MTRR_TYPE_UNCACHABLE;
11093 goto exit;
11094 }
11095
11096 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011097 ipat = VMX_EPT_IPAT_BIT;
11098 cache = MTRR_TYPE_WRBACK;
11099 goto exit;
11100 }
11101
11102 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11103 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011104 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011105 cache = MTRR_TYPE_WRBACK;
11106 else
11107 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011108 goto exit;
11109 }
11110
Xiao Guangrongff536042015-06-15 16:55:22 +080011111 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011112
11113exit:
11114 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011115}
11116
Sheng Yang17cc3932010-01-05 19:02:27 +080011117static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011118{
Sheng Yang878403b2010-01-05 19:02:29 +080011119 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11120 return PT_DIRECTORY_LEVEL;
11121 else
11122 /* For shadow and EPT supported 1GB page */
11123 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011124}
11125
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011126static void vmcs_set_secondary_exec_control(u32 new_ctl)
11127{
11128 /*
11129 * These bits in the secondary execution controls field
11130 * are dynamic, the others are mostly based on the hypervisor
11131 * architecture and the guest's CPUID. Do not touch the
11132 * dynamic bits.
11133 */
11134 u32 mask =
11135 SECONDARY_EXEC_SHADOW_VMCS |
11136 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011137 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11138 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011139
11140 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11141
11142 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11143 (new_ctl & ~mask) | (cur_ctl & mask));
11144}
11145
David Matlack8322ebb2016-11-29 18:14:09 -080011146/*
11147 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11148 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11149 */
11150static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11151{
11152 struct vcpu_vmx *vmx = to_vmx(vcpu);
11153 struct kvm_cpuid_entry2 *entry;
11154
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011155 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11156 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011157
11158#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11159 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011160 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011161} while (0)
11162
11163 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11164 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11165 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11166 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11167 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11168 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11169 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11170 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11171 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11172 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11173 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11174 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11175 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11176 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11177 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11178
11179 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11180 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11181 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11182 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11183 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011184 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011185
11186#undef cr4_fixed1_update
11187}
11188
Sheng Yang0e851882009-12-18 16:48:46 +080011189static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11190{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011191 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011192
Paolo Bonzini80154d72017-08-24 13:55:35 +020011193 if (cpu_has_secondary_exec_ctrls()) {
11194 vmx_compute_secondary_exec_control(vmx);
11195 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011196 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011197
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011198 if (nested_vmx_allowed(vcpu))
11199 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11200 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11201 else
11202 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11203 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011204
11205 if (nested_vmx_allowed(vcpu))
11206 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080011207}
11208
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011209static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11210{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011211 if (func == 1 && nested)
11212 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011213}
11214
Yang Zhang25d92082013-08-06 12:00:32 +030011215static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11216 struct x86_exception *fault)
11217{
Jan Kiszka533558b2014-01-04 18:47:20 +010011218 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011220 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011221 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011222
Bandan Dasc5f983f2017-05-05 15:25:14 -040011223 if (vmx->nested.pml_full) {
11224 exit_reason = EXIT_REASON_PML_FULL;
11225 vmx->nested.pml_full = false;
11226 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11227 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011228 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011229 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011230 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011231
11232 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011233 vmcs12->guest_physical_address = fault->address;
11234}
11235
Peter Feiner995f00a2017-06-30 17:26:32 -070011236static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11237{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011238 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011239}
11240
Nadav Har'El155a97a2013-08-05 11:07:16 +030011241/* Callbacks for nested_ept_init_mmu_context: */
11242
11243static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11244{
11245 /* return the page table to be shadowed - in our case, EPT12 */
11246 return get_vmcs12(vcpu)->ept_pointer;
11247}
11248
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011249static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011250{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011251 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011252 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011253 return 1;
11254
Paolo Bonziniad896af2013-10-02 16:56:14 +020011255 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011256 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011257 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011258 nested_ept_ad_enabled(vcpu),
11259 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011260 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11261 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11262 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11263
11264 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011265 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011266}
11267
11268static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11269{
11270 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11271}
11272
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011273static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11274 u16 error_code)
11275{
11276 bool inequality, bit;
11277
11278 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11279 inequality =
11280 (error_code & vmcs12->page_fault_error_code_mask) !=
11281 vmcs12->page_fault_error_code_match;
11282 return inequality ^ bit;
11283}
11284
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011285static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11286 struct x86_exception *fault)
11287{
11288 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11289
11290 WARN_ON(!is_guest_mode(vcpu));
11291
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011292 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11293 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011294 vmcs12->vm_exit_intr_error_code = fault->error_code;
11295 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11296 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11297 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11298 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011299 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011300 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011301 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011302}
11303
Paolo Bonzinic9923842017-12-13 14:16:30 +010011304static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11305 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011306
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011307static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011308{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011309 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011310 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011311 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011312 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011313
11314 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011315 /*
11316 * Translate L1 physical address to host physical
11317 * address for vmcs02. Keep the page pinned, so this
11318 * physical address remains valid. We keep a reference
11319 * to it so we can release it later.
11320 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011321 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011322 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011323 vmx->nested.apic_access_page = NULL;
11324 }
11325 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011326 /*
11327 * If translation failed, no matter: This feature asks
11328 * to exit when accessing the given address, and if it
11329 * can never be accessed, this feature won't do
11330 * anything anyway.
11331 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011332 if (!is_error_page(page)) {
11333 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011334 hpa = page_to_phys(vmx->nested.apic_access_page);
11335 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11336 } else {
11337 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11338 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11339 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011340 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011341
11342 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011343 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011344 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011345 vmx->nested.virtual_apic_page = NULL;
11346 }
11347 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011348
11349 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011350 * If translation failed, VM entry will fail because
11351 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11352 * Failing the vm entry is _not_ what the processor
11353 * does but it's basically the only possibility we
11354 * have. We could still enter the guest if CR8 load
11355 * exits are enabled, CR8 store exits are enabled, and
11356 * virtualize APIC access is disabled; in this case
11357 * the processor would never use the TPR shadow and we
11358 * could simply clear the bit from the execution
11359 * control. But such a configuration is useless, so
11360 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011361 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011362 if (!is_error_page(page)) {
11363 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011364 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11365 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11366 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011367 }
11368
Wincy Van705699a2015-02-03 23:58:17 +080011369 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011370 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11371 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011372 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011373 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011374 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011375 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11376 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011377 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011378 vmx->nested.pi_desc_page = page;
11379 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011380 vmx->nested.pi_desc =
11381 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11382 (unsigned long)(vmcs12->posted_intr_desc_addr &
11383 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011384 vmcs_write64(POSTED_INTR_DESC_ADDR,
11385 page_to_phys(vmx->nested.pi_desc_page) +
11386 (unsigned long)(vmcs12->posted_intr_desc_addr &
11387 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011388 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011389 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011390 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11391 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011392 else
11393 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11394 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011395}
11396
Jan Kiszkaf41245002014-03-07 20:03:13 +010011397static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11398{
11399 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11400 struct vcpu_vmx *vmx = to_vmx(vcpu);
11401
11402 if (vcpu->arch.virtual_tsc_khz == 0)
11403 return;
11404
11405 /* Make sure short timeouts reliably trigger an immediate vmexit.
11406 * hrtimer_start does not guarantee this. */
11407 if (preemption_timeout <= 1) {
11408 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11409 return;
11410 }
11411
11412 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11413 preemption_timeout *= 1000000;
11414 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11415 hrtimer_start(&vmx->nested.preemption_timer,
11416 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11417}
11418
Jim Mattson56a20512017-07-06 16:33:06 -070011419static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11420 struct vmcs12 *vmcs12)
11421{
11422 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11423 return 0;
11424
11425 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11426 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11427 return -EINVAL;
11428
11429 return 0;
11430}
11431
Wincy Van3af18d92015-02-03 23:49:31 +080011432static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11433 struct vmcs12 *vmcs12)
11434{
Wincy Van3af18d92015-02-03 23:49:31 +080011435 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11436 return 0;
11437
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011438 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011439 return -EINVAL;
11440
11441 return 0;
11442}
11443
Jim Mattson712b12d2017-08-24 13:24:47 -070011444static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11445 struct vmcs12 *vmcs12)
11446{
11447 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11448 return 0;
11449
11450 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11451 return -EINVAL;
11452
11453 return 0;
11454}
11455
Wincy Van3af18d92015-02-03 23:49:31 +080011456/*
11457 * Merge L0's and L1's MSR bitmap, return false to indicate that
11458 * we do not use the hardware.
11459 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011460static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11461 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011462{
Wincy Van82f0dd42015-02-03 23:57:18 +080011463 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011464 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011465 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011466 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011467 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011468 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011469 *
11470 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11471 * ensures that we do not accidentally generate an L02 MSR bitmap
11472 * from the L12 MSR bitmap that is too permissive.
11473 * 2. That L1 or L2s have actually used the MSR. This avoids
11474 * unnecessarily merging of the bitmap if the MSR is unused. This
11475 * works properly because we only update the L01 MSR bitmap lazily.
11476 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11477 * updated to reflect this when L1 (or its L2s) actually write to
11478 * the MSR.
11479 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011480 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11481 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011482
Paolo Bonzinic9923842017-12-13 14:16:30 +010011483 /* Nothing to do if the MSR bitmap is not in use. */
11484 if (!cpu_has_vmx_msr_bitmap() ||
11485 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11486 return false;
11487
Ashok Raj15d45072018-02-01 22:59:43 +010011488 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011489 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011490 return false;
11491
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011492 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11493 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011494 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011495
Radim Krčmářd048c092016-08-08 20:16:22 +020011496 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011497 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11498 /*
11499 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11500 * just lets the processor take the value from the virtual-APIC page;
11501 * take those 256 bits directly from the L1 bitmap.
11502 */
11503 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11504 unsigned word = msr / BITS_PER_LONG;
11505 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11506 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011507 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011508 } else {
11509 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11510 unsigned word = msr / BITS_PER_LONG;
11511 msr_bitmap_l0[word] = ~0;
11512 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11513 }
11514 }
11515
11516 nested_vmx_disable_intercept_for_msr(
11517 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011518 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011519 MSR_TYPE_W);
11520
11521 if (nested_cpu_has_vid(vmcs12)) {
11522 nested_vmx_disable_intercept_for_msr(
11523 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011524 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011525 MSR_TYPE_W);
11526 nested_vmx_disable_intercept_for_msr(
11527 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011528 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011529 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011530 }
Ashok Raj15d45072018-02-01 22:59:43 +010011531
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011532 if (spec_ctrl)
11533 nested_vmx_disable_intercept_for_msr(
11534 msr_bitmap_l1, msr_bitmap_l0,
11535 MSR_IA32_SPEC_CTRL,
11536 MSR_TYPE_R | MSR_TYPE_W);
11537
Ashok Raj15d45072018-02-01 22:59:43 +010011538 if (pred_cmd)
11539 nested_vmx_disable_intercept_for_msr(
11540 msr_bitmap_l1, msr_bitmap_l0,
11541 MSR_IA32_PRED_CMD,
11542 MSR_TYPE_W);
11543
Wincy Vanf2b93282015-02-03 23:56:03 +080011544 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011545 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011546
11547 return true;
11548}
11549
Liran Alon61ada742018-06-23 02:35:08 +030011550static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11551 struct vmcs12 *vmcs12)
11552{
11553 struct vmcs12 *shadow;
11554 struct page *page;
11555
11556 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11557 vmcs12->vmcs_link_pointer == -1ull)
11558 return;
11559
11560 shadow = get_shadow_vmcs12(vcpu);
11561 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11562
11563 memcpy(shadow, kmap(page), VMCS12_SIZE);
11564
11565 kunmap(page);
11566 kvm_release_page_clean(page);
11567}
11568
11569static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11570 struct vmcs12 *vmcs12)
11571{
11572 struct vcpu_vmx *vmx = to_vmx(vcpu);
11573
11574 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11575 vmcs12->vmcs_link_pointer == -1ull)
11576 return;
11577
11578 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11579 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11580}
11581
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011582static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11583 struct vmcs12 *vmcs12)
11584{
11585 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11586 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11587 return -EINVAL;
11588 else
11589 return 0;
11590}
11591
Wincy Vanf2b93282015-02-03 23:56:03 +080011592static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11593 struct vmcs12 *vmcs12)
11594{
Wincy Van82f0dd42015-02-03 23:57:18 +080011595 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011596 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011597 !nested_cpu_has_vid(vmcs12) &&
11598 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011599 return 0;
11600
11601 /*
11602 * If virtualize x2apic mode is enabled,
11603 * virtualize apic access must be disabled.
11604 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011605 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11606 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011607 return -EINVAL;
11608
Wincy Van608406e2015-02-03 23:57:51 +080011609 /*
11610 * If virtual interrupt delivery is enabled,
11611 * we must exit on external interrupts.
11612 */
11613 if (nested_cpu_has_vid(vmcs12) &&
11614 !nested_exit_on_intr(vcpu))
11615 return -EINVAL;
11616
Wincy Van705699a2015-02-03 23:58:17 +080011617 /*
11618 * bits 15:8 should be zero in posted_intr_nv,
11619 * the descriptor address has been already checked
11620 * in nested_get_vmcs12_pages.
11621 */
11622 if (nested_cpu_has_posted_intr(vmcs12) &&
11623 (!nested_cpu_has_vid(vmcs12) ||
11624 !nested_exit_intr_ack_set(vcpu) ||
11625 vmcs12->posted_intr_nv & 0xff00))
11626 return -EINVAL;
11627
Wincy Vanf2b93282015-02-03 23:56:03 +080011628 /* tpr shadow is needed by all apicv features. */
11629 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11630 return -EINVAL;
11631
11632 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011633}
11634
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011635static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11636 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011637 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011638{
Liran Alone2536742018-06-23 02:35:02 +030011639 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011640 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011641 u64 count, addr;
11642
Liran Alone2536742018-06-23 02:35:02 +030011643 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11644 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011645 WARN_ON(1);
11646 return -EINVAL;
11647 }
11648 if (count == 0)
11649 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011650 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011651 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11652 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011653 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011654 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11655 addr_field, maxphyaddr, count, addr);
11656 return -EINVAL;
11657 }
11658 return 0;
11659}
11660
11661static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11662 struct vmcs12 *vmcs12)
11663{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011664 if (vmcs12->vm_exit_msr_load_count == 0 &&
11665 vmcs12->vm_exit_msr_store_count == 0 &&
11666 vmcs12->vm_entry_msr_load_count == 0)
11667 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011668 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011669 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011670 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011671 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011672 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011673 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011674 return -EINVAL;
11675 return 0;
11676}
11677
Bandan Dasc5f983f2017-05-05 15:25:14 -040011678static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11679 struct vmcs12 *vmcs12)
11680{
11681 u64 address = vmcs12->pml_address;
11682 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11683
11684 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11685 if (!nested_cpu_has_ept(vmcs12) ||
11686 !IS_ALIGNED(address, 4096) ||
11687 address >> maxphyaddr)
11688 return -EINVAL;
11689 }
11690
11691 return 0;
11692}
11693
Liran Alona8a7c022018-06-23 02:35:06 +030011694static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11695 struct vmcs12 *vmcs12)
11696{
11697 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11698 return 0;
11699
11700 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11701 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11702 return -EINVAL;
11703
11704 return 0;
11705}
11706
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011707static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11708 struct vmx_msr_entry *e)
11709{
11710 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011711 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011712 return -EINVAL;
11713 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11714 e->index == MSR_IA32_UCODE_REV)
11715 return -EINVAL;
11716 if (e->reserved != 0)
11717 return -EINVAL;
11718 return 0;
11719}
11720
11721static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11722 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011723{
11724 if (e->index == MSR_FS_BASE ||
11725 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011726 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11727 nested_vmx_msr_check_common(vcpu, e))
11728 return -EINVAL;
11729 return 0;
11730}
11731
11732static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11733 struct vmx_msr_entry *e)
11734{
11735 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11736 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011737 return -EINVAL;
11738 return 0;
11739}
11740
11741/*
11742 * Load guest's/host's msr at nested entry/exit.
11743 * return 0 for success, entry index for failure.
11744 */
11745static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11746{
11747 u32 i;
11748 struct vmx_msr_entry e;
11749 struct msr_data msr;
11750
11751 msr.host_initiated = false;
11752 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011753 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11754 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011755 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011756 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11757 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011758 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011759 }
11760 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011761 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011762 "%s check failed (%u, 0x%x, 0x%x)\n",
11763 __func__, i, e.index, e.reserved);
11764 goto fail;
11765 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011766 msr.index = e.index;
11767 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011768 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011769 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011770 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11771 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011772 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011773 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011774 }
11775 return 0;
11776fail:
11777 return i + 1;
11778}
11779
11780static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11781{
11782 u32 i;
11783 struct vmx_msr_entry e;
11784
11785 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011786 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011787 if (kvm_vcpu_read_guest(vcpu,
11788 gpa + i * sizeof(e),
11789 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011790 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011791 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11792 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011793 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011794 }
11795 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011796 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011797 "%s check failed (%u, 0x%x, 0x%x)\n",
11798 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011799 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011800 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011801 msr_info.host_initiated = false;
11802 msr_info.index = e.index;
11803 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011804 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011805 "%s cannot read MSR (%u, 0x%x)\n",
11806 __func__, i, e.index);
11807 return -EINVAL;
11808 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011809 if (kvm_vcpu_write_guest(vcpu,
11810 gpa + i * sizeof(e) +
11811 offsetof(struct vmx_msr_entry, value),
11812 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011813 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011814 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011815 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011816 return -EINVAL;
11817 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011818 }
11819 return 0;
11820}
11821
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011822static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11823{
11824 unsigned long invalid_mask;
11825
11826 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11827 return (val & invalid_mask) == 0;
11828}
11829
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011830/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011831 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11832 * emulating VM entry into a guest with EPT enabled.
11833 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11834 * is assigned to entry_failure_code on failure.
11835 */
11836static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011837 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011838{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011839 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011840 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011841 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11842 return 1;
11843 }
11844
11845 /*
11846 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11847 * must not be dereferenced.
11848 */
11849 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11850 !nested_ept) {
11851 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11852 *entry_failure_code = ENTRY_FAIL_PDPTE;
11853 return 1;
11854 }
11855 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011856 }
11857
Junaid Shahid50c28f22018-06-27 14:59:11 -070011858 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011859 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011860
11861 vcpu->arch.cr3 = cr3;
11862 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11863
11864 kvm_init_mmu(vcpu, false);
11865
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011866 return 0;
11867}
11868
Jim Mattson6514dc32018-04-26 16:09:12 -070011869static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011870{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011871 struct vcpu_vmx *vmx = to_vmx(vcpu);
11872
11873 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11874 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11875 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11876 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11877 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11878 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11879 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11880 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11881 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11882 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11883 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11884 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11885 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11886 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11887 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11888 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11889 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11890 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11891 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11892 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11893 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11894 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11895 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11896 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11897 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11898 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11899 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11900 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11901 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11902 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11903 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011904
11905 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11906 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11907 vmcs12->guest_pending_dbg_exceptions);
11908 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11909 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11910
11911 if (nested_cpu_has_xsaves(vmcs12))
11912 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11913 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11914
11915 if (cpu_has_vmx_posted_intr())
11916 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11917
11918 /*
11919 * Whether page-faults are trapped is determined by a combination of
11920 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11921 * If enable_ept, L0 doesn't care about page faults and we should
11922 * set all of these to L1's desires. However, if !enable_ept, L0 does
11923 * care about (at least some) page faults, and because it is not easy
11924 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11925 * to exit on each and every L2 page fault. This is done by setting
11926 * MASK=MATCH=0 and (see below) EB.PF=1.
11927 * Note that below we don't need special code to set EB.PF beyond the
11928 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11929 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11930 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11931 */
11932 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11933 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11934 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11935 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11936
11937 /* All VMFUNCs are currently emulated through L0 vmexits. */
11938 if (cpu_has_vmx_vmfunc())
11939 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11940
11941 if (cpu_has_vmx_apicv()) {
11942 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11943 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11944 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11945 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11946 }
11947
11948 /*
11949 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11950 * Some constant fields are set here by vmx_set_constant_host_state().
11951 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011952 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
11953 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011954 */
11955 vmx_set_constant_host_state(vmx);
11956
11957 /*
11958 * Set the MSR load/store lists to match L0's settings.
11959 */
11960 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011961 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11962 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11963 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11964 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011965
11966 set_cr4_guest_host_mask(vmx);
11967
11968 if (vmx_mpx_supported())
11969 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11970
11971 if (enable_vpid) {
11972 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
11973 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
11974 else
11975 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
11976 }
11977
11978 /*
11979 * L1 may access the L2's PDPTR, so save them to construct vmcs12
11980 */
11981 if (enable_ept) {
11982 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
11983 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
11984 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
11985 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
11986 }
Radim Krčmář80132f42018-02-02 18:26:58 +010011987
11988 if (cpu_has_vmx_msr_bitmap())
11989 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011990}
11991
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011992/*
11993 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
11994 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080011995 * 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 +030011996 * guest in a way that will both be appropriate to L1's requests, and our
11997 * needs. In addition to modifying the active vmcs (which is vmcs02), this
11998 * function also has additional necessary side-effects, like setting various
11999 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012000 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12001 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012002 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012003static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012004 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012005{
12006 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012007 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012008
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012009 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012010 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012011 vmx->nested.dirty_vmcs12 = false;
12012 }
12013
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012014 /*
12015 * First, the fields that are shadowed. This must be kept in sync
12016 * with vmx_shadow_fields.h.
12017 */
12018
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012019 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012020 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012021 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012022 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12023 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012024
Jim Mattson6514dc32018-04-26 16:09:12 -070012025 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012026 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012027 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12028 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12029 } else {
12030 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12031 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12032 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012033 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012034 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12035 vmcs12->vm_entry_intr_info_field);
12036 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12037 vmcs12->vm_entry_exception_error_code);
12038 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12039 vmcs12->vm_entry_instruction_len);
12040 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12041 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012042 vmx->loaded_vmcs->nmi_known_unmasked =
12043 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012044 } else {
12045 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12046 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012047 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012048
Jan Kiszkaf41245002014-03-07 20:03:13 +010012049 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012050
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012051 /* Preemption timer setting is only taken from vmcs01. */
12052 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12053 exec_control |= vmcs_config.pin_based_exec_ctrl;
12054 if (vmx->hv_deadline_tsc == -1)
12055 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12056
12057 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012058 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012059 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12060 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012061 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012062 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012063 }
Wincy Van705699a2015-02-03 23:58:17 +080012064
Jan Kiszkaf41245002014-03-07 20:03:13 +010012065 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012066
Jan Kiszkaf41245002014-03-07 20:03:13 +010012067 vmx->nested.preemption_timer_expired = false;
12068 if (nested_cpu_has_preemption_timer(vmcs12))
12069 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012070
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012071 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012072 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012073
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012074 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012075 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012076 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012077 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012078 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012079 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012080 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12081 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012082 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012083 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12084 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12085 ~SECONDARY_EXEC_ENABLE_PML;
12086 exec_control |= vmcs12_exec_ctrl;
12087 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012088
Liran Alon32c7acf2018-06-23 02:35:11 +030012089 /* VMCS shadowing for L2 is emulated for now */
12090 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12091
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012092 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012093 vmcs_write16(GUEST_INTR_STATUS,
12094 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012095
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012096 /*
12097 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12098 * nested_get_vmcs12_pages will either fix it up or
12099 * remove the VM execution control.
12100 */
12101 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12102 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12103
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012104 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12105 }
12106
Jim Mattson83bafef2016-10-04 10:48:38 -070012107 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012108 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12109 * entry, but only if the current (host) sp changed from the value
12110 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12111 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12112 * here we just force the write to happen on entry.
12113 */
12114 vmx->host_rsp = 0;
12115
12116 exec_control = vmx_exec_control(vmx); /* L0's desires */
12117 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12118 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12119 exec_control &= ~CPU_BASED_TPR_SHADOW;
12120 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012121
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012122 /*
12123 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12124 * nested_get_vmcs12_pages can't fix it up, the illegal value
12125 * will result in a VM entry failure.
12126 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012127 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012128 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012129 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012130 } else {
12131#ifdef CONFIG_X86_64
12132 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12133 CPU_BASED_CR8_STORE_EXITING;
12134#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012135 }
12136
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012137 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012138 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12139 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012140 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012141 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12142 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12143
12144 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12145
12146 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12147 * bitwise-or of what L1 wants to trap for L2, and what we want to
12148 * trap. Note that CR0.TS also needs updating - we do this later.
12149 */
12150 update_exception_bitmap(vcpu);
12151 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12152 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12153
Nadav Har'El8049d652013-08-05 11:07:06 +030012154 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12155 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12156 * bits are further modified by vmx_set_efer() below.
12157 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012158 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012159
12160 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12161 * emulated by vmx_set_efer(), below.
12162 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012163 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012164 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12165 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012166 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12167
Jim Mattson6514dc32018-04-26 16:09:12 -070012168 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012169 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012170 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012171 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012172 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012173 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012174 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012175
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012176 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12177
Peter Feinerc95ba922016-08-17 09:36:47 -070012178 if (kvm_has_tsc_control)
12179 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012180
12181 if (enable_vpid) {
12182 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012183 * There is no direct mapping between vpid02 and vpid12, the
12184 * vpid02 is per-vCPU for L0 and reused while the value of
12185 * vpid12 is changed w/ one invvpid during nested vmentry.
12186 * The vpid12 is allocated by L1 for L2, so it will not
12187 * influence global bitmap(for vpid01 and vpid02 allocation)
12188 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012189 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012190 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012191 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12192 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012193 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012194 }
12195 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012196 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012197 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012198 }
12199
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012200 if (enable_pml) {
12201 /*
12202 * Conceptually we want to copy the PML address and index from
12203 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12204 * since we always flush the log on each vmexit, this happens
12205 * to be equivalent to simply resetting the fields in vmcs02.
12206 */
12207 ASSERT(vmx->pml_pg);
12208 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12209 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12210 }
12211
Nadav Har'El155a97a2013-08-05 11:07:16 +030012212 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012213 if (nested_ept_init_mmu_context(vcpu)) {
12214 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12215 return 1;
12216 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012217 } else if (nested_cpu_has2(vmcs12,
12218 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012219 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012220 }
12221
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012222 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012223 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12224 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012225 * The CR0_READ_SHADOW is what L2 should have expected to read given
12226 * the specifications by L1; It's not enough to take
12227 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12228 * have more bits than L1 expected.
12229 */
12230 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12231 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12232
12233 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12234 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12235
Jim Mattson6514dc32018-04-26 16:09:12 -070012236 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012237 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012238 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12239 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12240 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12241 else
12242 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12243 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12244 vmx_set_efer(vcpu, vcpu->arch.efer);
12245
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012246 /*
12247 * Guest state is invalid and unrestricted guest is disabled,
12248 * which means L1 attempted VMEntry to L2 with invalid state.
12249 * Fail the VMEntry.
12250 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012251 if (vmx->emulation_required) {
12252 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012253 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012254 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012255
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012256 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012257 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012258 entry_failure_code))
12259 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012260
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012261 if (!enable_ept)
12262 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12263
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012264 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12265 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012266 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012267}
12268
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012269static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12270{
12271 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12272 nested_cpu_has_virtual_nmis(vmcs12))
12273 return -EINVAL;
12274
12275 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12276 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12277 return -EINVAL;
12278
12279 return 0;
12280}
12281
Jim Mattsonca0bde22016-11-30 12:03:46 -080012282static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12283{
12284 struct vcpu_vmx *vmx = to_vmx(vcpu);
12285
12286 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12287 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12288 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12289
Jim Mattson56a20512017-07-06 16:33:06 -070012290 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12291 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12292
Jim Mattsonca0bde22016-11-30 12:03:46 -080012293 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12294 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12295
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012296 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12297 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12298
Jim Mattson712b12d2017-08-24 13:24:47 -070012299 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12300 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12301
Jim Mattsonca0bde22016-11-30 12:03:46 -080012302 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12303 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12304
12305 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12306 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12307
Bandan Dasc5f983f2017-05-05 15:25:14 -040012308 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12309 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12310
Liran Alona8a7c022018-06-23 02:35:06 +030012311 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12312 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12313
Jim Mattsonca0bde22016-11-30 12:03:46 -080012314 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012315 vmx->nested.msrs.procbased_ctls_low,
12316 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012317 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12318 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012319 vmx->nested.msrs.secondary_ctls_low,
12320 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012321 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012322 vmx->nested.msrs.pinbased_ctls_low,
12323 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012324 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012325 vmx->nested.msrs.exit_ctls_low,
12326 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012327 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012328 vmx->nested.msrs.entry_ctls_low,
12329 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012330 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12331
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012332 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012333 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12334
Bandan Das41ab9372017-08-03 15:54:43 -040012335 if (nested_cpu_has_vmfunc(vmcs12)) {
12336 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012337 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012338 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12339
12340 if (nested_cpu_has_eptp_switching(vmcs12)) {
12341 if (!nested_cpu_has_ept(vmcs12) ||
12342 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12343 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12344 }
12345 }
Bandan Das27c42a12017-08-03 15:54:42 -040012346
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012347 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12348 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12349
Jim Mattsonca0bde22016-11-30 12:03:46 -080012350 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12351 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12352 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12353 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12354
Marc Orr04473782018-06-20 17:21:29 -070012355 /*
12356 * From the Intel SDM, volume 3:
12357 * Fields relevant to VM-entry event injection must be set properly.
12358 * These fields are the VM-entry interruption-information field, the
12359 * VM-entry exception error code, and the VM-entry instruction length.
12360 */
12361 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12362 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12363 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12364 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12365 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12366 bool should_have_error_code;
12367 bool urg = nested_cpu_has2(vmcs12,
12368 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12369 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12370
12371 /* VM-entry interruption-info field: interruption type */
12372 if (intr_type == INTR_TYPE_RESERVED ||
12373 (intr_type == INTR_TYPE_OTHER_EVENT &&
12374 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12375 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12376
12377 /* VM-entry interruption-info field: vector */
12378 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12379 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12380 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12381 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12382
12383 /* VM-entry interruption-info field: deliver error code */
12384 should_have_error_code =
12385 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12386 x86_exception_has_error_code(vector);
12387 if (has_error_code != should_have_error_code)
12388 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12389
12390 /* VM-entry exception error code */
12391 if (has_error_code &&
12392 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12393 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12394
12395 /* VM-entry interruption-info field: reserved bits */
12396 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12397 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12398
12399 /* VM-entry instruction length */
12400 switch (intr_type) {
12401 case INTR_TYPE_SOFT_EXCEPTION:
12402 case INTR_TYPE_SOFT_INTR:
12403 case INTR_TYPE_PRIV_SW_EXCEPTION:
12404 if ((vmcs12->vm_entry_instruction_len > 15) ||
12405 (vmcs12->vm_entry_instruction_len == 0 &&
12406 !nested_cpu_has_zero_length_injection(vcpu)))
12407 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12408 }
12409 }
12410
Jim Mattsonca0bde22016-11-30 12:03:46 -080012411 return 0;
12412}
12413
Liran Alonf145d902018-06-23 02:35:07 +030012414static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12415 struct vmcs12 *vmcs12)
12416{
12417 int r;
12418 struct page *page;
12419 struct vmcs12 *shadow;
12420
12421 if (vmcs12->vmcs_link_pointer == -1ull)
12422 return 0;
12423
12424 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12425 return -EINVAL;
12426
12427 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12428 if (is_error_page(page))
12429 return -EINVAL;
12430
12431 r = 0;
12432 shadow = kmap(page);
12433 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12434 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12435 r = -EINVAL;
12436 kunmap(page);
12437 kvm_release_page_clean(page);
12438 return r;
12439}
12440
Jim Mattsonca0bde22016-11-30 12:03:46 -080012441static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12442 u32 *exit_qual)
12443{
12444 bool ia32e;
12445
12446 *exit_qual = ENTRY_FAIL_DEFAULT;
12447
12448 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12449 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12450 return 1;
12451
Liran Alonf145d902018-06-23 02:35:07 +030012452 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012453 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12454 return 1;
12455 }
12456
12457 /*
12458 * If the load IA32_EFER VM-entry control is 1, the following checks
12459 * are performed on the field for the IA32_EFER MSR:
12460 * - Bits reserved in the IA32_EFER MSR must be 0.
12461 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12462 * the IA-32e mode guest VM-exit control. It must also be identical
12463 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12464 * CR0.PG) is 1.
12465 */
12466 if (to_vmx(vcpu)->nested.nested_run_pending &&
12467 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12468 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12469 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12470 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12471 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12472 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12473 return 1;
12474 }
12475
12476 /*
12477 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12478 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12479 * the values of the LMA and LME bits in the field must each be that of
12480 * the host address-space size VM-exit control.
12481 */
12482 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12483 ia32e = (vmcs12->vm_exit_controls &
12484 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12485 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12486 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12487 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12488 return 1;
12489 }
12490
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012491 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12492 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12493 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12494 return 1;
12495
Jim Mattsonca0bde22016-11-30 12:03:46 -080012496 return 0;
12497}
12498
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012499/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012500 * If exit_qual is NULL, this is being called from state restore (either RSM
12501 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012502 */
12503static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012504{
12505 struct vcpu_vmx *vmx = to_vmx(vcpu);
12506 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012507 bool from_vmentry = !!exit_qual;
12508 u32 dummy_exit_qual;
12509 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012510
Jim Mattson858e25c2016-11-30 12:03:47 -080012511 enter_guest_mode(vcpu);
12512
12513 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12514 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12515
Jim Mattsonde3a0022017-11-27 17:22:25 -060012516 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012517 vmx_segment_cache_clear(vmx);
12518
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012519 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12520 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12521
12522 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012523 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012524 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012525
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012526 if (from_vmentry) {
12527 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012528
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012529 r = EXIT_REASON_MSR_LOAD_FAIL;
12530 *exit_qual = nested_vmx_load_msr(vcpu,
12531 vmcs12->vm_entry_msr_load_addr,
12532 vmcs12->vm_entry_msr_load_count);
12533 if (*exit_qual)
12534 goto fail;
12535 } else {
12536 /*
12537 * The MMU is not initialized to point at the right entities yet and
12538 * "get pages" would need to read data from the guest (i.e. we will
12539 * need to perform gpa to hpa translation). Request a call
12540 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12541 * have already been set at vmentry time and should not be reset.
12542 */
12543 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12544 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012545
Jim Mattson858e25c2016-11-30 12:03:47 -080012546 /*
12547 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12548 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12549 * returned as far as L1 is concerned. It will only return (and set
12550 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12551 */
12552 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012553
12554fail:
12555 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12556 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12557 leave_guest_mode(vcpu);
12558 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012559 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012560}
12561
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012562/*
12563 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12564 * for running an L2 nested guest.
12565 */
12566static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12567{
12568 struct vmcs12 *vmcs12;
12569 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012570 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012571 u32 exit_qual;
12572 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012573
Kyle Hueyeb277562016-11-29 12:40:39 -080012574 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012575 return 1;
12576
Kyle Hueyeb277562016-11-29 12:40:39 -080012577 if (!nested_vmx_check_vmcs12(vcpu))
12578 goto out;
12579
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012580 vmcs12 = get_vmcs12(vcpu);
12581
Liran Alona6192d42018-06-23 02:35:04 +030012582 /*
12583 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12584 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12585 * rather than RFLAGS.ZF, and no error number is stored to the
12586 * VM-instruction error field.
12587 */
12588 if (vmcs12->hdr.shadow_vmcs) {
12589 nested_vmx_failInvalid(vcpu);
12590 goto out;
12591 }
12592
Abel Gordon012f83c2013-04-18 14:39:25 +030012593 if (enable_shadow_vmcs)
12594 copy_shadow_to_vmcs12(vmx);
12595
Nadav Har'El7c177932011-05-25 23:12:04 +030012596 /*
12597 * The nested entry process starts with enforcing various prerequisites
12598 * on vmcs12 as required by the Intel SDM, and act appropriately when
12599 * they fail: As the SDM explains, some conditions should cause the
12600 * instruction to fail, while others will cause the instruction to seem
12601 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12602 * To speed up the normal (success) code path, we should avoid checking
12603 * for misconfigurations which will anyway be caught by the processor
12604 * when using the merged vmcs02.
12605 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012606 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12607 nested_vmx_failValid(vcpu,
12608 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12609 goto out;
12610 }
12611
Nadav Har'El7c177932011-05-25 23:12:04 +030012612 if (vmcs12->launch_state == launch) {
12613 nested_vmx_failValid(vcpu,
12614 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12615 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012616 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012617 }
12618
Jim Mattsonca0bde22016-11-30 12:03:46 -080012619 ret = check_vmentry_prereqs(vcpu, vmcs12);
12620 if (ret) {
12621 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012622 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012623 }
12624
Nadav Har'El7c177932011-05-25 23:12:04 +030012625 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012626 * After this point, the trap flag no longer triggers a singlestep trap
12627 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12628 * This is not 100% correct; for performance reasons, we delegate most
12629 * of the checks on host state to the processor. If those fail,
12630 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012631 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012632 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012633
Jim Mattsonca0bde22016-11-30 12:03:46 -080012634 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12635 if (ret) {
12636 nested_vmx_entry_failure(vcpu, vmcs12,
12637 EXIT_REASON_INVALID_STATE, exit_qual);
12638 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012639 }
12640
12641 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012642 * We're finally done with prerequisite checking, and can start with
12643 * the nested entry.
12644 */
12645
Jim Mattson6514dc32018-04-26 16:09:12 -070012646 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012647 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012648 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012649 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012650 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012651 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012652 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012653
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012654 /* Hide L1D cache contents from the nested guest. */
12655 vmx->vcpu.arch.l1tf_flush_l1d = true;
12656
Chao Gao135a06c2018-02-11 10:06:30 +080012657 /*
Liran Alon61ada742018-06-23 02:35:08 +030012658 * Must happen outside of enter_vmx_non_root_mode() as it will
12659 * also be used as part of restoring nVMX state for
12660 * snapshot restore (migration).
12661 *
12662 * In this flow, it is assumed that vmcs12 cache was
12663 * trasferred as part of captured nVMX state and should
12664 * therefore not be read from guest memory (which may not
12665 * exist on destination host yet).
12666 */
12667 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12668
12669 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012670 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12671 * by event injection, halt vcpu.
12672 */
12673 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012674 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12675 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012676 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012677 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012678 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012679
12680out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012681 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012682}
12683
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012684/*
12685 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12686 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12687 * This function returns the new value we should put in vmcs12.guest_cr0.
12688 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12689 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12690 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12691 * didn't trap the bit, because if L1 did, so would L0).
12692 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12693 * been modified by L2, and L1 knows it. So just leave the old value of
12694 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12695 * isn't relevant, because if L0 traps this bit it can set it to anything.
12696 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12697 * changed these bits, and therefore they need to be updated, but L0
12698 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12699 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12700 */
12701static inline unsigned long
12702vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12703{
12704 return
12705 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12706 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12707 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12708 vcpu->arch.cr0_guest_owned_bits));
12709}
12710
12711static inline unsigned long
12712vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12713{
12714 return
12715 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12716 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12717 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12718 vcpu->arch.cr4_guest_owned_bits));
12719}
12720
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012721static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12722 struct vmcs12 *vmcs12)
12723{
12724 u32 idt_vectoring;
12725 unsigned int nr;
12726
Wanpeng Li664f8e22017-08-24 03:35:09 -070012727 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012728 nr = vcpu->arch.exception.nr;
12729 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12730
12731 if (kvm_exception_is_soft(nr)) {
12732 vmcs12->vm_exit_instruction_len =
12733 vcpu->arch.event_exit_inst_len;
12734 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12735 } else
12736 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12737
12738 if (vcpu->arch.exception.has_error_code) {
12739 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12740 vmcs12->idt_vectoring_error_code =
12741 vcpu->arch.exception.error_code;
12742 }
12743
12744 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012745 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012746 vmcs12->idt_vectoring_info_field =
12747 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012748 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012749 nr = vcpu->arch.interrupt.nr;
12750 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12751
12752 if (vcpu->arch.interrupt.soft) {
12753 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12754 vmcs12->vm_entry_instruction_len =
12755 vcpu->arch.event_exit_inst_len;
12756 } else
12757 idt_vectoring |= INTR_TYPE_EXT_INTR;
12758
12759 vmcs12->idt_vectoring_info_field = idt_vectoring;
12760 }
12761}
12762
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012763static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12764{
12765 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012766 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012767 bool block_nested_events =
12768 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012769
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012770 if (vcpu->arch.exception.pending &&
12771 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012772 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012773 return -EBUSY;
12774 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012775 return 0;
12776 }
12777
Jan Kiszkaf41245002014-03-07 20:03:13 +010012778 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12779 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012780 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012781 return -EBUSY;
12782 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12783 return 0;
12784 }
12785
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012786 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012787 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012788 return -EBUSY;
12789 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12790 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12791 INTR_INFO_VALID_MASK, 0);
12792 /*
12793 * The NMI-triggered VM exit counts as injection:
12794 * clear this one and block further NMIs.
12795 */
12796 vcpu->arch.nmi_pending = 0;
12797 vmx_set_nmi_mask(vcpu, true);
12798 return 0;
12799 }
12800
12801 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12802 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012803 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012804 return -EBUSY;
12805 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012806 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012807 }
12808
David Hildenbrand6342c502017-01-25 11:58:58 +010012809 vmx_complete_nested_posted_interrupt(vcpu);
12810 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012811}
12812
Jan Kiszkaf41245002014-03-07 20:03:13 +010012813static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12814{
12815 ktime_t remaining =
12816 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12817 u64 value;
12818
12819 if (ktime_to_ns(remaining) <= 0)
12820 return 0;
12821
12822 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12823 do_div(value, 1000000);
12824 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12825}
12826
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012827/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012828 * Update the guest state fields of vmcs12 to reflect changes that
12829 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12830 * VM-entry controls is also updated, since this is really a guest
12831 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012832 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012833static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012834{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012835 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12836 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12837
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012838 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12839 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12840 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12841
12842 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12843 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12844 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12845 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12846 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12847 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12848 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12849 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12850 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12851 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12852 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12853 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12854 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12855 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12856 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12857 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12858 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12859 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12860 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12861 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12862 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12863 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12864 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12865 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12866 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12867 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12868 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12869 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12870 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12871 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12872 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12873 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12874 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12875 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12876 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12877 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12878
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012879 vmcs12->guest_interruptibility_info =
12880 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12881 vmcs12->guest_pending_dbg_exceptions =
12882 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012883 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12884 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12885 else
12886 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012887
Jan Kiszkaf41245002014-03-07 20:03:13 +010012888 if (nested_cpu_has_preemption_timer(vmcs12)) {
12889 if (vmcs12->vm_exit_controls &
12890 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12891 vmcs12->vmx_preemption_timer_value =
12892 vmx_get_preemption_timer_value(vcpu);
12893 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12894 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012895
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012896 /*
12897 * In some cases (usually, nested EPT), L2 is allowed to change its
12898 * own CR3 without exiting. If it has changed it, we must keep it.
12899 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12900 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12901 *
12902 * Additionally, restore L2's PDPTR to vmcs12.
12903 */
12904 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012905 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012906 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12907 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12908 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12909 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12910 }
12911
Jim Mattsond281e132017-06-01 12:44:46 -070012912 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012913
Wincy Van608406e2015-02-03 23:57:51 +080012914 if (nested_cpu_has_vid(vmcs12))
12915 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12916
Jan Kiszkac18911a2013-03-13 16:06:41 +010012917 vmcs12->vm_entry_controls =
12918 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012919 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012920
Jan Kiszka2996fca2014-06-16 13:59:43 +020012921 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12922 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12923 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12924 }
12925
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012926 /* TODO: These cannot have changed unless we have MSR bitmaps and
12927 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012928 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012929 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012930 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12931 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012932 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12933 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12934 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012935 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012936 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012937}
12938
12939/*
12940 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12941 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12942 * and this function updates it to reflect the changes to the guest state while
12943 * L2 was running (and perhaps made some exits which were handled directly by L0
12944 * without going back to L1), and to reflect the exit reason.
12945 * Note that we do not have to copy here all VMCS fields, just those that
12946 * could have changed by the L2 guest or the exit - i.e., the guest-state and
12947 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
12948 * which already writes to vmcs12 directly.
12949 */
12950static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12951 u32 exit_reason, u32 exit_intr_info,
12952 unsigned long exit_qualification)
12953{
12954 /* update guest state fields: */
12955 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012956
12957 /* update exit information fields: */
12958
Jan Kiszka533558b2014-01-04 18:47:20 +010012959 vmcs12->vm_exit_reason = exit_reason;
12960 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010012961 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020012962
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012963 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012964 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
12965 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
12966
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012967 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070012968 vmcs12->launch_state = 1;
12969
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012970 /* vm_entry_intr_info_field is cleared on exit. Emulate this
12971 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012972 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012973
12974 /*
12975 * Transfer the event that L0 or L1 may wanted to inject into
12976 * L2 to IDT_VECTORING_INFO_FIELD.
12977 */
12978 vmcs12_save_pending_event(vcpu, vmcs12);
12979 }
12980
12981 /*
12982 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
12983 * preserved above and would only end up incorrectly in L1.
12984 */
12985 vcpu->arch.nmi_injected = false;
12986 kvm_clear_exception_queue(vcpu);
12987 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012988}
12989
Wanpeng Li5af41572017-11-05 16:54:49 -080012990static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
12991 struct vmcs12 *vmcs12)
12992{
12993 u32 entry_failure_code;
12994
12995 nested_ept_uninit_mmu_context(vcpu);
12996
12997 /*
12998 * Only PDPTE load can fail as the value of cr3 was checked on entry and
12999 * couldn't have changed.
13000 */
13001 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13002 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13003
13004 if (!enable_ept)
13005 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13006}
13007
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013008/*
13009 * A part of what we need to when the nested L2 guest exits and we want to
13010 * run its L1 parent, is to reset L1's guest state to the host state specified
13011 * in vmcs12.
13012 * This function is to be called not only on normal nested exit, but also on
13013 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13014 * Failures During or After Loading Guest State").
13015 * This function should be called when the active VMCS is L1's (vmcs01).
13016 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013017static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13018 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013019{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013020 struct kvm_segment seg;
13021
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013022 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13023 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013024 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013025 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13026 else
13027 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13028 vmx_set_efer(vcpu, vcpu->arch.efer);
13029
13030 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13031 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013032 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013033 /*
13034 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013035 * actually changed, because vmx_set_cr0 refers to efer set above.
13036 *
13037 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13038 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013039 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013040 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013041 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013042
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013043 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013044 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013045 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013046
Wanpeng Li5af41572017-11-05 16:54:49 -080013047 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013048
Liran Alon6f1e03b2018-05-22 17:16:14 +030013049 /*
13050 * If vmcs01 don't use VPID, CPU flushes TLB on every
13051 * VMEntry/VMExit. Thus, no need to flush TLB.
13052 *
13053 * If vmcs12 uses VPID, TLB entries populated by L2 are
13054 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13055 * with vmx->vpid. Thus, no need to flush TLB.
13056 *
13057 * Therefore, flush TLB only in case vmcs01 uses VPID and
13058 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13059 * are both tagged with vmx->vpid.
13060 */
13061 if (enable_vpid &&
13062 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080013063 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013064 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013065
13066 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13067 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13068 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13069 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13070 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020013071 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13072 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013073
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013074 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13075 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13076 vmcs_write64(GUEST_BNDCFGS, 0);
13077
Jan Kiszka44811c02013-08-04 17:17:27 +020013078 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013079 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013080 vcpu->arch.pat = vmcs12->host_ia32_pat;
13081 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013082 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13083 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13084 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013085
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013086 /* Set L1 segment info according to Intel SDM
13087 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13088 seg = (struct kvm_segment) {
13089 .base = 0,
13090 .limit = 0xFFFFFFFF,
13091 .selector = vmcs12->host_cs_selector,
13092 .type = 11,
13093 .present = 1,
13094 .s = 1,
13095 .g = 1
13096 };
13097 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13098 seg.l = 1;
13099 else
13100 seg.db = 1;
13101 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13102 seg = (struct kvm_segment) {
13103 .base = 0,
13104 .limit = 0xFFFFFFFF,
13105 .type = 3,
13106 .present = 1,
13107 .s = 1,
13108 .db = 1,
13109 .g = 1
13110 };
13111 seg.selector = vmcs12->host_ds_selector;
13112 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13113 seg.selector = vmcs12->host_es_selector;
13114 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13115 seg.selector = vmcs12->host_ss_selector;
13116 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13117 seg.selector = vmcs12->host_fs_selector;
13118 seg.base = vmcs12->host_fs_base;
13119 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13120 seg.selector = vmcs12->host_gs_selector;
13121 seg.base = vmcs12->host_gs_base;
13122 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13123 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013124 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013125 .limit = 0x67,
13126 .selector = vmcs12->host_tr_selector,
13127 .type = 11,
13128 .present = 1
13129 };
13130 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13131
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013132 kvm_set_dr(vcpu, 7, 0x400);
13133 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013134
Wincy Van3af18d92015-02-03 23:49:31 +080013135 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013136 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013137
Wincy Vanff651cb2014-12-11 08:52:58 +030013138 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13139 vmcs12->vm_exit_msr_load_count))
13140 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013141}
13142
13143/*
13144 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13145 * and modify vmcs12 to make it see what it would expect to see there if
13146 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13147 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013148static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13149 u32 exit_intr_info,
13150 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013151{
13152 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013153 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13154
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013155 /* trying to cancel vmlaunch/vmresume is a bug */
13156 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13157
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013158 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013159 * The only expected VM-instruction error is "VM entry with
13160 * invalid control field(s)." Anything else indicates a
13161 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013162 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013163 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13164 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13165
13166 leave_guest_mode(vcpu);
13167
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013168 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13169 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13170
Jim Mattson4f350c62017-09-14 16:31:44 -070013171 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013172 if (exit_reason == -1)
13173 sync_vmcs12(vcpu, vmcs12);
13174 else
13175 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13176 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013177
Liran Alon61ada742018-06-23 02:35:08 +030013178 /*
13179 * Must happen outside of sync_vmcs12() as it will
13180 * also be used to capture vmcs12 cache as part of
13181 * capturing nVMX state for snapshot (migration).
13182 *
13183 * Otherwise, this flush will dirty guest memory at a
13184 * point it is already assumed by user-space to be
13185 * immutable.
13186 */
13187 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13188
Jim Mattson4f350c62017-09-14 16:31:44 -070013189 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13190 vmcs12->vm_exit_msr_store_count))
13191 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013192 }
13193
Jim Mattson4f350c62017-09-14 16:31:44 -070013194 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013195 vm_entry_controls_reset_shadow(vmx);
13196 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013197 vmx_segment_cache_clear(vmx);
13198
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013199 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013200 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13201 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013202 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013203 if (vmx->hv_deadline_tsc == -1)
13204 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13205 PIN_BASED_VMX_PREEMPTION_TIMER);
13206 else
13207 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13208 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070013209 if (kvm_has_tsc_control)
13210 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013211
Jim Mattson8d860bb2018-05-09 16:56:05 -040013212 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13213 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13214 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013215 } else if (!nested_cpu_has_ept(vmcs12) &&
13216 nested_cpu_has2(vmcs12,
13217 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013218 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013219 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013220
13221 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13222 vmx->host_rsp = 0;
13223
13224 /* Unpin physical memory we referred to in vmcs02 */
13225 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013226 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013227 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013228 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013229 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013230 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013231 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013232 }
Wincy Van705699a2015-02-03 23:58:17 +080013233 if (vmx->nested.pi_desc_page) {
13234 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013235 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013236 vmx->nested.pi_desc_page = NULL;
13237 vmx->nested.pi_desc = NULL;
13238 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013239
13240 /*
Tang Chen38b99172014-09-24 15:57:54 +080013241 * We are now running in L2, mmu_notifier will force to reload the
13242 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13243 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013244 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013245
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013246 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013247 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013248
13249 /* in case we halted in L2 */
13250 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013251
13252 if (likely(!vmx->fail)) {
13253 /*
13254 * TODO: SDM says that with acknowledge interrupt on
13255 * exit, bit 31 of the VM-exit interrupt information
13256 * (valid interrupt) is always set to 1 on
13257 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13258 * need kvm_cpu_has_interrupt(). See the commit
13259 * message for details.
13260 */
13261 if (nested_exit_intr_ack_set(vcpu) &&
13262 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13263 kvm_cpu_has_interrupt(vcpu)) {
13264 int irq = kvm_cpu_get_interrupt(vcpu);
13265 WARN_ON(irq < 0);
13266 vmcs12->vm_exit_intr_info = irq |
13267 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13268 }
13269
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013270 if (exit_reason != -1)
13271 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13272 vmcs12->exit_qualification,
13273 vmcs12->idt_vectoring_info_field,
13274 vmcs12->vm_exit_intr_info,
13275 vmcs12->vm_exit_intr_error_code,
13276 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013277
13278 load_vmcs12_host_state(vcpu, vmcs12);
13279
13280 return;
13281 }
13282
13283 /*
13284 * After an early L2 VM-entry failure, we're now back
13285 * in L1 which thinks it just finished a VMLAUNCH or
13286 * VMRESUME instruction, so we need to set the failure
13287 * flag and the VM-instruction error field of the VMCS
13288 * accordingly.
13289 */
13290 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013291
13292 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13293
Jim Mattson4f350c62017-09-14 16:31:44 -070013294 /*
13295 * The emulated instruction was already skipped in
13296 * nested_vmx_run, but the updated RIP was never
13297 * written back to the vmcs01.
13298 */
13299 skip_emulated_instruction(vcpu);
13300 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013301}
13302
Nadav Har'El7c177932011-05-25 23:12:04 +030013303/*
Jan Kiszka42124922014-01-04 18:47:19 +010013304 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13305 */
13306static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13307{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013308 if (is_guest_mode(vcpu)) {
13309 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013310 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013311 }
Jan Kiszka42124922014-01-04 18:47:19 +010013312 free_nested(to_vmx(vcpu));
13313}
13314
13315/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013316 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13317 * 23.7 "VM-entry failures during or after loading guest state" (this also
13318 * lists the acceptable exit-reason and exit-qualification parameters).
13319 * It should only be called before L2 actually succeeded to run, and when
13320 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13321 */
13322static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13323 struct vmcs12 *vmcs12,
13324 u32 reason, unsigned long qualification)
13325{
13326 load_vmcs12_host_state(vcpu, vmcs12);
13327 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13328 vmcs12->exit_qualification = qualification;
13329 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013330 if (enable_shadow_vmcs)
13331 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013332}
13333
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013334static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13335 struct x86_instruction_info *info,
13336 enum x86_intercept_stage stage)
13337{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013338 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13339 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13340
13341 /*
13342 * RDPID causes #UD if disabled through secondary execution controls.
13343 * Because it is marked as EmulateOnUD, we need to intercept it here.
13344 */
13345 if (info->intercept == x86_intercept_rdtscp &&
13346 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13347 ctxt->exception.vector = UD_VECTOR;
13348 ctxt->exception.error_code_valid = false;
13349 return X86EMUL_PROPAGATE_FAULT;
13350 }
13351
13352 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013353 return X86EMUL_CONTINUE;
13354}
13355
Yunhong Jiang64672c92016-06-13 14:19:59 -070013356#ifdef CONFIG_X86_64
13357/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13358static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13359 u64 divisor, u64 *result)
13360{
13361 u64 low = a << shift, high = a >> (64 - shift);
13362
13363 /* To avoid the overflow on divq */
13364 if (high >= divisor)
13365 return 1;
13366
13367 /* Low hold the result, high hold rem which is discarded */
13368 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13369 "rm" (divisor), "0" (low), "1" (high));
13370 *result = low;
13371
13372 return 0;
13373}
13374
13375static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13376{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013377 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013378 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013379
13380 if (kvm_mwait_in_guest(vcpu->kvm))
13381 return -EOPNOTSUPP;
13382
13383 vmx = to_vmx(vcpu);
13384 tscl = rdtsc();
13385 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13386 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013387 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13388
13389 if (delta_tsc > lapic_timer_advance_cycles)
13390 delta_tsc -= lapic_timer_advance_cycles;
13391 else
13392 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013393
13394 /* Convert to host delta tsc if tsc scaling is enabled */
13395 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13396 u64_shl_div_u64(delta_tsc,
13397 kvm_tsc_scaling_ratio_frac_bits,
13398 vcpu->arch.tsc_scaling_ratio,
13399 &delta_tsc))
13400 return -ERANGE;
13401
13402 /*
13403 * If the delta tsc can't fit in the 32 bit after the multi shift,
13404 * we can't use the preemption timer.
13405 * It's possible that it fits on later vmentries, but checking
13406 * on every vmentry is costly so we just use an hrtimer.
13407 */
13408 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13409 return -ERANGE;
13410
13411 vmx->hv_deadline_tsc = tscl + delta_tsc;
13412 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13413 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013414
13415 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013416}
13417
13418static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13419{
13420 struct vcpu_vmx *vmx = to_vmx(vcpu);
13421 vmx->hv_deadline_tsc = -1;
13422 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13423 PIN_BASED_VMX_PREEMPTION_TIMER);
13424}
13425#endif
13426
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013427static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013428{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013429 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013430 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013431}
13432
Kai Huang843e4332015-01-28 10:54:28 +080013433static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13434 struct kvm_memory_slot *slot)
13435{
13436 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13437 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13438}
13439
13440static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13441 struct kvm_memory_slot *slot)
13442{
13443 kvm_mmu_slot_set_dirty(kvm, slot);
13444}
13445
13446static void vmx_flush_log_dirty(struct kvm *kvm)
13447{
13448 kvm_flush_pml_buffers(kvm);
13449}
13450
Bandan Dasc5f983f2017-05-05 15:25:14 -040013451static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13452{
13453 struct vmcs12 *vmcs12;
13454 struct vcpu_vmx *vmx = to_vmx(vcpu);
13455 gpa_t gpa;
13456 struct page *page = NULL;
13457 u64 *pml_address;
13458
13459 if (is_guest_mode(vcpu)) {
13460 WARN_ON_ONCE(vmx->nested.pml_full);
13461
13462 /*
13463 * Check if PML is enabled for the nested guest.
13464 * Whether eptp bit 6 is set is already checked
13465 * as part of A/D emulation.
13466 */
13467 vmcs12 = get_vmcs12(vcpu);
13468 if (!nested_cpu_has_pml(vmcs12))
13469 return 0;
13470
Dan Carpenter47698862017-05-10 22:43:17 +030013471 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013472 vmx->nested.pml_full = true;
13473 return 1;
13474 }
13475
13476 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13477
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013478 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13479 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013480 return 0;
13481
13482 pml_address = kmap(page);
13483 pml_address[vmcs12->guest_pml_index--] = gpa;
13484 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013485 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013486 }
13487
13488 return 0;
13489}
13490
Kai Huang843e4332015-01-28 10:54:28 +080013491static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13492 struct kvm_memory_slot *memslot,
13493 gfn_t offset, unsigned long mask)
13494{
13495 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13496}
13497
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013498static void __pi_post_block(struct kvm_vcpu *vcpu)
13499{
13500 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13501 struct pi_desc old, new;
13502 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013503
13504 do {
13505 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013506 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13507 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013508
13509 dest = cpu_physical_id(vcpu->cpu);
13510
13511 if (x2apic_enabled())
13512 new.ndst = dest;
13513 else
13514 new.ndst = (dest << 8) & 0xFF00;
13515
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013516 /* set 'NV' to 'notification vector' */
13517 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013518 } while (cmpxchg64(&pi_desc->control, old.control,
13519 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013520
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013521 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13522 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013523 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013524 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013525 vcpu->pre_pcpu = -1;
13526 }
13527}
13528
Feng Wuefc64402015-09-18 22:29:51 +080013529/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013530 * This routine does the following things for vCPU which is going
13531 * to be blocked if VT-d PI is enabled.
13532 * - Store the vCPU to the wakeup list, so when interrupts happen
13533 * we can find the right vCPU to wake up.
13534 * - Change the Posted-interrupt descriptor as below:
13535 * 'NDST' <-- vcpu->pre_pcpu
13536 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13537 * - If 'ON' is set during this process, which means at least one
13538 * interrupt is posted for this vCPU, we cannot block it, in
13539 * this case, return 1, otherwise, return 0.
13540 *
13541 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013542static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013543{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013544 unsigned int dest;
13545 struct pi_desc old, new;
13546 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13547
13548 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013549 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13550 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013551 return 0;
13552
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013553 WARN_ON(irqs_disabled());
13554 local_irq_disable();
13555 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13556 vcpu->pre_pcpu = vcpu->cpu;
13557 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13558 list_add_tail(&vcpu->blocked_vcpu_list,
13559 &per_cpu(blocked_vcpu_on_cpu,
13560 vcpu->pre_pcpu));
13561 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13562 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013563
13564 do {
13565 old.control = new.control = pi_desc->control;
13566
Feng Wubf9f6ac2015-09-18 22:29:55 +080013567 WARN((pi_desc->sn == 1),
13568 "Warning: SN field of posted-interrupts "
13569 "is set before blocking\n");
13570
13571 /*
13572 * Since vCPU can be preempted during this process,
13573 * vcpu->cpu could be different with pre_pcpu, we
13574 * need to set pre_pcpu as the destination of wakeup
13575 * notification event, then we can find the right vCPU
13576 * to wakeup in wakeup handler if interrupts happen
13577 * when the vCPU is in blocked state.
13578 */
13579 dest = cpu_physical_id(vcpu->pre_pcpu);
13580
13581 if (x2apic_enabled())
13582 new.ndst = dest;
13583 else
13584 new.ndst = (dest << 8) & 0xFF00;
13585
13586 /* set 'NV' to 'wakeup vector' */
13587 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013588 } while (cmpxchg64(&pi_desc->control, old.control,
13589 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013590
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013591 /* We should not block the vCPU if an interrupt is posted for it. */
13592 if (pi_test_on(pi_desc) == 1)
13593 __pi_post_block(vcpu);
13594
13595 local_irq_enable();
13596 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013597}
13598
Yunhong Jiangbc225122016-06-13 14:19:58 -070013599static int vmx_pre_block(struct kvm_vcpu *vcpu)
13600{
13601 if (pi_pre_block(vcpu))
13602 return 1;
13603
Yunhong Jiang64672c92016-06-13 14:19:59 -070013604 if (kvm_lapic_hv_timer_in_use(vcpu))
13605 kvm_lapic_switch_to_sw_timer(vcpu);
13606
Yunhong Jiangbc225122016-06-13 14:19:58 -070013607 return 0;
13608}
13609
13610static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013611{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013612 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013613 return;
13614
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013615 WARN_ON(irqs_disabled());
13616 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013617 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013618 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013619}
13620
Yunhong Jiangbc225122016-06-13 14:19:58 -070013621static void vmx_post_block(struct kvm_vcpu *vcpu)
13622{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013623 if (kvm_x86_ops->set_hv_timer)
13624 kvm_lapic_switch_to_hv_timer(vcpu);
13625
Yunhong Jiangbc225122016-06-13 14:19:58 -070013626 pi_post_block(vcpu);
13627}
13628
Feng Wubf9f6ac2015-09-18 22:29:55 +080013629/*
Feng Wuefc64402015-09-18 22:29:51 +080013630 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13631 *
13632 * @kvm: kvm
13633 * @host_irq: host irq of the interrupt
13634 * @guest_irq: gsi of the interrupt
13635 * @set: set or unset PI
13636 * returns 0 on success, < 0 on failure
13637 */
13638static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13639 uint32_t guest_irq, bool set)
13640{
13641 struct kvm_kernel_irq_routing_entry *e;
13642 struct kvm_irq_routing_table *irq_rt;
13643 struct kvm_lapic_irq irq;
13644 struct kvm_vcpu *vcpu;
13645 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013646 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013647
13648 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013649 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13650 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013651 return 0;
13652
13653 idx = srcu_read_lock(&kvm->irq_srcu);
13654 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013655 if (guest_irq >= irq_rt->nr_rt_entries ||
13656 hlist_empty(&irq_rt->map[guest_irq])) {
13657 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13658 guest_irq, irq_rt->nr_rt_entries);
13659 goto out;
13660 }
Feng Wuefc64402015-09-18 22:29:51 +080013661
13662 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13663 if (e->type != KVM_IRQ_ROUTING_MSI)
13664 continue;
13665 /*
13666 * VT-d PI cannot support posting multicast/broadcast
13667 * interrupts to a vCPU, we still use interrupt remapping
13668 * for these kind of interrupts.
13669 *
13670 * For lowest-priority interrupts, we only support
13671 * those with single CPU as the destination, e.g. user
13672 * configures the interrupts via /proc/irq or uses
13673 * irqbalance to make the interrupts single-CPU.
13674 *
13675 * We will support full lowest-priority interrupt later.
13676 */
13677
Radim Krčmář371313132016-07-12 22:09:27 +020013678 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013679 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13680 /*
13681 * Make sure the IRTE is in remapped mode if
13682 * we don't handle it in posted mode.
13683 */
13684 ret = irq_set_vcpu_affinity(host_irq, NULL);
13685 if (ret < 0) {
13686 printk(KERN_INFO
13687 "failed to back to remapped mode, irq: %u\n",
13688 host_irq);
13689 goto out;
13690 }
13691
Feng Wuefc64402015-09-18 22:29:51 +080013692 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013693 }
Feng Wuefc64402015-09-18 22:29:51 +080013694
13695 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13696 vcpu_info.vector = irq.vector;
13697
hu huajun2698d822018-04-11 15:16:40 +080013698 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013699 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13700
13701 if (set)
13702 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080013703 else
Feng Wuefc64402015-09-18 22:29:51 +080013704 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013705
13706 if (ret < 0) {
13707 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13708 __func__);
13709 goto out;
13710 }
13711 }
13712
13713 ret = 0;
13714out:
13715 srcu_read_unlock(&kvm->irq_srcu, idx);
13716 return ret;
13717}
13718
Ashok Rajc45dcc72016-06-22 14:59:56 +080013719static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13720{
13721 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13722 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13723 FEATURE_CONTROL_LMCE;
13724 else
13725 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13726 ~FEATURE_CONTROL_LMCE;
13727}
13728
Ladi Prosek72d7b372017-10-11 16:54:41 +020013729static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13730{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013731 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13732 if (to_vmx(vcpu)->nested.nested_run_pending)
13733 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013734 return 1;
13735}
13736
Ladi Prosek0234bf82017-10-11 16:54:40 +020013737static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13738{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013739 struct vcpu_vmx *vmx = to_vmx(vcpu);
13740
13741 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13742 if (vmx->nested.smm.guest_mode)
13743 nested_vmx_vmexit(vcpu, -1, 0, 0);
13744
13745 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13746 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013747 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013748 return 0;
13749}
13750
13751static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13752{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013753 struct vcpu_vmx *vmx = to_vmx(vcpu);
13754 int ret;
13755
13756 if (vmx->nested.smm.vmxon) {
13757 vmx->nested.vmxon = true;
13758 vmx->nested.smm.vmxon = false;
13759 }
13760
13761 if (vmx->nested.smm.guest_mode) {
13762 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013763 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013764 vcpu->arch.hflags |= HF_SMM_MASK;
13765 if (ret)
13766 return ret;
13767
13768 vmx->nested.smm.guest_mode = false;
13769 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013770 return 0;
13771}
13772
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013773static int enable_smi_window(struct kvm_vcpu *vcpu)
13774{
13775 return 0;
13776}
13777
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013778static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13779 struct kvm_nested_state __user *user_kvm_nested_state,
13780 u32 user_data_size)
13781{
13782 struct vcpu_vmx *vmx;
13783 struct vmcs12 *vmcs12;
13784 struct kvm_nested_state kvm_state = {
13785 .flags = 0,
13786 .format = 0,
13787 .size = sizeof(kvm_state),
13788 .vmx.vmxon_pa = -1ull,
13789 .vmx.vmcs_pa = -1ull,
13790 };
13791
13792 if (!vcpu)
13793 return kvm_state.size + 2 * VMCS12_SIZE;
13794
13795 vmx = to_vmx(vcpu);
13796 vmcs12 = get_vmcs12(vcpu);
13797 if (nested_vmx_allowed(vcpu) &&
13798 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13799 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13800 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13801
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013802 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013803 kvm_state.size += VMCS12_SIZE;
13804
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013805 if (is_guest_mode(vcpu) &&
13806 nested_cpu_has_shadow_vmcs(vmcs12) &&
13807 vmcs12->vmcs_link_pointer != -1ull)
13808 kvm_state.size += VMCS12_SIZE;
13809 }
13810
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013811 if (vmx->nested.smm.vmxon)
13812 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13813
13814 if (vmx->nested.smm.guest_mode)
13815 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13816
13817 if (is_guest_mode(vcpu)) {
13818 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13819
13820 if (vmx->nested.nested_run_pending)
13821 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13822 }
13823 }
13824
13825 if (user_data_size < kvm_state.size)
13826 goto out;
13827
13828 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13829 return -EFAULT;
13830
13831 if (vmx->nested.current_vmptr == -1ull)
13832 goto out;
13833
13834 /*
13835 * When running L2, the authoritative vmcs12 state is in the
13836 * vmcs02. When running L1, the authoritative vmcs12 state is
13837 * in the shadow vmcs linked to vmcs01, unless
13838 * sync_shadow_vmcs is set, in which case, the authoritative
13839 * vmcs12 state is in the vmcs12 already.
13840 */
13841 if (is_guest_mode(vcpu))
13842 sync_vmcs12(vcpu, vmcs12);
13843 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13844 copy_shadow_to_vmcs12(vmx);
13845
13846 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13847 return -EFAULT;
13848
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013849 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13850 vmcs12->vmcs_link_pointer != -1ull) {
13851 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13852 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13853 return -EFAULT;
13854 }
13855
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013856out:
13857 return kvm_state.size;
13858}
13859
13860static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13861 struct kvm_nested_state __user *user_kvm_nested_state,
13862 struct kvm_nested_state *kvm_state)
13863{
13864 struct vcpu_vmx *vmx = to_vmx(vcpu);
13865 struct vmcs12 *vmcs12;
13866 u32 exit_qual;
13867 int ret;
13868
13869 if (kvm_state->format != 0)
13870 return -EINVAL;
13871
13872 if (!nested_vmx_allowed(vcpu))
13873 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13874
13875 if (kvm_state->vmx.vmxon_pa == -1ull) {
13876 if (kvm_state->vmx.smm.flags)
13877 return -EINVAL;
13878
13879 if (kvm_state->vmx.vmcs_pa != -1ull)
13880 return -EINVAL;
13881
13882 vmx_leave_nested(vcpu);
13883 return 0;
13884 }
13885
13886 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13887 return -EINVAL;
13888
13889 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13890 return -EINVAL;
13891
13892 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13893 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13894 return -EINVAL;
13895
13896 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13897 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13898 return -EINVAL;
13899
13900 if (kvm_state->vmx.smm.flags &
13901 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13902 return -EINVAL;
13903
13904 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13905 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13906 return -EINVAL;
13907
13908 vmx_leave_nested(vcpu);
13909 if (kvm_state->vmx.vmxon_pa == -1ull)
13910 return 0;
13911
13912 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13913 ret = enter_vmx_operation(vcpu);
13914 if (ret)
13915 return ret;
13916
13917 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13918
13919 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13920 vmx->nested.smm.vmxon = true;
13921 vmx->nested.vmxon = false;
13922
13923 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13924 vmx->nested.smm.guest_mode = true;
13925 }
13926
13927 vmcs12 = get_vmcs12(vcpu);
13928 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13929 return -EFAULT;
13930
Liran Alon392b2f22018-06-23 02:35:01 +030013931 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013932 return -EINVAL;
13933
13934 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13935 return 0;
13936
13937 vmx->nested.nested_run_pending =
13938 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13939
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013940 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13941 vmcs12->vmcs_link_pointer != -1ull) {
13942 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13943 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13944 return -EINVAL;
13945
13946 if (copy_from_user(shadow_vmcs12,
13947 user_kvm_nested_state->data + VMCS12_SIZE,
13948 sizeof(*vmcs12)))
13949 return -EFAULT;
13950
13951 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
13952 !shadow_vmcs12->hdr.shadow_vmcs)
13953 return -EINVAL;
13954 }
13955
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013956 if (check_vmentry_prereqs(vcpu, vmcs12) ||
13957 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
13958 return -EINVAL;
13959
13960 if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING)
13961 vmx->nested.nested_run_pending = 1;
13962
13963 vmx->nested.dirty_vmcs12 = true;
13964 ret = enter_vmx_non_root_mode(vcpu, NULL);
13965 if (ret)
13966 return -EINVAL;
13967
13968 return 0;
13969}
13970
Kees Cook404f6aa2016-08-08 16:29:06 -070013971static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080013972 .cpu_has_kvm_support = cpu_has_kvm_support,
13973 .disabled_by_bios = vmx_disabled_by_bios,
13974 .hardware_setup = hardware_setup,
13975 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030013976 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013977 .hardware_enable = hardware_enable,
13978 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080013979 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020013980 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013981
Wanpeng Lib31c1142018-03-12 04:53:04 -070013982 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070013983 .vm_alloc = vmx_vm_alloc,
13984 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070013985
Avi Kivity6aa8b732006-12-10 02:21:36 -080013986 .vcpu_create = vmx_create_vcpu,
13987 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030013988 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013989
Sean Christopherson6d6095b2018-07-23 12:32:44 -070013990 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013991 .vcpu_load = vmx_vcpu_load,
13992 .vcpu_put = vmx_vcpu_put,
13993
Paolo Bonzinia96036b2015-11-10 11:55:36 +010013994 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060013995 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080013996 .get_msr = vmx_get_msr,
13997 .set_msr = vmx_set_msr,
13998 .get_segment_base = vmx_get_segment_base,
13999 .get_segment = vmx_get_segment,
14000 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014001 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014002 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014003 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014004 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014005 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014006 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014007 .set_cr3 = vmx_set_cr3,
14008 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014009 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014010 .get_idt = vmx_get_idt,
14011 .set_idt = vmx_set_idt,
14012 .get_gdt = vmx_get_gdt,
14013 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014014 .get_dr6 = vmx_get_dr6,
14015 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014016 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014017 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014018 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014019 .get_rflags = vmx_get_rflags,
14020 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014021
Avi Kivity6aa8b732006-12-10 02:21:36 -080014022 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014023 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014024
Avi Kivity6aa8b732006-12-10 02:21:36 -080014025 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014026 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014027 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014028 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14029 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014030 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014031 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014032 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014033 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014034 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014035 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014036 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014037 .get_nmi_mask = vmx_get_nmi_mask,
14038 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014039 .enable_nmi_window = enable_nmi_window,
14040 .enable_irq_window = enable_irq_window,
14041 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014042 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014043 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014044 .get_enable_apicv = vmx_get_enable_apicv,
14045 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014046 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014047 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014048 .hwapic_irr_update = vmx_hwapic_irr_update,
14049 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080014050 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14051 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014052
Izik Eiduscbc94022007-10-25 00:29:55 +020014053 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014054 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014055 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014056 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014057
Avi Kivity586f9602010-11-18 13:09:54 +020014058 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014059
Sheng Yang17cc3932010-01-05 19:02:27 +080014060 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014061
14062 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014063
14064 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014065 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014066
14067 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014068
14069 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014070
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014071 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014072 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014073
14074 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014075
14076 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014077 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014078 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014079 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014080 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014081
14082 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014083
14084 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014085
14086 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14087 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14088 .flush_log_dirty = vmx_flush_log_dirty,
14089 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014090 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014091
Feng Wubf9f6ac2015-09-18 22:29:55 +080014092 .pre_block = vmx_pre_block,
14093 .post_block = vmx_post_block,
14094
Wei Huang25462f72015-06-19 15:45:05 +020014095 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014096
14097 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014098
14099#ifdef CONFIG_X86_64
14100 .set_hv_timer = vmx_set_hv_timer,
14101 .cancel_hv_timer = vmx_cancel_hv_timer,
14102#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014103
14104 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014105
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014106 .get_nested_state = vmx_get_nested_state,
14107 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014108 .get_vmcs12_pages = nested_get_vmcs12_pages,
14109
Ladi Prosek72d7b372017-10-11 16:54:41 +020014110 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014111 .pre_enter_smm = vmx_pre_enter_smm,
14112 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014113 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014114};
14115
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014116static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014117{
14118 if (vmx_l1d_flush_pages) {
14119 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14120 vmx_l1d_flush_pages = NULL;
14121 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014122 /* Restore state so sysfs ignores VMX */
14123 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014124}
14125
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014126static void vmx_exit(void)
14127{
14128#ifdef CONFIG_KEXEC_CORE
14129 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14130 synchronize_rcu();
14131#endif
14132
14133 kvm_exit();
14134
14135#if IS_ENABLED(CONFIG_HYPERV)
14136 if (static_branch_unlikely(&enable_evmcs)) {
14137 int cpu;
14138 struct hv_vp_assist_page *vp_ap;
14139 /*
14140 * Reset everything to support using non-enlightened VMCS
14141 * access later (e.g. when we reload the module with
14142 * enlightened_vmcs=0)
14143 */
14144 for_each_online_cpu(cpu) {
14145 vp_ap = hv_get_vp_assist_page(cpu);
14146
14147 if (!vp_ap)
14148 continue;
14149
14150 vp_ap->current_nested_vmcs = 0;
14151 vp_ap->enlighten_vmentry = 0;
14152 }
14153
14154 static_branch_disable(&enable_evmcs);
14155 }
14156#endif
14157 vmx_cleanup_l1d_flush();
14158}
14159module_exit(vmx_exit);
14160
Avi Kivity6aa8b732006-12-10 02:21:36 -080014161static int __init vmx_init(void)
14162{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014163 int r;
14164
14165#if IS_ENABLED(CONFIG_HYPERV)
14166 /*
14167 * Enlightened VMCS usage should be recommended and the host needs
14168 * to support eVMCS v1 or above. We can also disable eVMCS support
14169 * with module parameter.
14170 */
14171 if (enlightened_vmcs &&
14172 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14173 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14174 KVM_EVMCS_VERSION) {
14175 int cpu;
14176
14177 /* Check that we have assist pages on all online CPUs */
14178 for_each_online_cpu(cpu) {
14179 if (!hv_get_vp_assist_page(cpu)) {
14180 enlightened_vmcs = false;
14181 break;
14182 }
14183 }
14184
14185 if (enlightened_vmcs) {
14186 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14187 static_branch_enable(&enable_evmcs);
14188 }
14189 } else {
14190 enlightened_vmcs = false;
14191 }
14192#endif
14193
14194 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014195 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014196 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014197 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014198
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014199 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014200 * Must be called after kvm_init() so enable_ept is properly set
14201 * up. Hand the parameter mitigation value in which was stored in
14202 * the pre module init parser. If no parameter was given, it will
14203 * contain 'auto' which will be turned into the default 'cond'
14204 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014205 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014206 if (boot_cpu_has(X86_BUG_L1TF)) {
14207 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14208 if (r) {
14209 vmx_exit();
14210 return r;
14211 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014212 }
14213
Dave Young2965faa2015-09-09 15:38:55 -070014214#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014215 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14216 crash_vmclear_local_loaded_vmcss);
14217#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014218 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014219
He, Qingfdef3ad2007-04-30 09:45:24 +030014220 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014221}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014222module_init(vmx_init);