blob: 581bdbd9844b4b448ab1c7040b85b19a21055146 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140#define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200143
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800144#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200145#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
Avi Kivity78ac8b42010-04-08 18:19:35 +0300148#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
Jan Kiszkaf4124502014-03-07 20:03:13 +0100150#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156#define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162/*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500166 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200195
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200201 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200202} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200203 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
204 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
205 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
206 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
207 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
208 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200209};
210
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200211#define L1D_CACHE_ORDER 4
212static void *vmx_l1d_flush_pages;
213
214static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
215{
216 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200217 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200218
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200219 if (!enable_ept) {
220 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
221 return 0;
222 }
223
Yi Wangd806afa2018-08-16 13:42:39 +0800224 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
225 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200226
Yi Wangd806afa2018-08-16 13:42:39 +0800227 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
228 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
229 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
230 return 0;
231 }
232 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200233
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200234 /* If set to auto use the default l1tf mitigation method */
235 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
236 switch (l1tf_mitigation) {
237 case L1TF_MITIGATION_OFF:
238 l1tf = VMENTER_L1D_FLUSH_NEVER;
239 break;
240 case L1TF_MITIGATION_FLUSH_NOWARN:
241 case L1TF_MITIGATION_FLUSH:
242 case L1TF_MITIGATION_FLUSH_NOSMT:
243 l1tf = VMENTER_L1D_FLUSH_COND;
244 break;
245 case L1TF_MITIGATION_FULL:
246 case L1TF_MITIGATION_FULL_FORCE:
247 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
248 break;
249 }
250 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
251 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
252 }
253
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200254 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
255 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
256 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
257 if (!page)
258 return -ENOMEM;
259 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200260
261 /*
262 * Initialize each page with a different pattern in
263 * order to protect against KSM in the nested
264 * virtualization case.
265 */
266 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
267 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
268 PAGE_SIZE);
269 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200270 }
271
272 l1tf_vmx_mitigation = l1tf;
273
Thomas Gleixner895ae472018-07-13 16:23:22 +0200274 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
275 static_branch_enable(&vmx_l1d_should_flush);
276 else
277 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200278
Nicolai Stange427362a2018-07-21 22:25:00 +0200279 if (l1tf == VMENTER_L1D_FLUSH_COND)
280 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200281 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200282 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200283 return 0;
284}
285
286static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200287{
288 unsigned int i;
289
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200290 if (s) {
291 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200292 if (vmentry_l1d_param[i].for_parse &&
293 sysfs_streq(s, vmentry_l1d_param[i].option))
294 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200295 }
296 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200297 return -EINVAL;
298}
299
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200300static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
301{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200302 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200304 l1tf = vmentry_l1d_flush_parse(s);
305 if (l1tf < 0)
306 return l1tf;
307
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200308 if (!boot_cpu_has(X86_BUG_L1TF))
309 return 0;
310
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200311 /*
312 * Has vmx_init() run already? If not then this is the pre init
313 * parameter parsing. In that case just store the value and let
314 * vmx_init() do the proper setup after enable_ept has been
315 * established.
316 */
317 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
318 vmentry_l1d_flush_param = l1tf;
319 return 0;
320 }
321
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200322 mutex_lock(&vmx_l1d_flush_mutex);
323 ret = vmx_setup_l1d_flush(l1tf);
324 mutex_unlock(&vmx_l1d_flush_mutex);
325 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200326}
327
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200328static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
329{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200330 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
331 return sprintf(s, "???\n");
332
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200333 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200334}
335
336static const struct kernel_param_ops vmentry_l1d_flush_ops = {
337 .set = vmentry_l1d_flush_set,
338 .get = vmentry_l1d_flush_get,
339};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200340module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200341
Tianyu Lan877ad952018-07-19 08:40:23 +0000342enum ept_pointers_status {
343 EPT_POINTERS_CHECK = 0,
344 EPT_POINTERS_MATCH = 1,
345 EPT_POINTERS_MISMATCH = 2
346};
347
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700348struct kvm_vmx {
349 struct kvm kvm;
350
351 unsigned int tss_addr;
352 bool ept_identity_pagetable_done;
353 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000354
355 enum ept_pointers_status ept_pointers_match;
356 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700357};
358
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200359#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300360
Liran Alon392b2f22018-06-23 02:35:01 +0300361struct vmcs_hdr {
362 u32 revision_id:31;
363 u32 shadow_vmcs:1;
364};
365
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400366struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300367 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400368 u32 abort;
369 char data[0];
370};
371
Nadav Har'Eld462b812011-05-24 15:26:10 +0300372/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700373 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
374 * and whose values change infrequently, but are not constant. I.e. this is
375 * used as a write-through cache of the corresponding VMCS fields.
376 */
377struct vmcs_host_state {
378 unsigned long cr3; /* May not match real cr3 */
379 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700380 unsigned long gs_base;
381 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700382
383 u16 fs_sel, gs_sel, ldt_sel;
384#ifdef CONFIG_X86_64
385 u16 ds_sel, es_sel;
386#endif
387};
388
389/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300390 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
391 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
392 * loaded on this CPU (so we can clear them if the CPU goes down).
393 */
394struct loaded_vmcs {
395 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700396 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300397 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200398 bool launched;
399 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700400 bool hv_timer_armed;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100401 /* Support for vnmi-less CPUs */
402 int soft_vnmi_blocked;
403 ktime_t entry_time;
404 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100405 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300406 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700407 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300408};
409
Avi Kivity26bb0982009-09-07 11:14:12 +0300410struct shared_msr_entry {
411 unsigned index;
412 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200413 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300414};
415
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300416/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300417 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
418 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
419 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
420 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
421 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
422 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600423 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300424 * underlying hardware which will be used to run L2.
425 * This structure is packed to ensure that its layout is identical across
426 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700427 *
428 * IMPORTANT: Changing the layout of existing fields in this structure
429 * will break save/restore compatibility with older kvm releases. When
430 * adding new fields, either use space in the reserved padding* arrays
431 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300432 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300433typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300434struct __packed vmcs12 {
435 /* According to the Intel spec, a VMCS region must start with the
436 * following two fields. Then follow implementation-specific data.
437 */
Liran Alon392b2f22018-06-23 02:35:01 +0300438 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300439 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300440
Nadav Har'El27d6c862011-05-25 23:06:59 +0300441 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
442 u32 padding[7]; /* room for future expansion */
443
Nadav Har'El22bd0352011-05-25 23:05:57 +0300444 u64 io_bitmap_a;
445 u64 io_bitmap_b;
446 u64 msr_bitmap;
447 u64 vm_exit_msr_store_addr;
448 u64 vm_exit_msr_load_addr;
449 u64 vm_entry_msr_load_addr;
450 u64 tsc_offset;
451 u64 virtual_apic_page_addr;
452 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800453 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300454 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800455 u64 eoi_exit_bitmap0;
456 u64 eoi_exit_bitmap1;
457 u64 eoi_exit_bitmap2;
458 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800459 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300460 u64 guest_physical_address;
461 u64 vmcs_link_pointer;
462 u64 guest_ia32_debugctl;
463 u64 guest_ia32_pat;
464 u64 guest_ia32_efer;
465 u64 guest_ia32_perf_global_ctrl;
466 u64 guest_pdptr0;
467 u64 guest_pdptr1;
468 u64 guest_pdptr2;
469 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100470 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300471 u64 host_ia32_pat;
472 u64 host_ia32_efer;
473 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700474 u64 vmread_bitmap;
475 u64 vmwrite_bitmap;
476 u64 vm_function_control;
477 u64 eptp_list_address;
478 u64 pml_address;
479 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300480 /*
481 * To allow migration of L1 (complete with its L2 guests) between
482 * machines of different natural widths (32 or 64 bit), we cannot have
483 * unsigned long fields with no explict size. We use u64 (aliased
484 * natural_width) instead. Luckily, x86 is little-endian.
485 */
486 natural_width cr0_guest_host_mask;
487 natural_width cr4_guest_host_mask;
488 natural_width cr0_read_shadow;
489 natural_width cr4_read_shadow;
490 natural_width cr3_target_value0;
491 natural_width cr3_target_value1;
492 natural_width cr3_target_value2;
493 natural_width cr3_target_value3;
494 natural_width exit_qualification;
495 natural_width guest_linear_address;
496 natural_width guest_cr0;
497 natural_width guest_cr3;
498 natural_width guest_cr4;
499 natural_width guest_es_base;
500 natural_width guest_cs_base;
501 natural_width guest_ss_base;
502 natural_width guest_ds_base;
503 natural_width guest_fs_base;
504 natural_width guest_gs_base;
505 natural_width guest_ldtr_base;
506 natural_width guest_tr_base;
507 natural_width guest_gdtr_base;
508 natural_width guest_idtr_base;
509 natural_width guest_dr7;
510 natural_width guest_rsp;
511 natural_width guest_rip;
512 natural_width guest_rflags;
513 natural_width guest_pending_dbg_exceptions;
514 natural_width guest_sysenter_esp;
515 natural_width guest_sysenter_eip;
516 natural_width host_cr0;
517 natural_width host_cr3;
518 natural_width host_cr4;
519 natural_width host_fs_base;
520 natural_width host_gs_base;
521 natural_width host_tr_base;
522 natural_width host_gdtr_base;
523 natural_width host_idtr_base;
524 natural_width host_ia32_sysenter_esp;
525 natural_width host_ia32_sysenter_eip;
526 natural_width host_rsp;
527 natural_width host_rip;
528 natural_width paddingl[8]; /* room for future expansion */
529 u32 pin_based_vm_exec_control;
530 u32 cpu_based_vm_exec_control;
531 u32 exception_bitmap;
532 u32 page_fault_error_code_mask;
533 u32 page_fault_error_code_match;
534 u32 cr3_target_count;
535 u32 vm_exit_controls;
536 u32 vm_exit_msr_store_count;
537 u32 vm_exit_msr_load_count;
538 u32 vm_entry_controls;
539 u32 vm_entry_msr_load_count;
540 u32 vm_entry_intr_info_field;
541 u32 vm_entry_exception_error_code;
542 u32 vm_entry_instruction_len;
543 u32 tpr_threshold;
544 u32 secondary_vm_exec_control;
545 u32 vm_instruction_error;
546 u32 vm_exit_reason;
547 u32 vm_exit_intr_info;
548 u32 vm_exit_intr_error_code;
549 u32 idt_vectoring_info_field;
550 u32 idt_vectoring_error_code;
551 u32 vm_exit_instruction_len;
552 u32 vmx_instruction_info;
553 u32 guest_es_limit;
554 u32 guest_cs_limit;
555 u32 guest_ss_limit;
556 u32 guest_ds_limit;
557 u32 guest_fs_limit;
558 u32 guest_gs_limit;
559 u32 guest_ldtr_limit;
560 u32 guest_tr_limit;
561 u32 guest_gdtr_limit;
562 u32 guest_idtr_limit;
563 u32 guest_es_ar_bytes;
564 u32 guest_cs_ar_bytes;
565 u32 guest_ss_ar_bytes;
566 u32 guest_ds_ar_bytes;
567 u32 guest_fs_ar_bytes;
568 u32 guest_gs_ar_bytes;
569 u32 guest_ldtr_ar_bytes;
570 u32 guest_tr_ar_bytes;
571 u32 guest_interruptibility_info;
572 u32 guest_activity_state;
573 u32 guest_sysenter_cs;
574 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100575 u32 vmx_preemption_timer_value;
576 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300577 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800578 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300579 u16 guest_es_selector;
580 u16 guest_cs_selector;
581 u16 guest_ss_selector;
582 u16 guest_ds_selector;
583 u16 guest_fs_selector;
584 u16 guest_gs_selector;
585 u16 guest_ldtr_selector;
586 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800587 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300588 u16 host_es_selector;
589 u16 host_cs_selector;
590 u16 host_ss_selector;
591 u16 host_ds_selector;
592 u16 host_fs_selector;
593 u16 host_gs_selector;
594 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700595 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300596};
597
598/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700599 * For save/restore compatibility, the vmcs12 field offsets must not change.
600 */
601#define CHECK_OFFSET(field, loc) \
602 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
603 "Offset of " #field " in struct vmcs12 has changed.")
604
605static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300606 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700607 CHECK_OFFSET(abort, 4);
608 CHECK_OFFSET(launch_state, 8);
609 CHECK_OFFSET(io_bitmap_a, 40);
610 CHECK_OFFSET(io_bitmap_b, 48);
611 CHECK_OFFSET(msr_bitmap, 56);
612 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
613 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
614 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
615 CHECK_OFFSET(tsc_offset, 88);
616 CHECK_OFFSET(virtual_apic_page_addr, 96);
617 CHECK_OFFSET(apic_access_addr, 104);
618 CHECK_OFFSET(posted_intr_desc_addr, 112);
619 CHECK_OFFSET(ept_pointer, 120);
620 CHECK_OFFSET(eoi_exit_bitmap0, 128);
621 CHECK_OFFSET(eoi_exit_bitmap1, 136);
622 CHECK_OFFSET(eoi_exit_bitmap2, 144);
623 CHECK_OFFSET(eoi_exit_bitmap3, 152);
624 CHECK_OFFSET(xss_exit_bitmap, 160);
625 CHECK_OFFSET(guest_physical_address, 168);
626 CHECK_OFFSET(vmcs_link_pointer, 176);
627 CHECK_OFFSET(guest_ia32_debugctl, 184);
628 CHECK_OFFSET(guest_ia32_pat, 192);
629 CHECK_OFFSET(guest_ia32_efer, 200);
630 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
631 CHECK_OFFSET(guest_pdptr0, 216);
632 CHECK_OFFSET(guest_pdptr1, 224);
633 CHECK_OFFSET(guest_pdptr2, 232);
634 CHECK_OFFSET(guest_pdptr3, 240);
635 CHECK_OFFSET(guest_bndcfgs, 248);
636 CHECK_OFFSET(host_ia32_pat, 256);
637 CHECK_OFFSET(host_ia32_efer, 264);
638 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
639 CHECK_OFFSET(vmread_bitmap, 280);
640 CHECK_OFFSET(vmwrite_bitmap, 288);
641 CHECK_OFFSET(vm_function_control, 296);
642 CHECK_OFFSET(eptp_list_address, 304);
643 CHECK_OFFSET(pml_address, 312);
644 CHECK_OFFSET(cr0_guest_host_mask, 344);
645 CHECK_OFFSET(cr4_guest_host_mask, 352);
646 CHECK_OFFSET(cr0_read_shadow, 360);
647 CHECK_OFFSET(cr4_read_shadow, 368);
648 CHECK_OFFSET(cr3_target_value0, 376);
649 CHECK_OFFSET(cr3_target_value1, 384);
650 CHECK_OFFSET(cr3_target_value2, 392);
651 CHECK_OFFSET(cr3_target_value3, 400);
652 CHECK_OFFSET(exit_qualification, 408);
653 CHECK_OFFSET(guest_linear_address, 416);
654 CHECK_OFFSET(guest_cr0, 424);
655 CHECK_OFFSET(guest_cr3, 432);
656 CHECK_OFFSET(guest_cr4, 440);
657 CHECK_OFFSET(guest_es_base, 448);
658 CHECK_OFFSET(guest_cs_base, 456);
659 CHECK_OFFSET(guest_ss_base, 464);
660 CHECK_OFFSET(guest_ds_base, 472);
661 CHECK_OFFSET(guest_fs_base, 480);
662 CHECK_OFFSET(guest_gs_base, 488);
663 CHECK_OFFSET(guest_ldtr_base, 496);
664 CHECK_OFFSET(guest_tr_base, 504);
665 CHECK_OFFSET(guest_gdtr_base, 512);
666 CHECK_OFFSET(guest_idtr_base, 520);
667 CHECK_OFFSET(guest_dr7, 528);
668 CHECK_OFFSET(guest_rsp, 536);
669 CHECK_OFFSET(guest_rip, 544);
670 CHECK_OFFSET(guest_rflags, 552);
671 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
672 CHECK_OFFSET(guest_sysenter_esp, 568);
673 CHECK_OFFSET(guest_sysenter_eip, 576);
674 CHECK_OFFSET(host_cr0, 584);
675 CHECK_OFFSET(host_cr3, 592);
676 CHECK_OFFSET(host_cr4, 600);
677 CHECK_OFFSET(host_fs_base, 608);
678 CHECK_OFFSET(host_gs_base, 616);
679 CHECK_OFFSET(host_tr_base, 624);
680 CHECK_OFFSET(host_gdtr_base, 632);
681 CHECK_OFFSET(host_idtr_base, 640);
682 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
683 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
684 CHECK_OFFSET(host_rsp, 664);
685 CHECK_OFFSET(host_rip, 672);
686 CHECK_OFFSET(pin_based_vm_exec_control, 744);
687 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
688 CHECK_OFFSET(exception_bitmap, 752);
689 CHECK_OFFSET(page_fault_error_code_mask, 756);
690 CHECK_OFFSET(page_fault_error_code_match, 760);
691 CHECK_OFFSET(cr3_target_count, 764);
692 CHECK_OFFSET(vm_exit_controls, 768);
693 CHECK_OFFSET(vm_exit_msr_store_count, 772);
694 CHECK_OFFSET(vm_exit_msr_load_count, 776);
695 CHECK_OFFSET(vm_entry_controls, 780);
696 CHECK_OFFSET(vm_entry_msr_load_count, 784);
697 CHECK_OFFSET(vm_entry_intr_info_field, 788);
698 CHECK_OFFSET(vm_entry_exception_error_code, 792);
699 CHECK_OFFSET(vm_entry_instruction_len, 796);
700 CHECK_OFFSET(tpr_threshold, 800);
701 CHECK_OFFSET(secondary_vm_exec_control, 804);
702 CHECK_OFFSET(vm_instruction_error, 808);
703 CHECK_OFFSET(vm_exit_reason, 812);
704 CHECK_OFFSET(vm_exit_intr_info, 816);
705 CHECK_OFFSET(vm_exit_intr_error_code, 820);
706 CHECK_OFFSET(idt_vectoring_info_field, 824);
707 CHECK_OFFSET(idt_vectoring_error_code, 828);
708 CHECK_OFFSET(vm_exit_instruction_len, 832);
709 CHECK_OFFSET(vmx_instruction_info, 836);
710 CHECK_OFFSET(guest_es_limit, 840);
711 CHECK_OFFSET(guest_cs_limit, 844);
712 CHECK_OFFSET(guest_ss_limit, 848);
713 CHECK_OFFSET(guest_ds_limit, 852);
714 CHECK_OFFSET(guest_fs_limit, 856);
715 CHECK_OFFSET(guest_gs_limit, 860);
716 CHECK_OFFSET(guest_ldtr_limit, 864);
717 CHECK_OFFSET(guest_tr_limit, 868);
718 CHECK_OFFSET(guest_gdtr_limit, 872);
719 CHECK_OFFSET(guest_idtr_limit, 876);
720 CHECK_OFFSET(guest_es_ar_bytes, 880);
721 CHECK_OFFSET(guest_cs_ar_bytes, 884);
722 CHECK_OFFSET(guest_ss_ar_bytes, 888);
723 CHECK_OFFSET(guest_ds_ar_bytes, 892);
724 CHECK_OFFSET(guest_fs_ar_bytes, 896);
725 CHECK_OFFSET(guest_gs_ar_bytes, 900);
726 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
727 CHECK_OFFSET(guest_tr_ar_bytes, 908);
728 CHECK_OFFSET(guest_interruptibility_info, 912);
729 CHECK_OFFSET(guest_activity_state, 916);
730 CHECK_OFFSET(guest_sysenter_cs, 920);
731 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
732 CHECK_OFFSET(vmx_preemption_timer_value, 928);
733 CHECK_OFFSET(virtual_processor_id, 960);
734 CHECK_OFFSET(posted_intr_nv, 962);
735 CHECK_OFFSET(guest_es_selector, 964);
736 CHECK_OFFSET(guest_cs_selector, 966);
737 CHECK_OFFSET(guest_ss_selector, 968);
738 CHECK_OFFSET(guest_ds_selector, 970);
739 CHECK_OFFSET(guest_fs_selector, 972);
740 CHECK_OFFSET(guest_gs_selector, 974);
741 CHECK_OFFSET(guest_ldtr_selector, 976);
742 CHECK_OFFSET(guest_tr_selector, 978);
743 CHECK_OFFSET(guest_intr_status, 980);
744 CHECK_OFFSET(host_es_selector, 982);
745 CHECK_OFFSET(host_cs_selector, 984);
746 CHECK_OFFSET(host_ss_selector, 986);
747 CHECK_OFFSET(host_ds_selector, 988);
748 CHECK_OFFSET(host_fs_selector, 990);
749 CHECK_OFFSET(host_gs_selector, 992);
750 CHECK_OFFSET(host_tr_selector, 994);
751 CHECK_OFFSET(guest_pml_index, 996);
752}
753
754/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300755 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
756 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
757 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700758 *
759 * IMPORTANT: Changing this value will break save/restore compatibility with
760 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300761 */
762#define VMCS12_REVISION 0x11e57ed0
763
764/*
765 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
766 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
767 * current implementation, 4K are reserved to avoid future complications.
768 */
769#define VMCS12_SIZE 0x1000
770
771/*
Jim Mattson5b157062017-12-22 12:11:12 -0800772 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
773 * supported VMCS12 field encoding.
774 */
775#define VMCS12_MAX_FIELD_INDEX 0x17
776
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100777struct nested_vmx_msrs {
778 /*
779 * We only store the "true" versions of the VMX capability MSRs. We
780 * generate the "non-true" versions by setting the must-be-1 bits
781 * according to the SDM.
782 */
783 u32 procbased_ctls_low;
784 u32 procbased_ctls_high;
785 u32 secondary_ctls_low;
786 u32 secondary_ctls_high;
787 u32 pinbased_ctls_low;
788 u32 pinbased_ctls_high;
789 u32 exit_ctls_low;
790 u32 exit_ctls_high;
791 u32 entry_ctls_low;
792 u32 entry_ctls_high;
793 u32 misc_low;
794 u32 misc_high;
795 u32 ept_caps;
796 u32 vpid_caps;
797 u64 basic;
798 u64 cr0_fixed0;
799 u64 cr0_fixed1;
800 u64 cr4_fixed0;
801 u64 cr4_fixed1;
802 u64 vmcs_enum;
803 u64 vmfunc_controls;
804};
805
Jim Mattson5b157062017-12-22 12:11:12 -0800806/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300807 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
808 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
809 */
810struct nested_vmx {
811 /* Has the level1 guest done vmxon? */
812 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400813 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400814 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300815
816 /* The guest-physical address of the current VMCS L1 keeps for L2 */
817 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700818 /*
819 * Cache of the guest's VMCS, existing outside of guest memory.
820 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700821 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700822 */
823 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300824 /*
Liran Alon61ada742018-06-23 02:35:08 +0300825 * Cache of the guest's shadow VMCS, existing outside of guest
826 * memory. Loaded from guest memory during VM entry. Flushed
827 * to guest memory during VM exit.
828 */
829 struct vmcs12 *cached_shadow_vmcs12;
830 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300831 * Indicates if the shadow vmcs must be updated with the
832 * data hold by vmcs12
833 */
834 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100835 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300836
Jim Mattson8d860bb2018-05-09 16:56:05 -0400837 bool change_vmcs01_virtual_apic_mode;
838
Nadav Har'El644d7112011-05-25 23:12:35 +0300839 /* L2 must run next, and mustn't decide to exit to L1. */
840 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600841
842 struct loaded_vmcs vmcs02;
843
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300844 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600845 * Guest pages referred to in the vmcs02 with host-physical
846 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300847 */
848 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800849 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800850 struct page *pi_desc_page;
851 struct pi_desc *pi_desc;
852 bool pi_pending;
853 u16 posted_intr_nv;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100854
855 struct hrtimer preemption_timer;
856 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200857
858 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
859 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800860
Wanpeng Li5c614b32015-10-13 09:18:36 -0700861 u16 vpid02;
862 u16 last_vpid;
863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100864 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200865
866 /* SMM related state */
867 struct {
868 /* in VMX operation on SMM entry? */
869 bool vmxon;
870 /* in guest mode on SMM entry? */
871 bool guest_mode;
872 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300873};
874
Yang Zhang01e439b2013-04-11 19:25:12 +0800875#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800876#define POSTED_INTR_SN 1
877
Yang Zhang01e439b2013-04-11 19:25:12 +0800878/* Posted-Interrupt Descriptor */
879struct pi_desc {
880 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800881 union {
882 struct {
883 /* bit 256 - Outstanding Notification */
884 u16 on : 1,
885 /* bit 257 - Suppress Notification */
886 sn : 1,
887 /* bit 271:258 - Reserved */
888 rsvd_1 : 14;
889 /* bit 279:272 - Notification Vector */
890 u8 nv;
891 /* bit 287:280 - Reserved */
892 u8 rsvd_2;
893 /* bit 319:288 - Notification Destination */
894 u32 ndst;
895 };
896 u64 control;
897 };
898 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800899} __aligned(64);
900
Yang Zhanga20ed542013-04-11 19:25:15 +0800901static bool pi_test_and_set_on(struct pi_desc *pi_desc)
902{
903 return test_and_set_bit(POSTED_INTR_ON,
904 (unsigned long *)&pi_desc->control);
905}
906
907static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
908{
909 return test_and_clear_bit(POSTED_INTR_ON,
910 (unsigned long *)&pi_desc->control);
911}
912
913static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
914{
915 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
916}
917
Feng Wuebbfc762015-09-18 22:29:46 +0800918static inline void pi_clear_sn(struct pi_desc *pi_desc)
919{
920 return clear_bit(POSTED_INTR_SN,
921 (unsigned long *)&pi_desc->control);
922}
923
924static inline void pi_set_sn(struct pi_desc *pi_desc)
925{
926 return set_bit(POSTED_INTR_SN,
927 (unsigned long *)&pi_desc->control);
928}
929
Paolo Bonziniad361092016-09-20 16:15:05 +0200930static inline void pi_clear_on(struct pi_desc *pi_desc)
931{
932 clear_bit(POSTED_INTR_ON,
933 (unsigned long *)&pi_desc->control);
934}
935
Feng Wuebbfc762015-09-18 22:29:46 +0800936static inline int pi_test_on(struct pi_desc *pi_desc)
937{
938 return test_bit(POSTED_INTR_ON,
939 (unsigned long *)&pi_desc->control);
940}
941
942static inline int pi_test_sn(struct pi_desc *pi_desc)
943{
944 return test_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400948struct vmx_msrs {
949 unsigned int nr;
950 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
951};
952
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400953struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000954 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300955 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300956 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100957 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300958 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200959 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200960 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300961 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400962 int nmsrs;
963 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800964 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400965#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300966 u64 msr_host_kernel_gs_base;
967 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100969
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100970 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100971 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100972
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 u32 vm_entry_controls_shadow;
974 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200975 u32 secondary_exec_control;
976
Nadav Har'Eld462b812011-05-24 15:26:10 +0300977 /*
978 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
979 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700980 * guest (L2), it points to a different VMCS. loaded_cpu_state points
981 * to the VMCS whose state is loaded into the CPU registers that only
982 * need to be switched when transitioning to/from the kernel; a NULL
983 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984 */
985 struct loaded_vmcs vmcs01;
986 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700987 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300989 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400990 struct vmx_msrs guest;
991 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300992 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700993
Avi Kivity9c8cba32007-11-22 11:42:59 +0200994 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300995 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300996 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300997 struct kvm_segment segs[8];
998 } rmode;
999 struct {
1000 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001001 struct kvm_save_segment {
1002 u16 selector;
1003 unsigned long base;
1004 u32 limit;
1005 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001006 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001007 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001008 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001009 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001010
Andi Kleena0861c02009-06-08 17:37:09 +08001011 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001012
Yang Zhang01e439b2013-04-11 19:25:12 +08001013 /* Posted interrupt descriptor */
1014 struct pi_desc pi_desc;
1015
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001016 /* Support for a guest hypervisor (nested VMX) */
1017 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001018
1019 /* Dynamic PLE window. */
1020 int ple_window;
1021 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001022
Sean Christophersond264ee02018-08-27 15:21:12 -07001023 bool req_immediate_exit;
1024
Kai Huang843e4332015-01-28 10:54:28 +08001025 /* Support for PML */
1026#define PML_ENTITY_NUM 512
1027 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001028
Yunhong Jiang64672c92016-06-13 14:19:59 -07001029 /* apic deadline value in host tsc */
1030 u64 hv_deadline_tsc;
1031
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001032 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001033
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001034 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001035
Wanpeng Li74c55932017-11-29 01:31:20 -08001036 unsigned long host_debugctlmsr;
1037
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001038 /*
1039 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1040 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1041 * in msr_ia32_feature_control_valid_bits.
1042 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001043 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001044 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001045 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046};
1047
Avi Kivity2fb92db2011-04-27 19:42:18 +03001048enum segment_cache_field {
1049 SEG_FIELD_SEL = 0,
1050 SEG_FIELD_BASE = 1,
1051 SEG_FIELD_LIMIT = 2,
1052 SEG_FIELD_AR = 3,
1053
1054 SEG_FIELD_NR = 4
1055};
1056
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001057static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1058{
1059 return container_of(kvm, struct kvm_vmx, kvm);
1060}
1061
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1063{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001064 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065}
1066
Feng Wuefc64402015-09-18 22:29:51 +08001067static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1068{
1069 return &(to_vmx(vcpu)->pi_desc);
1070}
1071
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001072#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001073#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1075#define FIELD64(number, name) \
1076 FIELD(number, name), \
1077 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078
Abel Gordon4607c2d2013-04-18 14:35:55 +03001079
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001080static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001081#define SHADOW_FIELD_RO(x) x,
1082#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001083};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001084static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001085 ARRAY_SIZE(shadow_read_only_fields);
1086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001087static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001088#define SHADOW_FIELD_RW(x) x,
1089#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001091static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001092 ARRAY_SIZE(shadow_read_write_fields);
1093
Mathias Krause772e0312012-08-30 01:30:19 +02001094static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001095 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001096 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1098 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1099 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1100 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1101 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1102 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1103 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1104 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001105 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001106 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001107 FIELD(HOST_ES_SELECTOR, host_es_selector),
1108 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1109 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1110 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1111 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1112 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1113 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1114 FIELD64(IO_BITMAP_A, io_bitmap_a),
1115 FIELD64(IO_BITMAP_B, io_bitmap_b),
1116 FIELD64(MSR_BITMAP, msr_bitmap),
1117 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1118 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1119 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001120 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD64(TSC_OFFSET, tsc_offset),
1122 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1123 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001124 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001125 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001126 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001127 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1128 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1129 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1130 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001131 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001132 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1133 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001134 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001135 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1136 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1137 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1138 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1139 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1140 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1141 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1142 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1143 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1144 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001145 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1147 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1148 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1149 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1150 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1151 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1152 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1153 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1154 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1155 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1156 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1157 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1158 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1159 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1160 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1161 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1162 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1163 FIELD(TPR_THRESHOLD, tpr_threshold),
1164 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1165 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1166 FIELD(VM_EXIT_REASON, vm_exit_reason),
1167 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1168 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1169 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1170 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1171 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1172 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1173 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1174 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1175 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1176 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1177 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1178 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1179 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1180 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1181 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1182 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1183 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1184 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1185 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1186 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1187 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1188 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1189 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1190 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1191 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1192 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1193 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1194 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001195 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001196 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1197 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1198 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1199 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1200 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1201 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1202 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1203 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1204 FIELD(EXIT_QUALIFICATION, exit_qualification),
1205 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1206 FIELD(GUEST_CR0, guest_cr0),
1207 FIELD(GUEST_CR3, guest_cr3),
1208 FIELD(GUEST_CR4, guest_cr4),
1209 FIELD(GUEST_ES_BASE, guest_es_base),
1210 FIELD(GUEST_CS_BASE, guest_cs_base),
1211 FIELD(GUEST_SS_BASE, guest_ss_base),
1212 FIELD(GUEST_DS_BASE, guest_ds_base),
1213 FIELD(GUEST_FS_BASE, guest_fs_base),
1214 FIELD(GUEST_GS_BASE, guest_gs_base),
1215 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1216 FIELD(GUEST_TR_BASE, guest_tr_base),
1217 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1218 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1219 FIELD(GUEST_DR7, guest_dr7),
1220 FIELD(GUEST_RSP, guest_rsp),
1221 FIELD(GUEST_RIP, guest_rip),
1222 FIELD(GUEST_RFLAGS, guest_rflags),
1223 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1224 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1225 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1226 FIELD(HOST_CR0, host_cr0),
1227 FIELD(HOST_CR3, host_cr3),
1228 FIELD(HOST_CR4, host_cr4),
1229 FIELD(HOST_FS_BASE, host_fs_base),
1230 FIELD(HOST_GS_BASE, host_gs_base),
1231 FIELD(HOST_TR_BASE, host_tr_base),
1232 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1233 FIELD(HOST_IDTR_BASE, host_idtr_base),
1234 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1235 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1236 FIELD(HOST_RSP, host_rsp),
1237 FIELD(HOST_RIP, host_rip),
1238};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001239
1240static inline short vmcs_field_to_offset(unsigned long field)
1241{
Dan Williams085331d2018-01-31 17:47:03 -08001242 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1243 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001244 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001245
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001246 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001247 return -ENOENT;
1248
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001249 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001250 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001251 return -ENOENT;
1252
Linus Torvalds15303ba2018-02-10 13:16:35 -08001253 index = array_index_nospec(index, size);
1254 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001255 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001257 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001258}
1259
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001260static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1261{
David Matlack4f2777b2016-07-13 17:16:37 -07001262 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001263}
1264
Liran Alon61ada742018-06-23 02:35:08 +03001265static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1266{
1267 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1268}
1269
Peter Feiner995f00a2017-06-30 17:26:32 -07001270static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001271static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001272static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001273static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001274static void vmx_set_segment(struct kvm_vcpu *vcpu,
1275 struct kvm_segment *var, int seg);
1276static void vmx_get_segment(struct kvm_vcpu *vcpu,
1277 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001278static bool guest_state_valid(struct kvm_vcpu *vcpu);
1279static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001280static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001281static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1282static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1283static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1284 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001285static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001286static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1287 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1290static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001291/*
1292 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1293 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1294 */
1295static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Feng Wubf9f6ac2015-09-18 22:29:55 +08001297/*
1298 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1299 * can find which vCPU should be waken up.
1300 */
1301static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1302static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1303
Radim Krčmář23611332016-09-29 22:41:33 +02001304enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001305 VMX_VMREAD_BITMAP,
1306 VMX_VMWRITE_BITMAP,
1307 VMX_BITMAP_NR
1308};
1309
1310static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1311
Radim Krčmář23611332016-09-29 22:41:33 +02001312#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1313#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001314
Avi Kivity110312c2010-12-21 12:54:20 +02001315static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001316static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001317
Sheng Yang2384d2b2008-01-17 15:14:33 +08001318static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1319static DEFINE_SPINLOCK(vmx_vpid_lock);
1320
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 int size;
1323 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001324 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001326 u32 pin_based_exec_ctrl;
1327 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001328 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329 u32 vmexit_ctrl;
1330 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001331 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
Hannes Ederefff9e52008-11-28 17:02:06 +01001334static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001335 u32 ept;
1336 u32 vpid;
1337} vmx_capability;
1338
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339#define VMX_SEGMENT_FIELD(seg) \
1340 [VCPU_SREG_##seg] = { \
1341 .selector = GUEST_##seg##_SELECTOR, \
1342 .base = GUEST_##seg##_BASE, \
1343 .limit = GUEST_##seg##_LIMIT, \
1344 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1345 }
1346
Mathias Krause772e0312012-08-30 01:30:19 +02001347static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 unsigned selector;
1349 unsigned base;
1350 unsigned limit;
1351 unsigned ar_bytes;
1352} kvm_vmx_segment_fields[] = {
1353 VMX_SEGMENT_FIELD(CS),
1354 VMX_SEGMENT_FIELD(DS),
1355 VMX_SEGMENT_FIELD(ES),
1356 VMX_SEGMENT_FIELD(FS),
1357 VMX_SEGMENT_FIELD(GS),
1358 VMX_SEGMENT_FIELD(SS),
1359 VMX_SEGMENT_FIELD(TR),
1360 VMX_SEGMENT_FIELD(LDTR),
1361};
1362
Avi Kivity26bb0982009-09-07 11:14:12 +03001363static u64 host_efer;
1364
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001365static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1366
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001367/*
Brian Gerst8c065852010-07-17 09:03:26 -04001368 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001369 * away by decrementing the array size.
1370 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001372#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001373 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001375 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001378DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1379
1380#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1381
1382#define KVM_EVMCS_VERSION 1
1383
1384#if IS_ENABLED(CONFIG_HYPERV)
1385static bool __read_mostly enlightened_vmcs = true;
1386module_param(enlightened_vmcs, bool, 0444);
1387
1388static inline void evmcs_write64(unsigned long field, u64 value)
1389{
1390 u16 clean_field;
1391 int offset = get_evmcs_offset(field, &clean_field);
1392
1393 if (offset < 0)
1394 return;
1395
1396 *(u64 *)((char *)current_evmcs + offset) = value;
1397
1398 current_evmcs->hv_clean_fields &= ~clean_field;
1399}
1400
1401static inline void evmcs_write32(unsigned long field, u32 value)
1402{
1403 u16 clean_field;
1404 int offset = get_evmcs_offset(field, &clean_field);
1405
1406 if (offset < 0)
1407 return;
1408
1409 *(u32 *)((char *)current_evmcs + offset) = value;
1410 current_evmcs->hv_clean_fields &= ~clean_field;
1411}
1412
1413static inline void evmcs_write16(unsigned long field, u16 value)
1414{
1415 u16 clean_field;
1416 int offset = get_evmcs_offset(field, &clean_field);
1417
1418 if (offset < 0)
1419 return;
1420
1421 *(u16 *)((char *)current_evmcs + offset) = value;
1422 current_evmcs->hv_clean_fields &= ~clean_field;
1423}
1424
1425static inline u64 evmcs_read64(unsigned long field)
1426{
1427 int offset = get_evmcs_offset(field, NULL);
1428
1429 if (offset < 0)
1430 return 0;
1431
1432 return *(u64 *)((char *)current_evmcs + offset);
1433}
1434
1435static inline u32 evmcs_read32(unsigned long field)
1436{
1437 int offset = get_evmcs_offset(field, NULL);
1438
1439 if (offset < 0)
1440 return 0;
1441
1442 return *(u32 *)((char *)current_evmcs + offset);
1443}
1444
1445static inline u16 evmcs_read16(unsigned long field)
1446{
1447 int offset = get_evmcs_offset(field, NULL);
1448
1449 if (offset < 0)
1450 return 0;
1451
1452 return *(u16 *)((char *)current_evmcs + offset);
1453}
1454
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001455static inline void evmcs_touch_msr_bitmap(void)
1456{
1457 if (unlikely(!current_evmcs))
1458 return;
1459
1460 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1461 current_evmcs->hv_clean_fields &=
1462 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1463}
1464
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001465static void evmcs_load(u64 phys_addr)
1466{
1467 struct hv_vp_assist_page *vp_ap =
1468 hv_get_vp_assist_page(smp_processor_id());
1469
1470 vp_ap->current_nested_vmcs = phys_addr;
1471 vp_ap->enlighten_vmentry = 1;
1472}
1473
1474static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1475{
1476 /*
1477 * Enlightened VMCSv1 doesn't support these:
1478 *
1479 * POSTED_INTR_NV = 0x00000002,
1480 * GUEST_INTR_STATUS = 0x00000810,
1481 * APIC_ACCESS_ADDR = 0x00002014,
1482 * POSTED_INTR_DESC_ADDR = 0x00002016,
1483 * EOI_EXIT_BITMAP0 = 0x0000201c,
1484 * EOI_EXIT_BITMAP1 = 0x0000201e,
1485 * EOI_EXIT_BITMAP2 = 0x00002020,
1486 * EOI_EXIT_BITMAP3 = 0x00002022,
1487 */
1488 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1489 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1490 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1492 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1493 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1494 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1495
1496 /*
1497 * GUEST_PML_INDEX = 0x00000812,
1498 * PML_ADDRESS = 0x0000200e,
1499 */
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1501
1502 /* VM_FUNCTION_CONTROL = 0x00002018, */
1503 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1504
1505 /*
1506 * EPTP_LIST_ADDRESS = 0x00002024,
1507 * VMREAD_BITMAP = 0x00002026,
1508 * VMWRITE_BITMAP = 0x00002028,
1509 */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1511
1512 /*
1513 * TSC_MULTIPLIER = 0x00002032,
1514 */
1515 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1516
1517 /*
1518 * PLE_GAP = 0x00004020,
1519 * PLE_WINDOW = 0x00004022,
1520 */
1521 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1522
1523 /*
1524 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1525 */
1526 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1527
1528 /*
1529 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1530 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1531 */
1532 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1533 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1534
1535 /*
1536 * Currently unsupported in KVM:
1537 * GUEST_IA32_RTIT_CTL = 0x00002814,
1538 */
1539}
Tianyu Lan877ad952018-07-19 08:40:23 +00001540
1541/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1542static void check_ept_pointer_match(struct kvm *kvm)
1543{
1544 struct kvm_vcpu *vcpu;
1545 u64 tmp_eptp = INVALID_PAGE;
1546 int i;
1547
1548 kvm_for_each_vcpu(i, vcpu, kvm) {
1549 if (!VALID_PAGE(tmp_eptp)) {
1550 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1551 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1552 to_kvm_vmx(kvm)->ept_pointers_match
1553 = EPT_POINTERS_MISMATCH;
1554 return;
1555 }
1556 }
1557
1558 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1559}
1560
1561static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1562{
1563 int ret;
1564
1565 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1566
1567 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1568 check_ept_pointer_match(kvm);
1569
1570 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1571 ret = -ENOTSUPP;
1572 goto out;
1573 }
1574
1575 ret = hyperv_flush_guest_mapping(
1576 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1577
1578out:
1579 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1580 return ret;
1581}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001582#else /* !IS_ENABLED(CONFIG_HYPERV) */
1583static inline void evmcs_write64(unsigned long field, u64 value) {}
1584static inline void evmcs_write32(unsigned long field, u32 value) {}
1585static inline void evmcs_write16(unsigned long field, u16 value) {}
1586static inline u64 evmcs_read64(unsigned long field) { return 0; }
1587static inline u32 evmcs_read32(unsigned long field) { return 0; }
1588static inline u16 evmcs_read16(unsigned long field) { return 0; }
1589static inline void evmcs_load(u64 phys_addr) {}
1590static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001591static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001592#endif /* IS_ENABLED(CONFIG_HYPERV) */
1593
Jan Kiszka5bb16012016-02-09 20:14:21 +01001594static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1597 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001598 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1599}
1600
Jan Kiszka6f054852016-02-09 20:15:18 +01001601static inline bool is_debug(u32 intr_info)
1602{
1603 return is_exception_n(intr_info, DB_VECTOR);
1604}
1605
1606static inline bool is_breakpoint(u32 intr_info)
1607{
1608 return is_exception_n(intr_info, BP_VECTOR);
1609}
1610
Jan Kiszka5bb16012016-02-09 20:14:21 +01001611static inline bool is_page_fault(u32 intr_info)
1612{
1613 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001617{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001619}
1620
Gui Jianfeng31299942010-03-15 17:29:09 +08001621static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001622{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001623 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001624}
1625
Liran Alon9e869482018-03-12 13:12:51 +02001626static inline bool is_gp_fault(u32 intr_info)
1627{
1628 return is_exception_n(intr_info, GP_VECTOR);
1629}
1630
Gui Jianfeng31299942010-03-15 17:29:09 +08001631static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632{
1633 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1634 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001638{
1639 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1640 INTR_INFO_VALID_MASK)) ==
1641 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1642}
1643
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001644/* Undocumented: icebp/int1 */
1645static inline bool is_icebp(u32 intr_info)
1646{
1647 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1648 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1649}
1650
Gui Jianfeng31299942010-03-15 17:29:09 +08001651static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001652{
Sheng Yang04547152009-04-01 15:52:31 +08001653 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001657{
Sheng Yang04547152009-04-01 15:52:31 +08001658 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001659}
1660
Paolo Bonzini35754c92015-07-29 12:05:37 +02001661static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001662{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001663 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001664}
1665
Gui Jianfeng31299942010-03-15 17:29:09 +08001666static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001667{
Sheng Yang04547152009-04-01 15:52:31 +08001668 return vmcs_config.cpu_based_exec_ctrl &
1669 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670}
1671
Avi Kivity774ead32007-12-26 13:57:04 +02001672static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1676}
1677
Yang Zhang8d146952013-01-25 10:18:50 +08001678static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1682}
1683
Yang Zhang83d4c282013-01-25 10:18:49 +08001684static inline bool cpu_has_vmx_apic_register_virt(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1688}
1689
Yang Zhangc7c9c562013-01-25 10:18:51 +08001690static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1694}
1695
Sean Christopherson0b665d32018-08-14 09:33:34 -07001696static inline bool cpu_has_vmx_encls_vmexit(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_ENCLS_EXITING;
1700}
1701
Yunhong Jiang64672c92016-06-13 14:19:59 -07001702/*
1703 * Comment's format: document - errata name - stepping - processor name.
1704 * Refer from
1705 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1706 */
1707static u32 vmx_preemption_cpu_tfms[] = {
1708/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17090x000206E6,
1710/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1711/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1712/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17130x00020652,
1714/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17150x00020655,
1716/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1717/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1718/*
1719 * 320767.pdf - AAP86 - B1 -
1720 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1721 */
17220x000106E5,
1723/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17240x000106A0,
1725/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17260x000106A1,
1727/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17280x000106A4,
1729 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1730 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1731 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17320x000106A5,
1733};
1734
1735static inline bool cpu_has_broken_vmx_preemption_timer(void)
1736{
1737 u32 eax = cpuid_eax(0x00000001), i;
1738
1739 /* Clear the reserved bits */
1740 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001741 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001742 if (eax == vmx_preemption_cpu_tfms[i])
1743 return true;
1744
1745 return false;
1746}
1747
1748static inline bool cpu_has_vmx_preemption_timer(void)
1749{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001750 return vmcs_config.pin_based_exec_ctrl &
1751 PIN_BASED_VMX_PREEMPTION_TIMER;
1752}
1753
Yang Zhang01e439b2013-04-11 19:25:12 +08001754static inline bool cpu_has_vmx_posted_intr(void)
1755{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001756 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1757 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001758}
1759
1760static inline bool cpu_has_vmx_apicv(void)
1761{
1762 return cpu_has_vmx_apic_register_virt() &&
1763 cpu_has_vmx_virtual_intr_delivery() &&
1764 cpu_has_vmx_posted_intr();
1765}
1766
Sheng Yang04547152009-04-01 15:52:31 +08001767static inline bool cpu_has_vmx_flexpriority(void)
1768{
1769 return cpu_has_vmx_tpr_shadow() &&
1770 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001771}
1772
Marcelo Tosattie7997942009-06-11 12:07:40 -03001773static inline bool cpu_has_vmx_ept_execute_only(void)
1774{
Gui Jianfeng31299942010-03-15 17:29:09 +08001775 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001776}
1777
Marcelo Tosattie7997942009-06-11 12:07:40 -03001778static inline bool cpu_has_vmx_ept_2m_page(void)
1779{
Gui Jianfeng31299942010-03-15 17:29:09 +08001780 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001781}
1782
Sheng Yang878403b2010-01-05 19:02:29 +08001783static inline bool cpu_has_vmx_ept_1g_page(void)
1784{
Gui Jianfeng31299942010-03-15 17:29:09 +08001785 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001786}
1787
Sheng Yang4bc9b982010-06-02 14:05:24 +08001788static inline bool cpu_has_vmx_ept_4levels(void)
1789{
1790 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1791}
1792
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001793static inline bool cpu_has_vmx_ept_mt_wb(void)
1794{
1795 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1796}
1797
Yu Zhang855feb62017-08-24 20:27:55 +08001798static inline bool cpu_has_vmx_ept_5levels(void)
1799{
1800 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1801}
1802
Xudong Hao83c3a332012-05-28 19:33:35 +08001803static inline bool cpu_has_vmx_ept_ad_bits(void)
1804{
1805 return vmx_capability.ept & VMX_EPT_AD_BIT;
1806}
1807
Gui Jianfeng31299942010-03-15 17:29:09 +08001808static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001809{
Gui Jianfeng31299942010-03-15 17:29:09 +08001810 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001811}
1812
Gui Jianfeng31299942010-03-15 17:29:09 +08001813static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001814{
Gui Jianfeng31299942010-03-15 17:29:09 +08001815 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001816}
1817
Liran Aloncd9a4912018-05-22 17:16:15 +03001818static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1819{
1820 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1821}
1822
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001823static inline bool cpu_has_vmx_invvpid_single(void)
1824{
1825 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1826}
1827
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001828static inline bool cpu_has_vmx_invvpid_global(void)
1829{
1830 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1831}
1832
Wanpeng Li08d839c2017-03-23 05:30:08 -07001833static inline bool cpu_has_vmx_invvpid(void)
1834{
1835 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1836}
1837
Gui Jianfeng31299942010-03-15 17:29:09 +08001838static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001839{
Sheng Yang04547152009-04-01 15:52:31 +08001840 return vmcs_config.cpu_based_2nd_exec_ctrl &
1841 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001842}
1843
Gui Jianfeng31299942010-03-15 17:29:09 +08001844static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001845{
1846 return vmcs_config.cpu_based_2nd_exec_ctrl &
1847 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1848}
1849
Gui Jianfeng31299942010-03-15 17:29:09 +08001850static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001851{
1852 return vmcs_config.cpu_based_2nd_exec_ctrl &
1853 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1854}
1855
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001856static inline bool cpu_has_vmx_basic_inout(void)
1857{
1858 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1859}
1860
Paolo Bonzini35754c92015-07-29 12:05:37 +02001861static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001862{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001863 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001864}
1865
Gui Jianfeng31299942010-03-15 17:29:09 +08001866static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001867{
Sheng Yang04547152009-04-01 15:52:31 +08001868 return vmcs_config.cpu_based_2nd_exec_ctrl &
1869 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001873{
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_RDTSCP;
1876}
1877
Mao, Junjiead756a12012-07-02 01:18:48 +00001878static inline bool cpu_has_vmx_invpcid(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_ENABLE_INVPCID;
1882}
1883
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001884static inline bool cpu_has_virtual_nmis(void)
1885{
1886 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1887}
1888
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001889static inline bool cpu_has_vmx_wbinvd_exit(void)
1890{
1891 return vmcs_config.cpu_based_2nd_exec_ctrl &
1892 SECONDARY_EXEC_WBINVD_EXITING;
1893}
1894
Abel Gordonabc4fc52013-04-18 14:35:25 +03001895static inline bool cpu_has_vmx_shadow_vmcs(void)
1896{
1897 u64 vmx_msr;
1898 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1899 /* check if the cpu supports writing r/o exit information fields */
1900 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1901 return false;
1902
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_SHADOW_VMCS;
1905}
1906
Kai Huang843e4332015-01-28 10:54:28 +08001907static inline bool cpu_has_vmx_pml(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1910}
1911
Haozhong Zhang64903d62015-10-20 15:39:09 +08001912static inline bool cpu_has_vmx_tsc_scaling(void)
1913{
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_TSC_SCALING;
1916}
1917
Bandan Das2a499e42017-08-03 15:54:41 -04001918static inline bool cpu_has_vmx_vmfunc(void)
1919{
1920 return vmcs_config.cpu_based_2nd_exec_ctrl &
1921 SECONDARY_EXEC_ENABLE_VMFUNC;
1922}
1923
Sean Christopherson64f7a112018-04-30 10:01:06 -07001924static bool vmx_umip_emulated(void)
1925{
1926 return vmcs_config.cpu_based_2nd_exec_ctrl &
1927 SECONDARY_EXEC_DESC;
1928}
1929
Sheng Yang04547152009-04-01 15:52:31 +08001930static inline bool report_flexpriority(void)
1931{
1932 return flexpriority_enabled;
1933}
1934
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001935static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1936{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001937 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001938}
1939
Jim Mattsonf4160e42018-05-29 09:11:33 -07001940/*
1941 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1942 * to modify any valid field of the VMCS, or are the VM-exit
1943 * information fields read-only?
1944 */
1945static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.misc_low &
1948 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1949}
1950
Marc Orr04473782018-06-20 17:21:29 -07001951static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1954}
1955
1956static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1957{
1958 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1959 CPU_BASED_MONITOR_TRAP_FLAG;
1960}
1961
Liran Alonfa97d7d2018-07-18 14:07:59 +02001962static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1963{
1964 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1965 SECONDARY_EXEC_SHADOW_VMCS;
1966}
1967
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001968static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1969{
1970 return vmcs12->cpu_based_vm_exec_control & bit;
1971}
1972
1973static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1974{
1975 return (vmcs12->cpu_based_vm_exec_control &
1976 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1977 (vmcs12->secondary_vm_exec_control & bit);
1978}
1979
Jan Kiszkaf4124502014-03-07 20:03:13 +01001980static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control &
1983 PIN_BASED_VMX_PREEMPTION_TIMER;
1984}
1985
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001986static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1987{
1988 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1989}
1990
1991static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1992{
1993 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1994}
1995
Nadav Har'El155a97a2013-08-05 11:07:16 +03001996static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1997{
1998 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1999}
2000
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002001static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2002{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002004}
2005
Bandan Dasc5f983f2017-05-05 15:25:14 -04002006static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2007{
2008 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2009}
2010
Wincy Vanf2b93282015-02-03 23:56:03 +08002011static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2012{
2013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2014}
2015
Wanpeng Li5c614b32015-10-13 09:18:36 -07002016static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2017{
2018 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2019}
2020
Wincy Van82f0dd42015-02-03 23:57:18 +08002021static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2022{
2023 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2024}
2025
Wincy Van608406e2015-02-03 23:57:51 +08002026static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2027{
2028 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2029}
2030
Wincy Van705699a2015-02-03 23:58:17 +08002031static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2032{
2033 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2034}
2035
Bandan Das27c42a12017-08-03 15:54:42 -04002036static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2037{
2038 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2039}
2040
Bandan Das41ab9372017-08-03 15:54:43 -04002041static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2042{
2043 return nested_cpu_has_vmfunc(vmcs12) &&
2044 (vmcs12->vm_function_control &
2045 VMX_VMFUNC_EPTP_SWITCHING);
2046}
2047
Liran Alonf792d272018-06-23 02:35:05 +03002048static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2049{
2050 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2051}
2052
Jim Mattsonef85b672016-12-12 11:01:37 -08002053static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002054{
2055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002056 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002057}
2058
Jan Kiszka533558b2014-01-04 18:47:20 +01002059static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2060 u32 exit_intr_info,
2061 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002062static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2063 struct vmcs12 *vmcs12,
2064 u32 reason, unsigned long qualification);
2065
Rusty Russell8b9cf982007-07-30 16:31:43 +10002066static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002067{
2068 int i;
2069
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002070 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002071 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002072 return i;
2073 return -1;
2074}
2075
Sheng Yang2384d2b2008-01-17 15:14:33 +08002076static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2077{
2078 struct {
2079 u64 vpid : 16;
2080 u64 rsvd : 48;
2081 u64 gva;
2082 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002083 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002084
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002085 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2086 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2087 : "memory");
2088 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002089}
2090
Sheng Yang14394422008-04-28 12:24:45 +08002091static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2092{
2093 struct {
2094 u64 eptp, gpa;
2095 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002096 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002097
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002098 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2099 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2100 : "memory");
2101 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002102}
2103
Avi Kivity26bb0982009-09-07 11:14:12 +03002104static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002105{
2106 int i;
2107
Rusty Russell8b9cf982007-07-30 16:31:43 +10002108 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002109 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002110 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002111 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void vmcs_clear(struct vmcs *vmcs)
2115{
2116 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002117 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002119 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2120 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2121 : "memory");
2122 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2124 vmcs, phys_addr);
2125}
2126
Nadav Har'Eld462b812011-05-24 15:26:10 +03002127static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2128{
2129 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002130 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2131 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002132 loaded_vmcs->cpu = -1;
2133 loaded_vmcs->launched = 0;
2134}
2135
Dongxiao Xu7725b892010-05-11 18:29:38 +08002136static void vmcs_load(struct vmcs *vmcs)
2137{
2138 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002139 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002140
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002141 if (static_branch_unlikely(&enable_evmcs))
2142 return evmcs_load(phys_addr);
2143
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002144 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2145 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2146 : "memory");
2147 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002148 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002149 vmcs, phys_addr);
2150}
2151
Dave Young2965faa2015-09-09 15:38:55 -07002152#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002153/*
2154 * This bitmap is used to indicate whether the vmclear
2155 * operation is enabled on all cpus. All disabled by
2156 * default.
2157 */
2158static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2159
2160static inline void crash_enable_local_vmclear(int cpu)
2161{
2162 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2163}
2164
2165static inline void crash_disable_local_vmclear(int cpu)
2166{
2167 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2168}
2169
2170static inline int crash_local_vmclear_enabled(int cpu)
2171{
2172 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2173}
2174
2175static void crash_vmclear_local_loaded_vmcss(void)
2176{
2177 int cpu = raw_smp_processor_id();
2178 struct loaded_vmcs *v;
2179
2180 if (!crash_local_vmclear_enabled(cpu))
2181 return;
2182
2183 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2184 loaded_vmcss_on_cpu_link)
2185 vmcs_clear(v->vmcs);
2186}
2187#else
2188static inline void crash_enable_local_vmclear(int cpu) { }
2189static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002190#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002191
Nadav Har'Eld462b812011-05-24 15:26:10 +03002192static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002194 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002195 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196
Nadav Har'Eld462b812011-05-24 15:26:10 +03002197 if (loaded_vmcs->cpu != cpu)
2198 return; /* vcpu migration can race with cpu offline */
2199 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002201 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002202 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002203
2204 /*
2205 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2206 * is before setting loaded_vmcs->vcpu to -1 which is done in
2207 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2208 * then adds the vmcs into percpu list before it is deleted.
2209 */
2210 smp_wmb();
2211
Nadav Har'Eld462b812011-05-24 15:26:10 +03002212 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002213 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Nadav Har'Eld462b812011-05-24 15:26:10 +03002216static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002217{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002218 int cpu = loaded_vmcs->cpu;
2219
2220 if (cpu != -1)
2221 smp_call_function_single(cpu,
2222 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002223}
2224
Junaid Shahidfaff8752018-06-29 13:10:05 -07002225static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2226{
2227 if (vpid == 0)
2228 return true;
2229
2230 if (cpu_has_vmx_invvpid_individual_addr()) {
2231 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002238static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002239{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002240 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002241 return;
2242
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002243 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002244 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002245}
2246
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002247static inline void vpid_sync_vcpu_global(void)
2248{
2249 if (cpu_has_vmx_invvpid_global())
2250 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2251}
2252
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002254{
2255 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002256 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002257 else
2258 vpid_sync_vcpu_global();
2259}
2260
Sheng Yang14394422008-04-28 12:24:45 +08002261static inline void ept_sync_global(void)
2262{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002263 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002264}
2265
2266static inline void ept_sync_context(u64 eptp)
2267{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002268 if (cpu_has_vmx_invept_context())
2269 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2270 else
2271 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002272}
2273
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002274static __always_inline void vmcs_check16(unsigned long field)
2275{
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2277 "16-bit accessor invalid for 64-bit field");
2278 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2279 "16-bit accessor invalid for 64-bit high field");
2280 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2281 "16-bit accessor invalid for 32-bit high field");
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2283 "16-bit accessor invalid for natural width field");
2284}
2285
2286static __always_inline void vmcs_check32(unsigned long field)
2287{
2288 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2289 "32-bit accessor invalid for 16-bit field");
2290 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2291 "32-bit accessor invalid for natural width field");
2292}
2293
2294static __always_inline void vmcs_check64(unsigned long field)
2295{
2296 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2297 "64-bit accessor invalid for 16-bit field");
2298 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2299 "64-bit accessor invalid for 64-bit high field");
2300 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2301 "64-bit accessor invalid for 32-bit field");
2302 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2303 "64-bit accessor invalid for natural width field");
2304}
2305
2306static __always_inline void vmcs_checkl(unsigned long field)
2307{
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2309 "Natural width accessor invalid for 16-bit field");
2310 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2311 "Natural width accessor invalid for 64-bit field");
2312 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2313 "Natural width accessor invalid for 64-bit high field");
2314 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2315 "Natural width accessor invalid for 32-bit field");
2316}
2317
2318static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319{
Avi Kivity5e520e62011-05-15 10:13:12 -04002320 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Avi Kivity5e520e62011-05-15 10:13:12 -04002322 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2323 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 return value;
2325}
2326
Avi Kivity96304212011-05-15 10:13:13 -04002327static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002330 if (static_branch_unlikely(&enable_evmcs))
2331 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002332 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
Avi Kivity96304212011-05-15 10:13:13 -04002335static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002338 if (static_branch_unlikely(&enable_evmcs))
2339 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341}
2342
Avi Kivity96304212011-05-15 10:13:13 -04002343static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002345 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002346 if (static_branch_unlikely(&enable_evmcs))
2347 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002348#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002349 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002351 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352#endif
2353}
2354
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355static __always_inline unsigned long vmcs_readl(unsigned long field)
2356{
2357 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002358 if (static_branch_unlikely(&enable_evmcs))
2359 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002360 return __vmcs_readl(field);
2361}
2362
Avi Kivitye52de1b2007-01-05 16:36:56 -08002363static noinline void vmwrite_error(unsigned long field, unsigned long value)
2364{
2365 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2366 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2367 dump_stack();
2368}
2369
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002370static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002372 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002374 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2375 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002376 if (unlikely(error))
2377 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002383 if (static_branch_unlikely(&enable_evmcs))
2384 return evmcs_write16(field, value);
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387}
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002392 if (static_branch_unlikely(&enable_evmcs))
2393 return evmcs_write32(field, value);
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002401 if (static_branch_unlikely(&enable_evmcs))
2402 return evmcs_write64(field, value);
2403
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002405#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408#endif
2409}
2410
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002412{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002414 if (static_branch_unlikely(&enable_evmcs))
2415 return evmcs_write64(field, value);
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418}
2419
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002420static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002421{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2423 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002424 if (static_branch_unlikely(&enable_evmcs))
2425 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2426
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002427 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2428}
2429
2430static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2431{
2432 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2433 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002434 if (static_branch_unlikely(&enable_evmcs))
2435 return evmcs_write32(field, evmcs_read32(field) | mask);
2436
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002437 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002438}
2439
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002440static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2441{
2442 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2443}
2444
Gleb Natapov2961e8762013-11-25 15:37:13 +02002445static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2446{
2447 vmcs_write32(VM_ENTRY_CONTROLS, val);
2448 vmx->vm_entry_controls_shadow = val;
2449}
2450
2451static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2452{
2453 if (vmx->vm_entry_controls_shadow != val)
2454 vm_entry_controls_init(vmx, val);
2455}
2456
2457static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2458{
2459 return vmx->vm_entry_controls_shadow;
2460}
2461
2462
2463static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2464{
2465 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2466}
2467
2468static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2469{
2470 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2471}
2472
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002473static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2474{
2475 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2476}
2477
Gleb Natapov2961e8762013-11-25 15:37:13 +02002478static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2479{
2480 vmcs_write32(VM_EXIT_CONTROLS, val);
2481 vmx->vm_exit_controls_shadow = val;
2482}
2483
2484static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2485{
2486 if (vmx->vm_exit_controls_shadow != val)
2487 vm_exit_controls_init(vmx, val);
2488}
2489
2490static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2491{
2492 return vmx->vm_exit_controls_shadow;
2493}
2494
2495
2496static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2497{
2498 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2499}
2500
2501static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2502{
2503 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2504}
2505
Avi Kivity2fb92db2011-04-27 19:42:18 +03002506static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2507{
2508 vmx->segment_cache.bitmask = 0;
2509}
2510
2511static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2512 unsigned field)
2513{
2514 bool ret;
2515 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2516
2517 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2518 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2519 vmx->segment_cache.bitmask = 0;
2520 }
2521 ret = vmx->segment_cache.bitmask & mask;
2522 vmx->segment_cache.bitmask |= mask;
2523 return ret;
2524}
2525
2526static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2527{
2528 u16 *p = &vmx->segment_cache.seg[seg].selector;
2529
2530 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2531 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2532 return *p;
2533}
2534
2535static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2536{
2537 ulong *p = &vmx->segment_cache.seg[seg].base;
2538
2539 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2540 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2541 return *p;
2542}
2543
2544static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2545{
2546 u32 *p = &vmx->segment_cache.seg[seg].limit;
2547
2548 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2549 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2550 return *p;
2551}
2552
2553static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2554{
2555 u32 *p = &vmx->segment_cache.seg[seg].ar;
2556
2557 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2558 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2559 return *p;
2560}
2561
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002562static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2563{
2564 u32 eb;
2565
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002566 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002567 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002568 /*
2569 * Guest access to VMware backdoor ports could legitimately
2570 * trigger #GP because of TSS I/O permission bitmap.
2571 * We intercept those #GP and allow access to them anyway
2572 * as VMware does.
2573 */
2574 if (enable_vmware_backdoor)
2575 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002576 if ((vcpu->guest_debug &
2577 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2578 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2579 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002580 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002581 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002582 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002583 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002584
2585 /* When we are running a nested L2 guest and L1 specified for it a
2586 * certain exception bitmap, we must trap the same exceptions and pass
2587 * them to L1. When running L2, we will only handle the exceptions
2588 * specified above if L1 did not want them.
2589 */
2590 if (is_guest_mode(vcpu))
2591 eb |= get_vmcs12(vcpu)->exception_bitmap;
2592
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002593 vmcs_write32(EXCEPTION_BITMAP, eb);
2594}
2595
Ashok Raj15d45072018-02-01 22:59:43 +01002596/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002597 * Check if MSR is intercepted for currently loaded MSR bitmap.
2598 */
2599static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2600{
2601 unsigned long *msr_bitmap;
2602 int f = sizeof(unsigned long);
2603
2604 if (!cpu_has_vmx_msr_bitmap())
2605 return true;
2606
2607 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2608
2609 if (msr <= 0x1fff) {
2610 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2611 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2612 msr &= 0x1fff;
2613 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2614 }
2615
2616 return true;
2617}
2618
2619/*
Ashok Raj15d45072018-02-01 22:59:43 +01002620 * Check if MSR is intercepted for L01 MSR bitmap.
2621 */
2622static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2623{
2624 unsigned long *msr_bitmap;
2625 int f = sizeof(unsigned long);
2626
2627 if (!cpu_has_vmx_msr_bitmap())
2628 return true;
2629
2630 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2631
2632 if (msr <= 0x1fff) {
2633 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2634 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2635 msr &= 0x1fff;
2636 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2637 }
2638
2639 return true;
2640}
2641
Gleb Natapov2961e8762013-11-25 15:37:13 +02002642static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2643 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002644{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002645 vm_entry_controls_clearbit(vmx, entry);
2646 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002647}
2648
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002649static int find_msr(struct vmx_msrs *m, unsigned int msr)
2650{
2651 unsigned int i;
2652
2653 for (i = 0; i < m->nr; ++i) {
2654 if (m->val[i].index == msr)
2655 return i;
2656 }
2657 return -ENOENT;
2658}
2659
Avi Kivity61d2ef22010-04-28 16:40:38 +03002660static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2661{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002662 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002663 struct msr_autoload *m = &vmx->msr_autoload;
2664
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002665 switch (msr) {
2666 case MSR_EFER:
2667 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002668 clear_atomic_switch_msr_special(vmx,
2669 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002670 VM_EXIT_LOAD_IA32_EFER);
2671 return;
2672 }
2673 break;
2674 case MSR_CORE_PERF_GLOBAL_CTRL:
2675 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002676 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002677 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2678 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2679 return;
2680 }
2681 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002682 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002683 i = find_msr(&m->guest, msr);
2684 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002685 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002686 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002687 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002688 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002689
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002690skip_guest:
2691 i = find_msr(&m->host, msr);
2692 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002693 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002694
2695 --m->host.nr;
2696 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002697 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002698}
2699
Gleb Natapov2961e8762013-11-25 15:37:13 +02002700static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2701 unsigned long entry, unsigned long exit,
2702 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2703 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002704{
2705 vmcs_write64(guest_val_vmcs, guest_val);
2706 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002707 vm_entry_controls_setbit(vmx, entry);
2708 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002709}
2710
Avi Kivity61d2ef22010-04-28 16:40:38 +03002711static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002712 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002713{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002714 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002715 struct msr_autoload *m = &vmx->msr_autoload;
2716
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002717 switch (msr) {
2718 case MSR_EFER:
2719 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002720 add_atomic_switch_msr_special(vmx,
2721 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002722 VM_EXIT_LOAD_IA32_EFER,
2723 GUEST_IA32_EFER,
2724 HOST_IA32_EFER,
2725 guest_val, host_val);
2726 return;
2727 }
2728 break;
2729 case MSR_CORE_PERF_GLOBAL_CTRL:
2730 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002731 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002732 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2733 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2734 GUEST_IA32_PERF_GLOBAL_CTRL,
2735 HOST_IA32_PERF_GLOBAL_CTRL,
2736 guest_val, host_val);
2737 return;
2738 }
2739 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002740 case MSR_IA32_PEBS_ENABLE:
2741 /* PEBS needs a quiescent period after being disabled (to write
2742 * a record). Disabling PEBS through VMX MSR swapping doesn't
2743 * provide that period, so a CPU could write host's record into
2744 * guest's memory.
2745 */
2746 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002747 }
2748
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002749 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002750 if (!entry_only)
2751 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002752
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002754 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002755 "Can't add msr %x\n", msr);
2756 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002757 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002758 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002759 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002760 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002761 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002762 m->guest.val[i].index = msr;
2763 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002764
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002765 if (entry_only)
2766 return;
2767
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002768 if (j < 0) {
2769 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002770 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002771 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002772 m->host.val[j].index = msr;
2773 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002774}
2775
Avi Kivity92c0d902009-10-29 11:00:16 +02002776static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002777{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002778 u64 guest_efer = vmx->vcpu.arch.efer;
2779 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002780
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002781 if (!enable_ept) {
2782 /*
2783 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2784 * host CPUID is more efficient than testing guest CPUID
2785 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2786 */
2787 if (boot_cpu_has(X86_FEATURE_SMEP))
2788 guest_efer |= EFER_NX;
2789 else if (!(guest_efer & EFER_NX))
2790 ignore_bits |= EFER_NX;
2791 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002792
Avi Kivity51c6cf62007-08-29 03:48:05 +03002793 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002794 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002795 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002796 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002797#ifdef CONFIG_X86_64
2798 ignore_bits |= EFER_LMA | EFER_LME;
2799 /* SCE is meaningful only in long mode on Intel */
2800 if (guest_efer & EFER_LMA)
2801 ignore_bits &= ~(u64)EFER_SCE;
2802#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002803
2804 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002805
2806 /*
2807 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2808 * On CPUs that support "load IA32_EFER", always switch EFER
2809 * atomically, since it's faster than switching it manually.
2810 */
2811 if (cpu_has_load_ia32_efer ||
2812 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813 if (!(guest_efer & EFER_LMA))
2814 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002815 if (guest_efer != host_efer)
2816 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002817 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002818 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002819 } else {
2820 guest_efer &= ~ignore_bits;
2821 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002822
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002823 vmx->guest_msrs[efer_offset].data = guest_efer;
2824 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2825
2826 return true;
2827 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002828}
2829
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002830#ifdef CONFIG_X86_32
2831/*
2832 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2833 * VMCS rather than the segment table. KVM uses this helper to figure
2834 * out the current bases to poke them into the VMCS before entry.
2835 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836static unsigned long segment_base(u16 selector)
2837{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 unsigned long v;
2840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 return 0;
2843
Thomas Garnier45fc8752017-03-14 10:05:08 -07002844 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 u16 ldt_selector = kvm_read_ldt();
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 return 0;
2851
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002852 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002853 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002854 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855 return v;
2856}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002857#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002858
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002859static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002860{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002862 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002863#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002864 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002865#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002866 unsigned long fs_base, gs_base;
2867 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002868 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002869
Sean Christophersond264ee02018-08-27 15:21:12 -07002870 vmx->req_immediate_exit = false;
2871
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 return;
2874
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002875 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002876 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002877
Avi Kivity33ed6322007-05-02 16:54:03 +03002878 /*
2879 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2880 * allow segment selectors with cpl > 0 or ti == 1.
2881 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002882 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002883
2884#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002885 savesegment(ds, host_state->ds_sel);
2886 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002887
2888 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002889 if (likely(is_64bit_mm(current->mm))) {
2890 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002891 fs_sel = current->thread.fsindex;
2892 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002895 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002896 savesegment(fs, fs_sel);
2897 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002898 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002899 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002900 }
2901
Avi Kivityc8770e72010-11-11 12:37:26 +02002902 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002903 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002904#else
Sean Christophersone368b872018-07-23 12:32:41 -07002905 savesegment(fs, fs_sel);
2906 savesegment(gs, gs_sel);
2907 fs_base = segment_base(fs_sel);
2908 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002909#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002910
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002911 if (unlikely(fs_sel != host_state->fs_sel)) {
2912 if (!(fs_sel & 7))
2913 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2914 else
2915 vmcs_write16(HOST_FS_SELECTOR, 0);
2916 host_state->fs_sel = fs_sel;
2917 }
2918 if (unlikely(gs_sel != host_state->gs_sel)) {
2919 if (!(gs_sel & 7))
2920 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2921 else
2922 vmcs_write16(HOST_GS_SELECTOR, 0);
2923 host_state->gs_sel = gs_sel;
2924 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002925 if (unlikely(fs_base != host_state->fs_base)) {
2926 vmcs_writel(HOST_FS_BASE, fs_base);
2927 host_state->fs_base = fs_base;
2928 }
2929 if (unlikely(gs_base != host_state->gs_base)) {
2930 vmcs_writel(HOST_GS_BASE, gs_base);
2931 host_state->gs_base = gs_base;
2932 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002933
Avi Kivity26bb0982009-09-07 11:14:12 +03002934 for (i = 0; i < vmx->save_nmsrs; ++i)
2935 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002936 vmx->guest_msrs[i].data,
2937 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002938}
2939
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002940static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002941{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002942 struct vmcs_host_state *host_state;
2943
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002944 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002945 return;
2946
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002947 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002948 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002949
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002950 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002951 vmx->loaded_cpu_state = NULL;
2952
Avi Kivityc8770e72010-11-11 12:37:26 +02002953#ifdef CONFIG_X86_64
2954 if (is_long_mode(&vmx->vcpu))
2955 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2956#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2958 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002959#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002961#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002962 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002963#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002964 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002965 if (host_state->fs_sel & 7)
2966 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002967#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002968 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2969 loadsegment(ds, host_state->ds_sel);
2970 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002971 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002972#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002973 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002974#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002975 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002976#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002977 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002978}
2979
Sean Christopherson678e3152018-07-23 12:32:43 -07002980#ifdef CONFIG_X86_64
2981static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002982{
Sean Christopherson678e3152018-07-23 12:32:43 -07002983 if (is_long_mode(&vmx->vcpu)) {
2984 preempt_disable();
2985 if (vmx->loaded_cpu_state)
2986 rdmsrl(MSR_KERNEL_GS_BASE,
2987 vmx->msr_guest_kernel_gs_base);
2988 preempt_enable();
2989 }
2990 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002991}
2992
Sean Christopherson678e3152018-07-23 12:32:43 -07002993static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2994{
2995 if (is_long_mode(&vmx->vcpu)) {
2996 preempt_disable();
2997 if (vmx->loaded_cpu_state)
2998 wrmsrl(MSR_KERNEL_GS_BASE, data);
2999 preempt_enable();
3000 }
3001 vmx->msr_guest_kernel_gs_base = data;
3002}
3003#endif
3004
Feng Wu28b835d2015-09-18 22:29:54 +08003005static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3006{
3007 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3008 struct pi_desc old, new;
3009 unsigned int dest;
3010
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003011 /*
3012 * In case of hot-plug or hot-unplug, we may have to undo
3013 * vmx_vcpu_pi_put even if there is no assigned device. And we
3014 * always keep PI.NDST up to date for simplicity: it makes the
3015 * code easier, and CPU migration is not a fast path.
3016 */
3017 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003018 return;
3019
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003020 /*
3021 * First handle the simple case where no cmpxchg is necessary; just
3022 * allow posting non-urgent interrupts.
3023 *
3024 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3025 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3026 * expects the VCPU to be on the blocked_vcpu_list that matches
3027 * PI.NDST.
3028 */
3029 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3030 vcpu->cpu == cpu) {
3031 pi_clear_sn(pi_desc);
3032 return;
3033 }
3034
3035 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003036 do {
3037 old.control = new.control = pi_desc->control;
3038
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003039 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003040
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003041 if (x2apic_enabled())
3042 new.ndst = dest;
3043 else
3044 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003045
Feng Wu28b835d2015-09-18 22:29:54 +08003046 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003047 } while (cmpxchg64(&pi_desc->control, old.control,
3048 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003049}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003050
Peter Feinerc95ba922016-08-17 09:36:47 -07003051static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3052{
3053 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3054 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3055}
3056
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057/*
3058 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3059 * vcpu mutex is already taken.
3060 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003061static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003064 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003066 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003067 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003068 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003069 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003070
3071 /*
3072 * Read loaded_vmcs->cpu should be before fetching
3073 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3074 * See the comments in __loaded_vmcs_clear().
3075 */
3076 smp_rmb();
3077
Nadav Har'Eld462b812011-05-24 15:26:10 +03003078 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3079 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003080 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003081 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003082 }
3083
3084 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3085 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3086 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003087 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003088 }
3089
3090 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003091 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003092 unsigned long sysenter_esp;
3093
3094 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003095
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 /*
3097 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003098 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003100 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003101 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003102 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003104 /*
3105 * VM exits change the host TR limit to 0x67 after a VM
3106 * exit. This is okay, since 0x67 covers everything except
3107 * the IO bitmap and have have code to handle the IO bitmap
3108 * being lost after a VM exit.
3109 */
3110 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3111
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3113 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003114
Nadav Har'Eld462b812011-05-24 15:26:10 +03003115 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116 }
Feng Wu28b835d2015-09-18 22:29:54 +08003117
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003118 /* Setup TSC multiplier */
3119 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003120 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3121 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003122
Feng Wu28b835d2015-09-18 22:29:54 +08003123 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003124 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003125 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003126}
3127
3128static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3129{
3130 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3131
3132 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003133 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3134 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003135 return;
3136
3137 /* Set SN when the vCPU is preempted */
3138 if (vcpu->preempted)
3139 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140}
3141
3142static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3143{
Feng Wu28b835d2015-09-18 22:29:54 +08003144 vmx_vcpu_pi_put(vcpu);
3145
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003146 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147}
3148
Wanpeng Lif244dee2017-07-20 01:11:54 -07003149static bool emulation_required(struct kvm_vcpu *vcpu)
3150{
3151 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3152}
3153
Avi Kivityedcafe32009-12-30 18:07:40 +02003154static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3155
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003156/*
3157 * Return the cr0 value that a nested guest would read. This is a combination
3158 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3159 * its hypervisor (cr0_read_shadow).
3160 */
3161static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3162{
3163 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3164 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3165}
3166static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3167{
3168 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3169 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3170}
3171
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3173{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003174 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003175
Avi Kivity6de12732011-03-07 12:51:22 +02003176 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3177 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3178 rflags = vmcs_readl(GUEST_RFLAGS);
3179 if (to_vmx(vcpu)->rmode.vm86_active) {
3180 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3181 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3182 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3183 }
3184 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003185 }
Avi Kivity6de12732011-03-07 12:51:22 +02003186 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187}
3188
3189static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3190{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003191 unsigned long old_rflags = vmx_get_rflags(vcpu);
3192
Avi Kivity6de12732011-03-07 12:51:22 +02003193 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3194 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003195 if (to_vmx(vcpu)->rmode.vm86_active) {
3196 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003197 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003198 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003200
3201 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3202 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203}
3204
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003205static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003206{
3207 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3208 int ret = 0;
3209
3210 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003211 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003212 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003213 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003214
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003215 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003216}
3217
3218static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3219{
3220 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3221 u32 interruptibility = interruptibility_old;
3222
3223 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3224
Jan Kiszka48005f62010-02-19 19:38:07 +01003225 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003226 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003227 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003228 interruptibility |= GUEST_INTR_STATE_STI;
3229
3230 if ((interruptibility != interruptibility_old))
3231 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3232}
3233
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3235{
3236 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003238 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003240 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241
Glauber Costa2809f5d2009-05-12 16:21:05 -04003242 /* skipping an emulated instruction also counts */
3243 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244}
3245
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003246static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3247 unsigned long exit_qual)
3248{
3249 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3250 unsigned int nr = vcpu->arch.exception.nr;
3251 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3252
3253 if (vcpu->arch.exception.has_error_code) {
3254 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3255 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3256 }
3257
3258 if (kvm_exception_is_soft(nr))
3259 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3260 else
3261 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3262
3263 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3264 vmx_get_nmi_mask(vcpu))
3265 intr_info |= INTR_INFO_UNBLOCK_NMI;
3266
3267 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3268}
3269
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003270/*
3271 * KVM wants to inject page-faults which it got to the guest. This function
3272 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003273 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003274static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003275{
3276 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003277 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003278
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003279 if (nr == PF_VECTOR) {
3280 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003281 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003282 return 1;
3283 }
3284 /*
3285 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3286 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3287 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3288 * can be written only when inject_pending_event runs. This should be
3289 * conditional on a new capability---if the capability is disabled,
3290 * kvm_multiple_exception would write the ancillary information to
3291 * CR2 or DR6, for backwards ABI-compatibility.
3292 */
3293 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3294 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003295 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003296 return 1;
3297 }
3298 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003299 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003300 if (nr == DB_VECTOR)
3301 *exit_qual = vcpu->arch.dr6;
3302 else
3303 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003304 return 1;
3305 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003306 }
3307
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003308 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003309}
3310
Wanpeng Licaa057a2018-03-12 04:53:03 -07003311static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3312{
3313 /*
3314 * Ensure that we clear the HLT state in the VMCS. We don't need to
3315 * explicitly skip the instruction because if the HLT state is set,
3316 * then the instruction is already executing and RIP has already been
3317 * advanced.
3318 */
3319 if (kvm_hlt_in_guest(vcpu->kvm) &&
3320 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3321 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3322}
3323
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003324static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003325{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003326 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003327 unsigned nr = vcpu->arch.exception.nr;
3328 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003329 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003330 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003331
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003332 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003333 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003334 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3335 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003336
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003337 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003338 int inc_eip = 0;
3339 if (kvm_exception_is_soft(nr))
3340 inc_eip = vcpu->arch.event_exit_inst_len;
3341 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003342 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003343 return;
3344 }
3345
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003346 WARN_ON_ONCE(vmx->emulation_required);
3347
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003348 if (kvm_exception_is_soft(nr)) {
3349 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3350 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003351 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3352 } else
3353 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3354
3355 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003356
3357 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003358}
3359
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003360static bool vmx_rdtscp_supported(void)
3361{
3362 return cpu_has_vmx_rdtscp();
3363}
3364
Mao, Junjiead756a12012-07-02 01:18:48 +00003365static bool vmx_invpcid_supported(void)
3366{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003367 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003368}
3369
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370/*
Eddie Donga75beee2007-05-17 18:55:15 +03003371 * Swap MSR entry in host/guest MSR entry array.
3372 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003373static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003374{
Avi Kivity26bb0982009-09-07 11:14:12 +03003375 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003376
3377 tmp = vmx->guest_msrs[to];
3378 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3379 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003380}
3381
3382/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003383 * Set up the vmcs to automatically save and restore system
3384 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3385 * mode, as fiddling with msrs is very expensive.
3386 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003388{
Avi Kivity26bb0982009-09-07 11:14:12 +03003389 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003390
Eddie Donga75beee2007-05-17 18:55:15 +03003391 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003392#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003393 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003394 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003395 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003396 move_msr_up(vmx, index, save_nmsrs++);
3397 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003398 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003399 move_msr_up(vmx, index, save_nmsrs++);
3400 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003401 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003402 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003403 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003404 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003405 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003406 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003407 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003408 * if efer.sce is enabled.
3409 */
Brian Gerst8c065852010-07-17 09:03:26 -04003410 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003411 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003412 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003413 }
Eddie Donga75beee2007-05-17 18:55:15 +03003414#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003415 index = __find_msr_index(vmx, MSR_EFER);
3416 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003417 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003418
Avi Kivity26bb0982009-09-07 11:14:12 +03003419 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003420
Yang Zhang8d146952013-01-25 10:18:50 +08003421 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003422 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003423}
3424
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003425static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003427 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003429 if (is_guest_mode(vcpu) &&
3430 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3431 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3432
3433 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434}
3435
3436/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003437 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003439static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003441 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003442 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003443 * We're here if L1 chose not to trap WRMSR to TSC. According
3444 * to the spec, this should set L1's TSC; The offset that L1
3445 * set for L2 remains unchanged, and still needs to be added
3446 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003447 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003448 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003449 /* recalculate vmcs02.TSC_OFFSET: */
3450 vmcs12 = get_vmcs12(vcpu);
3451 vmcs_write64(TSC_OFFSET, offset +
3452 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3453 vmcs12->tsc_offset : 0));
3454 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003455 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3456 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003457 vmcs_write64(TSC_OFFSET, offset);
3458 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459}
3460
Nadav Har'El801d3422011-05-25 23:02:23 +03003461/*
3462 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3463 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3464 * all guests if the "nested" module option is off, and can also be disabled
3465 * for a single guest by disabling its VMX cpuid bit.
3466 */
3467static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3468{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003469 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003470}
3471
Avi Kivity6aa8b732006-12-10 02:21:36 -08003472/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003473 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3474 * returned for the various VMX controls MSRs when nested VMX is enabled.
3475 * The same values should also be used to verify that vmcs12 control fields are
3476 * valid during nested entry from L1 to L2.
3477 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3478 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3479 * bit in the high half is on if the corresponding bit in the control field
3480 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003481 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003482static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003483{
Paolo Bonzini13893092018-02-26 13:40:09 +01003484 if (!nested) {
3485 memset(msrs, 0, sizeof(*msrs));
3486 return;
3487 }
3488
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003489 /*
3490 * Note that as a general rule, the high half of the MSRs (bits in
3491 * the control fields which may be 1) should be initialized by the
3492 * intersection of the underlying hardware's MSR (i.e., features which
3493 * can be supported) and the list of features we want to expose -
3494 * because they are known to be properly supported in our code.
3495 * Also, usually, the low half of the MSRs (bits which must be 1) can
3496 * be set to 0, meaning that L1 may turn off any of these bits. The
3497 * reason is that if one of these bits is necessary, it will appear
3498 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3499 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003500 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003501 * These rules have exceptions below.
3502 */
3503
3504 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003505 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 msrs->pinbased_ctls_low,
3507 msrs->pinbased_ctls_high);
3508 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003509 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003511 PIN_BASED_EXT_INTR_MASK |
3512 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003513 PIN_BASED_VIRTUAL_NMIS |
3514 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003515 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003516 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003517 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003518
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003519 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003520 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003521 msrs->exit_ctls_low,
3522 msrs->exit_ctls_high);
3523 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003524 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003525
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003526 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003527#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003528 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003529#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003530 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003531 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003532 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003533 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003534 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3535
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003536 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003538
Jan Kiszka2996fca2014-06-16 13:59:43 +02003539 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003540 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003541
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003542 /* entry controls */
3543 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003544 msrs->entry_ctls_low,
3545 msrs->entry_ctls_high);
3546 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003547 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003549#ifdef CONFIG_X86_64
3550 VM_ENTRY_IA32E_MODE |
3551#endif
3552 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003553 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003554 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003555 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003556 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003557
Jan Kiszka2996fca2014-06-16 13:59:43 +02003558 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003559 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003560
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003561 /* cpu-based controls */
3562 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003563 msrs->procbased_ctls_low,
3564 msrs->procbased_ctls_high);
3565 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003566 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003568 CPU_BASED_VIRTUAL_INTR_PENDING |
3569 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003570 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3571 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3572 CPU_BASED_CR3_STORE_EXITING |
3573#ifdef CONFIG_X86_64
3574 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3575#endif
3576 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003577 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3578 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3579 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3580 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003581 /*
3582 * We can allow some features even when not supported by the
3583 * hardware. For example, L1 can specify an MSR bitmap - and we
3584 * can use it to avoid exits to L1 - even when L0 runs L2
3585 * without MSR bitmaps.
3586 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003588 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003589 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003590
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003591 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003592 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003593 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3594
Paolo Bonzini80154d72017-08-24 13:55:35 +02003595 /*
3596 * secondary cpu-based controls. Do not include those that
3597 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3598 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003599 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003600 msrs->secondary_ctls_low,
3601 msrs->secondary_ctls_high);
3602 msrs->secondary_ctls_low = 0;
3603 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003604 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003605 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003606 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003607 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003608 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003609 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003610 /*
3611 * We can emulate "VMCS shadowing," even if the hardware
3612 * doesn't support it.
3613 */
3614 msrs->secondary_ctls_high |=
3615 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003616
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003617 if (enable_ept) {
3618 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003620 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003622 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003623 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003624 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003625 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003626 msrs->ept_caps &= vmx_capability.ept;
3627 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003628 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3629 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003630 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003631 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003632 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003634 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003635 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003636
Bandan Das27c42a12017-08-03 15:54:42 -04003637 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003639 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003640 /*
3641 * Advertise EPTP switching unconditionally
3642 * since we emulate it
3643 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003644 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003646 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003647 }
3648
Paolo Bonzinief697a72016-03-18 16:58:38 +01003649 /*
3650 * Old versions of KVM use the single-context version without
3651 * checking for support, so declare that it is supported even
3652 * though it is treated as global context. The alternative is
3653 * not failing the single-context invvpid, and it is worse.
3654 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003655 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003656 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003657 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003659 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003660 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003661
Radim Krčmář0790ec12015-03-17 14:02:32 +01003662 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003664 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3665
Jan Kiszkac18911a2013-03-13 16:06:41 +01003666 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003667 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003668 msrs->misc_low,
3669 msrs->misc_high);
3670 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3671 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003672 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003673 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003674 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003675 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003676
3677 /*
3678 * This MSR reports some information about VMX support. We
3679 * should return information about the VMX we emulate for the
3680 * guest, and the VMCS structure we give it - not about the
3681 * VMX support of the underlying hardware.
3682 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003684 VMCS12_REVISION |
3685 VMX_BASIC_TRUE_CTLS |
3686 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3687 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3688
3689 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003690 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003691
3692 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003693 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003694 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3695 * We picked the standard core2 setting.
3696 */
3697#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3698#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003699 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3700 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003701
3702 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003703 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3704 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003705
3706 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003707 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708}
3709
David Matlack38991522016-11-29 18:14:08 -08003710/*
3711 * if fixed0[i] == 1: val[i] must be 1
3712 * if fixed1[i] == 0: val[i] must be 0
3713 */
3714static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3715{
3716 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717}
3718
3719static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3720{
David Matlack38991522016-11-29 18:14:08 -08003721 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003722}
3723
3724static inline u64 vmx_control_msr(u32 low, u32 high)
3725{
3726 return low | ((u64)high << 32);
3727}
3728
David Matlack62cc6b9d2016-11-29 18:14:07 -08003729static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3730{
3731 superset &= mask;
3732 subset &= mask;
3733
3734 return (superset | subset) == superset;
3735}
3736
3737static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3738{
3739 const u64 feature_and_reserved =
3740 /* feature (except bit 48; see below) */
3741 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3742 /* reserved */
3743 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003744 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003745
3746 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3747 return -EINVAL;
3748
3749 /*
3750 * KVM does not emulate a version of VMX that constrains physical
3751 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3752 */
3753 if (data & BIT_ULL(48))
3754 return -EINVAL;
3755
3756 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3757 vmx_basic_vmcs_revision_id(data))
3758 return -EINVAL;
3759
3760 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3761 return -EINVAL;
3762
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003763 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003764 return 0;
3765}
3766
3767static int
3768vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3769{
3770 u64 supported;
3771 u32 *lowp, *highp;
3772
3773 switch (msr_index) {
3774 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3776 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777 break;
3778 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003779 lowp = &vmx->nested.msrs.procbased_ctls_low;
3780 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003781 break;
3782 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003783 lowp = &vmx->nested.msrs.exit_ctls_low;
3784 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003785 break;
3786 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003787 lowp = &vmx->nested.msrs.entry_ctls_low;
3788 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003789 break;
3790 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003791 lowp = &vmx->nested.msrs.secondary_ctls_low;
3792 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003793 break;
3794 default:
3795 BUG();
3796 }
3797
3798 supported = vmx_control_msr(*lowp, *highp);
3799
3800 /* Check must-be-1 bits are still 1. */
3801 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3802 return -EINVAL;
3803
3804 /* Check must-be-0 bits are still 0. */
3805 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3806 return -EINVAL;
3807
3808 *lowp = data;
3809 *highp = data >> 32;
3810 return 0;
3811}
3812
3813static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3814{
3815 const u64 feature_and_reserved_bits =
3816 /* feature */
3817 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3818 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3819 /* reserved */
3820 GENMASK_ULL(13, 9) | BIT_ULL(31);
3821 u64 vmx_misc;
3822
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003823 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3824 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003825
3826 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3827 return -EINVAL;
3828
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003829 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003830 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3831 vmx_misc_preemption_timer_rate(data) !=
3832 vmx_misc_preemption_timer_rate(vmx_misc))
3833 return -EINVAL;
3834
3835 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3836 return -EINVAL;
3837
3838 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3839 return -EINVAL;
3840
3841 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3842 return -EINVAL;
3843
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003844 vmx->nested.msrs.misc_low = data;
3845 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003846
3847 /*
3848 * If L1 has read-only VM-exit information fields, use the
3849 * less permissive vmx_vmwrite_bitmap to specify write
3850 * permissions for the shadow VMCS.
3851 */
3852 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3853 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3854
David Matlack62cc6b9d2016-11-29 18:14:07 -08003855 return 0;
3856}
3857
3858static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3859{
3860 u64 vmx_ept_vpid_cap;
3861
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003862 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3863 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003864
3865 /* Every bit is either reserved or a feature bit. */
3866 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3867 return -EINVAL;
3868
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003869 vmx->nested.msrs.ept_caps = data;
3870 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003871 return 0;
3872}
3873
3874static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3875{
3876 u64 *msr;
3877
3878 switch (msr_index) {
3879 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003880 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003881 break;
3882 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003883 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003884 break;
3885 default:
3886 BUG();
3887 }
3888
3889 /*
3890 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3891 * must be 1 in the restored value.
3892 */
3893 if (!is_bitwise_subset(data, *msr, -1ULL))
3894 return -EINVAL;
3895
3896 *msr = data;
3897 return 0;
3898}
3899
3900/*
3901 * Called when userspace is restoring VMX MSRs.
3902 *
3903 * Returns 0 on success, non-0 otherwise.
3904 */
3905static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3906{
3907 struct vcpu_vmx *vmx = to_vmx(vcpu);
3908
Jim Mattsona943ac52018-05-29 09:11:32 -07003909 /*
3910 * Don't allow changes to the VMX capability MSRs while the vCPU
3911 * is in VMX operation.
3912 */
3913 if (vmx->nested.vmxon)
3914 return -EBUSY;
3915
David Matlack62cc6b9d2016-11-29 18:14:07 -08003916 switch (msr_index) {
3917 case MSR_IA32_VMX_BASIC:
3918 return vmx_restore_vmx_basic(vmx, data);
3919 case MSR_IA32_VMX_PINBASED_CTLS:
3920 case MSR_IA32_VMX_PROCBASED_CTLS:
3921 case MSR_IA32_VMX_EXIT_CTLS:
3922 case MSR_IA32_VMX_ENTRY_CTLS:
3923 /*
3924 * The "non-true" VMX capability MSRs are generated from the
3925 * "true" MSRs, so we do not support restoring them directly.
3926 *
3927 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3928 * should restore the "true" MSRs with the must-be-1 bits
3929 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3930 * DEFAULT SETTINGS".
3931 */
3932 return -EINVAL;
3933 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3934 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3935 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3936 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3937 case MSR_IA32_VMX_PROCBASED_CTLS2:
3938 return vmx_restore_control_msr(vmx, msr_index, data);
3939 case MSR_IA32_VMX_MISC:
3940 return vmx_restore_vmx_misc(vmx, data);
3941 case MSR_IA32_VMX_CR0_FIXED0:
3942 case MSR_IA32_VMX_CR4_FIXED0:
3943 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3944 case MSR_IA32_VMX_CR0_FIXED1:
3945 case MSR_IA32_VMX_CR4_FIXED1:
3946 /*
3947 * These MSRs are generated based on the vCPU's CPUID, so we
3948 * do not support restoring them directly.
3949 */
3950 return -EINVAL;
3951 case MSR_IA32_VMX_EPT_VPID_CAP:
3952 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3953 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003954 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003955 return 0;
3956 default:
3957 /*
3958 * The rest of the VMX capability MSRs do not support restore.
3959 */
3960 return -EINVAL;
3961 }
3962}
3963
Jan Kiszkacae50132014-01-04 18:47:22 +01003964/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003965static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003966{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003967 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003968 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003969 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003970 break;
3971 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3972 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003973 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003974 msrs->pinbased_ctls_low,
3975 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003976 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3977 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003978 break;
3979 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3980 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003981 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003982 msrs->procbased_ctls_low,
3983 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003984 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3985 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 break;
3987 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3988 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003989 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003990 msrs->exit_ctls_low,
3991 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003992 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3993 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
3995 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3996 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003997 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003998 msrs->entry_ctls_low,
3999 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08004000 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
4001 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004004 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004005 msrs->misc_low,
4006 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004007 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004009 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004010 break;
4011 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004012 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004013 break;
4014 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004015 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004016 break;
4017 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004018 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 break;
4020 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004021 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 break;
4023 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004024 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004025 msrs->secondary_ctls_low,
4026 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004027 break;
4028 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004029 *pdata = msrs->ept_caps |
4030 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004032 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004033 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004034 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004035 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004036 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004037 }
4038
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004039 return 0;
4040}
4041
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004042static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4043 uint64_t val)
4044{
4045 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4046
4047 return !(val & ~valid_bits);
4048}
4049
Tom Lendacky801e4592018-02-21 13:39:51 -06004050static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4051{
Paolo Bonzini13893092018-02-26 13:40:09 +01004052 switch (msr->index) {
4053 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4054 if (!nested)
4055 return 1;
4056 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4057 default:
4058 return 1;
4059 }
4060
4061 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004062}
4063
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004064/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 * Reads an msr value (of 'msr_index') into 'pdata'.
4066 * Returns 0 on success, non-0 otherwise.
4067 * Assumes vcpu_load() was already called.
4068 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004071 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004072 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004074 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004075#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004077 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 break;
4079 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004080 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004082 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004083 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004084 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004085#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004086 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004087 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004088 case MSR_IA32_SPEC_CTRL:
4089 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004090 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4091 return 1;
4092
4093 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4094 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004095 case MSR_IA32_ARCH_CAPABILITIES:
4096 if (!msr_info->host_initiated &&
4097 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4098 return 1;
4099 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4100 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004102 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004103 break;
4104 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004105 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004106 break;
4107 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004108 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004109 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004110 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004111 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004112 (!msr_info->host_initiated &&
4113 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004114 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004115 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004116 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004117 case MSR_IA32_MCG_EXT_CTL:
4118 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004119 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004120 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004121 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004122 msr_info->data = vcpu->arch.mcg_ext_ctl;
4123 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004124 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004125 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004126 break;
4127 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4128 if (!nested_vmx_allowed(vcpu))
4129 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004130 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4131 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004132 case MSR_IA32_XSS:
4133 if (!vmx_xsaves_supported())
4134 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004136 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004137 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004138 if (!msr_info->host_initiated &&
4139 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004140 return 1;
4141 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004143 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004144 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004145 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004146 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004148 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149 }
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151 return 0;
4152}
4153
Jan Kiszkacae50132014-01-04 18:47:22 +01004154static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4155
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156/*
4157 * Writes msr value into into the appropriate "register".
4158 * Returns 0 on success, non-0 otherwise.
4159 * Assumes vcpu_load() was already called.
4160 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004161static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004163 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004164 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004165 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004166 u32 msr_index = msr_info->index;
4167 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004168
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004170 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004171 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004172 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004173#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004175 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004176 vmcs_writel(GUEST_FS_BASE, data);
4177 break;
4178 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004179 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 vmcs_writel(GUEST_GS_BASE, data);
4181 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004182 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004183 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004184 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185#endif
4186 case MSR_IA32_SYSENTER_CS:
4187 vmcs_write32(GUEST_SYSENTER_CS, data);
4188 break;
4189 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004190 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191 break;
4192 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004193 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004195 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004196 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004197 (!msr_info->host_initiated &&
4198 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004199 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004200 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004201 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004202 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004203 vmcs_write64(GUEST_BNDCFGS, data);
4204 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004205 case MSR_IA32_SPEC_CTRL:
4206 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004207 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4208 return 1;
4209
4210 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004211 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004212 return 1;
4213
4214 vmx->spec_ctrl = data;
4215
4216 if (!data)
4217 break;
4218
4219 /*
4220 * For non-nested:
4221 * When it's written (to non-zero) for the first time, pass
4222 * it through.
4223 *
4224 * For nested:
4225 * The handling of the MSR bitmap for L2 guests is done in
4226 * nested_vmx_merge_msr_bitmap. We should not touch the
4227 * vmcs02.msr_bitmap here since it gets completely overwritten
4228 * in the merging. We update the vmcs01 here for L1 as well
4229 * since it will end up touching the MSR anyway now.
4230 */
4231 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4232 MSR_IA32_SPEC_CTRL,
4233 MSR_TYPE_RW);
4234 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004235 case MSR_IA32_PRED_CMD:
4236 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004237 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4238 return 1;
4239
4240 if (data & ~PRED_CMD_IBPB)
4241 return 1;
4242
4243 if (!data)
4244 break;
4245
4246 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4247
4248 /*
4249 * For non-nested:
4250 * When it's written (to non-zero) for the first time, pass
4251 * it through.
4252 *
4253 * For nested:
4254 * The handling of the MSR bitmap for L2 guests is done in
4255 * nested_vmx_merge_msr_bitmap. We should not touch the
4256 * vmcs02.msr_bitmap here since it gets completely overwritten
4257 * in the merging.
4258 */
4259 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4260 MSR_TYPE_W);
4261 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004262 case MSR_IA32_ARCH_CAPABILITIES:
4263 if (!msr_info->host_initiated)
4264 return 1;
4265 vmx->arch_capabilities = data;
4266 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004267 case MSR_IA32_CR_PAT:
4268 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004269 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4270 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004271 vmcs_write64(GUEST_IA32_PAT, data);
4272 vcpu->arch.pat = data;
4273 break;
4274 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004275 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004276 break;
Will Auldba904632012-11-29 12:42:50 -08004277 case MSR_IA32_TSC_ADJUST:
4278 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004279 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004280 case MSR_IA32_MCG_EXT_CTL:
4281 if ((!msr_info->host_initiated &&
4282 !(to_vmx(vcpu)->msr_ia32_feature_control &
4283 FEATURE_CONTROL_LMCE)) ||
4284 (data & ~MCG_EXT_CTL_LMCE_EN))
4285 return 1;
4286 vcpu->arch.mcg_ext_ctl = data;
4287 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004288 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004289 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004290 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004291 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4292 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004293 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004294 if (msr_info->host_initiated && data == 0)
4295 vmx_leave_nested(vcpu);
4296 break;
4297 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004298 if (!msr_info->host_initiated)
4299 return 1; /* they are read-only */
4300 if (!nested_vmx_allowed(vcpu))
4301 return 1;
4302 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004303 case MSR_IA32_XSS:
4304 if (!vmx_xsaves_supported())
4305 return 1;
4306 /*
4307 * The only supported bit as of Skylake is bit 8, but
4308 * it is not supported on KVM.
4309 */
4310 if (data != 0)
4311 return 1;
4312 vcpu->arch.ia32_xss = data;
4313 if (vcpu->arch.ia32_xss != host_xss)
4314 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004315 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004316 else
4317 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4318 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004319 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004320 if (!msr_info->host_initiated &&
4321 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004322 return 1;
4323 /* Check reserved bit, higher 32 bits should be zero */
4324 if ((data >> 32) != 0)
4325 return 1;
4326 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004328 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004329 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004330 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004331 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004332 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4333 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004334 ret = kvm_set_shared_msr(msr->index, msr->data,
4335 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004336 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004337 if (ret)
4338 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004339 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004340 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004342 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004343 }
4344
Eddie Dong2cc51562007-05-21 07:28:09 +03004345 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346}
4347
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004348static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004350 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4351 switch (reg) {
4352 case VCPU_REGS_RSP:
4353 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4354 break;
4355 case VCPU_REGS_RIP:
4356 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4357 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004358 case VCPU_EXREG_PDPTR:
4359 if (enable_ept)
4360 ept_save_pdptrs(vcpu);
4361 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004362 default:
4363 break;
4364 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
Avi Kivity6aa8b732006-12-10 02:21:36 -08004367static __init int cpu_has_kvm_support(void)
4368{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004369 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004370}
4371
4372static __init int vmx_disabled_by_bios(void)
4373{
4374 u64 msr;
4375
4376 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004377 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004378 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004379 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4380 && tboot_enabled())
4381 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004383 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004384 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004385 && !tboot_enabled()) {
4386 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004387 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004388 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004389 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004390 /* launched w/o TXT and VMX disabled */
4391 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4392 && !tboot_enabled())
4393 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004394 }
4395
4396 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397}
4398
Dongxiao Xu7725b892010-05-11 18:29:38 +08004399static void kvm_cpu_vmxon(u64 addr)
4400{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004401 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004402 intel_pt_handle_vmx(1);
4403
Dongxiao Xu7725b892010-05-11 18:29:38 +08004404 asm volatile (ASM_VMX_VMXON_RAX
4405 : : "a"(&addr), "m"(addr)
4406 : "memory", "cc");
4407}
4408
Radim Krčmář13a34e02014-08-28 15:13:03 +02004409static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410{
4411 int cpu = raw_smp_processor_id();
4412 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004413 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004415 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004416 return -EBUSY;
4417
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004418 /*
4419 * This can happen if we hot-added a CPU but failed to allocate
4420 * VP assist page for it.
4421 */
4422 if (static_branch_unlikely(&enable_evmcs) &&
4423 !hv_get_vp_assist_page(cpu))
4424 return -EFAULT;
4425
Nadav Har'Eld462b812011-05-24 15:26:10 +03004426 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004427 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4428 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004429
4430 /*
4431 * Now we can enable the vmclear operation in kdump
4432 * since the loaded_vmcss_on_cpu list on this cpu
4433 * has been initialized.
4434 *
4435 * Though the cpu is not in VMX operation now, there
4436 * is no problem to enable the vmclear operation
4437 * for the loaded_vmcss_on_cpu list is empty!
4438 */
4439 crash_enable_local_vmclear(cpu);
4440
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004442
4443 test_bits = FEATURE_CONTROL_LOCKED;
4444 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4445 if (tboot_enabled())
4446 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4447
4448 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004450 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4451 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004452 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004453 if (enable_ept)
4454 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004455
4456 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004457}
4458
Nadav Har'Eld462b812011-05-24 15:26:10 +03004459static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004460{
4461 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004462 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004463
Nadav Har'Eld462b812011-05-24 15:26:10 +03004464 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4465 loaded_vmcss_on_cpu_link)
4466 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004467}
4468
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004469
4470/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4471 * tricks.
4472 */
4473static void kvm_cpu_vmxoff(void)
4474{
4475 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004476
4477 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004478 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004479}
4480
Radim Krčmář13a34e02014-08-28 15:13:03 +02004481static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004483 vmclear_local_loaded_vmcss();
4484 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004485}
4486
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004487static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004488 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489{
4490 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004491 u32 ctl = ctl_min | ctl_opt;
4492
4493 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4494
4495 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4496 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4497
4498 /* Ensure minimum (required) set of control bits are supported. */
4499 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004500 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004501
4502 *result = ctl;
4503 return 0;
4504}
4505
Avi Kivity110312c2010-12-21 12:54:20 +02004506static __init bool allow_1_setting(u32 msr, u32 ctl)
4507{
4508 u32 vmx_msr_low, vmx_msr_high;
4509
4510 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4511 return vmx_msr_high & ctl;
4512}
4513
Yang, Sheng002c7f72007-07-31 14:23:01 +03004514static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004515{
4516 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004517 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004518 u32 _pin_based_exec_control = 0;
4519 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004520 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004521 u32 _vmexit_control = 0;
4522 u32 _vmentry_control = 0;
4523
Paolo Bonzini13893092018-02-26 13:40:09 +01004524 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304525 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004526#ifdef CONFIG_X86_64
4527 CPU_BASED_CR8_LOAD_EXITING |
4528 CPU_BASED_CR8_STORE_EXITING |
4529#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004530 CPU_BASED_CR3_LOAD_EXITING |
4531 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004532 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004533 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004534 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004535 CPU_BASED_MWAIT_EXITING |
4536 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004537 CPU_BASED_INVLPG_EXITING |
4538 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004539
Sheng Yangf78e0e22007-10-29 09:40:42 +08004540 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004541 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004542 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004543 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4544 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004545 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004546#ifdef CONFIG_X86_64
4547 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4548 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4549 ~CPU_BASED_CR8_STORE_EXITING;
4550#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004551 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004552 min2 = 0;
4553 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004554 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004555 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004556 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004557 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004558 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004559 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004560 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004561 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004562 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004563 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004564 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004565 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004566 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004567 SECONDARY_EXEC_RDSEED_EXITING |
4568 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004569 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004570 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004571 SECONDARY_EXEC_ENABLE_VMFUNC |
4572 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004573 if (adjust_vmx_controls(min2, opt2,
4574 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004575 &_cpu_based_2nd_exec_control) < 0)
4576 return -EIO;
4577 }
4578#ifndef CONFIG_X86_64
4579 if (!(_cpu_based_2nd_exec_control &
4580 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4581 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4582#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004583
4584 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4585 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004586 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004587 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4588 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004589
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004590 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4591 &vmx_capability.ept, &vmx_capability.vpid);
4592
Sheng Yangd56f5462008-04-25 10:13:16 +08004593 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004594 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4595 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004596 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4597 CPU_BASED_CR3_STORE_EXITING |
4598 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004599 } else if (vmx_capability.ept) {
4600 vmx_capability.ept = 0;
4601 pr_warn_once("EPT CAP should not exist if not support "
4602 "1-setting enable EPT VM-execution control\n");
4603 }
4604 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4605 vmx_capability.vpid) {
4606 vmx_capability.vpid = 0;
4607 pr_warn_once("VPID CAP should not exist if not support "
4608 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004609 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004610
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004611 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612#ifdef CONFIG_X86_64
4613 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4614#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004615 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004616 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004617 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4618 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004619 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004620
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004621 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4622 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4623 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004624 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4625 &_pin_based_exec_control) < 0)
4626 return -EIO;
4627
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004628 if (cpu_has_broken_vmx_preemption_timer())
4629 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004630 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004631 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004632 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4633
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004634 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004635 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004636 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4637 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004638 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004640 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004641
4642 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4643 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645
4646#ifdef CONFIG_X86_64
4647 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4648 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650#endif
4651
4652 /* Require Write-Back (WB) memory type for VMCS accesses. */
4653 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004654 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004655
Yang, Sheng002c7f72007-07-31 14:23:01 +03004656 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004657 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004658 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004659
Liran Alon2307af12018-06-29 22:59:04 +03004660 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004661
Yang, Sheng002c7f72007-07-31 14:23:01 +03004662 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4663 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004664 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004665 vmcs_conf->vmexit_ctrl = _vmexit_control;
4666 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004667
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004668 if (static_branch_unlikely(&enable_evmcs))
4669 evmcs_sanitize_exec_ctrls(vmcs_conf);
4670
Avi Kivity110312c2010-12-21 12:54:20 +02004671 cpu_has_load_ia32_efer =
4672 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4673 VM_ENTRY_LOAD_IA32_EFER)
4674 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4675 VM_EXIT_LOAD_IA32_EFER);
4676
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004677 cpu_has_load_perf_global_ctrl =
4678 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4679 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4680 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4681 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4682
4683 /*
4684 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004685 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004686 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4687 *
4688 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4689 *
4690 * AAK155 (model 26)
4691 * AAP115 (model 30)
4692 * AAT100 (model 37)
4693 * BC86,AAY89,BD102 (model 44)
4694 * BA97 (model 46)
4695 *
4696 */
4697 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4698 switch (boot_cpu_data.x86_model) {
4699 case 26:
4700 case 30:
4701 case 37:
4702 case 44:
4703 case 46:
4704 cpu_has_load_perf_global_ctrl = false;
4705 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4706 "does not work properly. Using workaround\n");
4707 break;
4708 default:
4709 break;
4710 }
4711 }
4712
Borislav Petkov782511b2016-04-04 22:25:03 +02004713 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004714 rdmsrl(MSR_IA32_XSS, host_xss);
4715
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004716 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004717}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718
Liran Alon491a6032018-06-23 02:35:12 +03004719static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004720{
4721 int node = cpu_to_node(cpu);
4722 struct page *pages;
4723 struct vmcs *vmcs;
4724
Vlastimil Babka96db8002015-09-08 15:03:50 -07004725 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 if (!pages)
4727 return NULL;
4728 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004729 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004730
4731 /* KVM supports Enlightened VMCS v1 only */
4732 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004733 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004734 else
Liran Alon392b2f22018-06-23 02:35:01 +03004735 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004736
Liran Alon491a6032018-06-23 02:35:12 +03004737 if (shadow)
4738 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739 return vmcs;
4740}
4741
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742static void free_vmcs(struct vmcs *vmcs)
4743{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004744 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004745}
4746
Nadav Har'Eld462b812011-05-24 15:26:10 +03004747/*
4748 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4749 */
4750static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4751{
4752 if (!loaded_vmcs->vmcs)
4753 return;
4754 loaded_vmcs_clear(loaded_vmcs);
4755 free_vmcs(loaded_vmcs->vmcs);
4756 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004757 if (loaded_vmcs->msr_bitmap)
4758 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004759 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004760}
4761
Liran Alon491a6032018-06-23 02:35:12 +03004762static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004763{
Liran Alon491a6032018-06-23 02:35:12 +03004764 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004765}
4766
4767static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4768{
Liran Alon491a6032018-06-23 02:35:12 +03004769 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004770 if (!loaded_vmcs->vmcs)
4771 return -ENOMEM;
4772
4773 loaded_vmcs->shadow_vmcs = NULL;
4774 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004775
4776 if (cpu_has_vmx_msr_bitmap()) {
4777 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4778 if (!loaded_vmcs->msr_bitmap)
4779 goto out_vmcs;
4780 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004781
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004782 if (IS_ENABLED(CONFIG_HYPERV) &&
4783 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004784 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4785 struct hv_enlightened_vmcs *evmcs =
4786 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4787
4788 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4789 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004790 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004791
4792 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4793
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004794 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004795
4796out_vmcs:
4797 free_loaded_vmcs(loaded_vmcs);
4798 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004799}
4800
Sam Ravnborg39959582007-06-01 00:47:13 -07004801static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004802{
4803 int cpu;
4804
Zachary Amsden3230bb42009-09-29 11:38:37 -10004805 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004807 per_cpu(vmxarea, cpu) = NULL;
4808 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809}
4810
Jim Mattsond37f4262017-12-22 12:12:16 -08004811enum vmcs_field_width {
4812 VMCS_FIELD_WIDTH_U16 = 0,
4813 VMCS_FIELD_WIDTH_U64 = 1,
4814 VMCS_FIELD_WIDTH_U32 = 2,
4815 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004816};
4817
Jim Mattsond37f4262017-12-22 12:12:16 -08004818static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004819{
4820 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004821 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004822 return (field >> 13) & 0x3 ;
4823}
4824
4825static inline int vmcs_field_readonly(unsigned long field)
4826{
4827 return (((field >> 10) & 0x3) == 1);
4828}
4829
Bandan Dasfe2b2012014-04-21 15:20:14 -04004830static void init_vmcs_shadow_fields(void)
4831{
4832 int i, j;
4833
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004834 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4835 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004836 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004837 (i + 1 == max_shadow_read_only_fields ||
4838 shadow_read_only_fields[i + 1] != field + 1))
4839 pr_err("Missing field from shadow_read_only_field %x\n",
4840 field + 1);
4841
4842 clear_bit(field, vmx_vmread_bitmap);
4843#ifdef CONFIG_X86_64
4844 if (field & 1)
4845 continue;
4846#endif
4847 if (j < i)
4848 shadow_read_only_fields[j] = field;
4849 j++;
4850 }
4851 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004852
4853 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004854 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004855 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004856 (i + 1 == max_shadow_read_write_fields ||
4857 shadow_read_write_fields[i + 1] != field + 1))
4858 pr_err("Missing field from shadow_read_write_field %x\n",
4859 field + 1);
4860
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004861 /*
4862 * PML and the preemption timer can be emulated, but the
4863 * processor cannot vmwrite to fields that don't exist
4864 * on bare metal.
4865 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004866 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004867 case GUEST_PML_INDEX:
4868 if (!cpu_has_vmx_pml())
4869 continue;
4870 break;
4871 case VMX_PREEMPTION_TIMER_VALUE:
4872 if (!cpu_has_vmx_preemption_timer())
4873 continue;
4874 break;
4875 case GUEST_INTR_STATUS:
4876 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004877 continue;
4878 break;
4879 default:
4880 break;
4881 }
4882
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004883 clear_bit(field, vmx_vmwrite_bitmap);
4884 clear_bit(field, vmx_vmread_bitmap);
4885#ifdef CONFIG_X86_64
4886 if (field & 1)
4887 continue;
4888#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004889 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004890 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004891 j++;
4892 }
4893 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004894}
4895
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896static __init int alloc_kvm_area(void)
4897{
4898 int cpu;
4899
Zachary Amsden3230bb42009-09-29 11:38:37 -10004900 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004901 struct vmcs *vmcs;
4902
Liran Alon491a6032018-06-23 02:35:12 +03004903 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 if (!vmcs) {
4905 free_kvm_area();
4906 return -ENOMEM;
4907 }
4908
Liran Alon2307af12018-06-29 22:59:04 +03004909 /*
4910 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4911 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4912 * revision_id reported by MSR_IA32_VMX_BASIC.
4913 *
4914 * However, even though not explictly documented by
4915 * TLFS, VMXArea passed as VMXON argument should
4916 * still be marked with revision_id reported by
4917 * physical CPU.
4918 */
4919 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004920 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004921
Avi Kivity6aa8b732006-12-10 02:21:36 -08004922 per_cpu(vmxarea, cpu) = vmcs;
4923 }
4924 return 0;
4925}
4926
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004927static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004928 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004930 if (!emulate_invalid_guest_state) {
4931 /*
4932 * CS and SS RPL should be equal during guest entry according
4933 * to VMX spec, but in reality it is not always so. Since vcpu
4934 * is in the middle of the transition from real mode to
4935 * protected mode it is safe to assume that RPL 0 is a good
4936 * default value.
4937 */
4938 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004939 save->selector &= ~SEGMENT_RPL_MASK;
4940 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004941 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004943 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004944}
4945
4946static void enter_pmode(struct kvm_vcpu *vcpu)
4947{
4948 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004949 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950
Gleb Natapovd99e4152012-12-20 16:57:45 +02004951 /*
4952 * Update real mode segment cache. It may be not up-to-date if sement
4953 * register was written while vcpu was in a guest mode.
4954 */
4955 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4956 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4957 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4958 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4959 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4960 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4961
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004962 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963
Avi Kivity2fb92db2011-04-27 19:42:18 +03004964 vmx_segment_cache_clear(vmx);
4965
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004966 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967
4968 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004969 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4970 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971 vmcs_writel(GUEST_RFLAGS, flags);
4972
Rusty Russell66aee912007-07-17 23:34:16 +10004973 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4974 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004975
4976 update_exception_bitmap(vcpu);
4977
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004978 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4979 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4980 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4981 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4982 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4983 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004984}
4985
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004986static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004987{
Mathias Krause772e0312012-08-30 01:30:19 +02004988 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004989 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990
Gleb Natapovd99e4152012-12-20 16:57:45 +02004991 var.dpl = 0x3;
4992 if (seg == VCPU_SREG_CS)
4993 var.type = 0x3;
4994
4995 if (!emulate_invalid_guest_state) {
4996 var.selector = var.base >> 4;
4997 var.base = var.base & 0xffff0;
4998 var.limit = 0xffff;
4999 var.g = 0;
5000 var.db = 0;
5001 var.present = 1;
5002 var.s = 1;
5003 var.l = 0;
5004 var.unusable = 0;
5005 var.type = 0x3;
5006 var.avl = 0;
5007 if (save->base & 0xf)
5008 printk_once(KERN_WARNING "kvm: segment base is not "
5009 "paragraph aligned when entering "
5010 "protected mode (seg=%d)", seg);
5011 }
5012
5013 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005014 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005015 vmcs_write32(sf->limit, var.limit);
5016 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005017}
5018
5019static void enter_rmode(struct kvm_vcpu *vcpu)
5020{
5021 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005022 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005023 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005024
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005025 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5026 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5027 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5028 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5029 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005030 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5031 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005032
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005033 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034
Gleb Natapov776e58e2011-03-13 12:34:27 +02005035 /*
5036 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005037 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005038 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005039 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005040 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5041 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005042
Avi Kivity2fb92db2011-04-27 19:42:18 +03005043 vmx_segment_cache_clear(vmx);
5044
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005045 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5048
5049 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005050 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005052 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053
5054 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005055 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005056 update_exception_bitmap(vcpu);
5057
Gleb Natapovd99e4152012-12-20 16:57:45 +02005058 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5059 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5060 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5061 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5062 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5063 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005064
Eddie Dong8668a3c2007-10-10 14:26:45 +08005065 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005066}
5067
Amit Shah401d10d2009-02-20 22:53:37 +05305068static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5069{
5070 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005071 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5072
5073 if (!msr)
5074 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305075
Avi Kivity44ea2b12009-09-06 15:55:37 +03005076 /*
Sean Christopherson678e3152018-07-23 12:32:43 -07005077 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
5078 * 64-bit mode as a 64-bit kernel may frequently access the
5079 * MSR. This means we need to manually save/restore the MSR
5080 * when switching between guest and host state, but only if
5081 * the guest is in 64-bit mode. Sync our cached value if the
5082 * guest is transitioning to 32-bit mode and the CPU contains
5083 * guest state, i.e. the cache is stale.
Avi Kivity44ea2b12009-09-06 15:55:37 +03005084 */
Sean Christopherson678e3152018-07-23 12:32:43 -07005085#ifdef CONFIG_X86_64
5086 if (!(efer & EFER_LMA))
5087 (void)vmx_read_guest_kernel_gs_base(vmx);
5088#endif
Avi Kivityf6801df2010-01-21 15:31:50 +02005089 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305090 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005091 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305092 msr->data = efer;
5093 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005094 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305095
5096 msr->data = efer & ~EFER_LME;
5097 }
5098 setup_msrs(vmx);
5099}
5100
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005101#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005102
5103static void enter_lmode(struct kvm_vcpu *vcpu)
5104{
5105 u32 guest_tr_ar;
5106
Avi Kivity2fb92db2011-04-27 19:42:18 +03005107 vmx_segment_cache_clear(to_vmx(vcpu));
5108
Avi Kivity6aa8b732006-12-10 02:21:36 -08005109 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005110 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005111 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5112 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005114 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5115 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005116 }
Avi Kivityda38f432010-07-06 11:30:49 +03005117 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005118}
5119
5120static void exit_lmode(struct kvm_vcpu *vcpu)
5121{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005122 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005123 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005124}
5125
5126#endif
5127
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005128static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5129 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005130{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005131 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005132 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5133 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005134 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005135 } else {
5136 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005137 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005138}
5139
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005140static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005141{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005142 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005143}
5144
Junaid Shahidfaff8752018-06-29 13:10:05 -07005145static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5146{
5147 int vpid = to_vmx(vcpu)->vpid;
5148
5149 if (!vpid_sync_vcpu_addr(vpid, addr))
5150 vpid_sync_context(vpid);
5151
5152 /*
5153 * If VPIDs are not supported or enabled, then the above is a no-op.
5154 * But we don't really need a TLB flush in that case anyway, because
5155 * each VM entry/exit includes an implicit flush when VPID is 0.
5156 */
5157}
5158
Avi Kivitye8467fd2009-12-29 18:43:06 +02005159static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5160{
5161 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5162
5163 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5164 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5165}
5166
Avi Kivityaff48ba2010-12-05 18:56:11 +02005167static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5168{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005169 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005170 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5171 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5172}
5173
Anthony Liguori25c4c272007-04-27 09:29:21 +03005174static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005175{
Avi Kivityfc78f512009-12-07 12:16:48 +02005176 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5177
5178 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5179 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005180}
5181
Sheng Yang14394422008-04-28 12:24:45 +08005182static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5183{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005184 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5185
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005186 if (!test_bit(VCPU_EXREG_PDPTR,
5187 (unsigned long *)&vcpu->arch.regs_dirty))
5188 return;
5189
Sheng Yang14394422008-04-28 12:24:45 +08005190 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005191 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5192 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5193 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5194 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005195 }
5196}
5197
Avi Kivity8f5d5492009-05-31 18:41:29 +03005198static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5199{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005200 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5201
Avi Kivity8f5d5492009-05-31 18:41:29 +03005202 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005203 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5204 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5205 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5206 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005207 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005208
5209 __set_bit(VCPU_EXREG_PDPTR,
5210 (unsigned long *)&vcpu->arch.regs_avail);
5211 __set_bit(VCPU_EXREG_PDPTR,
5212 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005213}
5214
David Matlack38991522016-11-29 18:14:08 -08005215static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5216{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005217 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5218 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005219 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5220
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005221 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005222 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5223 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5224 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5225
5226 return fixed_bits_valid(val, fixed0, fixed1);
5227}
5228
5229static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5230{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005231 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5232 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005233
5234 return fixed_bits_valid(val, fixed0, fixed1);
5235}
5236
5237static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5238{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005239 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5240 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005241
5242 return fixed_bits_valid(val, fixed0, fixed1);
5243}
5244
5245/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5246#define nested_guest_cr4_valid nested_cr4_valid
5247#define nested_host_cr4_valid nested_cr4_valid
5248
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005249static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005250
5251static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5252 unsigned long cr0,
5253 struct kvm_vcpu *vcpu)
5254{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005255 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5256 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005257 if (!(cr0 & X86_CR0_PG)) {
5258 /* From paging/starting to nonpaging */
5259 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005260 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005261 (CPU_BASED_CR3_LOAD_EXITING |
5262 CPU_BASED_CR3_STORE_EXITING));
5263 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005264 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005265 } else if (!is_paging(vcpu)) {
5266 /* From nonpaging to paging */
5267 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005268 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005269 ~(CPU_BASED_CR3_LOAD_EXITING |
5270 CPU_BASED_CR3_STORE_EXITING));
5271 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005272 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005273 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005274
5275 if (!(cr0 & X86_CR0_WP))
5276 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005277}
5278
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5280{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005282 unsigned long hw_cr0;
5283
Gleb Natapov50378782013-02-04 16:00:28 +02005284 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005285 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005286 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005287 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005288 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005289
Gleb Natapov218e7632013-01-21 15:36:45 +02005290 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5291 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005292
Gleb Natapov218e7632013-01-21 15:36:45 +02005293 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5294 enter_rmode(vcpu);
5295 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005296
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005297#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005298 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005299 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005300 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005301 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005302 exit_lmode(vcpu);
5303 }
5304#endif
5305
Sean Christophersonb4d18512018-03-05 12:04:40 -08005306 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005307 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5308
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005310 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005311 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005312
5313 /* depends on vcpu->arch.cr0 to be set to a new value */
5314 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005315}
5316
Yu Zhang855feb62017-08-24 20:27:55 +08005317static int get_ept_level(struct kvm_vcpu *vcpu)
5318{
5319 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5320 return 5;
5321 return 4;
5322}
5323
Peter Feiner995f00a2017-06-30 17:26:32 -07005324static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005325{
Yu Zhang855feb62017-08-24 20:27:55 +08005326 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005327
Yu Zhang855feb62017-08-24 20:27:55 +08005328 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005329
Peter Feiner995f00a2017-06-30 17:26:32 -07005330 if (enable_ept_ad_bits &&
5331 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005332 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005333 eptp |= (root_hpa & PAGE_MASK);
5334
5335 return eptp;
5336}
5337
Avi Kivity6aa8b732006-12-10 02:21:36 -08005338static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5339{
Tianyu Lan877ad952018-07-19 08:40:23 +00005340 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005341 unsigned long guest_cr3;
5342 u64 eptp;
5343
5344 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005345 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005346 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005347 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005348
5349 if (kvm_x86_ops->tlb_remote_flush) {
5350 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5351 to_vmx(vcpu)->ept_pointer = eptp;
5352 to_kvm_vmx(kvm)->ept_pointers_match
5353 = EPT_POINTERS_CHECK;
5354 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5355 }
5356
Sean Christophersone90008d2018-03-05 12:04:37 -08005357 if (enable_unrestricted_guest || is_paging(vcpu) ||
5358 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005359 guest_cr3 = kvm_read_cr3(vcpu);
5360 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005361 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005362 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005363 }
5364
Sheng Yang14394422008-04-28 12:24:45 +08005365 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005366}
5367
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005368static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005369{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005370 /*
5371 * Pass through host's Machine Check Enable value to hw_cr4, which
5372 * is in force while we are in guest mode. Do not let guests control
5373 * this bit, even if host CR4.MCE == 0.
5374 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005375 unsigned long hw_cr4;
5376
5377 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5378 if (enable_unrestricted_guest)
5379 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5380 else if (to_vmx(vcpu)->rmode.vm86_active)
5381 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5382 else
5383 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005384
Sean Christopherson64f7a112018-04-30 10:01:06 -07005385 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5386 if (cr4 & X86_CR4_UMIP) {
5387 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005388 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005389 hw_cr4 &= ~X86_CR4_UMIP;
5390 } else if (!is_guest_mode(vcpu) ||
5391 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5392 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5393 SECONDARY_EXEC_DESC);
5394 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005395
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005396 if (cr4 & X86_CR4_VMXE) {
5397 /*
5398 * To use VMXON (and later other VMX instructions), a guest
5399 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5400 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005401 * is here. We operate under the default treatment of SMM,
5402 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005403 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005404 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005405 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005406 }
David Matlack38991522016-11-29 18:14:08 -08005407
5408 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005409 return 1;
5410
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005411 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005412
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005413 if (!enable_unrestricted_guest) {
5414 if (enable_ept) {
5415 if (!is_paging(vcpu)) {
5416 hw_cr4 &= ~X86_CR4_PAE;
5417 hw_cr4 |= X86_CR4_PSE;
5418 } else if (!(cr4 & X86_CR4_PAE)) {
5419 hw_cr4 &= ~X86_CR4_PAE;
5420 }
5421 }
5422
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005423 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005424 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5425 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5426 * to be manually disabled when guest switches to non-paging
5427 * mode.
5428 *
5429 * If !enable_unrestricted_guest, the CPU is always running
5430 * with CR0.PG=1 and CR4 needs to be modified.
5431 * If enable_unrestricted_guest, the CPU automatically
5432 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005433 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005434 if (!is_paging(vcpu))
5435 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5436 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005437
Sheng Yang14394422008-04-28 12:24:45 +08005438 vmcs_writel(CR4_READ_SHADOW, cr4);
5439 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005440 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005441}
5442
Avi Kivity6aa8b732006-12-10 02:21:36 -08005443static void vmx_get_segment(struct kvm_vcpu *vcpu,
5444 struct kvm_segment *var, int seg)
5445{
Avi Kivitya9179492011-01-03 14:28:52 +02005446 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005447 u32 ar;
5448
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005449 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005450 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005451 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005452 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005453 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005454 var->base = vmx_read_guest_seg_base(vmx, seg);
5455 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5456 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005457 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005458 var->base = vmx_read_guest_seg_base(vmx, seg);
5459 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5460 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5461 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005462 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005463 var->type = ar & 15;
5464 var->s = (ar >> 4) & 1;
5465 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005466 /*
5467 * Some userspaces do not preserve unusable property. Since usable
5468 * segment has to be present according to VMX spec we can use present
5469 * property to amend userspace bug by making unusable segment always
5470 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5471 * segment as unusable.
5472 */
5473 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005474 var->avl = (ar >> 12) & 1;
5475 var->l = (ar >> 13) & 1;
5476 var->db = (ar >> 14) & 1;
5477 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005478}
5479
Avi Kivitya9179492011-01-03 14:28:52 +02005480static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5481{
Avi Kivitya9179492011-01-03 14:28:52 +02005482 struct kvm_segment s;
5483
5484 if (to_vmx(vcpu)->rmode.vm86_active) {
5485 vmx_get_segment(vcpu, &s, seg);
5486 return s.base;
5487 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005488 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005489}
5490
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005491static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005492{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005493 struct vcpu_vmx *vmx = to_vmx(vcpu);
5494
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005495 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005496 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005497 else {
5498 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005499 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005500 }
Avi Kivity69c73022011-03-07 15:26:44 +02005501}
5502
Avi Kivity653e3102007-05-07 10:55:37 +03005503static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005504{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005505 u32 ar;
5506
Avi Kivityf0495f92012-06-07 17:06:10 +03005507 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005508 ar = 1 << 16;
5509 else {
5510 ar = var->type & 15;
5511 ar |= (var->s & 1) << 4;
5512 ar |= (var->dpl & 3) << 5;
5513 ar |= (var->present & 1) << 7;
5514 ar |= (var->avl & 1) << 12;
5515 ar |= (var->l & 1) << 13;
5516 ar |= (var->db & 1) << 14;
5517 ar |= (var->g & 1) << 15;
5518 }
Avi Kivity653e3102007-05-07 10:55:37 +03005519
5520 return ar;
5521}
5522
5523static void vmx_set_segment(struct kvm_vcpu *vcpu,
5524 struct kvm_segment *var, int seg)
5525{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005526 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005527 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005528
Avi Kivity2fb92db2011-04-27 19:42:18 +03005529 vmx_segment_cache_clear(vmx);
5530
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005531 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5532 vmx->rmode.segs[seg] = *var;
5533 if (seg == VCPU_SREG_TR)
5534 vmcs_write16(sf->selector, var->selector);
5535 else if (var->s)
5536 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005537 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005538 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005539
Avi Kivity653e3102007-05-07 10:55:37 +03005540 vmcs_writel(sf->base, var->base);
5541 vmcs_write32(sf->limit, var->limit);
5542 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005543
5544 /*
5545 * Fix the "Accessed" bit in AR field of segment registers for older
5546 * qemu binaries.
5547 * IA32 arch specifies that at the time of processor reset the
5548 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005549 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005550 * state vmexit when "unrestricted guest" mode is turned on.
5551 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5552 * tree. Newer qemu binaries with that qemu fix would not need this
5553 * kvm hack.
5554 */
5555 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005556 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005557
Gleb Natapovf924d662012-12-12 19:10:55 +02005558 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005559
5560out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005561 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562}
5563
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5565{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005566 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567
5568 *db = (ar >> 14) & 1;
5569 *l = (ar >> 13) & 1;
5570}
5571
Gleb Natapov89a27f42010-02-16 10:51:48 +02005572static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005574 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5575 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
5577
Gleb Natapov89a27f42010-02-16 10:51:48 +02005578static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005580 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5581 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005582}
5583
Gleb Natapov89a27f42010-02-16 10:51:48 +02005584static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005585{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005586 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5587 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005588}
5589
Gleb Natapov89a27f42010-02-16 10:51:48 +02005590static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005591{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005592 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5593 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005594}
5595
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005596static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5597{
5598 struct kvm_segment var;
5599 u32 ar;
5600
5601 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005602 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005603 if (seg == VCPU_SREG_CS)
5604 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005605 ar = vmx_segment_access_rights(&var);
5606
5607 if (var.base != (var.selector << 4))
5608 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005609 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005610 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005611 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005612 return false;
5613
5614 return true;
5615}
5616
5617static bool code_segment_valid(struct kvm_vcpu *vcpu)
5618{
5619 struct kvm_segment cs;
5620 unsigned int cs_rpl;
5621
5622 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005623 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005624
Avi Kivity1872a3f2009-01-04 23:26:52 +02005625 if (cs.unusable)
5626 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005627 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005628 return false;
5629 if (!cs.s)
5630 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005631 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005632 if (cs.dpl > cs_rpl)
5633 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005634 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005635 if (cs.dpl != cs_rpl)
5636 return false;
5637 }
5638 if (!cs.present)
5639 return false;
5640
5641 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5642 return true;
5643}
5644
5645static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5646{
5647 struct kvm_segment ss;
5648 unsigned int ss_rpl;
5649
5650 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005651 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005652
Avi Kivity1872a3f2009-01-04 23:26:52 +02005653 if (ss.unusable)
5654 return true;
5655 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005656 return false;
5657 if (!ss.s)
5658 return false;
5659 if (ss.dpl != ss_rpl) /* DPL != RPL */
5660 return false;
5661 if (!ss.present)
5662 return false;
5663
5664 return true;
5665}
5666
5667static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5668{
5669 struct kvm_segment var;
5670 unsigned int rpl;
5671
5672 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005673 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005674
Avi Kivity1872a3f2009-01-04 23:26:52 +02005675 if (var.unusable)
5676 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005677 if (!var.s)
5678 return false;
5679 if (!var.present)
5680 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005681 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005682 if (var.dpl < rpl) /* DPL < RPL */
5683 return false;
5684 }
5685
5686 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5687 * rights flags
5688 */
5689 return true;
5690}
5691
5692static bool tr_valid(struct kvm_vcpu *vcpu)
5693{
5694 struct kvm_segment tr;
5695
5696 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5697
Avi Kivity1872a3f2009-01-04 23:26:52 +02005698 if (tr.unusable)
5699 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005700 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005701 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005702 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005703 return false;
5704 if (!tr.present)
5705 return false;
5706
5707 return true;
5708}
5709
5710static bool ldtr_valid(struct kvm_vcpu *vcpu)
5711{
5712 struct kvm_segment ldtr;
5713
5714 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5715
Avi Kivity1872a3f2009-01-04 23:26:52 +02005716 if (ldtr.unusable)
5717 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005718 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005719 return false;
5720 if (ldtr.type != 2)
5721 return false;
5722 if (!ldtr.present)
5723 return false;
5724
5725 return true;
5726}
5727
5728static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5729{
5730 struct kvm_segment cs, ss;
5731
5732 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5733 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5734
Nadav Amitb32a9912015-03-29 16:33:04 +03005735 return ((cs.selector & SEGMENT_RPL_MASK) ==
5736 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005737}
5738
5739/*
5740 * Check if guest state is valid. Returns true if valid, false if
5741 * not.
5742 * We assume that registers are always usable
5743 */
5744static bool guest_state_valid(struct kvm_vcpu *vcpu)
5745{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005746 if (enable_unrestricted_guest)
5747 return true;
5748
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005749 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005750 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005751 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5752 return false;
5753 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5754 return false;
5755 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5756 return false;
5757 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5758 return false;
5759 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5760 return false;
5761 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5762 return false;
5763 } else {
5764 /* protected mode guest state checks */
5765 if (!cs_ss_rpl_check(vcpu))
5766 return false;
5767 if (!code_segment_valid(vcpu))
5768 return false;
5769 if (!stack_segment_valid(vcpu))
5770 return false;
5771 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5772 return false;
5773 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5774 return false;
5775 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5776 return false;
5777 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5778 return false;
5779 if (!tr_valid(vcpu))
5780 return false;
5781 if (!ldtr_valid(vcpu))
5782 return false;
5783 }
5784 /* TODO:
5785 * - Add checks on RIP
5786 * - Add checks on RFLAGS
5787 */
5788
5789 return true;
5790}
5791
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005792static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5793{
5794 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5795}
5796
Mike Dayd77c26f2007-10-08 09:02:08 -04005797static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005798{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005799 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005800 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005801 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005802
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005803 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005804 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005805 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5806 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005807 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005808 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005809 r = kvm_write_guest_page(kvm, fn++, &data,
5810 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005811 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005812 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005813 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5814 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005815 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005816 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5817 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005818 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005819 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005820 r = kvm_write_guest_page(kvm, fn, &data,
5821 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5822 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005823out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005824 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005825 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005826}
5827
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005828static int init_rmode_identity_map(struct kvm *kvm)
5829{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005830 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005831 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005832 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005833 u32 tmp;
5834
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005835 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005836 mutex_lock(&kvm->slots_lock);
5837
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005838 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005839 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005840
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005841 if (!kvm_vmx->ept_identity_map_addr)
5842 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5843 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005844
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005845 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005846 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005847 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005848 goto out2;
5849
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005850 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005851 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5852 if (r < 0)
5853 goto out;
5854 /* Set up identity-mapping pagetable for EPT in real mode */
5855 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5856 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5857 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5858 r = kvm_write_guest_page(kvm, identity_map_pfn,
5859 &tmp, i * sizeof(tmp), sizeof(tmp));
5860 if (r < 0)
5861 goto out;
5862 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005863 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005864
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005865out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005866 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005867
5868out2:
5869 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005870 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005871}
5872
Avi Kivity6aa8b732006-12-10 02:21:36 -08005873static void seg_setup(int seg)
5874{
Mathias Krause772e0312012-08-30 01:30:19 +02005875 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005876 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005877
5878 vmcs_write16(sf->selector, 0);
5879 vmcs_writel(sf->base, 0);
5880 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005881 ar = 0x93;
5882 if (seg == VCPU_SREG_CS)
5883 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005884
5885 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005886}
5887
Sheng Yangf78e0e22007-10-29 09:40:42 +08005888static int alloc_apic_access_page(struct kvm *kvm)
5889{
Xiao Guangrong44841412012-09-07 14:14:20 +08005890 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005891 int r = 0;
5892
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005893 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005894 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005895 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005896 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5897 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005898 if (r)
5899 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005900
Tang Chen73a6d942014-09-11 13:38:00 +08005901 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005902 if (is_error_page(page)) {
5903 r = -EFAULT;
5904 goto out;
5905 }
5906
Tang Chenc24ae0d2014-09-24 15:57:58 +08005907 /*
5908 * Do not pin the page in memory, so that memory hot-unplug
5909 * is able to migrate it.
5910 */
5911 put_page(page);
5912 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005913out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005914 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005915 return r;
5916}
5917
Wanpeng Li991e7a02015-09-16 17:30:05 +08005918static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005919{
5920 int vpid;
5921
Avi Kivity919818a2009-03-23 18:01:29 +02005922 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005923 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005924 spin_lock(&vmx_vpid_lock);
5925 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005926 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005927 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005928 else
5929 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005930 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005931 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005932}
5933
Wanpeng Li991e7a02015-09-16 17:30:05 +08005934static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005935{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005936 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005937 return;
5938 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005939 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005940 spin_unlock(&vmx_vpid_lock);
5941}
5942
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005943static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5944 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005945{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005946 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005947
5948 if (!cpu_has_vmx_msr_bitmap())
5949 return;
5950
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005951 if (static_branch_unlikely(&enable_evmcs))
5952 evmcs_touch_msr_bitmap();
5953
Sheng Yang25c5f222008-03-28 13:18:56 +08005954 /*
5955 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5956 * have the write-low and read-high bitmap offsets the wrong way round.
5957 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5958 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005959 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005960 if (type & MSR_TYPE_R)
5961 /* read-low */
5962 __clear_bit(msr, msr_bitmap + 0x000 / f);
5963
5964 if (type & MSR_TYPE_W)
5965 /* write-low */
5966 __clear_bit(msr, msr_bitmap + 0x800 / f);
5967
Sheng Yang25c5f222008-03-28 13:18:56 +08005968 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5969 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005970 if (type & MSR_TYPE_R)
5971 /* read-high */
5972 __clear_bit(msr, msr_bitmap + 0x400 / f);
5973
5974 if (type & MSR_TYPE_W)
5975 /* write-high */
5976 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5977
5978 }
5979}
5980
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005981static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5982 u32 msr, int type)
5983{
5984 int f = sizeof(unsigned long);
5985
5986 if (!cpu_has_vmx_msr_bitmap())
5987 return;
5988
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005989 if (static_branch_unlikely(&enable_evmcs))
5990 evmcs_touch_msr_bitmap();
5991
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005992 /*
5993 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5994 * have the write-low and read-high bitmap offsets the wrong way round.
5995 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5996 */
5997 if (msr <= 0x1fff) {
5998 if (type & MSR_TYPE_R)
5999 /* read-low */
6000 __set_bit(msr, msr_bitmap + 0x000 / f);
6001
6002 if (type & MSR_TYPE_W)
6003 /* write-low */
6004 __set_bit(msr, msr_bitmap + 0x800 / f);
6005
6006 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6007 msr &= 0x1fff;
6008 if (type & MSR_TYPE_R)
6009 /* read-high */
6010 __set_bit(msr, msr_bitmap + 0x400 / f);
6011
6012 if (type & MSR_TYPE_W)
6013 /* write-high */
6014 __set_bit(msr, msr_bitmap + 0xc00 / f);
6015
6016 }
6017}
6018
6019static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6020 u32 msr, int type, bool value)
6021{
6022 if (value)
6023 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6024 else
6025 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6026}
6027
Wincy Vanf2b93282015-02-03 23:56:03 +08006028/*
6029 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6030 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6031 */
6032static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6033 unsigned long *msr_bitmap_nested,
6034 u32 msr, int type)
6035{
6036 int f = sizeof(unsigned long);
6037
Wincy Vanf2b93282015-02-03 23:56:03 +08006038 /*
6039 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6040 * have the write-low and read-high bitmap offsets the wrong way round.
6041 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6042 */
6043 if (msr <= 0x1fff) {
6044 if (type & MSR_TYPE_R &&
6045 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6046 /* read-low */
6047 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6048
6049 if (type & MSR_TYPE_W &&
6050 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6051 /* write-low */
6052 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6053
6054 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6055 msr &= 0x1fff;
6056 if (type & MSR_TYPE_R &&
6057 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6058 /* read-high */
6059 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6060
6061 if (type & MSR_TYPE_W &&
6062 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6063 /* write-high */
6064 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6065
6066 }
6067}
6068
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006069static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006070{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006071 u8 mode = 0;
6072
6073 if (cpu_has_secondary_exec_ctrls() &&
6074 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6075 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6076 mode |= MSR_BITMAP_MODE_X2APIC;
6077 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6078 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6079 }
6080
6081 if (is_long_mode(vcpu))
6082 mode |= MSR_BITMAP_MODE_LM;
6083
6084 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006085}
6086
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006087#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6088
6089static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6090 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006091{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006092 int msr;
6093
6094 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6095 unsigned word = msr / BITS_PER_LONG;
6096 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6097 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006098 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006099
6100 if (mode & MSR_BITMAP_MODE_X2APIC) {
6101 /*
6102 * TPR reads and writes can be virtualized even if virtual interrupt
6103 * delivery is not in use.
6104 */
6105 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6106 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6107 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6108 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6109 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6110 }
6111 }
6112}
6113
6114static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6115{
6116 struct vcpu_vmx *vmx = to_vmx(vcpu);
6117 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6118 u8 mode = vmx_msr_bitmap_mode(vcpu);
6119 u8 changed = mode ^ vmx->msr_bitmap_mode;
6120
6121 if (!changed)
6122 return;
6123
6124 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
6125 !(mode & MSR_BITMAP_MODE_LM));
6126
6127 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6128 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6129
6130 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006131}
6132
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006133static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006134{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006135 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006136}
6137
David Matlackc9f04402017-08-01 14:00:40 -07006138static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6139{
6140 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6141 gfn_t gfn;
6142
6143 /*
6144 * Don't need to mark the APIC access page dirty; it is never
6145 * written to by the CPU during APIC virtualization.
6146 */
6147
6148 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6149 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6150 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6151 }
6152
6153 if (nested_cpu_has_posted_intr(vmcs12)) {
6154 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6155 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6156 }
6157}
6158
6159
David Hildenbrand6342c502017-01-25 11:58:58 +01006160static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006161{
6162 struct vcpu_vmx *vmx = to_vmx(vcpu);
6163 int max_irr;
6164 void *vapic_page;
6165 u16 status;
6166
David Matlackc9f04402017-08-01 14:00:40 -07006167 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6168 return;
Wincy Van705699a2015-02-03 23:58:17 +08006169
David Matlackc9f04402017-08-01 14:00:40 -07006170 vmx->nested.pi_pending = false;
6171 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6172 return;
Wincy Van705699a2015-02-03 23:58:17 +08006173
David Matlackc9f04402017-08-01 14:00:40 -07006174 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6175 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006176 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006177 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6178 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006179 kunmap(vmx->nested.virtual_apic_page);
6180
6181 status = vmcs_read16(GUEST_INTR_STATUS);
6182 if ((u8)max_irr > ((u8)status & 0xff)) {
6183 status &= ~0xff;
6184 status |= (u8)max_irr;
6185 vmcs_write16(GUEST_INTR_STATUS, status);
6186 }
6187 }
David Matlackc9f04402017-08-01 14:00:40 -07006188
6189 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006190}
6191
Liran Alone6c67d82018-09-04 10:56:52 +03006192static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6193{
6194 struct vcpu_vmx *vmx = to_vmx(vcpu);
6195 void *vapic_page;
6196 u32 vppr;
6197 int rvi;
6198
6199 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6200 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6201 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6202 return false;
6203
6204 rvi = vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6205
6206 vapic_page = kmap(vmx->nested.virtual_apic_page);
6207 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6208 kunmap(vmx->nested.virtual_apic_page);
6209
6210 return ((rvi & 0xf0) > (vppr & 0xf0));
6211}
6212
Wincy Van06a55242017-04-28 13:13:59 +08006213static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6214 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006215{
6216#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006217 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6218
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006219 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006220 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006221 * The vector of interrupt to be delivered to vcpu had
6222 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006223 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006224 * Following cases will be reached in this block, and
6225 * we always send a notification event in all cases as
6226 * explained below.
6227 *
6228 * Case 1: vcpu keeps in non-root mode. Sending a
6229 * notification event posts the interrupt to vcpu.
6230 *
6231 * Case 2: vcpu exits to root mode and is still
6232 * runnable. PIR will be synced to vIRR before the
6233 * next vcpu entry. Sending a notification event in
6234 * this case has no effect, as vcpu is not in root
6235 * mode.
6236 *
6237 * Case 3: vcpu exits to root mode and is blocked.
6238 * vcpu_block() has already synced PIR to vIRR and
6239 * never blocks vcpu if vIRR is not cleared. Therefore,
6240 * a blocked vcpu here does not wait for any requested
6241 * interrupts in PIR, and sending a notification event
6242 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006243 */
Feng Wu28b835d2015-09-18 22:29:54 +08006244
Wincy Van06a55242017-04-28 13:13:59 +08006245 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006246 return true;
6247 }
6248#endif
6249 return false;
6250}
6251
Wincy Van705699a2015-02-03 23:58:17 +08006252static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6253 int vector)
6254{
6255 struct vcpu_vmx *vmx = to_vmx(vcpu);
6256
6257 if (is_guest_mode(vcpu) &&
6258 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006259 /*
6260 * If a posted intr is not recognized by hardware,
6261 * we will accomplish it in the next vmentry.
6262 */
6263 vmx->nested.pi_pending = true;
6264 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006265 /* the PIR and ON have been set by L1. */
6266 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6267 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006268 return 0;
6269 }
6270 return -1;
6271}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006272/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006273 * Send interrupt to vcpu via posted interrupt way.
6274 * 1. If target vcpu is running(non-root mode), send posted interrupt
6275 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6276 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6277 * interrupt from PIR in next vmentry.
6278 */
6279static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6280{
6281 struct vcpu_vmx *vmx = to_vmx(vcpu);
6282 int r;
6283
Wincy Van705699a2015-02-03 23:58:17 +08006284 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6285 if (!r)
6286 return;
6287
Yang Zhanga20ed542013-04-11 19:25:15 +08006288 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6289 return;
6290
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006291 /* If a previous notification has sent the IPI, nothing to do. */
6292 if (pi_test_and_set_on(&vmx->pi_desc))
6293 return;
6294
Wincy Van06a55242017-04-28 13:13:59 +08006295 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006296 kvm_vcpu_kick(vcpu);
6297}
6298
Avi Kivity6aa8b732006-12-10 02:21:36 -08006299/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006300 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6301 * will not change in the lifetime of the guest.
6302 * Note that host-state that does change is set elsewhere. E.g., host-state
6303 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6304 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006305static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006306{
6307 u32 low32, high32;
6308 unsigned long tmpl;
6309 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006310 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006311
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006312 cr0 = read_cr0();
6313 WARN_ON(cr0 & X86_CR0_TS);
6314 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006315
6316 /*
6317 * Save the most likely value for this task's CR3 in the VMCS.
6318 * We can't use __get_current_cr3_fast() because we're not atomic.
6319 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006320 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006321 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006322 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006323
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006324 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006325 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006326 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006327 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006328
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006329 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006330#ifdef CONFIG_X86_64
6331 /*
6332 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006333 * vmx_prepare_switch_to_host(), in case userspace uses
6334 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006335 */
6336 vmcs_write16(HOST_DS_SELECTOR, 0);
6337 vmcs_write16(HOST_ES_SELECTOR, 0);
6338#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006339 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6340 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006341#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006342 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6343 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6344
Juergen Gross87930012017-09-04 12:25:27 +02006345 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006346 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006347 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006348
Avi Kivity83287ea422012-09-16 15:10:57 +03006349 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006350
6351 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6352 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6353 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6354 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6355
6356 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6357 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6358 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6359 }
6360}
6361
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006362static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6363{
6364 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6365 if (enable_ept)
6366 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006367 if (is_guest_mode(&vmx->vcpu))
6368 vmx->vcpu.arch.cr4_guest_owned_bits &=
6369 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006370 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6371}
6372
Yang Zhang01e439b2013-04-11 19:25:12 +08006373static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6374{
6375 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6376
Andrey Smetanind62caab2015-11-10 15:36:33 +03006377 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006378 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006379
6380 if (!enable_vnmi)
6381 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6382
Yunhong Jiang64672c92016-06-13 14:19:59 -07006383 /* Enable the preemption timer dynamically */
6384 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006385 return pin_based_exec_ctrl;
6386}
6387
Andrey Smetanind62caab2015-11-10 15:36:33 +03006388static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6389{
6390 struct vcpu_vmx *vmx = to_vmx(vcpu);
6391
6392 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006393 if (cpu_has_secondary_exec_ctrls()) {
6394 if (kvm_vcpu_apicv_active(vcpu))
6395 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6396 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6397 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6398 else
6399 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6400 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6401 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6402 }
6403
6404 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006405 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006406}
6407
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006408static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6409{
6410 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006411
6412 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6413 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6414
Paolo Bonzini35754c92015-07-29 12:05:37 +02006415 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006416 exec_control &= ~CPU_BASED_TPR_SHADOW;
6417#ifdef CONFIG_X86_64
6418 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6419 CPU_BASED_CR8_LOAD_EXITING;
6420#endif
6421 }
6422 if (!enable_ept)
6423 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6424 CPU_BASED_CR3_LOAD_EXITING |
6425 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006426 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6427 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6428 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006429 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6430 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431 return exec_control;
6432}
6433
Jim Mattson45ec3682017-08-23 16:32:04 -07006434static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006435{
Jim Mattson45ec3682017-08-23 16:32:04 -07006436 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006437 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006438}
6439
Jim Mattson75f4fc82017-08-23 16:32:03 -07006440static bool vmx_rdseed_supported(void)
6441{
6442 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006443 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006444}
6445
Paolo Bonzini80154d72017-08-24 13:55:35 +02006446static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006447{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006448 struct kvm_vcpu *vcpu = &vmx->vcpu;
6449
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006450 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006451
Paolo Bonzini80154d72017-08-24 13:55:35 +02006452 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006453 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6454 if (vmx->vpid == 0)
6455 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6456 if (!enable_ept) {
6457 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6458 enable_unrestricted_guest = 0;
6459 }
6460 if (!enable_unrestricted_guest)
6461 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006462 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006463 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006464 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006465 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6466 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006467 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006468
6469 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6470 * in vmx_set_cr4. */
6471 exec_control &= ~SECONDARY_EXEC_DESC;
6472
Abel Gordonabc4fc52013-04-18 14:35:25 +03006473 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6474 (handle_vmptrld).
6475 We can NOT enable shadow_vmcs here because we don't have yet
6476 a current VMCS12
6477 */
6478 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006479
6480 if (!enable_pml)
6481 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006482
Paolo Bonzini3db13482017-08-24 14:48:03 +02006483 if (vmx_xsaves_supported()) {
6484 /* Exposing XSAVES only when XSAVE is exposed */
6485 bool xsaves_enabled =
6486 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6487 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6488
6489 if (!xsaves_enabled)
6490 exec_control &= ~SECONDARY_EXEC_XSAVES;
6491
6492 if (nested) {
6493 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006494 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006495 SECONDARY_EXEC_XSAVES;
6496 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006497 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006498 ~SECONDARY_EXEC_XSAVES;
6499 }
6500 }
6501
Paolo Bonzini80154d72017-08-24 13:55:35 +02006502 if (vmx_rdtscp_supported()) {
6503 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6504 if (!rdtscp_enabled)
6505 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6506
6507 if (nested) {
6508 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006509 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006510 SECONDARY_EXEC_RDTSCP;
6511 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006512 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006513 ~SECONDARY_EXEC_RDTSCP;
6514 }
6515 }
6516
6517 if (vmx_invpcid_supported()) {
6518 /* Exposing INVPCID only when PCID is exposed */
6519 bool invpcid_enabled =
6520 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6521 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6522
6523 if (!invpcid_enabled) {
6524 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6525 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6526 }
6527
6528 if (nested) {
6529 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006530 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006531 SECONDARY_EXEC_ENABLE_INVPCID;
6532 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006533 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006534 ~SECONDARY_EXEC_ENABLE_INVPCID;
6535 }
6536 }
6537
Jim Mattson45ec3682017-08-23 16:32:04 -07006538 if (vmx_rdrand_supported()) {
6539 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6540 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006541 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006542
6543 if (nested) {
6544 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006545 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006546 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006547 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006548 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006549 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006550 }
6551 }
6552
Jim Mattson75f4fc82017-08-23 16:32:03 -07006553 if (vmx_rdseed_supported()) {
6554 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6555 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006556 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006557
6558 if (nested) {
6559 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006560 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006561 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006562 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006563 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006564 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006565 }
6566 }
6567
Paolo Bonzini80154d72017-08-24 13:55:35 +02006568 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006569}
6570
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006571static void ept_set_mmio_spte_mask(void)
6572{
6573 /*
6574 * EPT Misconfigurations can be generated if the value of bits 2:0
6575 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006576 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006577 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6578 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006579}
6580
Wanpeng Lif53cd632014-12-02 19:14:58 +08006581#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006582/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006583 * Sets up the vmcs for emulated real mode.
6584 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006585static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006586{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006587 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006588
Abel Gordon4607c2d2013-04-18 14:35:55 +03006589 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006590 /*
6591 * At vCPU creation, "VMWRITE to any supported field
6592 * in the VMCS" is supported, so use the more
6593 * permissive vmx_vmread_bitmap to specify both read
6594 * and write permissions for the shadow VMCS.
6595 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006596 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006597 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006598 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006599 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006600 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006601
Avi Kivity6aa8b732006-12-10 02:21:36 -08006602 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6603
Avi Kivity6aa8b732006-12-10 02:21:36 -08006604 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006605 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006606 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006607
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006608 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609
Dan Williamsdfa169b2016-06-02 11:17:24 -07006610 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006611 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006612 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006613 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006614 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006615
Andrey Smetanind62caab2015-11-10 15:36:33 +03006616 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006617 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6618 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6619 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6620 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6621
6622 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006623
Li RongQing0bcf2612015-12-03 13:29:34 +08006624 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006625 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006626 }
6627
Wanpeng Lib31c1142018-03-12 04:53:04 -07006628 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006629 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006630 vmx->ple_window = ple_window;
6631 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006632 }
6633
Xiao Guangrongc3707952011-07-12 03:28:04 +08006634 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6635 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006636 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6637
Avi Kivity9581d442010-10-19 16:46:55 +02006638 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6639 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006640 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6642 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643
Bandan Das2a499e42017-08-03 15:54:41 -04006644 if (cpu_has_vmx_vmfunc())
6645 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6646
Eddie Dong2cc51562007-05-21 07:28:09 +03006647 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6648 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006649 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006650 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006651 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006652
Radim Krčmář74545702015-04-27 15:11:25 +02006653 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6654 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006655
Paolo Bonzini03916db2014-07-24 14:21:57 +02006656 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006657 u32 index = vmx_msr_index[i];
6658 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006659 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006660
6661 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6662 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006663 if (wrmsr_safe(index, data_low, data_high) < 0)
6664 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006665 vmx->guest_msrs[j].index = i;
6666 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006667 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006668 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006669 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006670
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006671 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006672
6673 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006674
6675 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006676 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006677
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006678 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6679 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6680
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006681 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006682
Wanpeng Lif53cd632014-12-02 19:14:58 +08006683 if (vmx_xsaves_supported())
6684 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6685
Peter Feiner4e595162016-07-07 14:49:58 -07006686 if (enable_pml) {
6687 ASSERT(vmx->pml_pg);
6688 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6689 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6690 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006691
6692 if (cpu_has_vmx_encls_vmexit())
6693 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006694}
6695
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006696static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006697{
6698 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006699 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006700 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006701
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006702 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006703 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006704
Wanpeng Li518e7b92018-02-28 14:03:31 +08006705 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006706 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006707 kvm_set_cr8(vcpu, 0);
6708
6709 if (!init_event) {
6710 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6711 MSR_IA32_APICBASE_ENABLE;
6712 if (kvm_vcpu_is_reset_bsp(vcpu))
6713 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6714 apic_base_msr.host_initiated = true;
6715 kvm_set_apic_base(vcpu, &apic_base_msr);
6716 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006717
Avi Kivity2fb92db2011-04-27 19:42:18 +03006718 vmx_segment_cache_clear(vmx);
6719
Avi Kivity5706be02008-08-20 15:07:31 +03006720 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006721 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006722 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006723
6724 seg_setup(VCPU_SREG_DS);
6725 seg_setup(VCPU_SREG_ES);
6726 seg_setup(VCPU_SREG_FS);
6727 seg_setup(VCPU_SREG_GS);
6728 seg_setup(VCPU_SREG_SS);
6729
6730 vmcs_write16(GUEST_TR_SELECTOR, 0);
6731 vmcs_writel(GUEST_TR_BASE, 0);
6732 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6733 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6734
6735 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6736 vmcs_writel(GUEST_LDTR_BASE, 0);
6737 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6738 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6739
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006740 if (!init_event) {
6741 vmcs_write32(GUEST_SYSENTER_CS, 0);
6742 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6743 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6744 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6745 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006746
Wanpeng Lic37c2872017-11-20 14:52:21 -08006747 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006748 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006749
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006750 vmcs_writel(GUEST_GDTR_BASE, 0);
6751 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6752
6753 vmcs_writel(GUEST_IDTR_BASE, 0);
6754 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6755
Anthony Liguori443381a2010-12-06 10:53:38 -06006756 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006757 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006758 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006759 if (kvm_mpx_supported())
6760 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006761
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006762 setup_msrs(vmx);
6763
Avi Kivity6aa8b732006-12-10 02:21:36 -08006764 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6765
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006766 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006767 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006768 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006769 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006770 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006771 vmcs_write32(TPR_THRESHOLD, 0);
6772 }
6773
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006774 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006775
Sheng Yang2384d2b2008-01-17 15:14:33 +08006776 if (vmx->vpid != 0)
6777 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6778
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006779 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006780 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006781 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006782 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006783 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006784
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006785 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006786
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006787 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006788 if (init_event)
6789 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006790}
6791
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006792/*
6793 * In nested virtualization, check if L1 asked to exit on external interrupts.
6794 * For most existing hypervisors, this will always return true.
6795 */
6796static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6797{
6798 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6799 PIN_BASED_EXT_INTR_MASK;
6800}
6801
Bandan Das77b0f5d2014-04-19 18:17:45 -04006802/*
6803 * In nested virtualization, check if L1 has set
6804 * VM_EXIT_ACK_INTR_ON_EXIT
6805 */
6806static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6807{
6808 return get_vmcs12(vcpu)->vm_exit_controls &
6809 VM_EXIT_ACK_INTR_ON_EXIT;
6810}
6811
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006812static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6813{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006814 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006815}
6816
Jan Kiszkac9a79532014-03-07 20:03:15 +01006817static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006818{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006819 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6820 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006821}
6822
Jan Kiszkac9a79532014-03-07 20:03:15 +01006823static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006824{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006825 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006826 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006827 enable_irq_window(vcpu);
6828 return;
6829 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006830
Paolo Bonzini47c01522016-12-19 11:44:07 +01006831 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6832 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006833}
6834
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006835static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006836{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006837 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006838 uint32_t intr;
6839 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006840
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006841 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006842
Avi Kivityfa89a812008-09-01 15:57:51 +03006843 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006844 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006845 int inc_eip = 0;
6846 if (vcpu->arch.interrupt.soft)
6847 inc_eip = vcpu->arch.event_exit_inst_len;
6848 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006849 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006850 return;
6851 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006852 intr = irq | INTR_INFO_VALID_MASK;
6853 if (vcpu->arch.interrupt.soft) {
6854 intr |= INTR_TYPE_SOFT_INTR;
6855 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6856 vmx->vcpu.arch.event_exit_inst_len);
6857 } else
6858 intr |= INTR_TYPE_EXT_INTR;
6859 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006860
6861 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006862}
6863
Sheng Yangf08864b2008-05-15 18:23:25 +08006864static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6865{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006866 struct vcpu_vmx *vmx = to_vmx(vcpu);
6867
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006868 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006869 /*
6870 * Tracking the NMI-blocked state in software is built upon
6871 * finding the next open IRQ window. This, in turn, depends on
6872 * well-behaving guests: They have to keep IRQs disabled at
6873 * least as long as the NMI handler runs. Otherwise we may
6874 * cause NMI nesting, maybe breaking the guest. But as this is
6875 * highly unlikely, we can live with the residual risk.
6876 */
6877 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6878 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6879 }
6880
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006881 ++vcpu->stat.nmi_injections;
6882 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006883
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006884 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006885 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006886 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006887 return;
6888 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006889
Sheng Yangf08864b2008-05-15 18:23:25 +08006890 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6891 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006892
6893 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006894}
6895
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006896static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6897{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006898 struct vcpu_vmx *vmx = to_vmx(vcpu);
6899 bool masked;
6900
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006901 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006902 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006903 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006904 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006905 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6906 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6907 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006908}
6909
6910static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6911{
6912 struct vcpu_vmx *vmx = to_vmx(vcpu);
6913
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006914 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006915 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6916 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6917 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6918 }
6919 } else {
6920 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6921 if (masked)
6922 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6923 GUEST_INTR_STATE_NMI);
6924 else
6925 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6926 GUEST_INTR_STATE_NMI);
6927 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006928}
6929
Jan Kiszka2505dc92013-04-14 12:12:47 +02006930static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6931{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006932 if (to_vmx(vcpu)->nested.nested_run_pending)
6933 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006934
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006935 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006936 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6937 return 0;
6938
Jan Kiszka2505dc92013-04-14 12:12:47 +02006939 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6940 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6941 | GUEST_INTR_STATE_NMI));
6942}
6943
Gleb Natapov78646122009-03-23 12:12:11 +02006944static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6945{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006946 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6947 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006948 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6949 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006950}
6951
Izik Eiduscbc94022007-10-25 00:29:55 +02006952static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6953{
6954 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006955
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006956 if (enable_unrestricted_guest)
6957 return 0;
6958
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006959 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6960 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006961 if (ret)
6962 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006963 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006964 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006965}
6966
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006967static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6968{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006969 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006970 return 0;
6971}
6972
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006973static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006974{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006975 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006976 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006977 /*
6978 * Update instruction length as we may reinject the exception
6979 * from user space while in guest debugging mode.
6980 */
6981 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6982 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006983 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006984 return false;
6985 /* fall through */
6986 case DB_VECTOR:
6987 if (vcpu->guest_debug &
6988 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6989 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006990 /* fall through */
6991 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006992 case OF_VECTOR:
6993 case BR_VECTOR:
6994 case UD_VECTOR:
6995 case DF_VECTOR:
6996 case SS_VECTOR:
6997 case GP_VECTOR:
6998 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006999 return true;
7000 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02007001 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007002 return false;
7003}
7004
7005static int handle_rmode_exception(struct kvm_vcpu *vcpu,
7006 int vec, u32 err_code)
7007{
7008 /*
7009 * Instruction with address size override prefix opcode 0x67
7010 * Cause the #SS fault with 0 error code in VM86 mode.
7011 */
7012 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007013 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007014 if (vcpu->arch.halt_request) {
7015 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007016 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007017 }
7018 return 1;
7019 }
7020 return 0;
7021 }
7022
7023 /*
7024 * Forward all other exceptions that are valid in real mode.
7025 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7026 * the required debugging infrastructure rework.
7027 */
7028 kvm_queue_exception(vcpu, vec);
7029 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030}
7031
Andi Kleena0861c02009-06-08 17:37:09 +08007032/*
7033 * Trigger machine check on the host. We assume all the MSRs are already set up
7034 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7035 * We pass a fake environment to the machine check handler because we want
7036 * the guest to be always treated like user space, no matter what context
7037 * it used internally.
7038 */
7039static void kvm_machine_check(void)
7040{
7041#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7042 struct pt_regs regs = {
7043 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7044 .flags = X86_EFLAGS_IF,
7045 };
7046
7047 do_machine_check(&regs, 0);
7048#endif
7049}
7050
Avi Kivity851ba692009-08-24 11:10:17 +03007051static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007052{
7053 /* already handled by vcpu_run */
7054 return 1;
7055}
7056
Avi Kivity851ba692009-08-24 11:10:17 +03007057static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058{
Avi Kivity1155f762007-11-22 11:30:47 +02007059 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007060 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007061 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007062 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007063 u32 vect_info;
7064 enum emulation_result er;
7065
Avi Kivity1155f762007-11-22 11:30:47 +02007066 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007067 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007068
Andi Kleena0861c02009-06-08 17:37:09 +08007069 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007070 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007071
Jim Mattsonef85b672016-12-12 11:01:37 -08007072 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007073 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007074
Wanpeng Li082d06e2018-04-03 16:28:48 -07007075 if (is_invalid_opcode(intr_info))
7076 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007077
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007079 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007080 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007081
Liran Alon9e869482018-03-12 13:12:51 +02007082 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7083 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007084 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007085 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7086 if (er == EMULATE_USER_EXIT)
7087 return 0;
7088 else if (er != EMULATE_DONE)
7089 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7090 return 1;
7091 }
7092
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007093 /*
7094 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7095 * MMIO, it is better to report an internal error.
7096 * See the comments in vmx_handle_exit.
7097 */
7098 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7099 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7100 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7101 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007102 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007103 vcpu->run->internal.data[0] = vect_info;
7104 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007105 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007106 return 0;
7107 }
7108
Avi Kivity6aa8b732006-12-10 02:21:36 -08007109 if (is_page_fault(intr_info)) {
7110 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007111 /* EPT won't cause page fault directly */
7112 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007113 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007114 }
7115
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007116 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007117
7118 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7119 return handle_rmode_exception(vcpu, ex_no, error_code);
7120
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007121 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007122 case AC_VECTOR:
7123 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7124 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 case DB_VECTOR:
7126 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7127 if (!(vcpu->guest_debug &
7128 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007129 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007130 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007131 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007132 skip_emulated_instruction(vcpu);
7133
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007134 kvm_queue_exception(vcpu, DB_VECTOR);
7135 return 1;
7136 }
7137 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7138 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7139 /* fall through */
7140 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007141 /*
7142 * Update instruction length as we may reinject #BP from
7143 * user space while in guest debugging mode. Reading it for
7144 * #DB as well causes no harm, it is not used in that case.
7145 */
7146 vmx->vcpu.arch.event_exit_inst_len =
7147 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007149 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007150 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7151 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007152 break;
7153 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007154 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7155 kvm_run->ex.exception = ex_no;
7156 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007157 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007158 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007159 return 0;
7160}
7161
Avi Kivity851ba692009-08-24 11:10:17 +03007162static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007163{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007164 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007165 return 1;
7166}
7167
Avi Kivity851ba692009-08-24 11:10:17 +03007168static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007169{
Avi Kivity851ba692009-08-24 11:10:17 +03007170 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007171 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007172 return 0;
7173}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007174
Avi Kivity851ba692009-08-24 11:10:17 +03007175static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007176{
He, Qingbfdaab02007-09-12 14:18:28 +08007177 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007178 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007179 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180
He, Qingbfdaab02007-09-12 14:18:28 +08007181 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007182 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007183
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007184 ++vcpu->stat.io_exits;
7185
Sean Christopherson432baf62018-03-08 08:57:26 -08007186 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007187 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007188
7189 port = exit_qualification >> 16;
7190 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007191 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007192
Sean Christophersondca7f122018-03-08 08:57:27 -08007193 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007194}
7195
Ingo Molnar102d8322007-02-19 14:37:47 +02007196static void
7197vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7198{
7199 /*
7200 * Patch in the VMCALL instruction:
7201 */
7202 hypercall[0] = 0x0f;
7203 hypercall[1] = 0x01;
7204 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007205}
7206
Guo Chao0fa06072012-06-28 15:16:19 +08007207/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007208static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7209{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7212 unsigned long orig_val = val;
7213
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007214 /*
7215 * We get here when L2 changed cr0 in a way that did not change
7216 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 * but did change L0 shadowed bits. So we first calculate the
7218 * effective cr0 value that L1 would like to write into the
7219 * hardware. It consists of the L2-owned bits from the new
7220 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007222 val = (val & ~vmcs12->cr0_guest_host_mask) |
7223 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7224
David Matlack38991522016-11-29 18:14:08 -08007225 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007226 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007227
7228 if (kvm_set_cr0(vcpu, val))
7229 return 1;
7230 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007231 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007232 } else {
7233 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007234 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007235 return 1;
David Matlack38991522016-11-29 18:14:08 -08007236
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007237 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007238 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007239}
7240
7241static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7242{
7243 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007244 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7245 unsigned long orig_val = val;
7246
7247 /* analogously to handle_set_cr0 */
7248 val = (val & ~vmcs12->cr4_guest_host_mask) |
7249 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7250 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007251 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007252 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253 return 0;
7254 } else
7255 return kvm_set_cr4(vcpu, val);
7256}
7257
Paolo Bonzini0367f202016-07-12 10:44:55 +02007258static int handle_desc(struct kvm_vcpu *vcpu)
7259{
7260 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007261 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007262}
7263
Avi Kivity851ba692009-08-24 11:10:17 +03007264static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007265{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007266 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007267 int cr;
7268 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007269 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007271
He, Qingbfdaab02007-09-12 14:18:28 +08007272 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007273 cr = exit_qualification & 15;
7274 reg = (exit_qualification >> 8) & 15;
7275 switch ((exit_qualification >> 4) & 3) {
7276 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007277 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007278 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007279 switch (cr) {
7280 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007281 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007282 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007283 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007284 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007285 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007286 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007288 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007289 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007290 case 8: {
7291 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007292 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007293 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007294 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007295 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007296 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007297 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007298 return ret;
7299 /*
7300 * TODO: we might be squashing a
7301 * KVM_GUESTDBG_SINGLESTEP-triggered
7302 * KVM_EXIT_DEBUG here.
7303 */
Avi Kivity851ba692009-08-24 11:10:17 +03007304 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007305 return 0;
7306 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007307 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007309 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007310 WARN_ONCE(1, "Guest should always own CR0.TS");
7311 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007312 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007313 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007314 case 1: /*mov from cr*/
7315 switch (cr) {
7316 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007317 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007318 val = kvm_read_cr3(vcpu);
7319 kvm_register_write(vcpu, reg, val);
7320 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007321 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007322 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007323 val = kvm_get_cr8(vcpu);
7324 kvm_register_write(vcpu, reg, val);
7325 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007326 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007327 }
7328 break;
7329 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007330 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007331 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007332 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007333
Kyle Huey6affcbe2016-11-29 12:40:40 -08007334 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007335 default:
7336 break;
7337 }
Avi Kivity851ba692009-08-24 11:10:17 +03007338 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007339 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007340 (int)(exit_qualification >> 4) & 3, cr);
7341 return 0;
7342}
7343
Avi Kivity851ba692009-08-24 11:10:17 +03007344static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007345{
He, Qingbfdaab02007-09-12 14:18:28 +08007346 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007347 int dr, dr7, reg;
7348
7349 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7350 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7351
7352 /* First, if DR does not exist, trigger UD */
7353 if (!kvm_require_dr(vcpu, dr))
7354 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007355
Jan Kiszkaf2483412010-01-20 18:20:20 +01007356 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007357 if (!kvm_require_cpl(vcpu, 0))
7358 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007359 dr7 = vmcs_readl(GUEST_DR7);
7360 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007361 /*
7362 * As the vm-exit takes precedence over the debug trap, we
7363 * need to emulate the latter, either for the host or the
7364 * guest debugging itself.
7365 */
7366 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007367 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007368 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007369 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007370 vcpu->run->debug.arch.exception = DB_VECTOR;
7371 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007372 return 0;
7373 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007374 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007375 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007376 kvm_queue_exception(vcpu, DB_VECTOR);
7377 return 1;
7378 }
7379 }
7380
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007381 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007382 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7383 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007384
7385 /*
7386 * No more DR vmexits; force a reload of the debug registers
7387 * and reenter on this instruction. The next vmexit will
7388 * retrieve the full state of the debug registers.
7389 */
7390 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7391 return 1;
7392 }
7393
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007394 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7395 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007396 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007397
7398 if (kvm_get_dr(vcpu, dr, &val))
7399 return 1;
7400 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007401 } else
Nadav Amit57773922014-06-18 17:19:23 +03007402 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007403 return 1;
7404
Kyle Huey6affcbe2016-11-29 12:40:40 -08007405 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406}
7407
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007408static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7409{
7410 return vcpu->arch.dr6;
7411}
7412
7413static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7414{
7415}
7416
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007417static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7418{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007419 get_debugreg(vcpu->arch.db[0], 0);
7420 get_debugreg(vcpu->arch.db[1], 1);
7421 get_debugreg(vcpu->arch.db[2], 2);
7422 get_debugreg(vcpu->arch.db[3], 3);
7423 get_debugreg(vcpu->arch.dr6, 6);
7424 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7425
7426 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007427 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007428}
7429
Gleb Natapov020df072010-04-13 10:05:23 +03007430static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7431{
7432 vmcs_writel(GUEST_DR7, val);
7433}
7434
Avi Kivity851ba692009-08-24 11:10:17 +03007435static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007436{
Kyle Huey6a908b62016-11-29 12:40:37 -08007437 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007438}
7439
Avi Kivity851ba692009-08-24 11:10:17 +03007440static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007441{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007442 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007443 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007444
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007445 msr_info.index = ecx;
7446 msr_info.host_initiated = false;
7447 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007448 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007449 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450 return 1;
7451 }
7452
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007453 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007454
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007456 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7457 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007458 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007459}
7460
Avi Kivity851ba692009-08-24 11:10:17 +03007461static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007462{
Will Auld8fe8ab42012-11-29 12:42:12 -08007463 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007464 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7465 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7466 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467
Will Auld8fe8ab42012-11-29 12:42:12 -08007468 msr.data = data;
7469 msr.index = ecx;
7470 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007471 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007472 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007473 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007474 return 1;
7475 }
7476
Avi Kivity59200272010-01-25 19:47:02 +02007477 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007478 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007482{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007483 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007484 return 1;
7485}
7486
Avi Kivity851ba692009-08-24 11:10:17 +03007487static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007488{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007489 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7490 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007491
Avi Kivity3842d132010-07-27 12:30:24 +03007492 kvm_make_request(KVM_REQ_EVENT, vcpu);
7493
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007494 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007495 return 1;
7496}
7497
Avi Kivity851ba692009-08-24 11:10:17 +03007498static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007499{
Avi Kivityd3bef152007-06-05 15:53:05 +03007500 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007501}
7502
Avi Kivity851ba692009-08-24 11:10:17 +03007503static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007504{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007505 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007506}
7507
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007508static int handle_invd(struct kvm_vcpu *vcpu)
7509{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007510 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007511}
7512
Avi Kivity851ba692009-08-24 11:10:17 +03007513static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007514{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007515 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007516
7517 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007518 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007519}
7520
Avi Kivityfee84b02011-11-10 14:57:25 +02007521static int handle_rdpmc(struct kvm_vcpu *vcpu)
7522{
7523 int err;
7524
7525 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007526 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007527}
7528
Avi Kivity851ba692009-08-24 11:10:17 +03007529static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007530{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007531 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007532}
7533
Dexuan Cui2acf9232010-06-10 11:27:12 +08007534static int handle_xsetbv(struct kvm_vcpu *vcpu)
7535{
7536 u64 new_bv = kvm_read_edx_eax(vcpu);
7537 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7538
7539 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007540 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007541 return 1;
7542}
7543
Wanpeng Lif53cd632014-12-02 19:14:58 +08007544static int handle_xsaves(struct kvm_vcpu *vcpu)
7545{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007546 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007547 WARN(1, "this should never happen\n");
7548 return 1;
7549}
7550
7551static int handle_xrstors(struct kvm_vcpu *vcpu)
7552{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007553 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007554 WARN(1, "this should never happen\n");
7555 return 1;
7556}
7557
Avi Kivity851ba692009-08-24 11:10:17 +03007558static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007559{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007560 if (likely(fasteoi)) {
7561 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7562 int access_type, offset;
7563
7564 access_type = exit_qualification & APIC_ACCESS_TYPE;
7565 offset = exit_qualification & APIC_ACCESS_OFFSET;
7566 /*
7567 * Sane guest uses MOV to write EOI, with written value
7568 * not cared. So make a short-circuit here by avoiding
7569 * heavy instruction emulation.
7570 */
7571 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7572 (offset == APIC_EOI)) {
7573 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007574 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007575 }
7576 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007577 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007578}
7579
Yang Zhangc7c9c562013-01-25 10:18:51 +08007580static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7581{
7582 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7583 int vector = exit_qualification & 0xff;
7584
7585 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7586 kvm_apic_set_eoi_accelerated(vcpu, vector);
7587 return 1;
7588}
7589
Yang Zhang83d4c282013-01-25 10:18:49 +08007590static int handle_apic_write(struct kvm_vcpu *vcpu)
7591{
7592 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7593 u32 offset = exit_qualification & 0xfff;
7594
7595 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7596 kvm_apic_write_nodecode(vcpu, offset);
7597 return 1;
7598}
7599
Avi Kivity851ba692009-08-24 11:10:17 +03007600static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007601{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007602 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007603 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007604 bool has_error_code = false;
7605 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007606 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007607 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007608
7609 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007610 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007611 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007612
7613 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7614
7615 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007616 if (reason == TASK_SWITCH_GATE && idt_v) {
7617 switch (type) {
7618 case INTR_TYPE_NMI_INTR:
7619 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007620 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007621 break;
7622 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007623 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007624 kvm_clear_interrupt_queue(vcpu);
7625 break;
7626 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007627 if (vmx->idt_vectoring_info &
7628 VECTORING_INFO_DELIVER_CODE_MASK) {
7629 has_error_code = true;
7630 error_code =
7631 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7632 }
7633 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007634 case INTR_TYPE_SOFT_EXCEPTION:
7635 kvm_clear_exception_queue(vcpu);
7636 break;
7637 default:
7638 break;
7639 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007640 }
Izik Eidus37817f22008-03-24 23:14:53 +02007641 tss_selector = exit_qualification;
7642
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007643 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7644 type != INTR_TYPE_EXT_INTR &&
7645 type != INTR_TYPE_NMI_INTR))
7646 skip_emulated_instruction(vcpu);
7647
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007648 if (kvm_task_switch(vcpu, tss_selector,
7649 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7650 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007651 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7652 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7653 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007654 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007655 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007656
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007657 /*
7658 * TODO: What about debug traps on tss switch?
7659 * Are we supposed to inject them and update dr6?
7660 */
7661
7662 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007663}
7664
Avi Kivity851ba692009-08-24 11:10:17 +03007665static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007666{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007667 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007668 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007669 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007670
Sheng Yangf9c617f2009-03-25 10:08:52 +08007671 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007672
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007673 /*
7674 * EPT violation happened while executing iret from NMI,
7675 * "blocked by NMI" bit has to be set before next VM entry.
7676 * There are errata that may cause this bit to not be set:
7677 * AAK134, BY25.
7678 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007679 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007680 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007681 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007682 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7683
Sheng Yang14394422008-04-28 12:24:45 +08007684 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007685 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007686
Junaid Shahid27959a42016-12-06 16:46:10 -08007687 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007688 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007689 ? PFERR_USER_MASK : 0;
7690 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007691 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007692 ? PFERR_WRITE_MASK : 0;
7693 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007694 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007695 ? PFERR_FETCH_MASK : 0;
7696 /* ept page table entry is present? */
7697 error_code |= (exit_qualification &
7698 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7699 EPT_VIOLATION_EXECUTABLE))
7700 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007701
Paolo Bonzinieebed242016-11-28 14:39:58 +01007702 error_code |= (exit_qualification & 0x100) != 0 ?
7703 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007704
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007705 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007706 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007707}
7708
Avi Kivity851ba692009-08-24 11:10:17 +03007709static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007710{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007711 gpa_t gpa;
7712
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007713 /*
7714 * A nested guest cannot optimize MMIO vmexits, because we have an
7715 * nGPA here instead of the required GPA.
7716 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007717 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007718 if (!is_guest_mode(vcpu) &&
7719 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007720 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007721 /*
7722 * Doing kvm_skip_emulated_instruction() depends on undefined
7723 * behavior: Intel's manual doesn't mandate
7724 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7725 * occurs and while on real hardware it was observed to be set,
7726 * other hypervisors (namely Hyper-V) don't set it, we end up
7727 * advancing IP with some random value. Disable fast mmio when
7728 * running nested and keep it for real hardware in hope that
7729 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7730 */
7731 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7732 return kvm_skip_emulated_instruction(vcpu);
7733 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007734 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007735 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007736 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007737
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007738 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007739}
7740
Avi Kivity851ba692009-08-24 11:10:17 +03007741static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007742{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007743 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007744 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7745 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007746 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007747 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007748
7749 return 1;
7750}
7751
Mohammed Gamal80ced182009-09-01 12:48:18 +02007752static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007753{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007754 struct vcpu_vmx *vmx = to_vmx(vcpu);
7755 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007756 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007757 u32 cpu_exec_ctrl;
7758 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007759 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007760
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007761 /*
7762 * We should never reach the point where we are emulating L2
7763 * due to invalid guest state as that means we incorrectly
7764 * allowed a nested VMEntry with an invalid vmcs12.
7765 */
7766 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7767
Avi Kivity49e9d552010-09-19 14:34:08 +02007768 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7769 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007770
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007771 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007772 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007773 return handle_interrupt_window(&vmx->vcpu);
7774
Radim Krčmář72875d82017-04-26 22:32:19 +02007775 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007776 return 1;
7777
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007778 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007779
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007780 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007781 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007782 ret = 0;
7783 goto out;
7784 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007785
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007786 if (err != EMULATE_DONE)
7787 goto emulation_error;
7788
7789 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7790 vcpu->arch.exception.pending)
7791 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007792
Gleb Natapov8d76c492013-05-08 18:38:44 +03007793 if (vcpu->arch.halt_request) {
7794 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007795 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007796 goto out;
7797 }
7798
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007799 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007800 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007801 if (need_resched())
7802 schedule();
7803 }
7804
Mohammed Gamal80ced182009-09-01 12:48:18 +02007805out:
7806 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007807
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007808emulation_error:
7809 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7810 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7811 vcpu->run->internal.ndata = 0;
7812 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007813}
7814
7815static void grow_ple_window(struct kvm_vcpu *vcpu)
7816{
7817 struct vcpu_vmx *vmx = to_vmx(vcpu);
7818 int old = vmx->ple_window;
7819
Babu Mogerc8e88712018-03-16 16:37:24 -04007820 vmx->ple_window = __grow_ple_window(old, ple_window,
7821 ple_window_grow,
7822 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007823
7824 if (vmx->ple_window != old)
7825 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007826
7827 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007828}
7829
7830static void shrink_ple_window(struct kvm_vcpu *vcpu)
7831{
7832 struct vcpu_vmx *vmx = to_vmx(vcpu);
7833 int old = vmx->ple_window;
7834
Babu Mogerc8e88712018-03-16 16:37:24 -04007835 vmx->ple_window = __shrink_ple_window(old, ple_window,
7836 ple_window_shrink,
7837 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007838
7839 if (vmx->ple_window != old)
7840 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007841
7842 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007843}
7844
7845/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007846 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7847 */
7848static void wakeup_handler(void)
7849{
7850 struct kvm_vcpu *vcpu;
7851 int cpu = smp_processor_id();
7852
7853 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7854 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7855 blocked_vcpu_list) {
7856 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7857
7858 if (pi_test_on(pi_desc) == 1)
7859 kvm_vcpu_kick(vcpu);
7860 }
7861 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7862}
7863
Peng Haoe01bca22018-04-07 05:47:32 +08007864static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007865{
7866 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7867 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7868 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7869 0ull, VMX_EPT_EXECUTABLE_MASK,
7870 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007871 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007872
7873 ept_set_mmio_spte_mask();
7874 kvm_enable_tdp();
7875}
7876
Tiejun Chenf2c76482014-10-28 10:14:47 +08007877static __init int hardware_setup(void)
7878{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007879 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007880 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007881
7882 rdmsrl_safe(MSR_EFER, &host_efer);
7883
7884 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7885 kvm_define_shared_msr(i, vmx_msr_index[i]);
7886
Radim Krčmář23611332016-09-29 22:41:33 +02007887 for (i = 0; i < VMX_BITMAP_NR; i++) {
7888 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7889 if (!vmx_bitmap[i])
7890 goto out;
7891 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007892
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007893 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7894 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7895
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007896 if (setup_vmcs_config(&vmcs_config) < 0) {
7897 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007898 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007899 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007900
7901 if (boot_cpu_has(X86_FEATURE_NX))
7902 kvm_enable_efer_bits(EFER_NX);
7903
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007904 if (boot_cpu_has(X86_FEATURE_MPX)) {
7905 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7906 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7907 }
7908
Wanpeng Li08d839c2017-03-23 05:30:08 -07007909 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7910 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007911 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007912
Tiejun Chenf2c76482014-10-28 10:14:47 +08007913 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007914 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007915 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007916 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007917 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007918
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007919 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007920 enable_ept_ad_bits = 0;
7921
Wanpeng Li8ad81822017-10-09 15:51:53 -07007922 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007923 enable_unrestricted_guest = 0;
7924
Paolo Bonziniad15a292015-01-30 16:18:49 +01007925 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007926 flexpriority_enabled = 0;
7927
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007928 if (!cpu_has_virtual_nmis())
7929 enable_vnmi = 0;
7930
Paolo Bonziniad15a292015-01-30 16:18:49 +01007931 /*
7932 * set_apic_access_page_addr() is used to reload apic access
7933 * page upon invalidation. No need to do anything if not
7934 * using the APIC_ACCESS_ADDR VMCS field.
7935 */
7936 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007937 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007938
7939 if (!cpu_has_vmx_tpr_shadow())
7940 kvm_x86_ops->update_cr8_intercept = NULL;
7941
7942 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7943 kvm_disable_largepages();
7944
Tianyu Lan877ad952018-07-19 08:40:23 +00007945#if IS_ENABLED(CONFIG_HYPERV)
7946 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7947 && enable_ept)
7948 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7949#endif
7950
Wanpeng Li0f107682017-09-28 18:06:24 -07007951 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007952 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007953 ple_window = 0;
7954 ple_window_grow = 0;
7955 ple_window_max = 0;
7956 ple_window_shrink = 0;
7957 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007958
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007959 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007960 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007961 kvm_x86_ops->sync_pir_to_irr = NULL;
7962 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007963
Haozhong Zhang64903d62015-10-20 15:39:09 +08007964 if (cpu_has_vmx_tsc_scaling()) {
7965 kvm_has_tsc_control = true;
7966 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7967 kvm_tsc_scaling_ratio_frac_bits = 48;
7968 }
7969
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007970 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7971
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007972 if (enable_ept)
7973 vmx_enable_tdp();
7974 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007975 kvm_disable_tdp();
7976
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007977 if (!nested) {
7978 kvm_x86_ops->get_nested_state = NULL;
7979 kvm_x86_ops->set_nested_state = NULL;
7980 }
7981
Kai Huang843e4332015-01-28 10:54:28 +08007982 /*
7983 * Only enable PML when hardware supports PML feature, and both EPT
7984 * and EPT A/D bit features are enabled -- PML depends on them to work.
7985 */
7986 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7987 enable_pml = 0;
7988
7989 if (!enable_pml) {
7990 kvm_x86_ops->slot_enable_log_dirty = NULL;
7991 kvm_x86_ops->slot_disable_log_dirty = NULL;
7992 kvm_x86_ops->flush_log_dirty = NULL;
7993 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7994 }
7995
Sean Christophersond264ee02018-08-27 15:21:12 -07007996 if (!cpu_has_vmx_preemption_timer())
7997 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7998
Yunhong Jiang64672c92016-06-13 14:19:59 -07007999 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
8000 u64 vmx_msr;
8001
8002 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8003 cpu_preemption_timer_multi =
8004 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8005 } else {
8006 kvm_x86_ops->set_hv_timer = NULL;
8007 kvm_x86_ops->cancel_hv_timer = NULL;
8008 }
8009
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01008010 if (!cpu_has_vmx_shadow_vmcs())
8011 enable_shadow_vmcs = 0;
8012 if (enable_shadow_vmcs)
8013 init_vmcs_shadow_fields();
8014
Feng Wubf9f6ac2015-09-18 22:29:55 +08008015 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01008016 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08008017
Ashok Rajc45dcc72016-06-22 14:59:56 +08008018 kvm_mce_cap_supported |= MCG_LMCE_P;
8019
Tiejun Chenf2c76482014-10-28 10:14:47 +08008020 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008021
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008022out:
Radim Krčmář23611332016-09-29 22:41:33 +02008023 for (i = 0; i < VMX_BITMAP_NR; i++)
8024 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008025
8026 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008027}
8028
8029static __exit void hardware_unsetup(void)
8030{
Radim Krčmář23611332016-09-29 22:41:33 +02008031 int i;
8032
8033 for (i = 0; i < VMX_BITMAP_NR; i++)
8034 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008035
Tiejun Chenf2c76482014-10-28 10:14:47 +08008036 free_kvm_area();
8037}
8038
Avi Kivity6aa8b732006-12-10 02:21:36 -08008039/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008040 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8041 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8042 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008043static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008044{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008045 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008046 grow_ple_window(vcpu);
8047
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008048 /*
8049 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8050 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8051 * never set PAUSE_EXITING and just set PLE if supported,
8052 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8053 */
8054 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008055 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008056}
8057
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008058static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008059{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008060 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008061}
8062
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008063static int handle_mwait(struct kvm_vcpu *vcpu)
8064{
8065 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8066 return handle_nop(vcpu);
8067}
8068
Jim Mattson45ec3682017-08-23 16:32:04 -07008069static int handle_invalid_op(struct kvm_vcpu *vcpu)
8070{
8071 kvm_queue_exception(vcpu, UD_VECTOR);
8072 return 1;
8073}
8074
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008075static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8076{
8077 return 1;
8078}
8079
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008080static int handle_monitor(struct kvm_vcpu *vcpu)
8081{
8082 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8083 return handle_nop(vcpu);
8084}
8085
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008086/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008087 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8088 * set the success or error code of an emulated VMX instruction, as specified
8089 * by Vol 2B, VMX Instruction Reference, "Conventions".
8090 */
8091static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8092{
8093 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8094 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8095 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8096}
8097
8098static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8099{
8100 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8101 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8102 X86_EFLAGS_SF | X86_EFLAGS_OF))
8103 | X86_EFLAGS_CF);
8104}
8105
Abel Gordon145c28d2013-04-18 14:36:55 +03008106static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008107 u32 vm_instruction_error)
8108{
8109 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8110 /*
8111 * failValid writes the error number to the current VMCS, which
8112 * can't be done there isn't a current VMCS.
8113 */
8114 nested_vmx_failInvalid(vcpu);
8115 return;
8116 }
8117 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8118 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8119 X86_EFLAGS_SF | X86_EFLAGS_OF))
8120 | X86_EFLAGS_ZF);
8121 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8122 /*
8123 * We don't need to force a shadow sync because
8124 * VM_INSTRUCTION_ERROR is not shadowed
8125 */
8126}
Abel Gordon145c28d2013-04-18 14:36:55 +03008127
Wincy Vanff651cb2014-12-11 08:52:58 +03008128static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8129{
8130 /* TODO: not to reset guest simply here. */
8131 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008132 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008133}
8134
Jan Kiszkaf4124502014-03-07 20:03:13 +01008135static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8136{
8137 struct vcpu_vmx *vmx =
8138 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8139
8140 vmx->nested.preemption_timer_expired = true;
8141 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8142 kvm_vcpu_kick(&vmx->vcpu);
8143
8144 return HRTIMER_NORESTART;
8145}
8146
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008147/*
Bandan Das19677e32014-05-06 02:19:15 -04008148 * Decode the memory-address operand of a vmx instruction, as recorded on an
8149 * exit caused by such an instruction (run by a guest hypervisor).
8150 * On success, returns 0. When the operand is invalid, returns 1 and throws
8151 * #UD or #GP.
8152 */
8153static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8154 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008156{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 gva_t off;
8158 bool exn;
8159 struct kvm_segment s;
8160
Bandan Das19677e32014-05-06 02:19:15 -04008161 /*
8162 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8163 * Execution", on an exit, vmx_instruction_info holds most of the
8164 * addressing components of the operand. Only the displacement part
8165 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8166 * For how an actual address is calculated from all these components,
8167 * refer to Vol. 1, "Operand Addressing".
8168 */
8169 int scaling = vmx_instruction_info & 3;
8170 int addr_size = (vmx_instruction_info >> 7) & 7;
8171 bool is_reg = vmx_instruction_info & (1u << 10);
8172 int seg_reg = (vmx_instruction_info >> 15) & 7;
8173 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8174 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8175 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8176 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8177
8178 if (is_reg) {
8179 kvm_queue_exception(vcpu, UD_VECTOR);
8180 return 1;
8181 }
8182
8183 /* Addr = segment_base + offset */
8184 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008185 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008186 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008187 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008188 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008189 off += kvm_register_read(vcpu, index_reg)<<scaling;
8190 vmx_get_segment(vcpu, &s, seg_reg);
8191 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008192
8193 if (addr_size == 1) /* 32 bit */
8194 *ret &= 0xffffffff;
8195
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008196 /* Checks for #GP/#SS exceptions. */
8197 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008198 if (is_long_mode(vcpu)) {
8199 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8200 * non-canonical form. This is the only check on the memory
8201 * destination for long mode!
8202 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008203 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008204 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008205 /* Protected mode: apply checks for segment validity in the
8206 * following order:
8207 * - segment type check (#GP(0) may be thrown)
8208 * - usability check (#GP(0)/#SS(0))
8209 * - limit check (#GP(0)/#SS(0))
8210 */
8211 if (wr)
8212 /* #GP(0) if the destination operand is located in a
8213 * read-only data segment or any code segment.
8214 */
8215 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8216 else
8217 /* #GP(0) if the source operand is located in an
8218 * execute-only code segment
8219 */
8220 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008221 if (exn) {
8222 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8223 return 1;
8224 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008225 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8226 */
8227 exn = (s.unusable != 0);
8228 /* Protected mode: #GP(0)/#SS(0) if the memory
8229 * operand is outside the segment limit.
8230 */
8231 exn = exn || (off + sizeof(u64) > s.limit);
8232 }
8233 if (exn) {
8234 kvm_queue_exception_e(vcpu,
8235 seg_reg == VCPU_SREG_SS ?
8236 SS_VECTOR : GP_VECTOR,
8237 0);
8238 return 1;
8239 }
8240
Bandan Das19677e32014-05-06 02:19:15 -04008241 return 0;
8242}
8243
Radim Krčmářcbf71272017-05-19 15:48:51 +02008244static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008245{
8246 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008247 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008248
8249 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008250 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008251 return 1;
8252
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008253 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008254 kvm_inject_page_fault(vcpu, &e);
8255 return 1;
8256 }
8257
Bandan Das3573e222014-05-06 02:19:16 -04008258 return 0;
8259}
8260
Liran Alonabfc52c2018-06-23 02:35:13 +03008261/*
8262 * Allocate a shadow VMCS and associate it with the currently loaded
8263 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8264 * VMCS is also VMCLEARed, so that it is ready for use.
8265 */
8266static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8267{
8268 struct vcpu_vmx *vmx = to_vmx(vcpu);
8269 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8270
8271 /*
8272 * We should allocate a shadow vmcs for vmcs01 only when L1
8273 * executes VMXON and free it when L1 executes VMXOFF.
8274 * As it is invalid to execute VMXON twice, we shouldn't reach
8275 * here when vmcs01 already have an allocated shadow vmcs.
8276 */
8277 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8278
8279 if (!loaded_vmcs->shadow_vmcs) {
8280 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8281 if (loaded_vmcs->shadow_vmcs)
8282 vmcs_clear(loaded_vmcs->shadow_vmcs);
8283 }
8284 return loaded_vmcs->shadow_vmcs;
8285}
8286
Jim Mattsone29acc52016-11-30 12:03:43 -08008287static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8288{
8289 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008290 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008291
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008292 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8293 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008294 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008295
8296 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8297 if (!vmx->nested.cached_vmcs12)
8298 goto out_cached_vmcs12;
8299
Liran Alon61ada742018-06-23 02:35:08 +03008300 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8301 if (!vmx->nested.cached_shadow_vmcs12)
8302 goto out_cached_shadow_vmcs12;
8303
Liran Alonabfc52c2018-06-23 02:35:13 +03008304 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8305 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008306
Jim Mattsone29acc52016-11-30 12:03:43 -08008307 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8308 HRTIMER_MODE_REL_PINNED);
8309 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8310
Roman Kagan63aff652018-07-19 21:59:07 +03008311 vmx->nested.vpid02 = allocate_vpid();
8312
Jim Mattsone29acc52016-11-30 12:03:43 -08008313 vmx->nested.vmxon = true;
8314 return 0;
8315
8316out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008317 kfree(vmx->nested.cached_shadow_vmcs12);
8318
8319out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008320 kfree(vmx->nested.cached_vmcs12);
8321
8322out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008323 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008324
Jim Mattsonde3a0022017-11-27 17:22:25 -06008325out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008326 return -ENOMEM;
8327}
8328
Bandan Das3573e222014-05-06 02:19:16 -04008329/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008330 * Emulate the VMXON instruction.
8331 * Currently, we just remember that VMX is active, and do not save or even
8332 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8333 * do not currently need to store anything in that guest-allocated memory
8334 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8335 * argument is different from the VMXON pointer (which the spec says they do).
8336 */
8337static int handle_vmon(struct kvm_vcpu *vcpu)
8338{
Jim Mattsone29acc52016-11-30 12:03:43 -08008339 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008340 gpa_t vmptr;
8341 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008342 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008343 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8344 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008345
Jim Mattson70f3aac2017-04-26 08:53:46 -07008346 /*
8347 * The Intel VMX Instruction Reference lists a bunch of bits that are
8348 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8349 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8350 * Otherwise, we should fail with #UD. But most faulting conditions
8351 * have already been checked by hardware, prior to the VM-exit for
8352 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8353 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008354 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008355 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008356 kvm_queue_exception(vcpu, UD_VECTOR);
8357 return 1;
8358 }
8359
Felix Wilhelm727ba742018-06-11 09:43:44 +02008360 /* CPL=0 must be checked manually. */
8361 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008362 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008363 return 1;
8364 }
8365
Abel Gordon145c28d2013-04-18 14:36:55 +03008366 if (vmx->nested.vmxon) {
8367 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008368 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008369 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008370
Haozhong Zhang3b840802016-06-22 14:59:54 +08008371 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008372 != VMXON_NEEDED_FEATURES) {
8373 kvm_inject_gp(vcpu, 0);
8374 return 1;
8375 }
8376
Radim Krčmářcbf71272017-05-19 15:48:51 +02008377 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008378 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008379
8380 /*
8381 * SDM 3: 24.11.5
8382 * The first 4 bytes of VMXON region contain the supported
8383 * VMCS revision identifier
8384 *
8385 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8386 * which replaces physical address width with 32
8387 */
8388 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8389 nested_vmx_failInvalid(vcpu);
8390 return kvm_skip_emulated_instruction(vcpu);
8391 }
8392
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008393 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8394 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008395 nested_vmx_failInvalid(vcpu);
8396 return kvm_skip_emulated_instruction(vcpu);
8397 }
8398 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8399 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008400 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008401 nested_vmx_failInvalid(vcpu);
8402 return kvm_skip_emulated_instruction(vcpu);
8403 }
8404 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008405 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008406
8407 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008408 ret = enter_vmx_operation(vcpu);
8409 if (ret)
8410 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008411
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008412 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008413 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008414}
8415
8416/*
8417 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8418 * for running VMX instructions (except VMXON, whose prerequisites are
8419 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008420 * Note that many of these exceptions have priority over VM exits, so they
8421 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008422 */
8423static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8424{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008425 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008426 kvm_queue_exception(vcpu, UD_VECTOR);
8427 return 0;
8428 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008429
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008430 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008431 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008432 return 0;
8433 }
8434
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008435 return 1;
8436}
8437
David Matlack8ca44e82017-08-01 14:00:39 -07008438static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8439{
8440 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8441 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8442}
8443
Abel Gordone7953d72013-04-18 14:37:55 +03008444static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8445{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008446 if (vmx->nested.current_vmptr == -1ull)
8447 return;
8448
Abel Gordon012f83c2013-04-18 14:39:25 +03008449 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008450 /* copy to memory all shadowed fields in case
8451 they were modified */
8452 copy_shadow_to_vmcs12(vmx);
8453 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008454 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008455 }
Wincy Van705699a2015-02-03 23:58:17 +08008456 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008457
8458 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008459 kvm_vcpu_write_guest_page(&vmx->vcpu,
8460 vmx->nested.current_vmptr >> PAGE_SHIFT,
8461 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008462
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008463 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008464}
8465
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008466/*
8467 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8468 * just stops using VMX.
8469 */
8470static void free_nested(struct vcpu_vmx *vmx)
8471{
Wanpeng Lib7455822017-11-22 14:04:00 -08008472 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008473 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008474
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008475 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008476 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008477 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008478 vmx->nested.posted_intr_nv = -1;
8479 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008480 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008481 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008482 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8483 free_vmcs(vmx->vmcs01.shadow_vmcs);
8484 vmx->vmcs01.shadow_vmcs = NULL;
8485 }
David Matlack4f2777b2016-07-13 17:16:37 -07008486 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008487 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008488 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008489 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008490 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008491 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008492 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008493 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008494 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008495 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008496 }
Wincy Van705699a2015-02-03 23:58:17 +08008497 if (vmx->nested.pi_desc_page) {
8498 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008499 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008500 vmx->nested.pi_desc_page = NULL;
8501 vmx->nested.pi_desc = NULL;
8502 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008503
Jim Mattsonde3a0022017-11-27 17:22:25 -06008504 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008505}
8506
8507/* Emulate the VMXOFF instruction */
8508static int handle_vmoff(struct kvm_vcpu *vcpu)
8509{
8510 if (!nested_vmx_check_permission(vcpu))
8511 return 1;
8512 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008513 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008514 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008515}
8516
Nadav Har'El27d6c862011-05-25 23:06:59 +03008517/* Emulate the VMCLEAR instruction */
8518static int handle_vmclear(struct kvm_vcpu *vcpu)
8519{
8520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008521 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008522 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008523
8524 if (!nested_vmx_check_permission(vcpu))
8525 return 1;
8526
Radim Krčmářcbf71272017-05-19 15:48:51 +02008527 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008528 return 1;
8529
Radim Krčmářcbf71272017-05-19 15:48:51 +02008530 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8531 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8532 return kvm_skip_emulated_instruction(vcpu);
8533 }
8534
8535 if (vmptr == vmx->nested.vmxon_ptr) {
8536 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8537 return kvm_skip_emulated_instruction(vcpu);
8538 }
8539
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008540 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008541 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008542
Jim Mattson587d7e722017-03-02 12:41:48 -08008543 kvm_vcpu_write_guest(vcpu,
8544 vmptr + offsetof(struct vmcs12, launch_state),
8545 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008546
Nadav Har'El27d6c862011-05-25 23:06:59 +03008547 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008548 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008549}
8550
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008551static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8552
8553/* Emulate the VMLAUNCH instruction */
8554static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8555{
8556 return nested_vmx_run(vcpu, true);
8557}
8558
8559/* Emulate the VMRESUME instruction */
8560static int handle_vmresume(struct kvm_vcpu *vcpu)
8561{
8562
8563 return nested_vmx_run(vcpu, false);
8564}
8565
Nadav Har'El49f705c2011-05-25 23:08:30 +03008566/*
8567 * Read a vmcs12 field. Since these can have varying lengths and we return
8568 * one type, we chose the biggest type (u64) and zero-extend the return value
8569 * to that size. Note that the caller, handle_vmread, might need to use only
8570 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8571 * 64-bit fields are to be returned).
8572 */
Liran Alone2536742018-06-23 02:35:02 +03008573static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008575{
8576 short offset = vmcs_field_to_offset(field);
8577 char *p;
8578
8579 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008580 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008581
Liran Alone2536742018-06-23 02:35:02 +03008582 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008583
Jim Mattsond37f4262017-12-22 12:12:16 -08008584 switch (vmcs_field_width(field)) {
8585 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008586 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008588 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008589 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008591 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008592 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008594 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008595 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008596 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008597 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008598 WARN_ON(1);
8599 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008600 }
8601}
8602
Abel Gordon20b97fe2013-04-18 14:36:25 +03008603
Liran Alone2536742018-06-23 02:35:02 +03008604static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008605 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008606 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008607 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008608 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008609 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008610
Jim Mattsond37f4262017-12-22 12:12:16 -08008611 switch (vmcs_field_width(field)) {
8612 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008613 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008614 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008615 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008616 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008617 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008618 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008619 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008620 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008621 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008622 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008623 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008624 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008625 WARN_ON(1);
8626 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008627 }
8628
8629}
8630
Jim Mattsonf4160e42018-05-29 09:11:33 -07008631/*
8632 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8633 * they have been modified by the L1 guest. Note that the "read-only"
8634 * VM-exit information fields are actually writable if the vCPU is
8635 * configured to support "VMWRITE to any supported field in the VMCS."
8636 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008637static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8638{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008639 const u16 *fields[] = {
8640 shadow_read_write_fields,
8641 shadow_read_only_fields
8642 };
8643 const int max_fields[] = {
8644 max_shadow_read_write_fields,
8645 max_shadow_read_only_fields
8646 };
8647 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008648 unsigned long field;
8649 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008650 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008651
Jan Kiszka282da872014-10-08 18:05:39 +02008652 preempt_disable();
8653
Abel Gordon16f5b902013-04-18 14:38:25 +03008654 vmcs_load(shadow_vmcs);
8655
Jim Mattsonf4160e42018-05-29 09:11:33 -07008656 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8657 for (i = 0; i < max_fields[q]; i++) {
8658 field = fields[q][i];
8659 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008660 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008661 }
8662 /*
8663 * Skip the VM-exit information fields if they are read-only.
8664 */
8665 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8666 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008667 }
8668
8669 vmcs_clear(shadow_vmcs);
8670 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008671
8672 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008673}
8674
Abel Gordonc3114422013-04-18 14:38:55 +03008675static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8676{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008677 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008678 shadow_read_write_fields,
8679 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008680 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008681 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008682 max_shadow_read_write_fields,
8683 max_shadow_read_only_fields
8684 };
8685 int i, q;
8686 unsigned long field;
8687 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008688 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008689
8690 vmcs_load(shadow_vmcs);
8691
Mathias Krausec2bae892013-06-26 20:36:21 +02008692 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008693 for (i = 0; i < max_fields[q]; i++) {
8694 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008695 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008696 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008697 }
8698 }
8699
8700 vmcs_clear(shadow_vmcs);
8701 vmcs_load(vmx->loaded_vmcs->vmcs);
8702}
8703
Nadav Har'El49f705c2011-05-25 23:08:30 +03008704/*
8705 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8706 * used before) all generate the same failure when it is missing.
8707 */
8708static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8709{
8710 struct vcpu_vmx *vmx = to_vmx(vcpu);
8711 if (vmx->nested.current_vmptr == -1ull) {
8712 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008713 return 0;
8714 }
8715 return 1;
8716}
8717
8718static int handle_vmread(struct kvm_vcpu *vcpu)
8719{
8720 unsigned long field;
8721 u64 field_value;
8722 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8723 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8724 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008725 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008726
Kyle Hueyeb277562016-11-29 12:40:39 -08008727 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008728 return 1;
8729
Kyle Huey6affcbe2016-11-29 12:40:40 -08008730 if (!nested_vmx_check_vmcs12(vcpu))
8731 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008732
Liran Alon6d894f42018-06-23 02:35:09 +03008733 if (!is_guest_mode(vcpu))
8734 vmcs12 = get_vmcs12(vcpu);
8735 else {
8736 /*
8737 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8738 * to shadowed-field sets the ALU flags for VMfailInvalid.
8739 */
8740 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8741 nested_vmx_failInvalid(vcpu);
8742 return kvm_skip_emulated_instruction(vcpu);
8743 }
8744 vmcs12 = get_shadow_vmcs12(vcpu);
8745 }
8746
Nadav Har'El49f705c2011-05-25 23:08:30 +03008747 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008748 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008750 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008751 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008752 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008753 }
8754 /*
8755 * Now copy part of this value to register or memory, as requested.
8756 * Note that the number of bits actually copied is 32 or 64 depending
8757 * on the guest's mode (32 or 64 bit), not on the given field's length.
8758 */
8759 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008760 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008761 field_value);
8762 } else {
8763 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008764 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008765 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008766 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008767 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8768 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008769 }
8770
8771 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008772 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773}
8774
8775
8776static int handle_vmwrite(struct kvm_vcpu *vcpu)
8777{
8778 unsigned long field;
8779 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008780 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008781 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8782 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008783
Nadav Har'El49f705c2011-05-25 23:08:30 +03008784 /* The value to write might be 32 or 64 bits, depending on L1's long
8785 * mode, and eventually we need to write that into a field of several
8786 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008787 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008788 * bits into the vmcs12 field.
8789 */
8790 u64 field_value = 0;
8791 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008792 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008793
Kyle Hueyeb277562016-11-29 12:40:39 -08008794 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008795 return 1;
8796
Kyle Huey6affcbe2016-11-29 12:40:40 -08008797 if (!nested_vmx_check_vmcs12(vcpu))
8798 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008799
Nadav Har'El49f705c2011-05-25 23:08:30 +03008800 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008801 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008802 (((vmx_instruction_info) >> 3) & 0xf));
8803 else {
8804 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008805 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008806 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008807 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8808 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008809 kvm_inject_page_fault(vcpu, &e);
8810 return 1;
8811 }
8812 }
8813
8814
Nadav Amit27e6fb52014-06-18 17:19:26 +03008815 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008816 /*
8817 * If the vCPU supports "VMWRITE to any supported field in the
8818 * VMCS," then the "read-only" fields are actually read/write.
8819 */
8820 if (vmcs_field_readonly(field) &&
8821 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008822 nested_vmx_failValid(vcpu,
8823 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008824 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008825 }
8826
Liran Alon6d894f42018-06-23 02:35:09 +03008827 if (!is_guest_mode(vcpu))
8828 vmcs12 = get_vmcs12(vcpu);
8829 else {
8830 /*
8831 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8832 * to shadowed-field sets the ALU flags for VMfailInvalid.
8833 */
8834 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8835 nested_vmx_failInvalid(vcpu);
8836 return kvm_skip_emulated_instruction(vcpu);
8837 }
8838 vmcs12 = get_shadow_vmcs12(vcpu);
8839
8840 }
8841
8842 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008843 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008844 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008845 }
8846
Liran Alon6d894f42018-06-23 02:35:09 +03008847 /*
8848 * Do not track vmcs12 dirty-state if in guest-mode
8849 * as we actually dirty shadow vmcs12 instead of vmcs12.
8850 */
8851 if (!is_guest_mode(vcpu)) {
8852 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008853#define SHADOW_FIELD_RW(x) case x:
8854#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008855 /*
8856 * The fields that can be updated by L1 without a vmexit are
8857 * always updated in the vmcs02, the others go down the slow
8858 * path of prepare_vmcs02.
8859 */
8860 break;
8861 default:
8862 vmx->nested.dirty_vmcs12 = true;
8863 break;
8864 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008865 }
8866
Nadav Har'El49f705c2011-05-25 23:08:30 +03008867 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008868 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008869}
8870
Jim Mattsona8bc2842016-11-30 12:03:44 -08008871static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8872{
8873 vmx->nested.current_vmptr = vmptr;
8874 if (enable_shadow_vmcs) {
8875 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8876 SECONDARY_EXEC_SHADOW_VMCS);
8877 vmcs_write64(VMCS_LINK_POINTER,
8878 __pa(vmx->vmcs01.shadow_vmcs));
8879 vmx->nested.sync_shadow_vmcs = true;
8880 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008881 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008882}
8883
Nadav Har'El63846662011-05-25 23:07:29 +03008884/* Emulate the VMPTRLD instruction */
8885static int handle_vmptrld(struct kvm_vcpu *vcpu)
8886{
8887 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008888 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008889
8890 if (!nested_vmx_check_permission(vcpu))
8891 return 1;
8892
Radim Krčmářcbf71272017-05-19 15:48:51 +02008893 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008894 return 1;
8895
Radim Krčmářcbf71272017-05-19 15:48:51 +02008896 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8897 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8898 return kvm_skip_emulated_instruction(vcpu);
8899 }
8900
8901 if (vmptr == vmx->nested.vmxon_ptr) {
8902 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8903 return kvm_skip_emulated_instruction(vcpu);
8904 }
8905
Nadav Har'El63846662011-05-25 23:07:29 +03008906 if (vmx->nested.current_vmptr != vmptr) {
8907 struct vmcs12 *new_vmcs12;
8908 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008909 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8910 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008911 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008912 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008913 }
8914 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008915 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008916 (new_vmcs12->hdr.shadow_vmcs &&
8917 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008918 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008919 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008920 nested_vmx_failValid(vcpu,
8921 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008922 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008923 }
Nadav Har'El63846662011-05-25 23:07:29 +03008924
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008925 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008926 /*
8927 * Load VMCS12 from guest memory since it is not already
8928 * cached.
8929 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008930 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8931 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008932 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008933
Jim Mattsona8bc2842016-11-30 12:03:44 -08008934 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008935 }
8936
8937 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008938 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008939}
8940
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008941/* Emulate the VMPTRST instruction */
8942static int handle_vmptrst(struct kvm_vcpu *vcpu)
8943{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008944 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8945 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8946 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008947 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008948 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008949
8950 if (!nested_vmx_check_permission(vcpu))
8951 return 1;
8952
Sean Christopherson0a06d422018-07-19 10:31:00 -07008953 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008954 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008955 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008956 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8957 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008958 kvm_inject_page_fault(vcpu, &e);
8959 return 1;
8960 }
8961 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008962 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008963}
8964
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008965/* Emulate the INVEPT instruction */
8966static int handle_invept(struct kvm_vcpu *vcpu)
8967{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008969 u32 vmx_instruction_info, types;
8970 unsigned long type;
8971 gva_t gva;
8972 struct x86_exception e;
8973 struct {
8974 u64 eptp, gpa;
8975 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008976
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008977 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008978 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008979 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008980 kvm_queue_exception(vcpu, UD_VECTOR);
8981 return 1;
8982 }
8983
8984 if (!nested_vmx_check_permission(vcpu))
8985 return 1;
8986
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008987 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008988 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008989
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008990 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008991
Jim Mattson85c856b2016-10-26 08:38:38 -07008992 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008993 nested_vmx_failValid(vcpu,
8994 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008995 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008996 }
8997
8998 /* According to the Intel VMX instruction reference, the memory
8999 * operand is read even if it isn't needed (e.g., for type==global)
9000 */
9001 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00009002 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009003 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009004 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009005 kvm_inject_page_fault(vcpu, &e);
9006 return 1;
9007 }
9008
9009 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009010 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04009011 /*
9012 * TODO: track mappings and invalidate
9013 * single context requests appropriately
9014 */
9015 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009016 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04009017 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009018 nested_vmx_succeed(vcpu);
9019 break;
9020 default:
9021 BUG_ON(1);
9022 break;
9023 }
9024
Kyle Huey6affcbe2016-11-29 12:40:40 -08009025 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009026}
9027
Petr Matouseka642fc32014-09-23 20:22:30 +02009028static int handle_invvpid(struct kvm_vcpu *vcpu)
9029{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009030 struct vcpu_vmx *vmx = to_vmx(vcpu);
9031 u32 vmx_instruction_info;
9032 unsigned long type, types;
9033 gva_t gva;
9034 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009035 struct {
9036 u64 vpid;
9037 u64 gla;
9038 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009039
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009040 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009041 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009042 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009043 kvm_queue_exception(vcpu, UD_VECTOR);
9044 return 1;
9045 }
9046
9047 if (!nested_vmx_check_permission(vcpu))
9048 return 1;
9049
9050 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9051 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9052
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009053 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009054 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009055
Jim Mattson85c856b2016-10-26 08:38:38 -07009056 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009057 nested_vmx_failValid(vcpu,
9058 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009059 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009060 }
9061
9062 /* according to the intel vmx instruction reference, the memory
9063 * operand is read even if it isn't needed (e.g., for type==global)
9064 */
9065 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9066 vmx_instruction_info, false, &gva))
9067 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009068 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009069 kvm_inject_page_fault(vcpu, &e);
9070 return 1;
9071 }
Jim Mattson40352602017-06-28 09:37:37 -07009072 if (operand.vpid >> 16) {
9073 nested_vmx_failValid(vcpu,
9074 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9075 return kvm_skip_emulated_instruction(vcpu);
9076 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009077
9078 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009079 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009080 if (!operand.vpid ||
9081 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009082 nested_vmx_failValid(vcpu,
9083 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9084 return kvm_skip_emulated_instruction(vcpu);
9085 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009086 if (cpu_has_vmx_invvpid_individual_addr() &&
9087 vmx->nested.vpid02) {
9088 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9089 vmx->nested.vpid02, operand.gla);
9090 } else
9091 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9092 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009093 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009094 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009095 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009096 nested_vmx_failValid(vcpu,
9097 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009098 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009099 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009100 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009101 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009102 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03009103 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009104 break;
9105 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009106 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009107 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009108 }
9109
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009110 nested_vmx_succeed(vcpu);
9111
Kyle Huey6affcbe2016-11-29 12:40:40 -08009112 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009113}
9114
Junaid Shahideb4b2482018-06-27 14:59:14 -07009115static int handle_invpcid(struct kvm_vcpu *vcpu)
9116{
9117 u32 vmx_instruction_info;
9118 unsigned long type;
9119 bool pcid_enabled;
9120 gva_t gva;
9121 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009122 unsigned i;
9123 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009124 struct {
9125 u64 pcid;
9126 u64 gla;
9127 } operand;
9128
9129 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9130 kvm_queue_exception(vcpu, UD_VECTOR);
9131 return 1;
9132 }
9133
9134 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9135 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9136
9137 if (type > 3) {
9138 kvm_inject_gp(vcpu, 0);
9139 return 1;
9140 }
9141
9142 /* According to the Intel instruction reference, the memory operand
9143 * is read even if it isn't needed (e.g., for type==all)
9144 */
9145 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9146 vmx_instruction_info, false, &gva))
9147 return 1;
9148
9149 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9150 kvm_inject_page_fault(vcpu, &e);
9151 return 1;
9152 }
9153
9154 if (operand.pcid >> 12 != 0) {
9155 kvm_inject_gp(vcpu, 0);
9156 return 1;
9157 }
9158
9159 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9160
9161 switch (type) {
9162 case INVPCID_TYPE_INDIV_ADDR:
9163 if ((!pcid_enabled && (operand.pcid != 0)) ||
9164 is_noncanonical_address(operand.gla, vcpu)) {
9165 kvm_inject_gp(vcpu, 0);
9166 return 1;
9167 }
9168 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9169 return kvm_skip_emulated_instruction(vcpu);
9170
9171 case INVPCID_TYPE_SINGLE_CTXT:
9172 if (!pcid_enabled && (operand.pcid != 0)) {
9173 kvm_inject_gp(vcpu, 0);
9174 return 1;
9175 }
9176
9177 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9178 kvm_mmu_sync_roots(vcpu);
9179 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9180 }
9181
Junaid Shahidb94742c2018-06-27 14:59:20 -07009182 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9183 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9184 == operand.pcid)
9185 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009186
Junaid Shahidb94742c2018-06-27 14:59:20 -07009187 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009188 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009189 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009190 * given PCID, then nothing needs to be done here because a
9191 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009192 */
9193
9194 return kvm_skip_emulated_instruction(vcpu);
9195
9196 case INVPCID_TYPE_ALL_NON_GLOBAL:
9197 /*
9198 * Currently, KVM doesn't mark global entries in the shadow
9199 * page tables, so a non-global flush just degenerates to a
9200 * global flush. If needed, we could optimize this later by
9201 * keeping track of global entries in shadow page tables.
9202 */
9203
9204 /* fall-through */
9205 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9206 kvm_mmu_unload(vcpu);
9207 return kvm_skip_emulated_instruction(vcpu);
9208
9209 default:
9210 BUG(); /* We have already checked above that type <= 3 */
9211 }
9212}
9213
Kai Huang843e4332015-01-28 10:54:28 +08009214static int handle_pml_full(struct kvm_vcpu *vcpu)
9215{
9216 unsigned long exit_qualification;
9217
9218 trace_kvm_pml_full(vcpu->vcpu_id);
9219
9220 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9221
9222 /*
9223 * PML buffer FULL happened while executing iret from NMI,
9224 * "blocked by NMI" bit has to be set before next VM entry.
9225 */
9226 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009227 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009228 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9229 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9230 GUEST_INTR_STATE_NMI);
9231
9232 /*
9233 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9234 * here.., and there's no userspace involvement needed for PML.
9235 */
9236 return 1;
9237}
9238
Yunhong Jiang64672c92016-06-13 14:19:59 -07009239static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9240{
Sean Christophersond264ee02018-08-27 15:21:12 -07009241 if (!to_vmx(vcpu)->req_immediate_exit)
9242 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009243 return 1;
9244}
9245
Bandan Das41ab9372017-08-03 15:54:43 -04009246static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9247{
9248 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009249 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9250
9251 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009252 switch (address & VMX_EPTP_MT_MASK) {
9253 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009254 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009255 return false;
9256 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009257 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009258 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009259 return false;
9260 break;
9261 default:
9262 return false;
9263 }
9264
David Hildenbrandbb97a012017-08-10 23:15:28 +02009265 /* only 4 levels page-walk length are valid */
9266 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009267 return false;
9268
9269 /* Reserved bits should not be set */
9270 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9271 return false;
9272
9273 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009274 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009275 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009276 return false;
9277 }
9278
9279 return true;
9280}
9281
9282static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9283 struct vmcs12 *vmcs12)
9284{
9285 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9286 u64 address;
9287 bool accessed_dirty;
9288 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9289
9290 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9291 !nested_cpu_has_ept(vmcs12))
9292 return 1;
9293
9294 if (index >= VMFUNC_EPTP_ENTRIES)
9295 return 1;
9296
9297
9298 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9299 &address, index * 8, 8))
9300 return 1;
9301
David Hildenbrandbb97a012017-08-10 23:15:28 +02009302 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009303
9304 /*
9305 * If the (L2) guest does a vmfunc to the currently
9306 * active ept pointer, we don't have to do anything else
9307 */
9308 if (vmcs12->ept_pointer != address) {
9309 if (!valid_ept_address(vcpu, address))
9310 return 1;
9311
9312 kvm_mmu_unload(vcpu);
9313 mmu->ept_ad = accessed_dirty;
9314 mmu->base_role.ad_disabled = !accessed_dirty;
9315 vmcs12->ept_pointer = address;
9316 /*
9317 * TODO: Check what's the correct approach in case
9318 * mmu reload fails. Currently, we just let the next
9319 * reload potentially fail
9320 */
9321 kvm_mmu_reload(vcpu);
9322 }
9323
9324 return 0;
9325}
9326
Bandan Das2a499e42017-08-03 15:54:41 -04009327static int handle_vmfunc(struct kvm_vcpu *vcpu)
9328{
Bandan Das27c42a12017-08-03 15:54:42 -04009329 struct vcpu_vmx *vmx = to_vmx(vcpu);
9330 struct vmcs12 *vmcs12;
9331 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9332
9333 /*
9334 * VMFUNC is only supported for nested guests, but we always enable the
9335 * secondary control for simplicity; for non-nested mode, fake that we
9336 * didn't by injecting #UD.
9337 */
9338 if (!is_guest_mode(vcpu)) {
9339 kvm_queue_exception(vcpu, UD_VECTOR);
9340 return 1;
9341 }
9342
9343 vmcs12 = get_vmcs12(vcpu);
9344 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9345 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009346
9347 switch (function) {
9348 case 0:
9349 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9350 goto fail;
9351 break;
9352 default:
9353 goto fail;
9354 }
9355 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009356
9357fail:
9358 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9359 vmcs_read32(VM_EXIT_INTR_INFO),
9360 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009361 return 1;
9362}
9363
Sean Christopherson0b665d32018-08-14 09:33:34 -07009364static int handle_encls(struct kvm_vcpu *vcpu)
9365{
9366 /*
9367 * SGX virtualization is not yet supported. There is no software
9368 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9369 * to prevent the guest from executing ENCLS.
9370 */
9371 kvm_queue_exception(vcpu, UD_VECTOR);
9372 return 1;
9373}
9374
Nadav Har'El0140cae2011-05-25 23:06:28 +03009375/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009376 * The exit handlers return 1 if the exit was handled fully and guest execution
9377 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9378 * to be done to userspace and return 0.
9379 */
Mathias Krause772e0312012-08-30 01:30:19 +02009380static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009381 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9382 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009383 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009384 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009385 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009386 [EXIT_REASON_CR_ACCESS] = handle_cr,
9387 [EXIT_REASON_DR_ACCESS] = handle_dr,
9388 [EXIT_REASON_CPUID] = handle_cpuid,
9389 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9390 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9391 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9392 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009393 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009394 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009395 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009396 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009397 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009398 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009399 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009400 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009401 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009402 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009403 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009404 [EXIT_REASON_VMOFF] = handle_vmoff,
9405 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009406 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9407 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009408 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009409 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009410 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009411 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009412 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009413 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009414 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9415 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009416 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9417 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009418 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009419 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009420 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009421 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009422 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009423 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009424 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009425 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009426 [EXIT_REASON_XSAVES] = handle_xsaves,
9427 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009428 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009429 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009430 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009431 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009432 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009433};
9434
9435static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009436 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009437
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009438static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9439 struct vmcs12 *vmcs12)
9440{
9441 unsigned long exit_qualification;
9442 gpa_t bitmap, last_bitmap;
9443 unsigned int port;
9444 int size;
9445 u8 b;
9446
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009447 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009448 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009449
9450 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9451
9452 port = exit_qualification >> 16;
9453 size = (exit_qualification & 7) + 1;
9454
9455 last_bitmap = (gpa_t)-1;
9456 b = -1;
9457
9458 while (size > 0) {
9459 if (port < 0x8000)
9460 bitmap = vmcs12->io_bitmap_a;
9461 else if (port < 0x10000)
9462 bitmap = vmcs12->io_bitmap_b;
9463 else
Joe Perches1d804d02015-03-30 16:46:09 -07009464 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009465 bitmap += (port & 0x7fff) / 8;
9466
9467 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009468 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009469 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009470 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009471 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009472
9473 port++;
9474 size--;
9475 last_bitmap = bitmap;
9476 }
9477
Joe Perches1d804d02015-03-30 16:46:09 -07009478 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009479}
9480
Nadav Har'El644d7112011-05-25 23:12:35 +03009481/*
9482 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9483 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9484 * disinterest in the current event (read or write a specific MSR) by using an
9485 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9486 */
9487static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9488 struct vmcs12 *vmcs12, u32 exit_reason)
9489{
9490 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9491 gpa_t bitmap;
9492
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009493 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009494 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009495
9496 /*
9497 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9498 * for the four combinations of read/write and low/high MSR numbers.
9499 * First we need to figure out which of the four to use:
9500 */
9501 bitmap = vmcs12->msr_bitmap;
9502 if (exit_reason == EXIT_REASON_MSR_WRITE)
9503 bitmap += 2048;
9504 if (msr_index >= 0xc0000000) {
9505 msr_index -= 0xc0000000;
9506 bitmap += 1024;
9507 }
9508
9509 /* Then read the msr_index'th bit from this bitmap: */
9510 if (msr_index < 1024*8) {
9511 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009512 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009513 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009514 return 1 & (b >> (msr_index & 7));
9515 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009516 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009517}
9518
9519/*
9520 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9521 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9522 * intercept (via guest_host_mask etc.) the current event.
9523 */
9524static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9525 struct vmcs12 *vmcs12)
9526{
9527 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9528 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009529 int reg;
9530 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009531
9532 switch ((exit_qualification >> 4) & 3) {
9533 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009534 reg = (exit_qualification >> 8) & 15;
9535 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009536 switch (cr) {
9537 case 0:
9538 if (vmcs12->cr0_guest_host_mask &
9539 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 break;
9542 case 3:
9543 if ((vmcs12->cr3_target_count >= 1 &&
9544 vmcs12->cr3_target_value0 == val) ||
9545 (vmcs12->cr3_target_count >= 2 &&
9546 vmcs12->cr3_target_value1 == val) ||
9547 (vmcs12->cr3_target_count >= 3 &&
9548 vmcs12->cr3_target_value2 == val) ||
9549 (vmcs12->cr3_target_count >= 4 &&
9550 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009551 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009552 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009553 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009554 break;
9555 case 4:
9556 if (vmcs12->cr4_guest_host_mask &
9557 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009558 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009559 break;
9560 case 8:
9561 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009562 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009563 break;
9564 }
9565 break;
9566 case 2: /* clts */
9567 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9568 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009569 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009570 break;
9571 case 1: /* mov from cr */
9572 switch (cr) {
9573 case 3:
9574 if (vmcs12->cpu_based_vm_exec_control &
9575 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009576 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009577 break;
9578 case 8:
9579 if (vmcs12->cpu_based_vm_exec_control &
9580 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009581 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009582 break;
9583 }
9584 break;
9585 case 3: /* lmsw */
9586 /*
9587 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9588 * cr0. Other attempted changes are ignored, with no exit.
9589 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009590 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009591 if (vmcs12->cr0_guest_host_mask & 0xe &
9592 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009593 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009594 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9595 !(vmcs12->cr0_read_shadow & 0x1) &&
9596 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009597 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009598 break;
9599 }
Joe Perches1d804d02015-03-30 16:46:09 -07009600 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009601}
9602
Liran Alona7cde482018-06-23 02:35:10 +03009603static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9604 struct vmcs12 *vmcs12, gpa_t bitmap)
9605{
9606 u32 vmx_instruction_info;
9607 unsigned long field;
9608 u8 b;
9609
9610 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9611 return true;
9612
9613 /* Decode instruction info and find the field to access */
9614 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9615 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9616
9617 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9618 if (field >> 15)
9619 return true;
9620
9621 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9622 return true;
9623
9624 return 1 & (b >> (field & 7));
9625}
9626
Nadav Har'El644d7112011-05-25 23:12:35 +03009627/*
9628 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9629 * should handle it ourselves in L0 (and then continue L2). Only call this
9630 * when in is_guest_mode (L2).
9631 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009632static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009633{
Nadav Har'El644d7112011-05-25 23:12:35 +03009634 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9635 struct vcpu_vmx *vmx = to_vmx(vcpu);
9636 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9637
Jim Mattson4f350c62017-09-14 16:31:44 -07009638 if (vmx->nested.nested_run_pending)
9639 return false;
9640
9641 if (unlikely(vmx->fail)) {
9642 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9643 vmcs_read32(VM_INSTRUCTION_ERROR));
9644 return true;
9645 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009646
David Matlackc9f04402017-08-01 14:00:40 -07009647 /*
9648 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009649 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9650 * Page). The CPU may write to these pages via their host
9651 * physical address while L2 is running, bypassing any
9652 * address-translation-based dirty tracking (e.g. EPT write
9653 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009654 *
9655 * Mark them dirty on every exit from L2 to prevent them from
9656 * getting out of sync with dirty tracking.
9657 */
9658 nested_mark_vmcs12_pages_dirty(vcpu);
9659
Jim Mattson4f350c62017-09-14 16:31:44 -07009660 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9661 vmcs_readl(EXIT_QUALIFICATION),
9662 vmx->idt_vectoring_info,
9663 intr_info,
9664 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9665 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009666
9667 switch (exit_reason) {
9668 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009669 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009670 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009671 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009672 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009673 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009674 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009675 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009676 else if (is_debug(intr_info) &&
9677 vcpu->guest_debug &
9678 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9679 return false;
9680 else if (is_breakpoint(intr_info) &&
9681 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9682 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009683 return vmcs12->exception_bitmap &
9684 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9685 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009686 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009687 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009688 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009689 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009690 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009691 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009692 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009693 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009694 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009695 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009696 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009697 case EXIT_REASON_HLT:
9698 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9699 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009700 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009701 case EXIT_REASON_INVLPG:
9702 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9703 case EXIT_REASON_RDPMC:
9704 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009705 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009706 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009707 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009708 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009709 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009710 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009711 case EXIT_REASON_VMREAD:
9712 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9713 vmcs12->vmread_bitmap);
9714 case EXIT_REASON_VMWRITE:
9715 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9716 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009717 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9718 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009719 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009720 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009721 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009722 /*
9723 * VMX instructions trap unconditionally. This allows L1 to
9724 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9725 */
Joe Perches1d804d02015-03-30 16:46:09 -07009726 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009727 case EXIT_REASON_CR_ACCESS:
9728 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9729 case EXIT_REASON_DR_ACCESS:
9730 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9731 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009732 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009733 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9734 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009735 case EXIT_REASON_MSR_READ:
9736 case EXIT_REASON_MSR_WRITE:
9737 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9738 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009739 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009740 case EXIT_REASON_MWAIT_INSTRUCTION:
9741 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009742 case EXIT_REASON_MONITOR_TRAP_FLAG:
9743 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009744 case EXIT_REASON_MONITOR_INSTRUCTION:
9745 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9746 case EXIT_REASON_PAUSE_INSTRUCTION:
9747 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9748 nested_cpu_has2(vmcs12,
9749 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9750 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009751 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009752 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009753 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009754 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009755 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009756 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009757 /*
9758 * The controls for "virtualize APIC accesses," "APIC-
9759 * register virtualization," and "virtual-interrupt
9760 * delivery" only come from vmcs12.
9761 */
Joe Perches1d804d02015-03-30 16:46:09 -07009762 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009763 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009764 /*
9765 * L0 always deals with the EPT violation. If nested EPT is
9766 * used, and the nested mmu code discovers that the address is
9767 * missing in the guest EPT table (EPT12), the EPT violation
9768 * will be injected with nested_ept_inject_page_fault()
9769 */
Joe Perches1d804d02015-03-30 16:46:09 -07009770 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009771 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009772 /*
9773 * L2 never uses directly L1's EPT, but rather L0's own EPT
9774 * table (shadow on EPT) or a merged EPT table that L0 built
9775 * (EPT on EPT). So any problems with the structure of the
9776 * table is L0's fault.
9777 */
Joe Perches1d804d02015-03-30 16:46:09 -07009778 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009779 case EXIT_REASON_INVPCID:
9780 return
9781 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9782 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009783 case EXIT_REASON_WBINVD:
9784 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9785 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009786 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009787 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9788 /*
9789 * This should never happen, since it is not possible to
9790 * set XSS to a non-zero value---neither in L1 nor in L2.
9791 * If if it were, XSS would have to be checked against
9792 * the XSS exit bitmap in vmcs12.
9793 */
9794 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009795 case EXIT_REASON_PREEMPTION_TIMER:
9796 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009797 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009798 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009799 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009800 case EXIT_REASON_VMFUNC:
9801 /* VM functions are emulated through L2->L0 vmexits. */
9802 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009803 case EXIT_REASON_ENCLS:
9804 /* SGX is never exposed to L1 */
9805 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009806 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009807 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009808 }
9809}
9810
Paolo Bonzini7313c692017-07-27 10:31:25 +02009811static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9812{
9813 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9814
9815 /*
9816 * At this point, the exit interruption info in exit_intr_info
9817 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9818 * we need to query the in-kernel LAPIC.
9819 */
9820 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9821 if ((exit_intr_info &
9822 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9823 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9824 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9825 vmcs12->vm_exit_intr_error_code =
9826 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9827 }
9828
9829 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9830 vmcs_readl(EXIT_QUALIFICATION));
9831 return 1;
9832}
9833
Avi Kivity586f9602010-11-18 13:09:54 +02009834static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9835{
9836 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9837 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9838}
9839
Kai Huanga3eaa862015-11-04 13:46:05 +08009840static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009841{
Kai Huanga3eaa862015-11-04 13:46:05 +08009842 if (vmx->pml_pg) {
9843 __free_page(vmx->pml_pg);
9844 vmx->pml_pg = NULL;
9845 }
Kai Huang843e4332015-01-28 10:54:28 +08009846}
9847
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009848static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009849{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009850 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009851 u64 *pml_buf;
9852 u16 pml_idx;
9853
9854 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9855
9856 /* Do nothing if PML buffer is empty */
9857 if (pml_idx == (PML_ENTITY_NUM - 1))
9858 return;
9859
9860 /* PML index always points to next available PML buffer entity */
9861 if (pml_idx >= PML_ENTITY_NUM)
9862 pml_idx = 0;
9863 else
9864 pml_idx++;
9865
9866 pml_buf = page_address(vmx->pml_pg);
9867 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9868 u64 gpa;
9869
9870 gpa = pml_buf[pml_idx];
9871 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009872 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009873 }
9874
9875 /* reset PML index */
9876 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9877}
9878
9879/*
9880 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9881 * Called before reporting dirty_bitmap to userspace.
9882 */
9883static void kvm_flush_pml_buffers(struct kvm *kvm)
9884{
9885 int i;
9886 struct kvm_vcpu *vcpu;
9887 /*
9888 * We only need to kick vcpu out of guest mode here, as PML buffer
9889 * is flushed at beginning of all VMEXITs, and it's obvious that only
9890 * vcpus running in guest are possible to have unflushed GPAs in PML
9891 * buffer.
9892 */
9893 kvm_for_each_vcpu(i, vcpu, kvm)
9894 kvm_vcpu_kick(vcpu);
9895}
9896
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009897static void vmx_dump_sel(char *name, uint32_t sel)
9898{
9899 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009900 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009901 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9902 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9903 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9904}
9905
9906static void vmx_dump_dtsel(char *name, uint32_t limit)
9907{
9908 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9909 name, vmcs_read32(limit),
9910 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9911}
9912
9913static void dump_vmcs(void)
9914{
9915 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9916 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9917 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9918 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9919 u32 secondary_exec_control = 0;
9920 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009921 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009922 int i, n;
9923
9924 if (cpu_has_secondary_exec_ctrls())
9925 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9926
9927 pr_err("*** Guest State ***\n");
9928 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9929 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9930 vmcs_readl(CR0_GUEST_HOST_MASK));
9931 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9932 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9933 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9934 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9935 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9936 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009937 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9938 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9939 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9940 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009941 }
9942 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9943 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9944 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9945 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9946 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9947 vmcs_readl(GUEST_SYSENTER_ESP),
9948 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9949 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9950 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9951 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9952 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9953 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9954 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9955 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9956 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9957 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9958 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9959 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9960 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009961 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9962 efer, vmcs_read64(GUEST_IA32_PAT));
9963 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9964 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009965 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009966 if (cpu_has_load_perf_global_ctrl &&
9967 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009968 pr_err("PerfGlobCtl = 0x%016llx\n",
9969 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009970 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009971 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009972 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9973 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9974 vmcs_read32(GUEST_ACTIVITY_STATE));
9975 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9976 pr_err("InterruptStatus = %04x\n",
9977 vmcs_read16(GUEST_INTR_STATUS));
9978
9979 pr_err("*** Host State ***\n");
9980 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9981 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9982 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9983 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9984 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9985 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9986 vmcs_read16(HOST_TR_SELECTOR));
9987 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9988 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9989 vmcs_readl(HOST_TR_BASE));
9990 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9991 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9992 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9993 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9994 vmcs_readl(HOST_CR4));
9995 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9996 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9997 vmcs_read32(HOST_IA32_SYSENTER_CS),
9998 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9999 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010000 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
10001 vmcs_read64(HOST_IA32_EFER),
10002 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010003 if (cpu_has_load_perf_global_ctrl &&
10004 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010005 pr_err("PerfGlobCtl = 0x%016llx\n",
10006 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010007
10008 pr_err("*** Control State ***\n");
10009 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
10010 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
10011 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
10012 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
10013 vmcs_read32(EXCEPTION_BITMAP),
10014 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10015 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10016 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10017 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10018 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10019 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10020 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10021 vmcs_read32(VM_EXIT_INTR_INFO),
10022 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10023 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10024 pr_err(" reason=%08x qualification=%016lx\n",
10025 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10026 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10027 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10028 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010029 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010030 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010031 pr_err("TSC Multiplier = 0x%016llx\n",
10032 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010033 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10034 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10035 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10036 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10037 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010038 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010039 n = vmcs_read32(CR3_TARGET_COUNT);
10040 for (i = 0; i + 1 < n; i += 4)
10041 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10042 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10043 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10044 if (i < n)
10045 pr_err("CR3 target%u=%016lx\n",
10046 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10047 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10048 pr_err("PLE Gap=%08x Window=%08x\n",
10049 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10050 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10051 pr_err("Virtual processor ID = 0x%04x\n",
10052 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10053}
10054
Avi Kivity6aa8b732006-12-10 02:21:36 -080010055/*
10056 * The guest has exited. See if we can fix it or if we need userspace
10057 * assistance.
10058 */
Avi Kivity851ba692009-08-24 11:10:17 +030010059static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010060{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010062 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010063 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010064
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010065 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10066
Kai Huang843e4332015-01-28 10:54:28 +080010067 /*
10068 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10069 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10070 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10071 * mode as if vcpus is in root mode, the PML buffer must has been
10072 * flushed already.
10073 */
10074 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010075 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010076
Mohammed Gamal80ced182009-09-01 12:48:18 +020010077 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010078 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010079 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010080
Paolo Bonzini7313c692017-07-27 10:31:25 +020010081 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10082 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010083
Mohammed Gamal51207022010-05-31 22:40:54 +030010084 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010085 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010086 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10087 vcpu->run->fail_entry.hardware_entry_failure_reason
10088 = exit_reason;
10089 return 0;
10090 }
10091
Avi Kivity29bd8a72007-09-10 17:27:03 +030010092 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010093 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10094 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010095 = vmcs_read32(VM_INSTRUCTION_ERROR);
10096 return 0;
10097 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010098
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010099 /*
10100 * Note:
10101 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10102 * delivery event since it indicates guest is accessing MMIO.
10103 * The vm-exit can be triggered again after return to guest that
10104 * will cause infinite loop.
10105 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010106 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010107 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010108 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010109 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010110 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10111 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10112 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010113 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010114 vcpu->run->internal.data[0] = vectoring_info;
10115 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010116 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10117 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10118 vcpu->run->internal.ndata++;
10119 vcpu->run->internal.data[3] =
10120 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10121 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010122 return 0;
10123 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010124
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010125 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010126 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10127 if (vmx_interrupt_allowed(vcpu)) {
10128 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10129 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10130 vcpu->arch.nmi_pending) {
10131 /*
10132 * This CPU don't support us in finding the end of an
10133 * NMI-blocked window if the guest runs with IRQs
10134 * disabled. So we pull the trigger after 1 s of
10135 * futile waiting, but inform the user about this.
10136 */
10137 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10138 "state on VCPU %d after 1 s timeout\n",
10139 __func__, vcpu->vcpu_id);
10140 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10141 }
10142 }
10143
Avi Kivity6aa8b732006-12-10 02:21:36 -080010144 if (exit_reason < kvm_vmx_max_exit_handlers
10145 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010146 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010147 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010148 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10149 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010150 kvm_queue_exception(vcpu, UD_VECTOR);
10151 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010152 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010153}
10154
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010155/*
10156 * Software based L1D cache flush which is used when microcode providing
10157 * the cache control MSR is not loaded.
10158 *
10159 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10160 * flush it is required to read in 64 KiB because the replacement algorithm
10161 * is not exactly LRU. This could be sized at runtime via topology
10162 * information but as all relevant affected CPUs have 32KiB L1D cache size
10163 * there is no point in doing so.
10164 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010165static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010166{
10167 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010168
10169 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010170 * This code is only executed when the the flush mode is 'cond' or
10171 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010172 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010173 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010174 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010175
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010176 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010177 * Clear the per-vcpu flush bit, it gets set again
10178 * either from vcpu_run() or from one of the unsafe
10179 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010180 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010181 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010182 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010183
10184 /*
10185 * Clear the per-cpu flush bit, it gets set again from
10186 * the interrupt handlers.
10187 */
10188 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10189 kvm_clear_cpu_l1tf_flush_l1d();
10190
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010191 if (!flush_l1d)
10192 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010193 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010194
10195 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010196
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010197 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10198 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10199 return;
10200 }
10201
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010202 asm volatile(
10203 /* First ensure the pages are in the TLB */
10204 "xorl %%eax, %%eax\n"
10205 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010206 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010207 "addl $4096, %%eax\n\t"
10208 "cmpl %%eax, %[size]\n\t"
10209 "jne .Lpopulate_tlb\n\t"
10210 "xorl %%eax, %%eax\n\t"
10211 "cpuid\n\t"
10212 /* Now fill the cache */
10213 "xorl %%eax, %%eax\n"
10214 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010215 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010216 "addl $64, %%eax\n\t"
10217 "cmpl %%eax, %[size]\n\t"
10218 "jne .Lfill_cache\n\t"
10219 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010220 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010221 [size] "r" (size)
10222 : "eax", "ebx", "ecx", "edx");
10223}
10224
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010225static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010226{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010227 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10228
10229 if (is_guest_mode(vcpu) &&
10230 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10231 return;
10232
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010233 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010234 vmcs_write32(TPR_THRESHOLD, 0);
10235 return;
10236 }
10237
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010238 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010239}
10240
Jim Mattson8d860bb2018-05-09 16:56:05 -040010241static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010242{
10243 u32 sec_exec_control;
10244
Jim Mattson8d860bb2018-05-09 16:56:05 -040010245 if (!lapic_in_kernel(vcpu))
10246 return;
10247
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010248 /* Postpone execution until vmcs01 is the current VMCS. */
10249 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010250 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010251 return;
10252 }
10253
Paolo Bonzini35754c92015-07-29 12:05:37 +020010254 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +080010255 return;
10256
10257 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010258 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10259 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010260
Jim Mattson8d860bb2018-05-09 16:56:05 -040010261 switch (kvm_get_apic_mode(vcpu)) {
10262 case LAPIC_MODE_INVALID:
10263 WARN_ONCE(true, "Invalid local APIC state");
10264 case LAPIC_MODE_DISABLED:
10265 break;
10266 case LAPIC_MODE_XAPIC:
10267 if (flexpriority_enabled) {
10268 sec_exec_control |=
10269 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10270 vmx_flush_tlb(vcpu, true);
10271 }
10272 break;
10273 case LAPIC_MODE_X2APIC:
10274 if (cpu_has_vmx_virtualize_x2apic_mode())
10275 sec_exec_control |=
10276 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10277 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010278 }
10279 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10280
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010281 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010282}
10283
Tang Chen38b99172014-09-24 15:57:54 +080010284static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10285{
Jim Mattsonab5df312018-05-09 17:02:03 -040010286 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010287 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010288 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010289 }
Tang Chen38b99172014-09-24 15:57:54 +080010290}
10291
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010292static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010293{
10294 u16 status;
10295 u8 old;
10296
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010297 if (max_isr == -1)
10298 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010299
10300 status = vmcs_read16(GUEST_INTR_STATUS);
10301 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010302 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010303 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010304 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010305 vmcs_write16(GUEST_INTR_STATUS, status);
10306 }
10307}
10308
10309static void vmx_set_rvi(int vector)
10310{
10311 u16 status;
10312 u8 old;
10313
Wei Wang4114c272014-11-05 10:53:43 +080010314 if (vector == -1)
10315 vector = 0;
10316
Yang Zhangc7c9c562013-01-25 10:18:51 +080010317 status = vmcs_read16(GUEST_INTR_STATUS);
10318 old = (u8)status & 0xff;
10319 if ((u8)vector != old) {
10320 status &= ~0xff;
10321 status |= (u8)vector;
10322 vmcs_write16(GUEST_INTR_STATUS, status);
10323 }
10324}
10325
10326static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10327{
Liran Alon851c1a182017-12-24 18:12:56 +020010328 /*
10329 * When running L2, updating RVI is only relevant when
10330 * vmcs12 virtual-interrupt-delivery enabled.
10331 * However, it can be enabled only when L1 also
10332 * intercepts external-interrupts and in that case
10333 * we should not update vmcs02 RVI but instead intercept
10334 * interrupt. Therefore, do nothing when running L2.
10335 */
10336 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010337 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010338}
10339
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010340static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010341{
10342 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010343 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010344 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010345
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010346 WARN_ON(!vcpu->arch.apicv_active);
10347 if (pi_test_on(&vmx->pi_desc)) {
10348 pi_clear_on(&vmx->pi_desc);
10349 /*
10350 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10351 * But on x86 this is just a compiler barrier anyway.
10352 */
10353 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010354 max_irr_updated =
10355 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10356
10357 /*
10358 * If we are running L2 and L1 has a new pending interrupt
10359 * which can be injected, we should re-evaluate
10360 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010361 * If L1 intercepts external-interrupts, we should
10362 * exit from L2 to L1. Otherwise, interrupt should be
10363 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010364 */
Liran Alon851c1a182017-12-24 18:12:56 +020010365 if (is_guest_mode(vcpu) && max_irr_updated) {
10366 if (nested_exit_on_intr(vcpu))
10367 kvm_vcpu_exiting_guest_mode(vcpu);
10368 else
10369 kvm_make_request(KVM_REQ_EVENT, vcpu);
10370 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010371 } else {
10372 max_irr = kvm_lapic_find_highest_irr(vcpu);
10373 }
10374 vmx_hwapic_irr_update(vcpu, max_irr);
10375 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010376}
10377
Andrey Smetanin63086302015-11-10 15:36:32 +030010378static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010379{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010380 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010381 return;
10382
Yang Zhangc7c9c562013-01-25 10:18:51 +080010383 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10384 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10385 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10386 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10387}
10388
Paolo Bonzini967235d2016-12-19 14:03:45 +010010389static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10390{
10391 struct vcpu_vmx *vmx = to_vmx(vcpu);
10392
10393 pi_clear_on(&vmx->pi_desc);
10394 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10395}
10396
Avi Kivity51aa01d2010-07-20 14:31:20 +030010397static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010398{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010399 u32 exit_intr_info = 0;
10400 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010401
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010402 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10403 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010404 return;
10405
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010406 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10407 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10408 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010409
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010410 /* if exit due to PF check for async PF */
10411 if (is_page_fault(exit_intr_info))
10412 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10413
Andi Kleena0861c02009-06-08 17:37:09 +080010414 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010415 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10416 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010417 kvm_machine_check();
10418
Gleb Natapov20f65982009-05-11 13:35:55 +030010419 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010420 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010421 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010422 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010423 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010424 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010425}
Gleb Natapov20f65982009-05-11 13:35:55 +030010426
Yang Zhanga547c6d2013-04-11 19:25:10 +080010427static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10428{
10429 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10430
Yang Zhanga547c6d2013-04-11 19:25:10 +080010431 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10432 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10433 unsigned int vector;
10434 unsigned long entry;
10435 gate_desc *desc;
10436 struct vcpu_vmx *vmx = to_vmx(vcpu);
10437#ifdef CONFIG_X86_64
10438 unsigned long tmp;
10439#endif
10440
10441 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10442 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010443 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010444 asm volatile(
10445#ifdef CONFIG_X86_64
10446 "mov %%" _ASM_SP ", %[sp]\n\t"
10447 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10448 "push $%c[ss]\n\t"
10449 "push %[sp]\n\t"
10450#endif
10451 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010452 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010453 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010454 :
10455#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010456 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010457#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010458 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010459 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010460 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010461 [ss]"i"(__KERNEL_DS),
10462 [cs]"i"(__KERNEL_CS)
10463 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010464 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010465}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010466STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010467
Tom Lendackybc226f02018-05-10 22:06:39 +020010468static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010469{
Tom Lendackybc226f02018-05-10 22:06:39 +020010470 switch (index) {
10471 case MSR_IA32_SMBASE:
10472 /*
10473 * We cannot do SMM unless we can run the guest in big
10474 * real mode.
10475 */
10476 return enable_unrestricted_guest || emulate_invalid_guest_state;
10477 case MSR_AMD64_VIRT_SPEC_CTRL:
10478 /* This is AMD only. */
10479 return false;
10480 default:
10481 return true;
10482 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010483}
10484
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010485static bool vmx_mpx_supported(void)
10486{
10487 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10488 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10489}
10490
Wanpeng Li55412b22014-12-02 19:21:30 +080010491static bool vmx_xsaves_supported(void)
10492{
10493 return vmcs_config.cpu_based_2nd_exec_ctrl &
10494 SECONDARY_EXEC_XSAVES;
10495}
10496
Avi Kivity51aa01d2010-07-20 14:31:20 +030010497static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10498{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010499 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010500 bool unblock_nmi;
10501 u8 vector;
10502 bool idtv_info_valid;
10503
10504 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010505
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010506 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010507 if (vmx->loaded_vmcs->nmi_known_unmasked)
10508 return;
10509 /*
10510 * Can't use vmx->exit_intr_info since we're not sure what
10511 * the exit reason is.
10512 */
10513 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10514 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10515 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10516 /*
10517 * SDM 3: 27.7.1.2 (September 2008)
10518 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10519 * a guest IRET fault.
10520 * SDM 3: 23.2.2 (September 2008)
10521 * Bit 12 is undefined in any of the following cases:
10522 * If the VM exit sets the valid bit in the IDT-vectoring
10523 * information field.
10524 * If the VM exit is due to a double fault.
10525 */
10526 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10527 vector != DF_VECTOR && !idtv_info_valid)
10528 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10529 GUEST_INTR_STATE_NMI);
10530 else
10531 vmx->loaded_vmcs->nmi_known_unmasked =
10532 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10533 & GUEST_INTR_STATE_NMI);
10534 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10535 vmx->loaded_vmcs->vnmi_blocked_time +=
10536 ktime_to_ns(ktime_sub(ktime_get(),
10537 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010538}
10539
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010540static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010541 u32 idt_vectoring_info,
10542 int instr_len_field,
10543 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010544{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010545 u8 vector;
10546 int type;
10547 bool idtv_info_valid;
10548
10549 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010550
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010551 vcpu->arch.nmi_injected = false;
10552 kvm_clear_exception_queue(vcpu);
10553 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554
10555 if (!idtv_info_valid)
10556 return;
10557
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010558 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010559
Avi Kivity668f6122008-07-02 09:28:55 +030010560 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10561 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010562
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010563 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010564 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010565 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010566 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010567 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010568 * Clear bit "block by NMI" before VM entry if a NMI
10569 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010570 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010571 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010572 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010573 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010574 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010575 /* fall through */
10576 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010577 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010578 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010579 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010580 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010581 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010582 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010583 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010584 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010585 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010586 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010587 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010588 break;
10589 default:
10590 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010591 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010592}
10593
Avi Kivity83422e12010-07-20 14:43:23 +030010594static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10595{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010596 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010597 VM_EXIT_INSTRUCTION_LEN,
10598 IDT_VECTORING_ERROR_CODE);
10599}
10600
Avi Kivityb463a6f2010-07-20 15:06:17 +030010601static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10602{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010603 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010604 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10605 VM_ENTRY_INSTRUCTION_LEN,
10606 VM_ENTRY_EXCEPTION_ERROR_CODE);
10607
10608 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10609}
10610
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010611static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10612{
10613 int i, nr_msrs;
10614 struct perf_guest_switch_msr *msrs;
10615
10616 msrs = perf_guest_get_msrs(&nr_msrs);
10617
10618 if (!msrs)
10619 return;
10620
10621 for (i = 0; i < nr_msrs; i++)
10622 if (msrs[i].host == msrs[i].guest)
10623 clear_atomic_switch_msr(vmx, msrs[i].msr);
10624 else
10625 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010626 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010627}
10628
Sean Christophersonf459a702018-08-27 15:21:11 -070010629static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10630{
10631 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10632 if (!vmx->loaded_vmcs->hv_timer_armed)
10633 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10634 PIN_BASED_VMX_PREEMPTION_TIMER);
10635 vmx->loaded_vmcs->hv_timer_armed = true;
10636}
10637
10638static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010639{
10640 struct vcpu_vmx *vmx = to_vmx(vcpu);
10641 u64 tscl;
10642 u32 delta_tsc;
10643
Sean Christophersond264ee02018-08-27 15:21:12 -070010644 if (vmx->req_immediate_exit) {
10645 vmx_arm_hv_timer(vmx, 0);
10646 return;
10647 }
10648
Sean Christophersonf459a702018-08-27 15:21:11 -070010649 if (vmx->hv_deadline_tsc != -1) {
10650 tscl = rdtsc();
10651 if (vmx->hv_deadline_tsc > tscl)
10652 /* set_hv_timer ensures the delta fits in 32-bits */
10653 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10654 cpu_preemption_timer_multi);
10655 else
10656 delta_tsc = 0;
10657
10658 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010659 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010660 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010661
Sean Christophersonf459a702018-08-27 15:21:11 -070010662 if (vmx->loaded_vmcs->hv_timer_armed)
10663 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10664 PIN_BASED_VMX_PREEMPTION_TIMER);
10665 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010666}
10667
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010668static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010669{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010671 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010672
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010673 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010674 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010675 vmx->loaded_vmcs->soft_vnmi_blocked))
10676 vmx->loaded_vmcs->entry_time = ktime_get();
10677
Avi Kivity104f2262010-11-18 13:12:52 +020010678 /* Don't enter VMX if guest state is invalid, let the exit handler
10679 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010680 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010681 return;
10682
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010683 if (vmx->ple_window_dirty) {
10684 vmx->ple_window_dirty = false;
10685 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10686 }
10687
Abel Gordon012f83c2013-04-18 14:39:25 +030010688 if (vmx->nested.sync_shadow_vmcs) {
10689 copy_vmcs12_to_shadow(vmx);
10690 vmx->nested.sync_shadow_vmcs = false;
10691 }
10692
Avi Kivity104f2262010-11-18 13:12:52 +020010693 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10694 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10695 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10696 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10697
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010698 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010699 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010700 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010701 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010702 }
10703
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010704 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010705 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010706 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010707 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010708 }
10709
Avi Kivity104f2262010-11-18 13:12:52 +020010710 /* When single-stepping over STI and MOV SS, we must clear the
10711 * corresponding interruptibility bits in the guest state. Otherwise
10712 * vmentry fails as it then expects bit 14 (BS) in pending debug
10713 * exceptions being set, but that's not correct for the guest debugging
10714 * case. */
10715 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10716 vmx_set_interrupt_shadow(vcpu, 0);
10717
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010718 if (static_cpu_has(X86_FEATURE_PKU) &&
10719 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10720 vcpu->arch.pkru != vmx->host_pkru)
10721 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010722
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010723 atomic_switch_perf_msrs(vmx);
10724
Sean Christophersonf459a702018-08-27 15:21:11 -070010725 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010726
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010727 /*
10728 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10729 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10730 * is no need to worry about the conditional branch over the wrmsr
10731 * being speculatively taken.
10732 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010733 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010734
Nadav Har'Eld462b812011-05-24 15:26:10 +030010735 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010736
10737 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10738 (unsigned long)&current_evmcs->host_rsp : 0;
10739
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010740 if (static_branch_unlikely(&vmx_l1d_should_flush))
10741 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010742
Avi Kivity104f2262010-11-18 13:12:52 +020010743 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010744 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010745 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10746 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10747 "push %%" _ASM_CX " \n\t"
10748 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010749 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010750 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010751 /* Avoid VMWRITE when Enlightened VMCS is in use */
10752 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10753 "jz 2f \n\t"
10754 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10755 "jmp 1f \n\t"
10756 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010757 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010758 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010759 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010760 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10761 "mov %%cr2, %%" _ASM_DX " \n\t"
10762 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010763 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010764 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010765 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010766 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010767 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010768 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010769 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10770 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10771 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10772 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10773 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10774 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010775#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010776 "mov %c[r8](%0), %%r8 \n\t"
10777 "mov %c[r9](%0), %%r9 \n\t"
10778 "mov %c[r10](%0), %%r10 \n\t"
10779 "mov %c[r11](%0), %%r11 \n\t"
10780 "mov %c[r12](%0), %%r12 \n\t"
10781 "mov %c[r13](%0), %%r13 \n\t"
10782 "mov %c[r14](%0), %%r14 \n\t"
10783 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010784#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010785 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010786
Avi Kivity6aa8b732006-12-10 02:21:36 -080010787 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010788 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010789 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010790 "jmp 2f \n\t"
10791 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10792 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010793 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010794 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010795 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010796 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010797 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10798 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10799 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10800 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10801 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10802 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10803 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010804#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010805 "mov %%r8, %c[r8](%0) \n\t"
10806 "mov %%r9, %c[r9](%0) \n\t"
10807 "mov %%r10, %c[r10](%0) \n\t"
10808 "mov %%r11, %c[r11](%0) \n\t"
10809 "mov %%r12, %c[r12](%0) \n\t"
10810 "mov %%r13, %c[r13](%0) \n\t"
10811 "mov %%r14, %c[r14](%0) \n\t"
10812 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010813 "xor %%r8d, %%r8d \n\t"
10814 "xor %%r9d, %%r9d \n\t"
10815 "xor %%r10d, %%r10d \n\t"
10816 "xor %%r11d, %%r11d \n\t"
10817 "xor %%r12d, %%r12d \n\t"
10818 "xor %%r13d, %%r13d \n\t"
10819 "xor %%r14d, %%r14d \n\t"
10820 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010821#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010822 "mov %%cr2, %%" _ASM_AX " \n\t"
10823 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010824
Jim Mattson0cb5b302018-01-03 14:31:38 -080010825 "xor %%eax, %%eax \n\t"
10826 "xor %%ebx, %%ebx \n\t"
10827 "xor %%esi, %%esi \n\t"
10828 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010829 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010830 ".pushsection .rodata \n\t"
10831 ".global vmx_return \n\t"
10832 "vmx_return: " _ASM_PTR " 2b \n\t"
10833 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010834 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010835 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010836 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010837 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010838 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10839 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10840 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10841 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10842 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10843 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10844 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010845#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010846 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10847 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10848 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10849 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10850 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10851 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10852 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10853 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010854#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010855 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10856 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010857 : "cc", "memory"
10858#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010859 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010860 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010861#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010862 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010863#endif
10864 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010865
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010866 /*
10867 * We do not use IBRS in the kernel. If this vCPU has used the
10868 * SPEC_CTRL MSR it may have left it on; save the value and
10869 * turn it off. This is much more efficient than blindly adding
10870 * it to the atomic save/restore list. Especially as the former
10871 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10872 *
10873 * For non-nested case:
10874 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10875 * save it.
10876 *
10877 * For nested case:
10878 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10879 * save it.
10880 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010881 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010882 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010883
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010884 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010885
David Woodhouse117cc7a2018-01-12 11:11:27 +000010886 /* Eliminate branch target predictions from guest mode */
10887 vmexit_fill_RSB();
10888
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010889 /* All fields are clean at this point */
10890 if (static_branch_unlikely(&enable_evmcs))
10891 current_evmcs->hv_clean_fields |=
10892 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10893
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010894 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010895 if (vmx->host_debugctlmsr)
10896 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010897
Avi Kivityaa67f602012-08-01 16:48:03 +030010898#ifndef CONFIG_X86_64
10899 /*
10900 * The sysexit path does not restore ds/es, so we must set them to
10901 * a reasonable value ourselves.
10902 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010903 * We can't defer this to vmx_prepare_switch_to_host() since that
10904 * function may be executed in interrupt context, which saves and
10905 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010906 */
10907 loadsegment(ds, __USER_DS);
10908 loadsegment(es, __USER_DS);
10909#endif
10910
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010911 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010912 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010913 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010914 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010915 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010916 vcpu->arch.regs_dirty = 0;
10917
Gleb Natapove0b890d2013-09-25 12:51:33 +030010918 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010919 * eager fpu is enabled if PKEY is supported and CR4 is switched
10920 * back on host, so it is safe to read guest PKRU from current
10921 * XSAVE.
10922 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010923 if (static_cpu_has(X86_FEATURE_PKU) &&
10924 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10925 vcpu->arch.pkru = __read_pkru();
10926 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010927 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010928 }
10929
Gleb Natapove0b890d2013-09-25 12:51:33 +030010930 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010931 vmx->idt_vectoring_info = 0;
10932
10933 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10934 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10935 return;
10936
10937 vmx->loaded_vmcs->launched = 1;
10938 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010939
Avi Kivity51aa01d2010-07-20 14:31:20 +030010940 vmx_complete_atomic_exit(vmx);
10941 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010942 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010943}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010944STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010945
Sean Christopherson434a1e92018-03-20 12:17:18 -070010946static struct kvm *vmx_vm_alloc(void)
10947{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010948 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010949 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010950}
10951
10952static void vmx_vm_free(struct kvm *kvm)
10953{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010954 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010955}
10956
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010957static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010958{
10959 struct vcpu_vmx *vmx = to_vmx(vcpu);
10960 int cpu;
10961
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010962 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010963 return;
10964
10965 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010966 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010967 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010968 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010969 put_cpu();
10970}
10971
Jim Mattson2f1fe812016-07-08 15:36:06 -070010972/*
10973 * Ensure that the current vmcs of the logical processor is the
10974 * vmcs01 of the vcpu before calling free_nested().
10975 */
10976static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10977{
10978 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010979
Christoffer Dallec7660c2017-12-04 21:35:23 +010010980 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010981 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010982 free_nested(vmx);
10983 vcpu_put(vcpu);
10984}
10985
Avi Kivity6aa8b732006-12-10 02:21:36 -080010986static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10987{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010988 struct vcpu_vmx *vmx = to_vmx(vcpu);
10989
Kai Huang843e4332015-01-28 10:54:28 +080010990 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010991 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010992 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010993 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010994 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010995 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010996 kfree(vmx->guest_msrs);
10997 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010998 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010999}
11000
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011001static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080011002{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011003 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100011004 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011005 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011006 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011007
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011008 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011009 return ERR_PTR(-ENOMEM);
11010
Wanpeng Li991e7a02015-09-16 17:30:05 +080011011 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011012
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011013 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11014 if (err)
11015 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011016
Peter Feiner4e595162016-07-07 14:49:58 -070011017 err = -ENOMEM;
11018
11019 /*
11020 * If PML is turned on, failure on enabling PML just results in failure
11021 * of creating the vcpu, therefore we can simplify PML logic (by
11022 * avoiding dealing with cases, such as enabling PML partially on vcpus
11023 * for the guest, etc.
11024 */
11025 if (enable_pml) {
11026 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11027 if (!vmx->pml_pg)
11028 goto uninit_vcpu;
11029 }
11030
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011031 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011032 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11033 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011034
Peter Feiner4e595162016-07-07 14:49:58 -070011035 if (!vmx->guest_msrs)
11036 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011037
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011038 err = alloc_loaded_vmcs(&vmx->vmcs01);
11039 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011040 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011041
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011042 msr_bitmap = vmx->vmcs01.msr_bitmap;
11043 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11044 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11045 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11046 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11047 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11048 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11049 vmx->msr_bitmap_mode = 0;
11050
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011051 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011052 cpu = get_cpu();
11053 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011054 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011055 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011056 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011057 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011058 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011059 err = alloc_apic_access_page(kvm);
11060 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011061 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011062 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011063
Sean Christophersone90008d2018-03-05 12:04:37 -080011064 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011065 err = init_rmode_identity_map(kvm);
11066 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011067 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011068 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011069
Roman Kagan63aff652018-07-19 21:59:07 +030011070 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011071 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11072 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011073
Wincy Van705699a2015-02-03 23:58:17 +080011074 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011075 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011076
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011077 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11078
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011079 /*
11080 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11081 * or POSTED_INTR_WAKEUP_VECTOR.
11082 */
11083 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11084 vmx->pi_desc.sn = 1;
11085
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011086 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011087
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011088free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011089 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011090free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011091 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011092free_pml:
11093 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011094uninit_vcpu:
11095 kvm_vcpu_uninit(&vmx->vcpu);
11096free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011097 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011098 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011099 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011100}
11101
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011102#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"
11103#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 -040011104
Wanpeng Lib31c1142018-03-12 04:53:04 -070011105static int vmx_vm_init(struct kvm *kvm)
11106{
Tianyu Lan877ad952018-07-19 08:40:23 +000011107 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11108
Wanpeng Lib31c1142018-03-12 04:53:04 -070011109 if (!ple_gap)
11110 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011111
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011112 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11113 switch (l1tf_mitigation) {
11114 case L1TF_MITIGATION_OFF:
11115 case L1TF_MITIGATION_FLUSH_NOWARN:
11116 /* 'I explicitly don't care' is set */
11117 break;
11118 case L1TF_MITIGATION_FLUSH:
11119 case L1TF_MITIGATION_FLUSH_NOSMT:
11120 case L1TF_MITIGATION_FULL:
11121 /*
11122 * Warn upon starting the first VM in a potentially
11123 * insecure environment.
11124 */
11125 if (cpu_smt_control == CPU_SMT_ENABLED)
11126 pr_warn_once(L1TF_MSG_SMT);
11127 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11128 pr_warn_once(L1TF_MSG_L1D);
11129 break;
11130 case L1TF_MITIGATION_FULL_FORCE:
11131 /* Flush is enforced */
11132 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011133 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011134 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011135 return 0;
11136}
11137
Yang, Sheng002c7f72007-07-31 14:23:01 +030011138static void __init vmx_check_processor_compat(void *rtn)
11139{
11140 struct vmcs_config vmcs_conf;
11141
11142 *(int *)rtn = 0;
11143 if (setup_vmcs_config(&vmcs_conf) < 0)
11144 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011145 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011146 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11147 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11148 smp_processor_id());
11149 *(int *)rtn = -EIO;
11150 }
11151}
11152
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011153static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011154{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011155 u8 cache;
11156 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011157
Sheng Yang522c68c2009-04-27 20:35:43 +080011158 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011159 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011160 * 2. EPT with VT-d:
11161 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011162 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011163 * b. VT-d with snooping control feature: snooping control feature of
11164 * VT-d engine can guarantee the cache correctness. Just set it
11165 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011166 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011167 * consistent with host MTRR
11168 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011169 if (is_mmio) {
11170 cache = MTRR_TYPE_UNCACHABLE;
11171 goto exit;
11172 }
11173
11174 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011175 ipat = VMX_EPT_IPAT_BIT;
11176 cache = MTRR_TYPE_WRBACK;
11177 goto exit;
11178 }
11179
11180 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11181 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011182 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011183 cache = MTRR_TYPE_WRBACK;
11184 else
11185 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011186 goto exit;
11187 }
11188
Xiao Guangrongff536042015-06-15 16:55:22 +080011189 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011190
11191exit:
11192 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011193}
11194
Sheng Yang17cc3932010-01-05 19:02:27 +080011195static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011196{
Sheng Yang878403b2010-01-05 19:02:29 +080011197 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11198 return PT_DIRECTORY_LEVEL;
11199 else
11200 /* For shadow and EPT supported 1GB page */
11201 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011202}
11203
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011204static void vmcs_set_secondary_exec_control(u32 new_ctl)
11205{
11206 /*
11207 * These bits in the secondary execution controls field
11208 * are dynamic, the others are mostly based on the hypervisor
11209 * architecture and the guest's CPUID. Do not touch the
11210 * dynamic bits.
11211 */
11212 u32 mask =
11213 SECONDARY_EXEC_SHADOW_VMCS |
11214 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011215 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11216 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011217
11218 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11219
11220 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11221 (new_ctl & ~mask) | (cur_ctl & mask));
11222}
11223
David Matlack8322ebb2016-11-29 18:14:09 -080011224/*
11225 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11226 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11227 */
11228static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11229{
11230 struct vcpu_vmx *vmx = to_vmx(vcpu);
11231 struct kvm_cpuid_entry2 *entry;
11232
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011233 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11234 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011235
11236#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11237 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011238 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011239} while (0)
11240
11241 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11242 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11243 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11244 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11245 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11246 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11247 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11248 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11249 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11250 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11251 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11252 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11253 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11254 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11255 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11256
11257 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11258 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11259 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11260 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11261 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011262 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011263
11264#undef cr4_fixed1_update
11265}
11266
Sheng Yang0e851882009-12-18 16:48:46 +080011267static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11268{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011269 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011270
Paolo Bonzini80154d72017-08-24 13:55:35 +020011271 if (cpu_has_secondary_exec_ctrls()) {
11272 vmx_compute_secondary_exec_control(vmx);
11273 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011274 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011275
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011276 if (nested_vmx_allowed(vcpu))
11277 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11278 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11279 else
11280 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11281 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011282
11283 if (nested_vmx_allowed(vcpu))
11284 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080011285}
11286
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011287static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11288{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011289 if (func == 1 && nested)
11290 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011291}
11292
Yang Zhang25d92082013-08-06 12:00:32 +030011293static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11294 struct x86_exception *fault)
11295{
Jan Kiszka533558b2014-01-04 18:47:20 +010011296 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011297 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011298 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011299 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011300
Bandan Dasc5f983f2017-05-05 15:25:14 -040011301 if (vmx->nested.pml_full) {
11302 exit_reason = EXIT_REASON_PML_FULL;
11303 vmx->nested.pml_full = false;
11304 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11305 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011306 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011307 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011308 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011309
11310 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011311 vmcs12->guest_physical_address = fault->address;
11312}
11313
Peter Feiner995f00a2017-06-30 17:26:32 -070011314static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11315{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011316 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011317}
11318
Nadav Har'El155a97a2013-08-05 11:07:16 +030011319/* Callbacks for nested_ept_init_mmu_context: */
11320
11321static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11322{
11323 /* return the page table to be shadowed - in our case, EPT12 */
11324 return get_vmcs12(vcpu)->ept_pointer;
11325}
11326
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011327static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011328{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011329 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011330 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011331 return 1;
11332
Paolo Bonziniad896af2013-10-02 16:56:14 +020011333 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011334 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011335 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011336 nested_ept_ad_enabled(vcpu),
11337 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011338 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11339 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11340 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11341
11342 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011343 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011344}
11345
11346static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11347{
11348 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11349}
11350
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011351static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11352 u16 error_code)
11353{
11354 bool inequality, bit;
11355
11356 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11357 inequality =
11358 (error_code & vmcs12->page_fault_error_code_mask) !=
11359 vmcs12->page_fault_error_code_match;
11360 return inequality ^ bit;
11361}
11362
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011363static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11364 struct x86_exception *fault)
11365{
11366 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11367
11368 WARN_ON(!is_guest_mode(vcpu));
11369
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011370 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11371 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011372 vmcs12->vm_exit_intr_error_code = fault->error_code;
11373 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11374 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11375 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11376 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011377 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011378 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011379 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011380}
11381
Paolo Bonzinic9923842017-12-13 14:16:30 +010011382static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11383 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011384
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011385static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011386{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011387 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011388 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011389 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011390 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011391
11392 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011393 /*
11394 * Translate L1 physical address to host physical
11395 * address for vmcs02. Keep the page pinned, so this
11396 * physical address remains valid. We keep a reference
11397 * to it so we can release it later.
11398 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011399 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011400 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011401 vmx->nested.apic_access_page = NULL;
11402 }
11403 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011404 /*
11405 * If translation failed, no matter: This feature asks
11406 * to exit when accessing the given address, and if it
11407 * can never be accessed, this feature won't do
11408 * anything anyway.
11409 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011410 if (!is_error_page(page)) {
11411 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011412 hpa = page_to_phys(vmx->nested.apic_access_page);
11413 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11414 } else {
11415 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11416 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11417 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011418 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011419
11420 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011421 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011422 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011423 vmx->nested.virtual_apic_page = NULL;
11424 }
11425 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011426
11427 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011428 * If translation failed, VM entry will fail because
11429 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11430 * Failing the vm entry is _not_ what the processor
11431 * does but it's basically the only possibility we
11432 * have. We could still enter the guest if CR8 load
11433 * exits are enabled, CR8 store exits are enabled, and
11434 * virtualize APIC access is disabled; in this case
11435 * the processor would never use the TPR shadow and we
11436 * could simply clear the bit from the execution
11437 * control. But such a configuration is useless, so
11438 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011439 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011440 if (!is_error_page(page)) {
11441 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011442 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11443 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11444 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011445 }
11446
Wincy Van705699a2015-02-03 23:58:17 +080011447 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011448 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11449 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011450 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011451 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011452 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011453 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11454 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011455 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011456 vmx->nested.pi_desc_page = page;
11457 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011458 vmx->nested.pi_desc =
11459 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11460 (unsigned long)(vmcs12->posted_intr_desc_addr &
11461 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011462 vmcs_write64(POSTED_INTR_DESC_ADDR,
11463 page_to_phys(vmx->nested.pi_desc_page) +
11464 (unsigned long)(vmcs12->posted_intr_desc_addr &
11465 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011466 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011467 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011468 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11469 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011470 else
11471 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11472 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011473}
11474
Jan Kiszkaf4124502014-03-07 20:03:13 +010011475static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11476{
11477 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11478 struct vcpu_vmx *vmx = to_vmx(vcpu);
11479
Sean Christopherson4c008122018-08-27 15:21:10 -070011480 /*
11481 * A timer value of zero is architecturally guaranteed to cause
11482 * a VMExit prior to executing any instructions in the guest.
11483 */
11484 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011485 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11486 return;
11487 }
11488
Sean Christopherson4c008122018-08-27 15:21:10 -070011489 if (vcpu->arch.virtual_tsc_khz == 0)
11490 return;
11491
Jan Kiszkaf4124502014-03-07 20:03:13 +010011492 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11493 preemption_timeout *= 1000000;
11494 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11495 hrtimer_start(&vmx->nested.preemption_timer,
11496 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11497}
11498
Jim Mattson56a20512017-07-06 16:33:06 -070011499static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11500 struct vmcs12 *vmcs12)
11501{
11502 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11503 return 0;
11504
11505 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11506 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11507 return -EINVAL;
11508
11509 return 0;
11510}
11511
Wincy Van3af18d92015-02-03 23:49:31 +080011512static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11513 struct vmcs12 *vmcs12)
11514{
Wincy Van3af18d92015-02-03 23:49:31 +080011515 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11516 return 0;
11517
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011518 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011519 return -EINVAL;
11520
11521 return 0;
11522}
11523
Jim Mattson712b12d2017-08-24 13:24:47 -070011524static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11525 struct vmcs12 *vmcs12)
11526{
11527 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11528 return 0;
11529
11530 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11531 return -EINVAL;
11532
11533 return 0;
11534}
11535
Wincy Van3af18d92015-02-03 23:49:31 +080011536/*
11537 * Merge L0's and L1's MSR bitmap, return false to indicate that
11538 * we do not use the hardware.
11539 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011540static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11541 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011542{
Wincy Van82f0dd42015-02-03 23:57:18 +080011543 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011544 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011545 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011546 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011547 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011548 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011549 *
11550 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11551 * ensures that we do not accidentally generate an L02 MSR bitmap
11552 * from the L12 MSR bitmap that is too permissive.
11553 * 2. That L1 or L2s have actually used the MSR. This avoids
11554 * unnecessarily merging of the bitmap if the MSR is unused. This
11555 * works properly because we only update the L01 MSR bitmap lazily.
11556 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11557 * updated to reflect this when L1 (or its L2s) actually write to
11558 * the MSR.
11559 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011560 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11561 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011562
Paolo Bonzinic9923842017-12-13 14:16:30 +010011563 /* Nothing to do if the MSR bitmap is not in use. */
11564 if (!cpu_has_vmx_msr_bitmap() ||
11565 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11566 return false;
11567
Ashok Raj15d45072018-02-01 22:59:43 +010011568 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011569 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011570 return false;
11571
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011572 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11573 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011574 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011575
Radim Krčmářd048c092016-08-08 20:16:22 +020011576 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011577 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11578 /*
11579 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11580 * just lets the processor take the value from the virtual-APIC page;
11581 * take those 256 bits directly from the L1 bitmap.
11582 */
11583 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11584 unsigned word = msr / BITS_PER_LONG;
11585 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11586 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011587 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011588 } else {
11589 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11590 unsigned word = msr / BITS_PER_LONG;
11591 msr_bitmap_l0[word] = ~0;
11592 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11593 }
11594 }
11595
11596 nested_vmx_disable_intercept_for_msr(
11597 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011598 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011599 MSR_TYPE_W);
11600
11601 if (nested_cpu_has_vid(vmcs12)) {
11602 nested_vmx_disable_intercept_for_msr(
11603 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011604 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011605 MSR_TYPE_W);
11606 nested_vmx_disable_intercept_for_msr(
11607 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011608 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011609 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011610 }
Ashok Raj15d45072018-02-01 22:59:43 +010011611
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011612 if (spec_ctrl)
11613 nested_vmx_disable_intercept_for_msr(
11614 msr_bitmap_l1, msr_bitmap_l0,
11615 MSR_IA32_SPEC_CTRL,
11616 MSR_TYPE_R | MSR_TYPE_W);
11617
Ashok Raj15d45072018-02-01 22:59:43 +010011618 if (pred_cmd)
11619 nested_vmx_disable_intercept_for_msr(
11620 msr_bitmap_l1, msr_bitmap_l0,
11621 MSR_IA32_PRED_CMD,
11622 MSR_TYPE_W);
11623
Wincy Vanf2b93282015-02-03 23:56:03 +080011624 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011625 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011626
11627 return true;
11628}
11629
Liran Alon61ada742018-06-23 02:35:08 +030011630static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11631 struct vmcs12 *vmcs12)
11632{
11633 struct vmcs12 *shadow;
11634 struct page *page;
11635
11636 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11637 vmcs12->vmcs_link_pointer == -1ull)
11638 return;
11639
11640 shadow = get_shadow_vmcs12(vcpu);
11641 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11642
11643 memcpy(shadow, kmap(page), VMCS12_SIZE);
11644
11645 kunmap(page);
11646 kvm_release_page_clean(page);
11647}
11648
11649static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11650 struct vmcs12 *vmcs12)
11651{
11652 struct vcpu_vmx *vmx = to_vmx(vcpu);
11653
11654 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11655 vmcs12->vmcs_link_pointer == -1ull)
11656 return;
11657
11658 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11659 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11660}
11661
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011662static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11663 struct vmcs12 *vmcs12)
11664{
11665 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11666 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11667 return -EINVAL;
11668 else
11669 return 0;
11670}
11671
Wincy Vanf2b93282015-02-03 23:56:03 +080011672static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11673 struct vmcs12 *vmcs12)
11674{
Wincy Van82f0dd42015-02-03 23:57:18 +080011675 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011676 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011677 !nested_cpu_has_vid(vmcs12) &&
11678 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011679 return 0;
11680
11681 /*
11682 * If virtualize x2apic mode is enabled,
11683 * virtualize apic access must be disabled.
11684 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011685 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11686 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011687 return -EINVAL;
11688
Wincy Van608406e2015-02-03 23:57:51 +080011689 /*
11690 * If virtual interrupt delivery is enabled,
11691 * we must exit on external interrupts.
11692 */
11693 if (nested_cpu_has_vid(vmcs12) &&
11694 !nested_exit_on_intr(vcpu))
11695 return -EINVAL;
11696
Wincy Van705699a2015-02-03 23:58:17 +080011697 /*
11698 * bits 15:8 should be zero in posted_intr_nv,
11699 * the descriptor address has been already checked
11700 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011701 *
11702 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011703 */
11704 if (nested_cpu_has_posted_intr(vmcs12) &&
11705 (!nested_cpu_has_vid(vmcs12) ||
11706 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011707 (vmcs12->posted_intr_nv & 0xff00) ||
11708 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11709 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011710 return -EINVAL;
11711
Wincy Vanf2b93282015-02-03 23:56:03 +080011712 /* tpr shadow is needed by all apicv features. */
11713 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11714 return -EINVAL;
11715
11716 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011717}
11718
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011719static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11720 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011721 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011722{
Liran Alone2536742018-06-23 02:35:02 +030011723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011724 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011725 u64 count, addr;
11726
Liran Alone2536742018-06-23 02:35:02 +030011727 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11728 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011729 WARN_ON(1);
11730 return -EINVAL;
11731 }
11732 if (count == 0)
11733 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011734 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11736 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011737 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011738 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11739 addr_field, maxphyaddr, count, addr);
11740 return -EINVAL;
11741 }
11742 return 0;
11743}
11744
11745static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11746 struct vmcs12 *vmcs12)
11747{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011748 if (vmcs12->vm_exit_msr_load_count == 0 &&
11749 vmcs12->vm_exit_msr_store_count == 0 &&
11750 vmcs12->vm_entry_msr_load_count == 0)
11751 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011752 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011753 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011754 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011755 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011756 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011757 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011758 return -EINVAL;
11759 return 0;
11760}
11761
Bandan Dasc5f983f2017-05-05 15:25:14 -040011762static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11763 struct vmcs12 *vmcs12)
11764{
11765 u64 address = vmcs12->pml_address;
11766 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11767
11768 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11769 if (!nested_cpu_has_ept(vmcs12) ||
11770 !IS_ALIGNED(address, 4096) ||
11771 address >> maxphyaddr)
11772 return -EINVAL;
11773 }
11774
11775 return 0;
11776}
11777
Liran Alona8a7c022018-06-23 02:35:06 +030011778static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11779 struct vmcs12 *vmcs12)
11780{
11781 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11782 return 0;
11783
11784 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11785 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11786 return -EINVAL;
11787
11788 return 0;
11789}
11790
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011791static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11792 struct vmx_msr_entry *e)
11793{
11794 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011795 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011796 return -EINVAL;
11797 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11798 e->index == MSR_IA32_UCODE_REV)
11799 return -EINVAL;
11800 if (e->reserved != 0)
11801 return -EINVAL;
11802 return 0;
11803}
11804
11805static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11806 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011807{
11808 if (e->index == MSR_FS_BASE ||
11809 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011810 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11811 nested_vmx_msr_check_common(vcpu, e))
11812 return -EINVAL;
11813 return 0;
11814}
11815
11816static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11817 struct vmx_msr_entry *e)
11818{
11819 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11820 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011821 return -EINVAL;
11822 return 0;
11823}
11824
11825/*
11826 * Load guest's/host's msr at nested entry/exit.
11827 * return 0 for success, entry index for failure.
11828 */
11829static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11830{
11831 u32 i;
11832 struct vmx_msr_entry e;
11833 struct msr_data msr;
11834
11835 msr.host_initiated = false;
11836 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011837 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11838 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011839 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011840 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11841 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011842 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011843 }
11844 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011845 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011846 "%s check failed (%u, 0x%x, 0x%x)\n",
11847 __func__, i, e.index, e.reserved);
11848 goto fail;
11849 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011850 msr.index = e.index;
11851 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011852 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011853 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011854 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11855 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011856 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011857 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011858 }
11859 return 0;
11860fail:
11861 return i + 1;
11862}
11863
11864static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11865{
11866 u32 i;
11867 struct vmx_msr_entry e;
11868
11869 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011870 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011871 if (kvm_vcpu_read_guest(vcpu,
11872 gpa + i * sizeof(e),
11873 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011874 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011875 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11876 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011877 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011878 }
11879 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011880 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011881 "%s check failed (%u, 0x%x, 0x%x)\n",
11882 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011883 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011884 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011885 msr_info.host_initiated = false;
11886 msr_info.index = e.index;
11887 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011888 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011889 "%s cannot read MSR (%u, 0x%x)\n",
11890 __func__, i, e.index);
11891 return -EINVAL;
11892 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011893 if (kvm_vcpu_write_guest(vcpu,
11894 gpa + i * sizeof(e) +
11895 offsetof(struct vmx_msr_entry, value),
11896 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011897 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011898 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011899 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011900 return -EINVAL;
11901 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011902 }
11903 return 0;
11904}
11905
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011906static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11907{
11908 unsigned long invalid_mask;
11909
11910 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11911 return (val & invalid_mask) == 0;
11912}
11913
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011914/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011915 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11916 * emulating VM entry into a guest with EPT enabled.
11917 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11918 * is assigned to entry_failure_code on failure.
11919 */
11920static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011921 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011922{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011923 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011924 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011925 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11926 return 1;
11927 }
11928
11929 /*
11930 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11931 * must not be dereferenced.
11932 */
11933 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11934 !nested_ept) {
11935 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11936 *entry_failure_code = ENTRY_FAIL_PDPTE;
11937 return 1;
11938 }
11939 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011940 }
11941
Junaid Shahid50c28f22018-06-27 14:59:11 -070011942 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011943 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011944
11945 vcpu->arch.cr3 = cr3;
11946 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11947
11948 kvm_init_mmu(vcpu, false);
11949
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011950 return 0;
11951}
11952
Jim Mattson6514dc32018-04-26 16:09:12 -070011953static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011954{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011955 struct vcpu_vmx *vmx = to_vmx(vcpu);
11956
11957 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11958 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11959 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11960 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11961 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11962 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11963 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11964 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11965 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11966 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11967 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11968 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11969 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11970 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11971 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11972 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11973 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11974 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11975 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11976 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11977 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11978 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11979 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11980 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11981 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11982 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11983 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11984 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11985 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11986 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11987 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011988
11989 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11990 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11991 vmcs12->guest_pending_dbg_exceptions);
11992 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11993 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11994
11995 if (nested_cpu_has_xsaves(vmcs12))
11996 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11997 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11998
11999 if (cpu_has_vmx_posted_intr())
12000 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12001
12002 /*
12003 * Whether page-faults are trapped is determined by a combination of
12004 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12005 * If enable_ept, L0 doesn't care about page faults and we should
12006 * set all of these to L1's desires. However, if !enable_ept, L0 does
12007 * care about (at least some) page faults, and because it is not easy
12008 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12009 * to exit on each and every L2 page fault. This is done by setting
12010 * MASK=MATCH=0 and (see below) EB.PF=1.
12011 * Note that below we don't need special code to set EB.PF beyond the
12012 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12013 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12014 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12015 */
12016 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12017 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12018 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12019 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12020
12021 /* All VMFUNCs are currently emulated through L0 vmexits. */
12022 if (cpu_has_vmx_vmfunc())
12023 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12024
12025 if (cpu_has_vmx_apicv()) {
12026 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12027 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12028 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12029 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12030 }
12031
12032 /*
12033 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12034 * Some constant fields are set here by vmx_set_constant_host_state().
12035 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012036 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12037 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012038 */
12039 vmx_set_constant_host_state(vmx);
12040
12041 /*
12042 * Set the MSR load/store lists to match L0's settings.
12043 */
12044 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012045 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12046 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12047 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12048 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012049
12050 set_cr4_guest_host_mask(vmx);
12051
12052 if (vmx_mpx_supported())
12053 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12054
12055 if (enable_vpid) {
12056 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12057 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12058 else
12059 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12060 }
12061
12062 /*
12063 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12064 */
12065 if (enable_ept) {
12066 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12067 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12068 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12069 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12070 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012071
12072 if (cpu_has_vmx_msr_bitmap())
12073 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012074}
12075
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012076/*
12077 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12078 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012079 * 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 +030012080 * guest in a way that will both be appropriate to L1's requests, and our
12081 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12082 * function also has additional necessary side-effects, like setting various
12083 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012084 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12085 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012086 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012087static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012088 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012089{
12090 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012091 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012092
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012093 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012094 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012095 vmx->nested.dirty_vmcs12 = false;
12096 }
12097
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012098 /*
12099 * First, the fields that are shadowed. This must be kept in sync
12100 * with vmx_shadow_fields.h.
12101 */
12102
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012103 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012104 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012105 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012106 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12107 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012108
Jim Mattson6514dc32018-04-26 16:09:12 -070012109 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012110 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012111 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12112 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12113 } else {
12114 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12115 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12116 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012117 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012118 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12119 vmcs12->vm_entry_intr_info_field);
12120 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12121 vmcs12->vm_entry_exception_error_code);
12122 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12123 vmcs12->vm_entry_instruction_len);
12124 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12125 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012126 vmx->loaded_vmcs->nmi_known_unmasked =
12127 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012128 } else {
12129 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12130 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012131 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012132
Jan Kiszkaf4124502014-03-07 20:03:13 +010012133 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012134
Sean Christophersonf459a702018-08-27 15:21:11 -070012135 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012136 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012137 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12138 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012139
12140 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012141 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012142 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12143 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012144 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012145 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012146 }
Wincy Van705699a2015-02-03 23:58:17 +080012147
Jan Kiszkaf4124502014-03-07 20:03:13 +010012148 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012149
Jan Kiszkaf4124502014-03-07 20:03:13 +010012150 vmx->nested.preemption_timer_expired = false;
12151 if (nested_cpu_has_preemption_timer(vmcs12))
12152 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012153
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012154 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012155 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012156
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012157 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012158 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012159 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012160 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012161 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012162 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012163 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12164 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012165 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012166 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12167 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12168 ~SECONDARY_EXEC_ENABLE_PML;
12169 exec_control |= vmcs12_exec_ctrl;
12170 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012171
Liran Alon32c7acf2018-06-23 02:35:11 +030012172 /* VMCS shadowing for L2 is emulated for now */
12173 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12174
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012175 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012176 vmcs_write16(GUEST_INTR_STATUS,
12177 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012178
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012179 /*
12180 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12181 * nested_get_vmcs12_pages will either fix it up or
12182 * remove the VM execution control.
12183 */
12184 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12185 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12186
Sean Christopherson0b665d32018-08-14 09:33:34 -070012187 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12188 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12189
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012190 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12191 }
12192
Jim Mattson83bafef2016-10-04 10:48:38 -070012193 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012194 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12195 * entry, but only if the current (host) sp changed from the value
12196 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12197 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12198 * here we just force the write to happen on entry.
12199 */
12200 vmx->host_rsp = 0;
12201
12202 exec_control = vmx_exec_control(vmx); /* L0's desires */
12203 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12204 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12205 exec_control &= ~CPU_BASED_TPR_SHADOW;
12206 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012207
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012208 /*
12209 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12210 * nested_get_vmcs12_pages can't fix it up, the illegal value
12211 * will result in a VM entry failure.
12212 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012213 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012214 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012215 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012216 } else {
12217#ifdef CONFIG_X86_64
12218 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12219 CPU_BASED_CR8_STORE_EXITING;
12220#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012221 }
12222
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012223 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012224 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12225 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012226 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012227 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12228 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12229
12230 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12231
12232 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12233 * bitwise-or of what L1 wants to trap for L2, and what we want to
12234 * trap. Note that CR0.TS also needs updating - we do this later.
12235 */
12236 update_exception_bitmap(vcpu);
12237 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12238 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12239
Nadav Har'El8049d652013-08-05 11:07:06 +030012240 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12241 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12242 * bits are further modified by vmx_set_efer() below.
12243 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012244 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012245
12246 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12247 * emulated by vmx_set_efer(), below.
12248 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012249 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012250 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12251 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012252 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12253
Jim Mattson6514dc32018-04-26 16:09:12 -070012254 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012255 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012256 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012257 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012258 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012259 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012260 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012261
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012262 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12263
Peter Feinerc95ba922016-08-17 09:36:47 -070012264 if (kvm_has_tsc_control)
12265 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012266
12267 if (enable_vpid) {
12268 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012269 * There is no direct mapping between vpid02 and vpid12, the
12270 * vpid02 is per-vCPU for L0 and reused while the value of
12271 * vpid12 is changed w/ one invvpid during nested vmentry.
12272 * The vpid12 is allocated by L1 for L2, so it will not
12273 * influence global bitmap(for vpid01 and vpid02 allocation)
12274 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012275 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012276 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012277 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12278 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012279 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012280 }
12281 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012282 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012283 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012284 }
12285
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012286 if (enable_pml) {
12287 /*
12288 * Conceptually we want to copy the PML address and index from
12289 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12290 * since we always flush the log on each vmexit, this happens
12291 * to be equivalent to simply resetting the fields in vmcs02.
12292 */
12293 ASSERT(vmx->pml_pg);
12294 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12295 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12296 }
12297
Nadav Har'El155a97a2013-08-05 11:07:16 +030012298 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012299 if (nested_ept_init_mmu_context(vcpu)) {
12300 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12301 return 1;
12302 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012303 } else if (nested_cpu_has2(vmcs12,
12304 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012305 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012306 }
12307
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012308 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012309 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12310 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012311 * The CR0_READ_SHADOW is what L2 should have expected to read given
12312 * the specifications by L1; It's not enough to take
12313 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12314 * have more bits than L1 expected.
12315 */
12316 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12317 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12318
12319 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12320 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12321
Jim Mattson6514dc32018-04-26 16:09:12 -070012322 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012323 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012324 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12325 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12326 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12327 else
12328 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12329 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12330 vmx_set_efer(vcpu, vcpu->arch.efer);
12331
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012332 /*
12333 * Guest state is invalid and unrestricted guest is disabled,
12334 * which means L1 attempted VMEntry to L2 with invalid state.
12335 * Fail the VMEntry.
12336 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012337 if (vmx->emulation_required) {
12338 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012339 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012340 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012341
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012342 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012343 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012344 entry_failure_code))
12345 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012346
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012347 if (!enable_ept)
12348 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12349
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012350 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12351 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012352 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012353}
12354
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012355static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12356{
12357 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12358 nested_cpu_has_virtual_nmis(vmcs12))
12359 return -EINVAL;
12360
12361 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12362 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12363 return -EINVAL;
12364
12365 return 0;
12366}
12367
Jim Mattsonca0bde22016-11-30 12:03:46 -080012368static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12369{
12370 struct vcpu_vmx *vmx = to_vmx(vcpu);
12371
12372 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12373 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12374 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12375
Jim Mattson56a20512017-07-06 16:33:06 -070012376 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12377 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12378
Jim Mattsonca0bde22016-11-30 12:03:46 -080012379 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12380 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12381
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012382 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12383 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12384
Jim Mattson712b12d2017-08-24 13:24:47 -070012385 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12386 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12387
Jim Mattsonca0bde22016-11-30 12:03:46 -080012388 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12389 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12390
12391 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12392 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12393
Bandan Dasc5f983f2017-05-05 15:25:14 -040012394 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12395 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12396
Liran Alona8a7c022018-06-23 02:35:06 +030012397 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12398 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12399
Jim Mattsonca0bde22016-11-30 12:03:46 -080012400 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012401 vmx->nested.msrs.procbased_ctls_low,
12402 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012403 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12404 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012405 vmx->nested.msrs.secondary_ctls_low,
12406 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012407 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012408 vmx->nested.msrs.pinbased_ctls_low,
12409 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012410 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012411 vmx->nested.msrs.exit_ctls_low,
12412 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012413 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012414 vmx->nested.msrs.entry_ctls_low,
12415 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012416 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12417
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012418 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012419 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12420
Bandan Das41ab9372017-08-03 15:54:43 -040012421 if (nested_cpu_has_vmfunc(vmcs12)) {
12422 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012423 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012424 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12425
12426 if (nested_cpu_has_eptp_switching(vmcs12)) {
12427 if (!nested_cpu_has_ept(vmcs12) ||
12428 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12429 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12430 }
12431 }
Bandan Das27c42a12017-08-03 15:54:42 -040012432
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012433 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12434 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12435
Jim Mattsonca0bde22016-11-30 12:03:46 -080012436 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12437 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12438 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12439 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12440
Marc Orr04473782018-06-20 17:21:29 -070012441 /*
12442 * From the Intel SDM, volume 3:
12443 * Fields relevant to VM-entry event injection must be set properly.
12444 * These fields are the VM-entry interruption-information field, the
12445 * VM-entry exception error code, and the VM-entry instruction length.
12446 */
12447 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12448 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12449 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12450 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12451 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12452 bool should_have_error_code;
12453 bool urg = nested_cpu_has2(vmcs12,
12454 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12455 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12456
12457 /* VM-entry interruption-info field: interruption type */
12458 if (intr_type == INTR_TYPE_RESERVED ||
12459 (intr_type == INTR_TYPE_OTHER_EVENT &&
12460 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12461 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12462
12463 /* VM-entry interruption-info field: vector */
12464 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12465 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12466 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12467 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12468
12469 /* VM-entry interruption-info field: deliver error code */
12470 should_have_error_code =
12471 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12472 x86_exception_has_error_code(vector);
12473 if (has_error_code != should_have_error_code)
12474 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12475
12476 /* VM-entry exception error code */
12477 if (has_error_code &&
12478 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12479 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12480
12481 /* VM-entry interruption-info field: reserved bits */
12482 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484
12485 /* VM-entry instruction length */
12486 switch (intr_type) {
12487 case INTR_TYPE_SOFT_EXCEPTION:
12488 case INTR_TYPE_SOFT_INTR:
12489 case INTR_TYPE_PRIV_SW_EXCEPTION:
12490 if ((vmcs12->vm_entry_instruction_len > 15) ||
12491 (vmcs12->vm_entry_instruction_len == 0 &&
12492 !nested_cpu_has_zero_length_injection(vcpu)))
12493 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12494 }
12495 }
12496
Jim Mattsonca0bde22016-11-30 12:03:46 -080012497 return 0;
12498}
12499
Liran Alonf145d902018-06-23 02:35:07 +030012500static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12501 struct vmcs12 *vmcs12)
12502{
12503 int r;
12504 struct page *page;
12505 struct vmcs12 *shadow;
12506
12507 if (vmcs12->vmcs_link_pointer == -1ull)
12508 return 0;
12509
12510 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12511 return -EINVAL;
12512
12513 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12514 if (is_error_page(page))
12515 return -EINVAL;
12516
12517 r = 0;
12518 shadow = kmap(page);
12519 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12520 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12521 r = -EINVAL;
12522 kunmap(page);
12523 kvm_release_page_clean(page);
12524 return r;
12525}
12526
Jim Mattsonca0bde22016-11-30 12:03:46 -080012527static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12528 u32 *exit_qual)
12529{
12530 bool ia32e;
12531
12532 *exit_qual = ENTRY_FAIL_DEFAULT;
12533
12534 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12535 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12536 return 1;
12537
Liran Alonf145d902018-06-23 02:35:07 +030012538 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012539 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12540 return 1;
12541 }
12542
12543 /*
12544 * If the load IA32_EFER VM-entry control is 1, the following checks
12545 * are performed on the field for the IA32_EFER MSR:
12546 * - Bits reserved in the IA32_EFER MSR must be 0.
12547 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12548 * the IA-32e mode guest VM-exit control. It must also be identical
12549 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12550 * CR0.PG) is 1.
12551 */
12552 if (to_vmx(vcpu)->nested.nested_run_pending &&
12553 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12554 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12555 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12556 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12557 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12558 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12559 return 1;
12560 }
12561
12562 /*
12563 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12564 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12565 * the values of the LMA and LME bits in the field must each be that of
12566 * the host address-space size VM-exit control.
12567 */
12568 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12569 ia32e = (vmcs12->vm_exit_controls &
12570 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12571 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12572 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12573 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12574 return 1;
12575 }
12576
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012577 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12578 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12579 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12580 return 1;
12581
Jim Mattsonca0bde22016-11-30 12:03:46 -080012582 return 0;
12583}
12584
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012585/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012586 * If exit_qual is NULL, this is being called from state restore (either RSM
12587 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012588 */
12589static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012590{
12591 struct vcpu_vmx *vmx = to_vmx(vcpu);
12592 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012593 bool from_vmentry = !!exit_qual;
12594 u32 dummy_exit_qual;
Liran Alonb5861e52018-09-03 15:20:22 +030012595 u32 vmcs01_cpu_exec_ctrl;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012596 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012597
Liran Alonb5861e52018-09-03 15:20:22 +030012598 vmcs01_cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
12599
Jim Mattson858e25c2016-11-30 12:03:47 -080012600 enter_guest_mode(vcpu);
12601
12602 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12603 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12604
Jim Mattsonde3a0022017-11-27 17:22:25 -060012605 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012606 vmx_segment_cache_clear(vmx);
12607
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012608 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12609 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12610
12611 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012612 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012613 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012614
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012615 if (from_vmentry) {
12616 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012617
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012618 r = EXIT_REASON_MSR_LOAD_FAIL;
12619 *exit_qual = nested_vmx_load_msr(vcpu,
12620 vmcs12->vm_entry_msr_load_addr,
12621 vmcs12->vm_entry_msr_load_count);
12622 if (*exit_qual)
12623 goto fail;
12624 } else {
12625 /*
12626 * The MMU is not initialized to point at the right entities yet and
12627 * "get pages" would need to read data from the guest (i.e. we will
12628 * need to perform gpa to hpa translation). Request a call
12629 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12630 * have already been set at vmentry time and should not be reset.
12631 */
12632 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12633 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012634
Jim Mattson858e25c2016-11-30 12:03:47 -080012635 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012636 * If L1 had a pending IRQ/NMI until it executed
12637 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12638 * disallowed (e.g. interrupts disabled), L0 needs to
12639 * evaluate if this pending event should cause an exit from L2
12640 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12641 * intercept EXTERNAL_INTERRUPT).
12642 *
12643 * Usually this would be handled by L0 requesting a
12644 * IRQ/NMI window by setting VMCS accordingly. However,
12645 * this setting was done on VMCS01 and now VMCS02 is active
12646 * instead. Thus, we force L0 to perform pending event
12647 * evaluation by requesting a KVM_REQ_EVENT.
12648 */
12649 if (vmcs01_cpu_exec_ctrl &
12650 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING)) {
12651 kvm_make_request(KVM_REQ_EVENT, vcpu);
12652 }
12653
12654 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012655 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12656 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12657 * returned as far as L1 is concerned. It will only return (and set
12658 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12659 */
12660 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012661
12662fail:
12663 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12664 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12665 leave_guest_mode(vcpu);
12666 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012667 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012668}
12669
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012670/*
12671 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12672 * for running an L2 nested guest.
12673 */
12674static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12675{
12676 struct vmcs12 *vmcs12;
12677 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012678 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012679 u32 exit_qual;
12680 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012681
Kyle Hueyeb277562016-11-29 12:40:39 -080012682 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012683 return 1;
12684
Kyle Hueyeb277562016-11-29 12:40:39 -080012685 if (!nested_vmx_check_vmcs12(vcpu))
12686 goto out;
12687
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012688 vmcs12 = get_vmcs12(vcpu);
12689
Liran Alona6192d42018-06-23 02:35:04 +030012690 /*
12691 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12692 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12693 * rather than RFLAGS.ZF, and no error number is stored to the
12694 * VM-instruction error field.
12695 */
12696 if (vmcs12->hdr.shadow_vmcs) {
12697 nested_vmx_failInvalid(vcpu);
12698 goto out;
12699 }
12700
Abel Gordon012f83c2013-04-18 14:39:25 +030012701 if (enable_shadow_vmcs)
12702 copy_shadow_to_vmcs12(vmx);
12703
Nadav Har'El7c177932011-05-25 23:12:04 +030012704 /*
12705 * The nested entry process starts with enforcing various prerequisites
12706 * on vmcs12 as required by the Intel SDM, and act appropriately when
12707 * they fail: As the SDM explains, some conditions should cause the
12708 * instruction to fail, while others will cause the instruction to seem
12709 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12710 * To speed up the normal (success) code path, we should avoid checking
12711 * for misconfigurations which will anyway be caught by the processor
12712 * when using the merged vmcs02.
12713 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012714 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12715 nested_vmx_failValid(vcpu,
12716 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12717 goto out;
12718 }
12719
Nadav Har'El7c177932011-05-25 23:12:04 +030012720 if (vmcs12->launch_state == launch) {
12721 nested_vmx_failValid(vcpu,
12722 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12723 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012724 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012725 }
12726
Jim Mattsonca0bde22016-11-30 12:03:46 -080012727 ret = check_vmentry_prereqs(vcpu, vmcs12);
12728 if (ret) {
12729 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012730 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012731 }
12732
Nadav Har'El7c177932011-05-25 23:12:04 +030012733 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012734 * After this point, the trap flag no longer triggers a singlestep trap
12735 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12736 * This is not 100% correct; for performance reasons, we delegate most
12737 * of the checks on host state to the processor. If those fail,
12738 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012739 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012740 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012741
Jim Mattsonca0bde22016-11-30 12:03:46 -080012742 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12743 if (ret) {
12744 nested_vmx_entry_failure(vcpu, vmcs12,
12745 EXIT_REASON_INVALID_STATE, exit_qual);
12746 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012747 }
12748
12749 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012750 * We're finally done with prerequisite checking, and can start with
12751 * the nested entry.
12752 */
12753
Jim Mattson6514dc32018-04-26 16:09:12 -070012754 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012755 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012756 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012757 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012758 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012759 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012760 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012761
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012762 /* Hide L1D cache contents from the nested guest. */
12763 vmx->vcpu.arch.l1tf_flush_l1d = true;
12764
Chao Gao135a06c2018-02-11 10:06:30 +080012765 /*
Liran Alon61ada742018-06-23 02:35:08 +030012766 * Must happen outside of enter_vmx_non_root_mode() as it will
12767 * also be used as part of restoring nVMX state for
12768 * snapshot restore (migration).
12769 *
12770 * In this flow, it is assumed that vmcs12 cache was
12771 * trasferred as part of captured nVMX state and should
12772 * therefore not be read from guest memory (which may not
12773 * exist on destination host yet).
12774 */
12775 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12776
12777 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012778 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12779 * by event injection, halt vcpu.
12780 */
12781 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012782 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12783 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012784 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012785 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012786 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012787
12788out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012789 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012790}
12791
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012792/*
12793 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12794 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12795 * This function returns the new value we should put in vmcs12.guest_cr0.
12796 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12797 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12798 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12799 * didn't trap the bit, because if L1 did, so would L0).
12800 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12801 * been modified by L2, and L1 knows it. So just leave the old value of
12802 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12803 * isn't relevant, because if L0 traps this bit it can set it to anything.
12804 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12805 * changed these bits, and therefore they need to be updated, but L0
12806 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12807 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12808 */
12809static inline unsigned long
12810vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12811{
12812 return
12813 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12814 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12815 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12816 vcpu->arch.cr0_guest_owned_bits));
12817}
12818
12819static inline unsigned long
12820vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12821{
12822 return
12823 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12824 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12825 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12826 vcpu->arch.cr4_guest_owned_bits));
12827}
12828
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012829static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12830 struct vmcs12 *vmcs12)
12831{
12832 u32 idt_vectoring;
12833 unsigned int nr;
12834
Wanpeng Li664f8e22017-08-24 03:35:09 -070012835 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012836 nr = vcpu->arch.exception.nr;
12837 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12838
12839 if (kvm_exception_is_soft(nr)) {
12840 vmcs12->vm_exit_instruction_len =
12841 vcpu->arch.event_exit_inst_len;
12842 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12843 } else
12844 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12845
12846 if (vcpu->arch.exception.has_error_code) {
12847 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12848 vmcs12->idt_vectoring_error_code =
12849 vcpu->arch.exception.error_code;
12850 }
12851
12852 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012853 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012854 vmcs12->idt_vectoring_info_field =
12855 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012856 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012857 nr = vcpu->arch.interrupt.nr;
12858 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12859
12860 if (vcpu->arch.interrupt.soft) {
12861 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12862 vmcs12->vm_entry_instruction_len =
12863 vcpu->arch.event_exit_inst_len;
12864 } else
12865 idt_vectoring |= INTR_TYPE_EXT_INTR;
12866
12867 vmcs12->idt_vectoring_info_field = idt_vectoring;
12868 }
12869}
12870
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012871static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12872{
12873 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012874 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012875 bool block_nested_events =
12876 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012877
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012878 if (vcpu->arch.exception.pending &&
12879 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012880 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012881 return -EBUSY;
12882 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012883 return 0;
12884 }
12885
Jan Kiszkaf4124502014-03-07 20:03:13 +010012886 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12887 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012888 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012889 return -EBUSY;
12890 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12891 return 0;
12892 }
12893
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012894 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012895 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012896 return -EBUSY;
12897 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12898 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12899 INTR_INFO_VALID_MASK, 0);
12900 /*
12901 * The NMI-triggered VM exit counts as injection:
12902 * clear this one and block further NMIs.
12903 */
12904 vcpu->arch.nmi_pending = 0;
12905 vmx_set_nmi_mask(vcpu, true);
12906 return 0;
12907 }
12908
12909 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12910 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012911 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012912 return -EBUSY;
12913 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012914 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012915 }
12916
David Hildenbrand6342c502017-01-25 11:58:58 +010012917 vmx_complete_nested_posted_interrupt(vcpu);
12918 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012919}
12920
Sean Christophersond264ee02018-08-27 15:21:12 -070012921static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12922{
12923 to_vmx(vcpu)->req_immediate_exit = true;
12924}
12925
Jan Kiszkaf4124502014-03-07 20:03:13 +010012926static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12927{
12928 ktime_t remaining =
12929 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12930 u64 value;
12931
12932 if (ktime_to_ns(remaining) <= 0)
12933 return 0;
12934
12935 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12936 do_div(value, 1000000);
12937 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12938}
12939
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012940/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012941 * Update the guest state fields of vmcs12 to reflect changes that
12942 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12943 * VM-entry controls is also updated, since this is really a guest
12944 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012945 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012946static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012947{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012948 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12949 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12950
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012951 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12952 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12953 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12954
12955 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12956 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12957 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12958 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12959 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12960 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12961 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12962 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12963 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12964 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12965 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12966 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12967 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12968 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12969 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12970 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12971 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12972 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12973 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12974 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12975 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12976 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12977 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12978 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12979 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12980 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12981 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12982 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12983 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12984 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12985 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12986 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12987 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12988 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12989 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12990 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12991
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012992 vmcs12->guest_interruptibility_info =
12993 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12994 vmcs12->guest_pending_dbg_exceptions =
12995 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012996 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12997 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12998 else
12999 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013000
Jan Kiszkaf4124502014-03-07 20:03:13 +010013001 if (nested_cpu_has_preemption_timer(vmcs12)) {
13002 if (vmcs12->vm_exit_controls &
13003 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13004 vmcs12->vmx_preemption_timer_value =
13005 vmx_get_preemption_timer_value(vcpu);
13006 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13007 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013008
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013009 /*
13010 * In some cases (usually, nested EPT), L2 is allowed to change its
13011 * own CR3 without exiting. If it has changed it, we must keep it.
13012 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13013 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13014 *
13015 * Additionally, restore L2's PDPTR to vmcs12.
13016 */
13017 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013018 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013019 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13020 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13021 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13022 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13023 }
13024
Jim Mattsond281e132017-06-01 12:44:46 -070013025 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013026
Wincy Van608406e2015-02-03 23:57:51 +080013027 if (nested_cpu_has_vid(vmcs12))
13028 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13029
Jan Kiszkac18911a2013-03-13 16:06:41 +010013030 vmcs12->vm_entry_controls =
13031 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013032 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013033
Jan Kiszka2996fca2014-06-16 13:59:43 +020013034 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13035 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13036 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13037 }
13038
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013039 /* TODO: These cannot have changed unless we have MSR bitmaps and
13040 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013041 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013042 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013043 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13044 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013045 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13046 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13047 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013048 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013049 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013050}
13051
13052/*
13053 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13054 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13055 * and this function updates it to reflect the changes to the guest state while
13056 * L2 was running (and perhaps made some exits which were handled directly by L0
13057 * without going back to L1), and to reflect the exit reason.
13058 * Note that we do not have to copy here all VMCS fields, just those that
13059 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13060 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13061 * which already writes to vmcs12 directly.
13062 */
13063static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13064 u32 exit_reason, u32 exit_intr_info,
13065 unsigned long exit_qualification)
13066{
13067 /* update guest state fields: */
13068 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013069
13070 /* update exit information fields: */
13071
Jan Kiszka533558b2014-01-04 18:47:20 +010013072 vmcs12->vm_exit_reason = exit_reason;
13073 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013074 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013075
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013076 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013077 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13078 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13079
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013080 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013081 vmcs12->launch_state = 1;
13082
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013083 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13084 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013085 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013086
13087 /*
13088 * Transfer the event that L0 or L1 may wanted to inject into
13089 * L2 to IDT_VECTORING_INFO_FIELD.
13090 */
13091 vmcs12_save_pending_event(vcpu, vmcs12);
13092 }
13093
13094 /*
13095 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13096 * preserved above and would only end up incorrectly in L1.
13097 */
13098 vcpu->arch.nmi_injected = false;
13099 kvm_clear_exception_queue(vcpu);
13100 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013101}
13102
Wanpeng Li5af41572017-11-05 16:54:49 -080013103static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
13104 struct vmcs12 *vmcs12)
13105{
13106 u32 entry_failure_code;
13107
13108 nested_ept_uninit_mmu_context(vcpu);
13109
13110 /*
13111 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13112 * couldn't have changed.
13113 */
13114 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13115 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13116
13117 if (!enable_ept)
13118 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13119}
13120
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013121/*
13122 * A part of what we need to when the nested L2 guest exits and we want to
13123 * run its L1 parent, is to reset L1's guest state to the host state specified
13124 * in vmcs12.
13125 * This function is to be called not only on normal nested exit, but also on
13126 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13127 * Failures During or After Loading Guest State").
13128 * This function should be called when the active VMCS is L1's (vmcs01).
13129 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013130static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13131 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013132{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013133 struct kvm_segment seg;
13134
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013135 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13136 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013137 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013138 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13139 else
13140 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13141 vmx_set_efer(vcpu, vcpu->arch.efer);
13142
13143 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13144 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013145 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013146 /*
13147 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013148 * actually changed, because vmx_set_cr0 refers to efer set above.
13149 *
13150 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13151 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013152 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013153 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013154 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013155
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013156 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013157 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013158 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013159
Wanpeng Li5af41572017-11-05 16:54:49 -080013160 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013161
Liran Alon6f1e03b2018-05-22 17:16:14 +030013162 /*
13163 * If vmcs01 don't use VPID, CPU flushes TLB on every
13164 * VMEntry/VMExit. Thus, no need to flush TLB.
13165 *
13166 * If vmcs12 uses VPID, TLB entries populated by L2 are
13167 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13168 * with vmx->vpid. Thus, no need to flush TLB.
13169 *
13170 * Therefore, flush TLB only in case vmcs01 uses VPID and
13171 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13172 * are both tagged with vmx->vpid.
13173 */
13174 if (enable_vpid &&
13175 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080013176 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013177 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013178
13179 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13180 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13181 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13182 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13183 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013184 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13185 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013186
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013187 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13188 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13189 vmcs_write64(GUEST_BNDCFGS, 0);
13190
Jan Kiszka44811c02013-08-04 17:17:27 +020013191 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013192 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013193 vcpu->arch.pat = vmcs12->host_ia32_pat;
13194 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013195 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13196 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13197 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013198
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013199 /* Set L1 segment info according to Intel SDM
13200 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13201 seg = (struct kvm_segment) {
13202 .base = 0,
13203 .limit = 0xFFFFFFFF,
13204 .selector = vmcs12->host_cs_selector,
13205 .type = 11,
13206 .present = 1,
13207 .s = 1,
13208 .g = 1
13209 };
13210 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13211 seg.l = 1;
13212 else
13213 seg.db = 1;
13214 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13215 seg = (struct kvm_segment) {
13216 .base = 0,
13217 .limit = 0xFFFFFFFF,
13218 .type = 3,
13219 .present = 1,
13220 .s = 1,
13221 .db = 1,
13222 .g = 1
13223 };
13224 seg.selector = vmcs12->host_ds_selector;
13225 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13226 seg.selector = vmcs12->host_es_selector;
13227 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13228 seg.selector = vmcs12->host_ss_selector;
13229 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13230 seg.selector = vmcs12->host_fs_selector;
13231 seg.base = vmcs12->host_fs_base;
13232 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13233 seg.selector = vmcs12->host_gs_selector;
13234 seg.base = vmcs12->host_gs_base;
13235 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13236 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013237 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013238 .limit = 0x67,
13239 .selector = vmcs12->host_tr_selector,
13240 .type = 11,
13241 .present = 1
13242 };
13243 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13244
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013245 kvm_set_dr(vcpu, 7, 0x400);
13246 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013247
Wincy Van3af18d92015-02-03 23:49:31 +080013248 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013249 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013250
Wincy Vanff651cb2014-12-11 08:52:58 +030013251 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13252 vmcs12->vm_exit_msr_load_count))
13253 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013254}
13255
13256/*
13257 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13258 * and modify vmcs12 to make it see what it would expect to see there if
13259 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13260 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013261static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13262 u32 exit_intr_info,
13263 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013264{
13265 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013266 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13267
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013268 /* trying to cancel vmlaunch/vmresume is a bug */
13269 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13270
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013271 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013272 * The only expected VM-instruction error is "VM entry with
13273 * invalid control field(s)." Anything else indicates a
13274 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013275 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013276 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13277 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13278
13279 leave_guest_mode(vcpu);
13280
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013281 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13282 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13283
Jim Mattson4f350c62017-09-14 16:31:44 -070013284 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013285 if (exit_reason == -1)
13286 sync_vmcs12(vcpu, vmcs12);
13287 else
13288 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13289 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013290
Liran Alon61ada742018-06-23 02:35:08 +030013291 /*
13292 * Must happen outside of sync_vmcs12() as it will
13293 * also be used to capture vmcs12 cache as part of
13294 * capturing nVMX state for snapshot (migration).
13295 *
13296 * Otherwise, this flush will dirty guest memory at a
13297 * point it is already assumed by user-space to be
13298 * immutable.
13299 */
13300 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13301
Jim Mattson4f350c62017-09-14 16:31:44 -070013302 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13303 vmcs12->vm_exit_msr_store_count))
13304 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013305 }
13306
Jim Mattson4f350c62017-09-14 16:31:44 -070013307 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013308 vm_entry_controls_reset_shadow(vmx);
13309 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013310 vmx_segment_cache_clear(vmx);
13311
Paolo Bonzini93140062016-07-06 13:23:51 +020013312 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013313 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13314 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013315 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013316
Peter Feinerc95ba922016-08-17 09:36:47 -070013317 if (kvm_has_tsc_control)
13318 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013319
Jim Mattson8d860bb2018-05-09 16:56:05 -040013320 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13321 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13322 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013323 } else if (!nested_cpu_has_ept(vmcs12) &&
13324 nested_cpu_has2(vmcs12,
13325 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013326 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013327 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013328
13329 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13330 vmx->host_rsp = 0;
13331
13332 /* Unpin physical memory we referred to in vmcs02 */
13333 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013334 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013335 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013336 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013337 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013338 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013339 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013340 }
Wincy Van705699a2015-02-03 23:58:17 +080013341 if (vmx->nested.pi_desc_page) {
13342 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013343 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013344 vmx->nested.pi_desc_page = NULL;
13345 vmx->nested.pi_desc = NULL;
13346 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013347
13348 /*
Tang Chen38b99172014-09-24 15:57:54 +080013349 * We are now running in L2, mmu_notifier will force to reload the
13350 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13351 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013352 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013353
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013354 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013355 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013356
13357 /* in case we halted in L2 */
13358 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013359
13360 if (likely(!vmx->fail)) {
13361 /*
13362 * TODO: SDM says that with acknowledge interrupt on
13363 * exit, bit 31 of the VM-exit interrupt information
13364 * (valid interrupt) is always set to 1 on
13365 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13366 * need kvm_cpu_has_interrupt(). See the commit
13367 * message for details.
13368 */
13369 if (nested_exit_intr_ack_set(vcpu) &&
13370 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13371 kvm_cpu_has_interrupt(vcpu)) {
13372 int irq = kvm_cpu_get_interrupt(vcpu);
13373 WARN_ON(irq < 0);
13374 vmcs12->vm_exit_intr_info = irq |
13375 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13376 }
13377
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013378 if (exit_reason != -1)
13379 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13380 vmcs12->exit_qualification,
13381 vmcs12->idt_vectoring_info_field,
13382 vmcs12->vm_exit_intr_info,
13383 vmcs12->vm_exit_intr_error_code,
13384 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013385
13386 load_vmcs12_host_state(vcpu, vmcs12);
13387
13388 return;
13389 }
13390
13391 /*
13392 * After an early L2 VM-entry failure, we're now back
13393 * in L1 which thinks it just finished a VMLAUNCH or
13394 * VMRESUME instruction, so we need to set the failure
13395 * flag and the VM-instruction error field of the VMCS
13396 * accordingly.
13397 */
13398 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013399
13400 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13401
Jim Mattson4f350c62017-09-14 16:31:44 -070013402 /*
13403 * The emulated instruction was already skipped in
13404 * nested_vmx_run, but the updated RIP was never
13405 * written back to the vmcs01.
13406 */
13407 skip_emulated_instruction(vcpu);
13408 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013409}
13410
Nadav Har'El7c177932011-05-25 23:12:04 +030013411/*
Jan Kiszka42124922014-01-04 18:47:19 +010013412 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13413 */
13414static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13415{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013416 if (is_guest_mode(vcpu)) {
13417 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013418 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013419 }
Jan Kiszka42124922014-01-04 18:47:19 +010013420 free_nested(to_vmx(vcpu));
13421}
13422
13423/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013424 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13425 * 23.7 "VM-entry failures during or after loading guest state" (this also
13426 * lists the acceptable exit-reason and exit-qualification parameters).
13427 * It should only be called before L2 actually succeeded to run, and when
13428 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13429 */
13430static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13431 struct vmcs12 *vmcs12,
13432 u32 reason, unsigned long qualification)
13433{
13434 load_vmcs12_host_state(vcpu, vmcs12);
13435 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13436 vmcs12->exit_qualification = qualification;
13437 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013438 if (enable_shadow_vmcs)
13439 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013440}
13441
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013442static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13443 struct x86_instruction_info *info,
13444 enum x86_intercept_stage stage)
13445{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013446 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13447 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13448
13449 /*
13450 * RDPID causes #UD if disabled through secondary execution controls.
13451 * Because it is marked as EmulateOnUD, we need to intercept it here.
13452 */
13453 if (info->intercept == x86_intercept_rdtscp &&
13454 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13455 ctxt->exception.vector = UD_VECTOR;
13456 ctxt->exception.error_code_valid = false;
13457 return X86EMUL_PROPAGATE_FAULT;
13458 }
13459
13460 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013461 return X86EMUL_CONTINUE;
13462}
13463
Yunhong Jiang64672c92016-06-13 14:19:59 -070013464#ifdef CONFIG_X86_64
13465/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13466static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13467 u64 divisor, u64 *result)
13468{
13469 u64 low = a << shift, high = a >> (64 - shift);
13470
13471 /* To avoid the overflow on divq */
13472 if (high >= divisor)
13473 return 1;
13474
13475 /* Low hold the result, high hold rem which is discarded */
13476 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13477 "rm" (divisor), "0" (low), "1" (high));
13478 *result = low;
13479
13480 return 0;
13481}
13482
13483static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13484{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013485 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013486 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013487
13488 if (kvm_mwait_in_guest(vcpu->kvm))
13489 return -EOPNOTSUPP;
13490
13491 vmx = to_vmx(vcpu);
13492 tscl = rdtsc();
13493 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13494 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013495 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13496
13497 if (delta_tsc > lapic_timer_advance_cycles)
13498 delta_tsc -= lapic_timer_advance_cycles;
13499 else
13500 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013501
13502 /* Convert to host delta tsc if tsc scaling is enabled */
13503 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13504 u64_shl_div_u64(delta_tsc,
13505 kvm_tsc_scaling_ratio_frac_bits,
13506 vcpu->arch.tsc_scaling_ratio,
13507 &delta_tsc))
13508 return -ERANGE;
13509
13510 /*
13511 * If the delta tsc can't fit in the 32 bit after the multi shift,
13512 * we can't use the preemption timer.
13513 * It's possible that it fits on later vmentries, but checking
13514 * on every vmentry is costly so we just use an hrtimer.
13515 */
13516 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13517 return -ERANGE;
13518
13519 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013520 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013521}
13522
13523static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13524{
Sean Christophersonf459a702018-08-27 15:21:11 -070013525 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013526}
13527#endif
13528
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013529static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013530{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013531 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013532 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013533}
13534
Kai Huang843e4332015-01-28 10:54:28 +080013535static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13536 struct kvm_memory_slot *slot)
13537{
13538 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13539 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13540}
13541
13542static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13543 struct kvm_memory_slot *slot)
13544{
13545 kvm_mmu_slot_set_dirty(kvm, slot);
13546}
13547
13548static void vmx_flush_log_dirty(struct kvm *kvm)
13549{
13550 kvm_flush_pml_buffers(kvm);
13551}
13552
Bandan Dasc5f983f2017-05-05 15:25:14 -040013553static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13554{
13555 struct vmcs12 *vmcs12;
13556 struct vcpu_vmx *vmx = to_vmx(vcpu);
13557 gpa_t gpa;
13558 struct page *page = NULL;
13559 u64 *pml_address;
13560
13561 if (is_guest_mode(vcpu)) {
13562 WARN_ON_ONCE(vmx->nested.pml_full);
13563
13564 /*
13565 * Check if PML is enabled for the nested guest.
13566 * Whether eptp bit 6 is set is already checked
13567 * as part of A/D emulation.
13568 */
13569 vmcs12 = get_vmcs12(vcpu);
13570 if (!nested_cpu_has_pml(vmcs12))
13571 return 0;
13572
Dan Carpenter47698862017-05-10 22:43:17 +030013573 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013574 vmx->nested.pml_full = true;
13575 return 1;
13576 }
13577
13578 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13579
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013580 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13581 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013582 return 0;
13583
13584 pml_address = kmap(page);
13585 pml_address[vmcs12->guest_pml_index--] = gpa;
13586 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013587 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013588 }
13589
13590 return 0;
13591}
13592
Kai Huang843e4332015-01-28 10:54:28 +080013593static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13594 struct kvm_memory_slot *memslot,
13595 gfn_t offset, unsigned long mask)
13596{
13597 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13598}
13599
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013600static void __pi_post_block(struct kvm_vcpu *vcpu)
13601{
13602 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13603 struct pi_desc old, new;
13604 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013605
13606 do {
13607 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013608 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13609 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013610
13611 dest = cpu_physical_id(vcpu->cpu);
13612
13613 if (x2apic_enabled())
13614 new.ndst = dest;
13615 else
13616 new.ndst = (dest << 8) & 0xFF00;
13617
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013618 /* set 'NV' to 'notification vector' */
13619 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013620 } while (cmpxchg64(&pi_desc->control, old.control,
13621 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013622
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013623 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13624 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013625 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013626 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013627 vcpu->pre_pcpu = -1;
13628 }
13629}
13630
Feng Wuefc64402015-09-18 22:29:51 +080013631/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013632 * This routine does the following things for vCPU which is going
13633 * to be blocked if VT-d PI is enabled.
13634 * - Store the vCPU to the wakeup list, so when interrupts happen
13635 * we can find the right vCPU to wake up.
13636 * - Change the Posted-interrupt descriptor as below:
13637 * 'NDST' <-- vcpu->pre_pcpu
13638 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13639 * - If 'ON' is set during this process, which means at least one
13640 * interrupt is posted for this vCPU, we cannot block it, in
13641 * this case, return 1, otherwise, return 0.
13642 *
13643 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013644static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013645{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013646 unsigned int dest;
13647 struct pi_desc old, new;
13648 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13649
13650 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013651 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13652 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013653 return 0;
13654
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013655 WARN_ON(irqs_disabled());
13656 local_irq_disable();
13657 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13658 vcpu->pre_pcpu = vcpu->cpu;
13659 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13660 list_add_tail(&vcpu->blocked_vcpu_list,
13661 &per_cpu(blocked_vcpu_on_cpu,
13662 vcpu->pre_pcpu));
13663 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13664 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013665
13666 do {
13667 old.control = new.control = pi_desc->control;
13668
Feng Wubf9f6ac2015-09-18 22:29:55 +080013669 WARN((pi_desc->sn == 1),
13670 "Warning: SN field of posted-interrupts "
13671 "is set before blocking\n");
13672
13673 /*
13674 * Since vCPU can be preempted during this process,
13675 * vcpu->cpu could be different with pre_pcpu, we
13676 * need to set pre_pcpu as the destination of wakeup
13677 * notification event, then we can find the right vCPU
13678 * to wakeup in wakeup handler if interrupts happen
13679 * when the vCPU is in blocked state.
13680 */
13681 dest = cpu_physical_id(vcpu->pre_pcpu);
13682
13683 if (x2apic_enabled())
13684 new.ndst = dest;
13685 else
13686 new.ndst = (dest << 8) & 0xFF00;
13687
13688 /* set 'NV' to 'wakeup vector' */
13689 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013690 } while (cmpxchg64(&pi_desc->control, old.control,
13691 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013692
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013693 /* We should not block the vCPU if an interrupt is posted for it. */
13694 if (pi_test_on(pi_desc) == 1)
13695 __pi_post_block(vcpu);
13696
13697 local_irq_enable();
13698 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013699}
13700
Yunhong Jiangbc225122016-06-13 14:19:58 -070013701static int vmx_pre_block(struct kvm_vcpu *vcpu)
13702{
13703 if (pi_pre_block(vcpu))
13704 return 1;
13705
Yunhong Jiang64672c92016-06-13 14:19:59 -070013706 if (kvm_lapic_hv_timer_in_use(vcpu))
13707 kvm_lapic_switch_to_sw_timer(vcpu);
13708
Yunhong Jiangbc225122016-06-13 14:19:58 -070013709 return 0;
13710}
13711
13712static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013713{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013714 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013715 return;
13716
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013717 WARN_ON(irqs_disabled());
13718 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013719 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013720 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013721}
13722
Yunhong Jiangbc225122016-06-13 14:19:58 -070013723static void vmx_post_block(struct kvm_vcpu *vcpu)
13724{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013725 if (kvm_x86_ops->set_hv_timer)
13726 kvm_lapic_switch_to_hv_timer(vcpu);
13727
Yunhong Jiangbc225122016-06-13 14:19:58 -070013728 pi_post_block(vcpu);
13729}
13730
Feng Wubf9f6ac2015-09-18 22:29:55 +080013731/*
Feng Wuefc64402015-09-18 22:29:51 +080013732 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13733 *
13734 * @kvm: kvm
13735 * @host_irq: host irq of the interrupt
13736 * @guest_irq: gsi of the interrupt
13737 * @set: set or unset PI
13738 * returns 0 on success, < 0 on failure
13739 */
13740static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13741 uint32_t guest_irq, bool set)
13742{
13743 struct kvm_kernel_irq_routing_entry *e;
13744 struct kvm_irq_routing_table *irq_rt;
13745 struct kvm_lapic_irq irq;
13746 struct kvm_vcpu *vcpu;
13747 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013748 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013749
13750 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013751 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13752 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013753 return 0;
13754
13755 idx = srcu_read_lock(&kvm->irq_srcu);
13756 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013757 if (guest_irq >= irq_rt->nr_rt_entries ||
13758 hlist_empty(&irq_rt->map[guest_irq])) {
13759 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13760 guest_irq, irq_rt->nr_rt_entries);
13761 goto out;
13762 }
Feng Wuefc64402015-09-18 22:29:51 +080013763
13764 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13765 if (e->type != KVM_IRQ_ROUTING_MSI)
13766 continue;
13767 /*
13768 * VT-d PI cannot support posting multicast/broadcast
13769 * interrupts to a vCPU, we still use interrupt remapping
13770 * for these kind of interrupts.
13771 *
13772 * For lowest-priority interrupts, we only support
13773 * those with single CPU as the destination, e.g. user
13774 * configures the interrupts via /proc/irq or uses
13775 * irqbalance to make the interrupts single-CPU.
13776 *
13777 * We will support full lowest-priority interrupt later.
13778 */
13779
Radim Krčmář371313132016-07-12 22:09:27 +020013780 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013781 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13782 /*
13783 * Make sure the IRTE is in remapped mode if
13784 * we don't handle it in posted mode.
13785 */
13786 ret = irq_set_vcpu_affinity(host_irq, NULL);
13787 if (ret < 0) {
13788 printk(KERN_INFO
13789 "failed to back to remapped mode, irq: %u\n",
13790 host_irq);
13791 goto out;
13792 }
13793
Feng Wuefc64402015-09-18 22:29:51 +080013794 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013795 }
Feng Wuefc64402015-09-18 22:29:51 +080013796
13797 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13798 vcpu_info.vector = irq.vector;
13799
hu huajun2698d822018-04-11 15:16:40 +080013800 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013801 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13802
13803 if (set)
13804 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013805 else
Feng Wuefc64402015-09-18 22:29:51 +080013806 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013807
13808 if (ret < 0) {
13809 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13810 __func__);
13811 goto out;
13812 }
13813 }
13814
13815 ret = 0;
13816out:
13817 srcu_read_unlock(&kvm->irq_srcu, idx);
13818 return ret;
13819}
13820
Ashok Rajc45dcc72016-06-22 14:59:56 +080013821static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13822{
13823 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13824 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13825 FEATURE_CONTROL_LMCE;
13826 else
13827 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13828 ~FEATURE_CONTROL_LMCE;
13829}
13830
Ladi Prosek72d7b372017-10-11 16:54:41 +020013831static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13832{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013833 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13834 if (to_vmx(vcpu)->nested.nested_run_pending)
13835 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013836 return 1;
13837}
13838
Ladi Prosek0234bf82017-10-11 16:54:40 +020013839static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13840{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013841 struct vcpu_vmx *vmx = to_vmx(vcpu);
13842
13843 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13844 if (vmx->nested.smm.guest_mode)
13845 nested_vmx_vmexit(vcpu, -1, 0, 0);
13846
13847 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13848 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013849 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013850 return 0;
13851}
13852
13853static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13854{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013855 struct vcpu_vmx *vmx = to_vmx(vcpu);
13856 int ret;
13857
13858 if (vmx->nested.smm.vmxon) {
13859 vmx->nested.vmxon = true;
13860 vmx->nested.smm.vmxon = false;
13861 }
13862
13863 if (vmx->nested.smm.guest_mode) {
13864 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013865 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013866 vcpu->arch.hflags |= HF_SMM_MASK;
13867 if (ret)
13868 return ret;
13869
13870 vmx->nested.smm.guest_mode = false;
13871 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013872 return 0;
13873}
13874
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013875static int enable_smi_window(struct kvm_vcpu *vcpu)
13876{
13877 return 0;
13878}
13879
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013880static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13881 struct kvm_nested_state __user *user_kvm_nested_state,
13882 u32 user_data_size)
13883{
13884 struct vcpu_vmx *vmx;
13885 struct vmcs12 *vmcs12;
13886 struct kvm_nested_state kvm_state = {
13887 .flags = 0,
13888 .format = 0,
13889 .size = sizeof(kvm_state),
13890 .vmx.vmxon_pa = -1ull,
13891 .vmx.vmcs_pa = -1ull,
13892 };
13893
13894 if (!vcpu)
13895 return kvm_state.size + 2 * VMCS12_SIZE;
13896
13897 vmx = to_vmx(vcpu);
13898 vmcs12 = get_vmcs12(vcpu);
13899 if (nested_vmx_allowed(vcpu) &&
13900 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13901 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13902 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13903
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013904 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013905 kvm_state.size += VMCS12_SIZE;
13906
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013907 if (is_guest_mode(vcpu) &&
13908 nested_cpu_has_shadow_vmcs(vmcs12) &&
13909 vmcs12->vmcs_link_pointer != -1ull)
13910 kvm_state.size += VMCS12_SIZE;
13911 }
13912
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013913 if (vmx->nested.smm.vmxon)
13914 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13915
13916 if (vmx->nested.smm.guest_mode)
13917 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13918
13919 if (is_guest_mode(vcpu)) {
13920 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13921
13922 if (vmx->nested.nested_run_pending)
13923 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13924 }
13925 }
13926
13927 if (user_data_size < kvm_state.size)
13928 goto out;
13929
13930 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13931 return -EFAULT;
13932
13933 if (vmx->nested.current_vmptr == -1ull)
13934 goto out;
13935
13936 /*
13937 * When running L2, the authoritative vmcs12 state is in the
13938 * vmcs02. When running L1, the authoritative vmcs12 state is
13939 * in the shadow vmcs linked to vmcs01, unless
13940 * sync_shadow_vmcs is set, in which case, the authoritative
13941 * vmcs12 state is in the vmcs12 already.
13942 */
13943 if (is_guest_mode(vcpu))
13944 sync_vmcs12(vcpu, vmcs12);
13945 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13946 copy_shadow_to_vmcs12(vmx);
13947
13948 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13949 return -EFAULT;
13950
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013951 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13952 vmcs12->vmcs_link_pointer != -1ull) {
13953 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13954 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13955 return -EFAULT;
13956 }
13957
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013958out:
13959 return kvm_state.size;
13960}
13961
13962static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13963 struct kvm_nested_state __user *user_kvm_nested_state,
13964 struct kvm_nested_state *kvm_state)
13965{
13966 struct vcpu_vmx *vmx = to_vmx(vcpu);
13967 struct vmcs12 *vmcs12;
13968 u32 exit_qual;
13969 int ret;
13970
13971 if (kvm_state->format != 0)
13972 return -EINVAL;
13973
13974 if (!nested_vmx_allowed(vcpu))
13975 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13976
13977 if (kvm_state->vmx.vmxon_pa == -1ull) {
13978 if (kvm_state->vmx.smm.flags)
13979 return -EINVAL;
13980
13981 if (kvm_state->vmx.vmcs_pa != -1ull)
13982 return -EINVAL;
13983
13984 vmx_leave_nested(vcpu);
13985 return 0;
13986 }
13987
13988 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13989 return -EINVAL;
13990
13991 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13992 return -EINVAL;
13993
13994 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13995 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13996 return -EINVAL;
13997
13998 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13999 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14000 return -EINVAL;
14001
14002 if (kvm_state->vmx.smm.flags &
14003 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14004 return -EINVAL;
14005
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014006 /*
14007 * SMM temporarily disables VMX, so we cannot be in guest mode,
14008 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14009 * must be zero.
14010 */
14011 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14012 return -EINVAL;
14013
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014014 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14015 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14016 return -EINVAL;
14017
14018 vmx_leave_nested(vcpu);
14019 if (kvm_state->vmx.vmxon_pa == -1ull)
14020 return 0;
14021
14022 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14023 ret = enter_vmx_operation(vcpu);
14024 if (ret)
14025 return ret;
14026
14027 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14028
14029 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14030 vmx->nested.smm.vmxon = true;
14031 vmx->nested.vmxon = false;
14032
14033 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14034 vmx->nested.smm.guest_mode = true;
14035 }
14036
14037 vmcs12 = get_vmcs12(vcpu);
14038 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14039 return -EFAULT;
14040
Liran Alon392b2f22018-06-23 02:35:01 +030014041 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014042 return -EINVAL;
14043
14044 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14045 return 0;
14046
14047 vmx->nested.nested_run_pending =
14048 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14049
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014050 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14051 vmcs12->vmcs_link_pointer != -1ull) {
14052 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14053 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14054 return -EINVAL;
14055
14056 if (copy_from_user(shadow_vmcs12,
14057 user_kvm_nested_state->data + VMCS12_SIZE,
14058 sizeof(*vmcs12)))
14059 return -EFAULT;
14060
14061 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14062 !shadow_vmcs12->hdr.shadow_vmcs)
14063 return -EINVAL;
14064 }
14065
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014066 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14067 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14068 return -EINVAL;
14069
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014070 vmx->nested.dirty_vmcs12 = true;
14071 ret = enter_vmx_non_root_mode(vcpu, NULL);
14072 if (ret)
14073 return -EINVAL;
14074
14075 return 0;
14076}
14077
Kees Cook404f6aa2016-08-08 16:29:06 -070014078static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014079 .cpu_has_kvm_support = cpu_has_kvm_support,
14080 .disabled_by_bios = vmx_disabled_by_bios,
14081 .hardware_setup = hardware_setup,
14082 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014083 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014084 .hardware_enable = hardware_enable,
14085 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014086 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014087 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014088
Wanpeng Lib31c1142018-03-12 04:53:04 -070014089 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014090 .vm_alloc = vmx_vm_alloc,
14091 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014092
Avi Kivity6aa8b732006-12-10 02:21:36 -080014093 .vcpu_create = vmx_create_vcpu,
14094 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014095 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014096
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014097 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014098 .vcpu_load = vmx_vcpu_load,
14099 .vcpu_put = vmx_vcpu_put,
14100
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014101 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014102 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014103 .get_msr = vmx_get_msr,
14104 .set_msr = vmx_set_msr,
14105 .get_segment_base = vmx_get_segment_base,
14106 .get_segment = vmx_get_segment,
14107 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014108 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014109 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014110 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014111 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014112 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014113 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014114 .set_cr3 = vmx_set_cr3,
14115 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014116 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014117 .get_idt = vmx_get_idt,
14118 .set_idt = vmx_set_idt,
14119 .get_gdt = vmx_get_gdt,
14120 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014121 .get_dr6 = vmx_get_dr6,
14122 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014123 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014124 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014125 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014126 .get_rflags = vmx_get_rflags,
14127 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014128
Avi Kivity6aa8b732006-12-10 02:21:36 -080014129 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014130 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014131
Avi Kivity6aa8b732006-12-10 02:21:36 -080014132 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014133 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014134 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014135 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14136 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014137 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014138 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014139 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014140 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014141 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014142 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014143 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014144 .get_nmi_mask = vmx_get_nmi_mask,
14145 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014146 .enable_nmi_window = enable_nmi_window,
14147 .enable_irq_window = enable_irq_window,
14148 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014149 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014150 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014151 .get_enable_apicv = vmx_get_enable_apicv,
14152 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014153 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014154 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014155 .hwapic_irr_update = vmx_hwapic_irr_update,
14156 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014157 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014158 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14159 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014160
Izik Eiduscbc94022007-10-25 00:29:55 +020014161 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014162 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014163 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014164 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014165
Avi Kivity586f9602010-11-18 13:09:54 +020014166 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014167
Sheng Yang17cc3932010-01-05 19:02:27 +080014168 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014169
14170 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014171
14172 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014173 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014174
14175 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014176
14177 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014178
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014179 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014180 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014181
14182 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014183
14184 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014185 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014186 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014187 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014188 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014189
14190 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014191 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014192
14193 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014194
14195 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14196 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14197 .flush_log_dirty = vmx_flush_log_dirty,
14198 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014199 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014200
Feng Wubf9f6ac2015-09-18 22:29:55 +080014201 .pre_block = vmx_pre_block,
14202 .post_block = vmx_post_block,
14203
Wei Huang25462f72015-06-19 15:45:05 +020014204 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014205
14206 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014207
14208#ifdef CONFIG_X86_64
14209 .set_hv_timer = vmx_set_hv_timer,
14210 .cancel_hv_timer = vmx_cancel_hv_timer,
14211#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014212
14213 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014214
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014215 .get_nested_state = vmx_get_nested_state,
14216 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014217 .get_vmcs12_pages = nested_get_vmcs12_pages,
14218
Ladi Prosek72d7b372017-10-11 16:54:41 +020014219 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014220 .pre_enter_smm = vmx_pre_enter_smm,
14221 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014222 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014223};
14224
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014225static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014226{
14227 if (vmx_l1d_flush_pages) {
14228 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14229 vmx_l1d_flush_pages = NULL;
14230 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014231 /* Restore state so sysfs ignores VMX */
14232 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014233}
14234
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014235static void vmx_exit(void)
14236{
14237#ifdef CONFIG_KEXEC_CORE
14238 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14239 synchronize_rcu();
14240#endif
14241
14242 kvm_exit();
14243
14244#if IS_ENABLED(CONFIG_HYPERV)
14245 if (static_branch_unlikely(&enable_evmcs)) {
14246 int cpu;
14247 struct hv_vp_assist_page *vp_ap;
14248 /*
14249 * Reset everything to support using non-enlightened VMCS
14250 * access later (e.g. when we reload the module with
14251 * enlightened_vmcs=0)
14252 */
14253 for_each_online_cpu(cpu) {
14254 vp_ap = hv_get_vp_assist_page(cpu);
14255
14256 if (!vp_ap)
14257 continue;
14258
14259 vp_ap->current_nested_vmcs = 0;
14260 vp_ap->enlighten_vmentry = 0;
14261 }
14262
14263 static_branch_disable(&enable_evmcs);
14264 }
14265#endif
14266 vmx_cleanup_l1d_flush();
14267}
14268module_exit(vmx_exit);
14269
Avi Kivity6aa8b732006-12-10 02:21:36 -080014270static int __init vmx_init(void)
14271{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014272 int r;
14273
14274#if IS_ENABLED(CONFIG_HYPERV)
14275 /*
14276 * Enlightened VMCS usage should be recommended and the host needs
14277 * to support eVMCS v1 or above. We can also disable eVMCS support
14278 * with module parameter.
14279 */
14280 if (enlightened_vmcs &&
14281 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14282 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14283 KVM_EVMCS_VERSION) {
14284 int cpu;
14285
14286 /* Check that we have assist pages on all online CPUs */
14287 for_each_online_cpu(cpu) {
14288 if (!hv_get_vp_assist_page(cpu)) {
14289 enlightened_vmcs = false;
14290 break;
14291 }
14292 }
14293
14294 if (enlightened_vmcs) {
14295 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14296 static_branch_enable(&enable_evmcs);
14297 }
14298 } else {
14299 enlightened_vmcs = false;
14300 }
14301#endif
14302
14303 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014304 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014305 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014306 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014307
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014308 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014309 * Must be called after kvm_init() so enable_ept is properly set
14310 * up. Hand the parameter mitigation value in which was stored in
14311 * the pre module init parser. If no parameter was given, it will
14312 * contain 'auto' which will be turned into the default 'cond'
14313 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014314 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014315 if (boot_cpu_has(X86_BUG_L1TF)) {
14316 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14317 if (r) {
14318 vmx_exit();
14319 return r;
14320 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014321 }
14322
Dave Young2965faa2015-09-09 15:38:55 -070014323#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014324 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14325 crash_vmclear_local_loaded_vmcss);
14326#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014327 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014328
He, Qingfdef3ad2007-04-30 09:45:24 +030014329 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014330}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014331module_init(vmx_init);