blob: 533a327372c876df0b1c2b99ea3558e2aaa92df4 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
124#define MSR_BITMAP_MODE_LM 4
125
Haozhong Zhang64903d62015-10-20 15:39:09 +0800126#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
127
Yunhong Jiang64672c92016-06-13 14:19:59 -0700128/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
129static int __read_mostly cpu_preemption_timer_multi;
130static bool __read_mostly enable_preemption_timer = 1;
131#ifdef CONFIG_X86_64
132module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
133#endif
134
Gleb Natapov50378782013-02-04 16:00:28 +0200135#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800136#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
137#define KVM_VM_CR0_ALWAYS_ON \
138 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
139 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200140#define KVM_CR4_GUEST_OWNED_BITS \
141 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800142 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200143
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800144#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200145#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
146#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
147
Avi Kivity78ac8b42010-04-08 18:19:35 +0300148#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
149
Jan Kiszkaf41245002014-03-07 20:03:13 +0100150#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
151
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800152/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300153 * Hyper-V requires all of these, so mark them as supported even though
154 * they are just treated the same as all-context.
155 */
156#define VMX_VPID_EXTENT_SUPPORTED_MASK \
157 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
158 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
160 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
161
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800162/*
163 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
164 * ple_gap: upper bound on the amount of time between two successive
165 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500166 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800167 * ple_window: upper bound on the amount of time a guest is allowed to execute
168 * in a PAUSE loop. Tests indicate that most spinlocks are held for
169 * less than 2^12 cycles
170 * Time is measured based on a counter that runs at the same rate as the TSC,
171 * refer SDM volume 3b section 21.6.13 & 22.1.3.
172 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400173static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200174
Babu Moger7fbc85a2018-03-16 16:37:22 -0400175static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
176module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800177
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200178/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400179static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400180module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200181
182/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400183static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400184module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200185
186/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400187static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
188module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200189
Avi Kivity83287ea422012-09-16 15:10:57 +0300190extern const ulong vmx_return;
191
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200193static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200194static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200195
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200196/* Storage for pre module init parameter parsing */
197static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200198
199static const struct {
200 const char *option;
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;
Paolo Bonzini8a1b4392017-11-06 13:31:12 +0100400 /* Support for vnmi-less CPUs */
401 int soft_vnmi_blocked;
402 ktime_t entry_time;
403 s64 vnmi_blocked_time;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100404 unsigned long *msr_bitmap;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300405 struct list_head loaded_vmcss_on_cpu_link;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700406 struct vmcs_host_state host_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300407};
408
Avi Kivity26bb0982009-09-07 11:14:12 +0300409struct shared_msr_entry {
410 unsigned index;
411 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200412 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300413};
414
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300415/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300416 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
417 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
418 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
419 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
420 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
421 * More than one of these structures may exist, if L1 runs multiple L2 guests.
Jim Mattsonde3a0022017-11-27 17:22:25 -0600422 * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300423 * underlying hardware which will be used to run L2.
424 * This structure is packed to ensure that its layout is identical across
425 * machines (necessary for live migration).
Jim Mattsonb348e792018-05-01 15:40:27 -0700426 *
427 * IMPORTANT: Changing the layout of existing fields in this structure
428 * will break save/restore compatibility with older kvm releases. When
429 * adding new fields, either use space in the reserved padding* arrays
430 * or add the new fields to the end of the structure.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300431 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300432typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300433struct __packed vmcs12 {
434 /* According to the Intel spec, a VMCS region must start with the
435 * following two fields. Then follow implementation-specific data.
436 */
Liran Alon392b2f22018-06-23 02:35:01 +0300437 struct vmcs_hdr hdr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300438 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300439
Nadav Har'El27d6c862011-05-25 23:06:59 +0300440 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
441 u32 padding[7]; /* room for future expansion */
442
Nadav Har'El22bd0352011-05-25 23:05:57 +0300443 u64 io_bitmap_a;
444 u64 io_bitmap_b;
445 u64 msr_bitmap;
446 u64 vm_exit_msr_store_addr;
447 u64 vm_exit_msr_load_addr;
448 u64 vm_entry_msr_load_addr;
449 u64 tsc_offset;
450 u64 virtual_apic_page_addr;
451 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800452 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300453 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800454 u64 eoi_exit_bitmap0;
455 u64 eoi_exit_bitmap1;
456 u64 eoi_exit_bitmap2;
457 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800458 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300459 u64 guest_physical_address;
460 u64 vmcs_link_pointer;
461 u64 guest_ia32_debugctl;
462 u64 guest_ia32_pat;
463 u64 guest_ia32_efer;
464 u64 guest_ia32_perf_global_ctrl;
465 u64 guest_pdptr0;
466 u64 guest_pdptr1;
467 u64 guest_pdptr2;
468 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100469 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300470 u64 host_ia32_pat;
471 u64 host_ia32_efer;
472 u64 host_ia32_perf_global_ctrl;
Jim Mattsonb348e792018-05-01 15:40:27 -0700473 u64 vmread_bitmap;
474 u64 vmwrite_bitmap;
475 u64 vm_function_control;
476 u64 eptp_list_address;
477 u64 pml_address;
478 u64 padding64[3]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300479 /*
480 * To allow migration of L1 (complete with its L2 guests) between
481 * machines of different natural widths (32 or 64 bit), we cannot have
482 * unsigned long fields with no explict size. We use u64 (aliased
483 * natural_width) instead. Luckily, x86 is little-endian.
484 */
485 natural_width cr0_guest_host_mask;
486 natural_width cr4_guest_host_mask;
487 natural_width cr0_read_shadow;
488 natural_width cr4_read_shadow;
489 natural_width cr3_target_value0;
490 natural_width cr3_target_value1;
491 natural_width cr3_target_value2;
492 natural_width cr3_target_value3;
493 natural_width exit_qualification;
494 natural_width guest_linear_address;
495 natural_width guest_cr0;
496 natural_width guest_cr3;
497 natural_width guest_cr4;
498 natural_width guest_es_base;
499 natural_width guest_cs_base;
500 natural_width guest_ss_base;
501 natural_width guest_ds_base;
502 natural_width guest_fs_base;
503 natural_width guest_gs_base;
504 natural_width guest_ldtr_base;
505 natural_width guest_tr_base;
506 natural_width guest_gdtr_base;
507 natural_width guest_idtr_base;
508 natural_width guest_dr7;
509 natural_width guest_rsp;
510 natural_width guest_rip;
511 natural_width guest_rflags;
512 natural_width guest_pending_dbg_exceptions;
513 natural_width guest_sysenter_esp;
514 natural_width guest_sysenter_eip;
515 natural_width host_cr0;
516 natural_width host_cr3;
517 natural_width host_cr4;
518 natural_width host_fs_base;
519 natural_width host_gs_base;
520 natural_width host_tr_base;
521 natural_width host_gdtr_base;
522 natural_width host_idtr_base;
523 natural_width host_ia32_sysenter_esp;
524 natural_width host_ia32_sysenter_eip;
525 natural_width host_rsp;
526 natural_width host_rip;
527 natural_width paddingl[8]; /* room for future expansion */
528 u32 pin_based_vm_exec_control;
529 u32 cpu_based_vm_exec_control;
530 u32 exception_bitmap;
531 u32 page_fault_error_code_mask;
532 u32 page_fault_error_code_match;
533 u32 cr3_target_count;
534 u32 vm_exit_controls;
535 u32 vm_exit_msr_store_count;
536 u32 vm_exit_msr_load_count;
537 u32 vm_entry_controls;
538 u32 vm_entry_msr_load_count;
539 u32 vm_entry_intr_info_field;
540 u32 vm_entry_exception_error_code;
541 u32 vm_entry_instruction_len;
542 u32 tpr_threshold;
543 u32 secondary_vm_exec_control;
544 u32 vm_instruction_error;
545 u32 vm_exit_reason;
546 u32 vm_exit_intr_info;
547 u32 vm_exit_intr_error_code;
548 u32 idt_vectoring_info_field;
549 u32 idt_vectoring_error_code;
550 u32 vm_exit_instruction_len;
551 u32 vmx_instruction_info;
552 u32 guest_es_limit;
553 u32 guest_cs_limit;
554 u32 guest_ss_limit;
555 u32 guest_ds_limit;
556 u32 guest_fs_limit;
557 u32 guest_gs_limit;
558 u32 guest_ldtr_limit;
559 u32 guest_tr_limit;
560 u32 guest_gdtr_limit;
561 u32 guest_idtr_limit;
562 u32 guest_es_ar_bytes;
563 u32 guest_cs_ar_bytes;
564 u32 guest_ss_ar_bytes;
565 u32 guest_ds_ar_bytes;
566 u32 guest_fs_ar_bytes;
567 u32 guest_gs_ar_bytes;
568 u32 guest_ldtr_ar_bytes;
569 u32 guest_tr_ar_bytes;
570 u32 guest_interruptibility_info;
571 u32 guest_activity_state;
572 u32 guest_sysenter_cs;
573 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100574 u32 vmx_preemption_timer_value;
575 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300576 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800577 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300578 u16 guest_es_selector;
579 u16 guest_cs_selector;
580 u16 guest_ss_selector;
581 u16 guest_ds_selector;
582 u16 guest_fs_selector;
583 u16 guest_gs_selector;
584 u16 guest_ldtr_selector;
585 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800586 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300587 u16 host_es_selector;
588 u16 host_cs_selector;
589 u16 host_ss_selector;
590 u16 host_ds_selector;
591 u16 host_fs_selector;
592 u16 host_gs_selector;
593 u16 host_tr_selector;
Jim Mattsonb348e792018-05-01 15:40:27 -0700594 u16 guest_pml_index;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300595};
596
597/*
Jim Mattson21ebf532018-05-01 15:40:28 -0700598 * For save/restore compatibility, the vmcs12 field offsets must not change.
599 */
600#define CHECK_OFFSET(field, loc) \
601 BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \
602 "Offset of " #field " in struct vmcs12 has changed.")
603
604static inline void vmx_check_vmcs12_offsets(void) {
Liran Alon392b2f22018-06-23 02:35:01 +0300605 CHECK_OFFSET(hdr, 0);
Jim Mattson21ebf532018-05-01 15:40:28 -0700606 CHECK_OFFSET(abort, 4);
607 CHECK_OFFSET(launch_state, 8);
608 CHECK_OFFSET(io_bitmap_a, 40);
609 CHECK_OFFSET(io_bitmap_b, 48);
610 CHECK_OFFSET(msr_bitmap, 56);
611 CHECK_OFFSET(vm_exit_msr_store_addr, 64);
612 CHECK_OFFSET(vm_exit_msr_load_addr, 72);
613 CHECK_OFFSET(vm_entry_msr_load_addr, 80);
614 CHECK_OFFSET(tsc_offset, 88);
615 CHECK_OFFSET(virtual_apic_page_addr, 96);
616 CHECK_OFFSET(apic_access_addr, 104);
617 CHECK_OFFSET(posted_intr_desc_addr, 112);
618 CHECK_OFFSET(ept_pointer, 120);
619 CHECK_OFFSET(eoi_exit_bitmap0, 128);
620 CHECK_OFFSET(eoi_exit_bitmap1, 136);
621 CHECK_OFFSET(eoi_exit_bitmap2, 144);
622 CHECK_OFFSET(eoi_exit_bitmap3, 152);
623 CHECK_OFFSET(xss_exit_bitmap, 160);
624 CHECK_OFFSET(guest_physical_address, 168);
625 CHECK_OFFSET(vmcs_link_pointer, 176);
626 CHECK_OFFSET(guest_ia32_debugctl, 184);
627 CHECK_OFFSET(guest_ia32_pat, 192);
628 CHECK_OFFSET(guest_ia32_efer, 200);
629 CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208);
630 CHECK_OFFSET(guest_pdptr0, 216);
631 CHECK_OFFSET(guest_pdptr1, 224);
632 CHECK_OFFSET(guest_pdptr2, 232);
633 CHECK_OFFSET(guest_pdptr3, 240);
634 CHECK_OFFSET(guest_bndcfgs, 248);
635 CHECK_OFFSET(host_ia32_pat, 256);
636 CHECK_OFFSET(host_ia32_efer, 264);
637 CHECK_OFFSET(host_ia32_perf_global_ctrl, 272);
638 CHECK_OFFSET(vmread_bitmap, 280);
639 CHECK_OFFSET(vmwrite_bitmap, 288);
640 CHECK_OFFSET(vm_function_control, 296);
641 CHECK_OFFSET(eptp_list_address, 304);
642 CHECK_OFFSET(pml_address, 312);
643 CHECK_OFFSET(cr0_guest_host_mask, 344);
644 CHECK_OFFSET(cr4_guest_host_mask, 352);
645 CHECK_OFFSET(cr0_read_shadow, 360);
646 CHECK_OFFSET(cr4_read_shadow, 368);
647 CHECK_OFFSET(cr3_target_value0, 376);
648 CHECK_OFFSET(cr3_target_value1, 384);
649 CHECK_OFFSET(cr3_target_value2, 392);
650 CHECK_OFFSET(cr3_target_value3, 400);
651 CHECK_OFFSET(exit_qualification, 408);
652 CHECK_OFFSET(guest_linear_address, 416);
653 CHECK_OFFSET(guest_cr0, 424);
654 CHECK_OFFSET(guest_cr3, 432);
655 CHECK_OFFSET(guest_cr4, 440);
656 CHECK_OFFSET(guest_es_base, 448);
657 CHECK_OFFSET(guest_cs_base, 456);
658 CHECK_OFFSET(guest_ss_base, 464);
659 CHECK_OFFSET(guest_ds_base, 472);
660 CHECK_OFFSET(guest_fs_base, 480);
661 CHECK_OFFSET(guest_gs_base, 488);
662 CHECK_OFFSET(guest_ldtr_base, 496);
663 CHECK_OFFSET(guest_tr_base, 504);
664 CHECK_OFFSET(guest_gdtr_base, 512);
665 CHECK_OFFSET(guest_idtr_base, 520);
666 CHECK_OFFSET(guest_dr7, 528);
667 CHECK_OFFSET(guest_rsp, 536);
668 CHECK_OFFSET(guest_rip, 544);
669 CHECK_OFFSET(guest_rflags, 552);
670 CHECK_OFFSET(guest_pending_dbg_exceptions, 560);
671 CHECK_OFFSET(guest_sysenter_esp, 568);
672 CHECK_OFFSET(guest_sysenter_eip, 576);
673 CHECK_OFFSET(host_cr0, 584);
674 CHECK_OFFSET(host_cr3, 592);
675 CHECK_OFFSET(host_cr4, 600);
676 CHECK_OFFSET(host_fs_base, 608);
677 CHECK_OFFSET(host_gs_base, 616);
678 CHECK_OFFSET(host_tr_base, 624);
679 CHECK_OFFSET(host_gdtr_base, 632);
680 CHECK_OFFSET(host_idtr_base, 640);
681 CHECK_OFFSET(host_ia32_sysenter_esp, 648);
682 CHECK_OFFSET(host_ia32_sysenter_eip, 656);
683 CHECK_OFFSET(host_rsp, 664);
684 CHECK_OFFSET(host_rip, 672);
685 CHECK_OFFSET(pin_based_vm_exec_control, 744);
686 CHECK_OFFSET(cpu_based_vm_exec_control, 748);
687 CHECK_OFFSET(exception_bitmap, 752);
688 CHECK_OFFSET(page_fault_error_code_mask, 756);
689 CHECK_OFFSET(page_fault_error_code_match, 760);
690 CHECK_OFFSET(cr3_target_count, 764);
691 CHECK_OFFSET(vm_exit_controls, 768);
692 CHECK_OFFSET(vm_exit_msr_store_count, 772);
693 CHECK_OFFSET(vm_exit_msr_load_count, 776);
694 CHECK_OFFSET(vm_entry_controls, 780);
695 CHECK_OFFSET(vm_entry_msr_load_count, 784);
696 CHECK_OFFSET(vm_entry_intr_info_field, 788);
697 CHECK_OFFSET(vm_entry_exception_error_code, 792);
698 CHECK_OFFSET(vm_entry_instruction_len, 796);
699 CHECK_OFFSET(tpr_threshold, 800);
700 CHECK_OFFSET(secondary_vm_exec_control, 804);
701 CHECK_OFFSET(vm_instruction_error, 808);
702 CHECK_OFFSET(vm_exit_reason, 812);
703 CHECK_OFFSET(vm_exit_intr_info, 816);
704 CHECK_OFFSET(vm_exit_intr_error_code, 820);
705 CHECK_OFFSET(idt_vectoring_info_field, 824);
706 CHECK_OFFSET(idt_vectoring_error_code, 828);
707 CHECK_OFFSET(vm_exit_instruction_len, 832);
708 CHECK_OFFSET(vmx_instruction_info, 836);
709 CHECK_OFFSET(guest_es_limit, 840);
710 CHECK_OFFSET(guest_cs_limit, 844);
711 CHECK_OFFSET(guest_ss_limit, 848);
712 CHECK_OFFSET(guest_ds_limit, 852);
713 CHECK_OFFSET(guest_fs_limit, 856);
714 CHECK_OFFSET(guest_gs_limit, 860);
715 CHECK_OFFSET(guest_ldtr_limit, 864);
716 CHECK_OFFSET(guest_tr_limit, 868);
717 CHECK_OFFSET(guest_gdtr_limit, 872);
718 CHECK_OFFSET(guest_idtr_limit, 876);
719 CHECK_OFFSET(guest_es_ar_bytes, 880);
720 CHECK_OFFSET(guest_cs_ar_bytes, 884);
721 CHECK_OFFSET(guest_ss_ar_bytes, 888);
722 CHECK_OFFSET(guest_ds_ar_bytes, 892);
723 CHECK_OFFSET(guest_fs_ar_bytes, 896);
724 CHECK_OFFSET(guest_gs_ar_bytes, 900);
725 CHECK_OFFSET(guest_ldtr_ar_bytes, 904);
726 CHECK_OFFSET(guest_tr_ar_bytes, 908);
727 CHECK_OFFSET(guest_interruptibility_info, 912);
728 CHECK_OFFSET(guest_activity_state, 916);
729 CHECK_OFFSET(guest_sysenter_cs, 920);
730 CHECK_OFFSET(host_ia32_sysenter_cs, 924);
731 CHECK_OFFSET(vmx_preemption_timer_value, 928);
732 CHECK_OFFSET(virtual_processor_id, 960);
733 CHECK_OFFSET(posted_intr_nv, 962);
734 CHECK_OFFSET(guest_es_selector, 964);
735 CHECK_OFFSET(guest_cs_selector, 966);
736 CHECK_OFFSET(guest_ss_selector, 968);
737 CHECK_OFFSET(guest_ds_selector, 970);
738 CHECK_OFFSET(guest_fs_selector, 972);
739 CHECK_OFFSET(guest_gs_selector, 974);
740 CHECK_OFFSET(guest_ldtr_selector, 976);
741 CHECK_OFFSET(guest_tr_selector, 978);
742 CHECK_OFFSET(guest_intr_status, 980);
743 CHECK_OFFSET(host_es_selector, 982);
744 CHECK_OFFSET(host_cs_selector, 984);
745 CHECK_OFFSET(host_ss_selector, 986);
746 CHECK_OFFSET(host_ds_selector, 988);
747 CHECK_OFFSET(host_fs_selector, 990);
748 CHECK_OFFSET(host_gs_selector, 992);
749 CHECK_OFFSET(host_tr_selector, 994);
750 CHECK_OFFSET(guest_pml_index, 996);
751}
752
753/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300754 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
755 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
756 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
Jim Mattsonb348e792018-05-01 15:40:27 -0700757 *
758 * IMPORTANT: Changing this value will break save/restore compatibility with
759 * older kvm releases.
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 */
761#define VMCS12_REVISION 0x11e57ed0
762
763/*
764 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
765 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
766 * current implementation, 4K are reserved to avoid future complications.
767 */
768#define VMCS12_SIZE 0x1000
769
770/*
Jim Mattson5b157062017-12-22 12:11:12 -0800771 * VMCS12_MAX_FIELD_INDEX is the highest index value used in any
772 * supported VMCS12 field encoding.
773 */
774#define VMCS12_MAX_FIELD_INDEX 0x17
775
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100776struct nested_vmx_msrs {
777 /*
778 * We only store the "true" versions of the VMX capability MSRs. We
779 * generate the "non-true" versions by setting the must-be-1 bits
780 * according to the SDM.
781 */
782 u32 procbased_ctls_low;
783 u32 procbased_ctls_high;
784 u32 secondary_ctls_low;
785 u32 secondary_ctls_high;
786 u32 pinbased_ctls_low;
787 u32 pinbased_ctls_high;
788 u32 exit_ctls_low;
789 u32 exit_ctls_high;
790 u32 entry_ctls_low;
791 u32 entry_ctls_high;
792 u32 misc_low;
793 u32 misc_high;
794 u32 ept_caps;
795 u32 vpid_caps;
796 u64 basic;
797 u64 cr0_fixed0;
798 u64 cr0_fixed1;
799 u64 cr4_fixed0;
800 u64 cr4_fixed1;
801 u64 vmcs_enum;
802 u64 vmfunc_controls;
803};
804
Jim Mattson5b157062017-12-22 12:11:12 -0800805/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300806 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
807 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
808 */
809struct nested_vmx {
810 /* Has the level1 guest done vmxon? */
811 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400812 gpa_t vmxon_ptr;
Bandan Dasc5f983f2017-05-05 15:25:14 -0400813 bool pml_full;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300814
815 /* The guest-physical address of the current VMCS L1 keeps for L2 */
816 gpa_t current_vmptr;
David Matlack4f2777b2016-07-13 17:16:37 -0700817 /*
818 * Cache of the guest's VMCS, existing outside of guest memory.
819 * Loaded from guest memory during VMPTRLD. Flushed to guest
David Matlack8ca44e82017-08-01 14:00:39 -0700820 * memory during VMCLEAR and VMPTRLD.
David Matlack4f2777b2016-07-13 17:16:37 -0700821 */
822 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300823 /*
Liran Alon61ada742018-06-23 02:35:08 +0300824 * Cache of the guest's shadow VMCS, existing outside of guest
825 * memory. Loaded from guest memory during VM entry. Flushed
826 * to guest memory during VM exit.
827 */
828 struct vmcs12 *cached_shadow_vmcs12;
829 /*
Abel Gordon012f83c2013-04-18 14:39:25 +0300830 * Indicates if the shadow vmcs must be updated with the
831 * data hold by vmcs12
832 */
833 bool sync_shadow_vmcs;
Paolo Bonzini74a497f2017-12-20 13:55:39 +0100834 bool dirty_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300835
Jim Mattson8d860bb2018-05-09 16:56:05 -0400836 bool change_vmcs01_virtual_apic_mode;
837
Nadav Har'El644d7112011-05-25 23:12:35 +0300838 /* L2 must run next, and mustn't decide to exit to L1. */
839 bool nested_run_pending;
Jim Mattsonde3a0022017-11-27 17:22:25 -0600840
841 struct loaded_vmcs vmcs02;
842
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300843 /*
Jim Mattsonde3a0022017-11-27 17:22:25 -0600844 * Guest pages referred to in the vmcs02 with host-physical
845 * pointers, so we must keep them pinned while L2 runs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300846 */
847 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800848 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800849 struct page *pi_desc_page;
850 struct pi_desc *pi_desc;
851 bool pi_pending;
852 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100853
854 struct hrtimer preemption_timer;
855 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200856
857 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
858 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800859
Wanpeng Li5c614b32015-10-13 09:18:36 -0700860 u16 vpid02;
861 u16 last_vpid;
862
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100863 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200864
865 /* SMM related state */
866 struct {
867 /* in VMX operation on SMM entry? */
868 bool vmxon;
869 /* in guest mode on SMM entry? */
870 bool guest_mode;
871 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300872};
873
Yang Zhang01e439b2013-04-11 19:25:12 +0800874#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800875#define POSTED_INTR_SN 1
876
Yang Zhang01e439b2013-04-11 19:25:12 +0800877/* Posted-Interrupt Descriptor */
878struct pi_desc {
879 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800880 union {
881 struct {
882 /* bit 256 - Outstanding Notification */
883 u16 on : 1,
884 /* bit 257 - Suppress Notification */
885 sn : 1,
886 /* bit 271:258 - Reserved */
887 rsvd_1 : 14;
888 /* bit 279:272 - Notification Vector */
889 u8 nv;
890 /* bit 287:280 - Reserved */
891 u8 rsvd_2;
892 /* bit 319:288 - Notification Destination */
893 u32 ndst;
894 };
895 u64 control;
896 };
897 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800898} __aligned(64);
899
Yang Zhanga20ed542013-04-11 19:25:15 +0800900static bool pi_test_and_set_on(struct pi_desc *pi_desc)
901{
902 return test_and_set_bit(POSTED_INTR_ON,
903 (unsigned long *)&pi_desc->control);
904}
905
906static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
907{
908 return test_and_clear_bit(POSTED_INTR_ON,
909 (unsigned long *)&pi_desc->control);
910}
911
912static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
913{
914 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
915}
916
Feng Wuebbfc762015-09-18 22:29:46 +0800917static inline void pi_clear_sn(struct pi_desc *pi_desc)
918{
919 return clear_bit(POSTED_INTR_SN,
920 (unsigned long *)&pi_desc->control);
921}
922
923static inline void pi_set_sn(struct pi_desc *pi_desc)
924{
925 return set_bit(POSTED_INTR_SN,
926 (unsigned long *)&pi_desc->control);
927}
928
Paolo Bonziniad361092016-09-20 16:15:05 +0200929static inline void pi_clear_on(struct pi_desc *pi_desc)
930{
931 clear_bit(POSTED_INTR_ON,
932 (unsigned long *)&pi_desc->control);
933}
934
Feng Wuebbfc762015-09-18 22:29:46 +0800935static inline int pi_test_on(struct pi_desc *pi_desc)
936{
937 return test_bit(POSTED_INTR_ON,
938 (unsigned long *)&pi_desc->control);
939}
940
941static inline int pi_test_sn(struct pi_desc *pi_desc)
942{
943 return test_bit(POSTED_INTR_SN,
944 (unsigned long *)&pi_desc->control);
945}
946
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400947struct vmx_msrs {
948 unsigned int nr;
949 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
950};
951
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400952struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000953 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300954 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300955 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100956 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300957 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200958 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200959 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300960 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400961 int nmsrs;
962 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800963 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400964#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300965 u64 msr_host_kernel_gs_base;
966 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400967#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100968
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100969 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100970 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100971
Gleb Natapov2961e8762013-11-25 15:37:13 +0200972 u32 vm_entry_controls_shadow;
973 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200974 u32 secondary_exec_control;
975
Nadav Har'Eld462b812011-05-24 15:26:10 +0300976 /*
977 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
978 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700979 * guest (L2), it points to a different VMCS. loaded_cpu_state points
980 * to the VMCS whose state is loaded into the CPU registers that only
981 * need to be switched when transitioning to/from the kernel; a NULL
982 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300983 */
984 struct loaded_vmcs vmcs01;
985 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700986 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300987 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300988 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400989 struct vmx_msrs guest;
990 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300991 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700992
Avi Kivity9c8cba32007-11-22 11:42:59 +0200993 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300994 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300995 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300996 struct kvm_segment segs[8];
997 } rmode;
998 struct {
999 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001000 struct kvm_save_segment {
1001 u16 selector;
1002 unsigned long base;
1003 u32 limit;
1004 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001005 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001006 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001007 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001008 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001009
Andi Kleena0861c02009-06-08 17:37:09 +08001010 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001011
Yang Zhang01e439b2013-04-11 19:25:12 +08001012 /* Posted interrupt descriptor */
1013 struct pi_desc pi_desc;
1014
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001015 /* Support for a guest hypervisor (nested VMX) */
1016 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001017
1018 /* Dynamic PLE window. */
1019 int ple_window;
1020 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001021
1022 /* Support for PML */
1023#define PML_ENTITY_NUM 512
1024 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001025
Yunhong Jiang64672c92016-06-13 14:19:59 -07001026 /* apic deadline value in host tsc */
1027 u64 hv_deadline_tsc;
1028
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001029 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001030
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001031 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001032
Wanpeng Li74c55932017-11-29 01:31:20 -08001033 unsigned long host_debugctlmsr;
1034
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001035 /*
1036 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1037 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1038 * in msr_ia32_feature_control_valid_bits.
1039 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001040 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001041 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001042 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001043};
1044
Avi Kivity2fb92db2011-04-27 19:42:18 +03001045enum segment_cache_field {
1046 SEG_FIELD_SEL = 0,
1047 SEG_FIELD_BASE = 1,
1048 SEG_FIELD_LIMIT = 2,
1049 SEG_FIELD_AR = 3,
1050
1051 SEG_FIELD_NR = 4
1052};
1053
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001054static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1055{
1056 return container_of(kvm, struct kvm_vmx, kvm);
1057}
1058
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001059static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1060{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001061 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062}
1063
Feng Wuefc64402015-09-18 22:29:51 +08001064static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1065{
1066 return &(to_vmx(vcpu)->pi_desc);
1067}
1068
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001069#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001070#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001071#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1072#define FIELD64(number, name) \
1073 FIELD(number, name), \
1074 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001075
Abel Gordon4607c2d2013-04-18 14:35:55 +03001076
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001077static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001078#define SHADOW_FIELD_RO(x) x,
1079#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001080};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001081static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001082 ARRAY_SIZE(shadow_read_only_fields);
1083
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001084static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001085#define SHADOW_FIELD_RW(x) x,
1086#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001087};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001088static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001089 ARRAY_SIZE(shadow_read_write_fields);
1090
Mathias Krause772e0312012-08-30 01:30:19 +02001091static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001092 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001093 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001094 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1095 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1096 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1097 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1098 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1099 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1100 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1101 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001102 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001103 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001104 FIELD(HOST_ES_SELECTOR, host_es_selector),
1105 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1106 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1107 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1108 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1109 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1110 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1111 FIELD64(IO_BITMAP_A, io_bitmap_a),
1112 FIELD64(IO_BITMAP_B, io_bitmap_b),
1113 FIELD64(MSR_BITMAP, msr_bitmap),
1114 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1115 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1116 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001117 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001118 FIELD64(TSC_OFFSET, tsc_offset),
1119 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1120 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001121 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001122 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001123 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001124 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1125 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1126 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1127 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001128 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001129 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1130 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001131 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001132 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1133 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1134 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1135 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1136 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1137 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1138 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1139 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1140 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1141 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001142 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001143 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1144 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1145 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1146 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1147 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1148 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1149 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1150 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1151 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1152 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1153 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1154 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1155 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1156 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1157 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1158 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1159 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1160 FIELD(TPR_THRESHOLD, tpr_threshold),
1161 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1162 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1163 FIELD(VM_EXIT_REASON, vm_exit_reason),
1164 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1165 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1166 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1167 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1168 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1169 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1170 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1171 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1172 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1173 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1174 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1175 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1176 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1177 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1178 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1179 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1180 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1181 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1182 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1183 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1184 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1185 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1186 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1187 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1188 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1189 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1190 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1191 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001192 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001193 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1194 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1195 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1196 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1197 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1198 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1199 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1200 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1201 FIELD(EXIT_QUALIFICATION, exit_qualification),
1202 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1203 FIELD(GUEST_CR0, guest_cr0),
1204 FIELD(GUEST_CR3, guest_cr3),
1205 FIELD(GUEST_CR4, guest_cr4),
1206 FIELD(GUEST_ES_BASE, guest_es_base),
1207 FIELD(GUEST_CS_BASE, guest_cs_base),
1208 FIELD(GUEST_SS_BASE, guest_ss_base),
1209 FIELD(GUEST_DS_BASE, guest_ds_base),
1210 FIELD(GUEST_FS_BASE, guest_fs_base),
1211 FIELD(GUEST_GS_BASE, guest_gs_base),
1212 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1213 FIELD(GUEST_TR_BASE, guest_tr_base),
1214 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1215 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1216 FIELD(GUEST_DR7, guest_dr7),
1217 FIELD(GUEST_RSP, guest_rsp),
1218 FIELD(GUEST_RIP, guest_rip),
1219 FIELD(GUEST_RFLAGS, guest_rflags),
1220 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1221 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1222 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1223 FIELD(HOST_CR0, host_cr0),
1224 FIELD(HOST_CR3, host_cr3),
1225 FIELD(HOST_CR4, host_cr4),
1226 FIELD(HOST_FS_BASE, host_fs_base),
1227 FIELD(HOST_GS_BASE, host_gs_base),
1228 FIELD(HOST_TR_BASE, host_tr_base),
1229 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1230 FIELD(HOST_IDTR_BASE, host_idtr_base),
1231 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1232 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1233 FIELD(HOST_RSP, host_rsp),
1234 FIELD(HOST_RIP, host_rip),
1235};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001236
1237static inline short vmcs_field_to_offset(unsigned long field)
1238{
Dan Williams085331d2018-01-31 17:47:03 -08001239 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1240 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001241 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001242
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001243 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001244 return -ENOENT;
1245
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001246 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001247 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001248 return -ENOENT;
1249
Linus Torvalds15303ba2018-02-10 13:16:35 -08001250 index = array_index_nospec(index, size);
1251 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001252 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001253 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001254 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001255}
1256
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001257static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1258{
David Matlack4f2777b2016-07-13 17:16:37 -07001259 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001260}
1261
Liran Alon61ada742018-06-23 02:35:08 +03001262static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1263{
1264 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1265}
1266
Peter Feiner995f00a2017-06-30 17:26:32 -07001267static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001268static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001269static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001270static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001271static void vmx_set_segment(struct kvm_vcpu *vcpu,
1272 struct kvm_segment *var, int seg);
1273static void vmx_get_segment(struct kvm_vcpu *vcpu,
1274 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001275static bool guest_state_valid(struct kvm_vcpu *vcpu);
1276static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001277static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001278static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1279static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1280static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1281 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001282static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001283static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1284 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001285
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1287static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001288/*
1289 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1290 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1291 */
1292static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001293
Feng Wubf9f6ac2015-09-18 22:29:55 +08001294/*
1295 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1296 * can find which vCPU should be waken up.
1297 */
1298static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1299static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1300
Radim Krčmář23611332016-09-29 22:41:33 +02001301enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001302 VMX_VMREAD_BITMAP,
1303 VMX_VMWRITE_BITMAP,
1304 VMX_BITMAP_NR
1305};
1306
1307static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1308
Radim Krčmář23611332016-09-29 22:41:33 +02001309#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1310#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001311
Avi Kivity110312c2010-12-21 12:54:20 +02001312static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001313static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001314
Sheng Yang2384d2b2008-01-17 15:14:33 +08001315static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1316static DEFINE_SPINLOCK(vmx_vpid_lock);
1317
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001318static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 int size;
1320 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001321 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001323 u32 pin_based_exec_ctrl;
1324 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001325 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001326 u32 vmexit_ctrl;
1327 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001328 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Hannes Ederefff9e52008-11-28 17:02:06 +01001331static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001332 u32 ept;
1333 u32 vpid;
1334} vmx_capability;
1335
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336#define VMX_SEGMENT_FIELD(seg) \
1337 [VCPU_SREG_##seg] = { \
1338 .selector = GUEST_##seg##_SELECTOR, \
1339 .base = GUEST_##seg##_BASE, \
1340 .limit = GUEST_##seg##_LIMIT, \
1341 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1342 }
1343
Mathias Krause772e0312012-08-30 01:30:19 +02001344static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345 unsigned selector;
1346 unsigned base;
1347 unsigned limit;
1348 unsigned ar_bytes;
1349} kvm_vmx_segment_fields[] = {
1350 VMX_SEGMENT_FIELD(CS),
1351 VMX_SEGMENT_FIELD(DS),
1352 VMX_SEGMENT_FIELD(ES),
1353 VMX_SEGMENT_FIELD(FS),
1354 VMX_SEGMENT_FIELD(GS),
1355 VMX_SEGMENT_FIELD(SS),
1356 VMX_SEGMENT_FIELD(TR),
1357 VMX_SEGMENT_FIELD(LDTR),
1358};
1359
Avi Kivity26bb0982009-09-07 11:14:12 +03001360static u64 host_efer;
1361
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001362static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1363
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001364/*
Brian Gerst8c065852010-07-17 09:03:26 -04001365 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001366 * away by decrementing the array size.
1367 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001369#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001370 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001372 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001375DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1376
1377#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1378
1379#define KVM_EVMCS_VERSION 1
1380
1381#if IS_ENABLED(CONFIG_HYPERV)
1382static bool __read_mostly enlightened_vmcs = true;
1383module_param(enlightened_vmcs, bool, 0444);
1384
1385static inline void evmcs_write64(unsigned long field, u64 value)
1386{
1387 u16 clean_field;
1388 int offset = get_evmcs_offset(field, &clean_field);
1389
1390 if (offset < 0)
1391 return;
1392
1393 *(u64 *)((char *)current_evmcs + offset) = value;
1394
1395 current_evmcs->hv_clean_fields &= ~clean_field;
1396}
1397
1398static inline void evmcs_write32(unsigned long field, u32 value)
1399{
1400 u16 clean_field;
1401 int offset = get_evmcs_offset(field, &clean_field);
1402
1403 if (offset < 0)
1404 return;
1405
1406 *(u32 *)((char *)current_evmcs + offset) = value;
1407 current_evmcs->hv_clean_fields &= ~clean_field;
1408}
1409
1410static inline void evmcs_write16(unsigned long field, u16 value)
1411{
1412 u16 clean_field;
1413 int offset = get_evmcs_offset(field, &clean_field);
1414
1415 if (offset < 0)
1416 return;
1417
1418 *(u16 *)((char *)current_evmcs + offset) = value;
1419 current_evmcs->hv_clean_fields &= ~clean_field;
1420}
1421
1422static inline u64 evmcs_read64(unsigned long field)
1423{
1424 int offset = get_evmcs_offset(field, NULL);
1425
1426 if (offset < 0)
1427 return 0;
1428
1429 return *(u64 *)((char *)current_evmcs + offset);
1430}
1431
1432static inline u32 evmcs_read32(unsigned long field)
1433{
1434 int offset = get_evmcs_offset(field, NULL);
1435
1436 if (offset < 0)
1437 return 0;
1438
1439 return *(u32 *)((char *)current_evmcs + offset);
1440}
1441
1442static inline u16 evmcs_read16(unsigned long field)
1443{
1444 int offset = get_evmcs_offset(field, NULL);
1445
1446 if (offset < 0)
1447 return 0;
1448
1449 return *(u16 *)((char *)current_evmcs + offset);
1450}
1451
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001452static inline void evmcs_touch_msr_bitmap(void)
1453{
1454 if (unlikely(!current_evmcs))
1455 return;
1456
1457 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1458 current_evmcs->hv_clean_fields &=
1459 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1460}
1461
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001462static void evmcs_load(u64 phys_addr)
1463{
1464 struct hv_vp_assist_page *vp_ap =
1465 hv_get_vp_assist_page(smp_processor_id());
1466
1467 vp_ap->current_nested_vmcs = phys_addr;
1468 vp_ap->enlighten_vmentry = 1;
1469}
1470
1471static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1472{
1473 /*
1474 * Enlightened VMCSv1 doesn't support these:
1475 *
1476 * POSTED_INTR_NV = 0x00000002,
1477 * GUEST_INTR_STATUS = 0x00000810,
1478 * APIC_ACCESS_ADDR = 0x00002014,
1479 * POSTED_INTR_DESC_ADDR = 0x00002016,
1480 * EOI_EXIT_BITMAP0 = 0x0000201c,
1481 * EOI_EXIT_BITMAP1 = 0x0000201e,
1482 * EOI_EXIT_BITMAP2 = 0x00002020,
1483 * EOI_EXIT_BITMAP3 = 0x00002022,
1484 */
1485 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1486 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1487 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1488 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1489 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1490 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1491 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1492
1493 /*
1494 * GUEST_PML_INDEX = 0x00000812,
1495 * PML_ADDRESS = 0x0000200e,
1496 */
1497 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1498
1499 /* VM_FUNCTION_CONTROL = 0x00002018, */
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1501
1502 /*
1503 * EPTP_LIST_ADDRESS = 0x00002024,
1504 * VMREAD_BITMAP = 0x00002026,
1505 * VMWRITE_BITMAP = 0x00002028,
1506 */
1507 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1508
1509 /*
1510 * TSC_MULTIPLIER = 0x00002032,
1511 */
1512 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1513
1514 /*
1515 * PLE_GAP = 0x00004020,
1516 * PLE_WINDOW = 0x00004022,
1517 */
1518 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1519
1520 /*
1521 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1522 */
1523 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1524
1525 /*
1526 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1527 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1528 */
1529 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1530 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1531
1532 /*
1533 * Currently unsupported in KVM:
1534 * GUEST_IA32_RTIT_CTL = 0x00002814,
1535 */
1536}
Tianyu Lan877ad952018-07-19 08:40:23 +00001537
1538/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1539static void check_ept_pointer_match(struct kvm *kvm)
1540{
1541 struct kvm_vcpu *vcpu;
1542 u64 tmp_eptp = INVALID_PAGE;
1543 int i;
1544
1545 kvm_for_each_vcpu(i, vcpu, kvm) {
1546 if (!VALID_PAGE(tmp_eptp)) {
1547 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1548 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1549 to_kvm_vmx(kvm)->ept_pointers_match
1550 = EPT_POINTERS_MISMATCH;
1551 return;
1552 }
1553 }
1554
1555 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1556}
1557
1558static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1559{
1560 int ret;
1561
1562 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1563
1564 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1565 check_ept_pointer_match(kvm);
1566
1567 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1568 ret = -ENOTSUPP;
1569 goto out;
1570 }
1571
1572 ret = hyperv_flush_guest_mapping(
1573 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1574
1575out:
1576 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1577 return ret;
1578}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001579#else /* !IS_ENABLED(CONFIG_HYPERV) */
1580static inline void evmcs_write64(unsigned long field, u64 value) {}
1581static inline void evmcs_write32(unsigned long field, u32 value) {}
1582static inline void evmcs_write16(unsigned long field, u16 value) {}
1583static inline u64 evmcs_read64(unsigned long field) { return 0; }
1584static inline u32 evmcs_read32(unsigned long field) { return 0; }
1585static inline u16 evmcs_read16(unsigned long field) { return 0; }
1586static inline void evmcs_load(u64 phys_addr) {}
1587static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001588static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001589#endif /* IS_ENABLED(CONFIG_HYPERV) */
1590
Jan Kiszka5bb16012016-02-09 20:14:21 +01001591static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592{
1593 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1594 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001595 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1596}
1597
Jan Kiszka6f054852016-02-09 20:15:18 +01001598static inline bool is_debug(u32 intr_info)
1599{
1600 return is_exception_n(intr_info, DB_VECTOR);
1601}
1602
1603static inline bool is_breakpoint(u32 intr_info)
1604{
1605 return is_exception_n(intr_info, BP_VECTOR);
1606}
1607
Jan Kiszka5bb16012016-02-09 20:14:21 +01001608static inline bool is_page_fault(u32 intr_info)
1609{
1610 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611}
1612
Gui Jianfeng31299942010-03-15 17:29:09 +08001613static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001614{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001615 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001616}
1617
Gui Jianfeng31299942010-03-15 17:29:09 +08001618static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001619{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001620 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001621}
1622
Liran Alon9e869482018-03-12 13:12:51 +02001623static inline bool is_gp_fault(u32 intr_info)
1624{
1625 return is_exception_n(intr_info, GP_VECTOR);
1626}
1627
Gui Jianfeng31299942010-03-15 17:29:09 +08001628static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629{
1630 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1631 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1632}
1633
Gui Jianfeng31299942010-03-15 17:29:09 +08001634static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001635{
1636 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1637 INTR_INFO_VALID_MASK)) ==
1638 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1639}
1640
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001641/* Undocumented: icebp/int1 */
1642static inline bool is_icebp(u32 intr_info)
1643{
1644 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1645 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1646}
1647
Gui Jianfeng31299942010-03-15 17:29:09 +08001648static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001649{
Sheng Yang04547152009-04-01 15:52:31 +08001650 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001651}
1652
Gui Jianfeng31299942010-03-15 17:29:09 +08001653static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001654{
Sheng Yang04547152009-04-01 15:52:31 +08001655 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001656}
1657
Paolo Bonzini35754c92015-07-29 12:05:37 +02001658static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001659{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001660 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001661}
1662
Gui Jianfeng31299942010-03-15 17:29:09 +08001663static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001664{
Sheng Yang04547152009-04-01 15:52:31 +08001665 return vmcs_config.cpu_based_exec_ctrl &
1666 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001667}
1668
Avi Kivity774ead32007-12-26 13:57:04 +02001669static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670{
Sheng Yang04547152009-04-01 15:52:31 +08001671 return vmcs_config.cpu_based_2nd_exec_ctrl &
1672 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1673}
1674
Yang Zhang8d146952013-01-25 10:18:50 +08001675static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1676{
1677 return vmcs_config.cpu_based_2nd_exec_ctrl &
1678 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1679}
1680
Yang Zhang83d4c282013-01-25 10:18:49 +08001681static inline bool cpu_has_vmx_apic_register_virt(void)
1682{
1683 return vmcs_config.cpu_based_2nd_exec_ctrl &
1684 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1685}
1686
Yang Zhangc7c9c562013-01-25 10:18:51 +08001687static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1688{
1689 return vmcs_config.cpu_based_2nd_exec_ctrl &
1690 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1691}
1692
Sean Christopherson0b665d32018-08-14 09:33:34 -07001693static inline bool cpu_has_vmx_encls_vmexit(void)
1694{
1695 return vmcs_config.cpu_based_2nd_exec_ctrl &
1696 SECONDARY_EXEC_ENCLS_EXITING;
1697}
1698
Yunhong Jiang64672c92016-06-13 14:19:59 -07001699/*
1700 * Comment's format: document - errata name - stepping - processor name.
1701 * Refer from
1702 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1703 */
1704static u32 vmx_preemption_cpu_tfms[] = {
1705/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17060x000206E6,
1707/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1708/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1709/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17100x00020652,
1711/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17120x00020655,
1713/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1714/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1715/*
1716 * 320767.pdf - AAP86 - B1 -
1717 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1718 */
17190x000106E5,
1720/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17210x000106A0,
1722/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17230x000106A1,
1724/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17250x000106A4,
1726 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1727 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1728 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17290x000106A5,
1730};
1731
1732static inline bool cpu_has_broken_vmx_preemption_timer(void)
1733{
1734 u32 eax = cpuid_eax(0x00000001), i;
1735
1736 /* Clear the reserved bits */
1737 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001738 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001739 if (eax == vmx_preemption_cpu_tfms[i])
1740 return true;
1741
1742 return false;
1743}
1744
1745static inline bool cpu_has_vmx_preemption_timer(void)
1746{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001747 return vmcs_config.pin_based_exec_ctrl &
1748 PIN_BASED_VMX_PREEMPTION_TIMER;
1749}
1750
Yang Zhang01e439b2013-04-11 19:25:12 +08001751static inline bool cpu_has_vmx_posted_intr(void)
1752{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001753 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1754 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001755}
1756
1757static inline bool cpu_has_vmx_apicv(void)
1758{
1759 return cpu_has_vmx_apic_register_virt() &&
1760 cpu_has_vmx_virtual_intr_delivery() &&
1761 cpu_has_vmx_posted_intr();
1762}
1763
Sheng Yang04547152009-04-01 15:52:31 +08001764static inline bool cpu_has_vmx_flexpriority(void)
1765{
1766 return cpu_has_vmx_tpr_shadow() &&
1767 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001768}
1769
Marcelo Tosattie7997942009-06-11 12:07:40 -03001770static inline bool cpu_has_vmx_ept_execute_only(void)
1771{
Gui Jianfeng31299942010-03-15 17:29:09 +08001772 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001773}
1774
Marcelo Tosattie7997942009-06-11 12:07:40 -03001775static inline bool cpu_has_vmx_ept_2m_page(void)
1776{
Gui Jianfeng31299942010-03-15 17:29:09 +08001777 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001778}
1779
Sheng Yang878403b2010-01-05 19:02:29 +08001780static inline bool cpu_has_vmx_ept_1g_page(void)
1781{
Gui Jianfeng31299942010-03-15 17:29:09 +08001782 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001783}
1784
Sheng Yang4bc9b982010-06-02 14:05:24 +08001785static inline bool cpu_has_vmx_ept_4levels(void)
1786{
1787 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1788}
1789
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001790static inline bool cpu_has_vmx_ept_mt_wb(void)
1791{
1792 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1793}
1794
Yu Zhang855feb62017-08-24 20:27:55 +08001795static inline bool cpu_has_vmx_ept_5levels(void)
1796{
1797 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1798}
1799
Xudong Hao83c3a332012-05-28 19:33:35 +08001800static inline bool cpu_has_vmx_ept_ad_bits(void)
1801{
1802 return vmx_capability.ept & VMX_EPT_AD_BIT;
1803}
1804
Gui Jianfeng31299942010-03-15 17:29:09 +08001805static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001806{
Gui Jianfeng31299942010-03-15 17:29:09 +08001807 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001808}
1809
Gui Jianfeng31299942010-03-15 17:29:09 +08001810static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001811{
Gui Jianfeng31299942010-03-15 17:29:09 +08001812 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001813}
1814
Liran Aloncd9a4912018-05-22 17:16:15 +03001815static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1816{
1817 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1818}
1819
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001820static inline bool cpu_has_vmx_invvpid_single(void)
1821{
1822 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1823}
1824
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001825static inline bool cpu_has_vmx_invvpid_global(void)
1826{
1827 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1828}
1829
Wanpeng Li08d839c2017-03-23 05:30:08 -07001830static inline bool cpu_has_vmx_invvpid(void)
1831{
1832 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1833}
1834
Gui Jianfeng31299942010-03-15 17:29:09 +08001835static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001836{
Sheng Yang04547152009-04-01 15:52:31 +08001837 return vmcs_config.cpu_based_2nd_exec_ctrl &
1838 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001839}
1840
Gui Jianfeng31299942010-03-15 17:29:09 +08001841static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001842{
1843 return vmcs_config.cpu_based_2nd_exec_ctrl &
1844 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1845}
1846
Gui Jianfeng31299942010-03-15 17:29:09 +08001847static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001848{
1849 return vmcs_config.cpu_based_2nd_exec_ctrl &
1850 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1851}
1852
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001853static inline bool cpu_has_vmx_basic_inout(void)
1854{
1855 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1856}
1857
Paolo Bonzini35754c92015-07-29 12:05:37 +02001858static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001859{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001860 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001861}
1862
Gui Jianfeng31299942010-03-15 17:29:09 +08001863static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001864{
Sheng Yang04547152009-04-01 15:52:31 +08001865 return vmcs_config.cpu_based_2nd_exec_ctrl &
1866 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001867}
1868
Gui Jianfeng31299942010-03-15 17:29:09 +08001869static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001870{
1871 return vmcs_config.cpu_based_2nd_exec_ctrl &
1872 SECONDARY_EXEC_RDTSCP;
1873}
1874
Mao, Junjiead756a12012-07-02 01:18:48 +00001875static inline bool cpu_has_vmx_invpcid(void)
1876{
1877 return vmcs_config.cpu_based_2nd_exec_ctrl &
1878 SECONDARY_EXEC_ENABLE_INVPCID;
1879}
1880
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001881static inline bool cpu_has_virtual_nmis(void)
1882{
1883 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1884}
1885
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001886static inline bool cpu_has_vmx_wbinvd_exit(void)
1887{
1888 return vmcs_config.cpu_based_2nd_exec_ctrl &
1889 SECONDARY_EXEC_WBINVD_EXITING;
1890}
1891
Abel Gordonabc4fc52013-04-18 14:35:25 +03001892static inline bool cpu_has_vmx_shadow_vmcs(void)
1893{
1894 u64 vmx_msr;
1895 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1896 /* check if the cpu supports writing r/o exit information fields */
1897 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1898 return false;
1899
1900 return vmcs_config.cpu_based_2nd_exec_ctrl &
1901 SECONDARY_EXEC_SHADOW_VMCS;
1902}
1903
Kai Huang843e4332015-01-28 10:54:28 +08001904static inline bool cpu_has_vmx_pml(void)
1905{
1906 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1907}
1908
Haozhong Zhang64903d62015-10-20 15:39:09 +08001909static inline bool cpu_has_vmx_tsc_scaling(void)
1910{
1911 return vmcs_config.cpu_based_2nd_exec_ctrl &
1912 SECONDARY_EXEC_TSC_SCALING;
1913}
1914
Bandan Das2a499e42017-08-03 15:54:41 -04001915static inline bool cpu_has_vmx_vmfunc(void)
1916{
1917 return vmcs_config.cpu_based_2nd_exec_ctrl &
1918 SECONDARY_EXEC_ENABLE_VMFUNC;
1919}
1920
Sean Christopherson64f7a112018-04-30 10:01:06 -07001921static bool vmx_umip_emulated(void)
1922{
1923 return vmcs_config.cpu_based_2nd_exec_ctrl &
1924 SECONDARY_EXEC_DESC;
1925}
1926
Sheng Yang04547152009-04-01 15:52:31 +08001927static inline bool report_flexpriority(void)
1928{
1929 return flexpriority_enabled;
1930}
1931
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001932static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1933{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001934 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001935}
1936
Jim Mattsonf4160e42018-05-29 09:11:33 -07001937/*
1938 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1939 * to modify any valid field of the VMCS, or are the VM-exit
1940 * information fields read-only?
1941 */
1942static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1943{
1944 return to_vmx(vcpu)->nested.msrs.misc_low &
1945 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1946}
1947
Marc Orr04473782018-06-20 17:21:29 -07001948static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1949{
1950 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1951}
1952
1953static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1954{
1955 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1956 CPU_BASED_MONITOR_TRAP_FLAG;
1957}
1958
Liran Alonfa97d7d2018-07-18 14:07:59 +02001959static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1960{
1961 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1962 SECONDARY_EXEC_SHADOW_VMCS;
1963}
1964
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001965static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1966{
1967 return vmcs12->cpu_based_vm_exec_control & bit;
1968}
1969
1970static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1971{
1972 return (vmcs12->cpu_based_vm_exec_control &
1973 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1974 (vmcs12->secondary_vm_exec_control & bit);
1975}
1976
Jan Kiszkaf41245002014-03-07 20:03:13 +01001977static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1978{
1979 return vmcs12->pin_based_vm_exec_control &
1980 PIN_BASED_VMX_PREEMPTION_TIMER;
1981}
1982
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001983static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1984{
1985 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1986}
1987
1988static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1989{
1990 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1991}
1992
Nadav Har'El155a97a2013-08-05 11:07:16 +03001993static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1994{
1995 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1996}
1997
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001998static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1999{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002000 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002001}
2002
Bandan Dasc5f983f2017-05-05 15:25:14 -04002003static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2004{
2005 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2006}
2007
Wincy Vanf2b93282015-02-03 23:56:03 +08002008static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2009{
2010 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2011}
2012
Wanpeng Li5c614b32015-10-13 09:18:36 -07002013static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2014{
2015 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2016}
2017
Wincy Van82f0dd42015-02-03 23:57:18 +08002018static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2019{
2020 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2021}
2022
Wincy Van608406e2015-02-03 23:57:51 +08002023static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2024{
2025 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2026}
2027
Wincy Van705699a2015-02-03 23:58:17 +08002028static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2029{
2030 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2031}
2032
Bandan Das27c42a12017-08-03 15:54:42 -04002033static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2034{
2035 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2036}
2037
Bandan Das41ab9372017-08-03 15:54:43 -04002038static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2039{
2040 return nested_cpu_has_vmfunc(vmcs12) &&
2041 (vmcs12->vm_function_control &
2042 VMX_VMFUNC_EPTP_SWITCHING);
2043}
2044
Liran Alonf792d272018-06-23 02:35:05 +03002045static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2046{
2047 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2048}
2049
Jim Mattsonef85b672016-12-12 11:01:37 -08002050static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002051{
2052 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002053 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002054}
2055
Jan Kiszka533558b2014-01-04 18:47:20 +01002056static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2057 u32 exit_intr_info,
2058 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002059static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2060 struct vmcs12 *vmcs12,
2061 u32 reason, unsigned long qualification);
2062
Rusty Russell8b9cf982007-07-30 16:31:43 +10002063static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002064{
2065 int i;
2066
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002067 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002068 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002069 return i;
2070 return -1;
2071}
2072
Sheng Yang2384d2b2008-01-17 15:14:33 +08002073static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2074{
2075 struct {
2076 u64 vpid : 16;
2077 u64 rsvd : 48;
2078 u64 gva;
2079 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002080 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002081
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002082 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2083 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2084 : "memory");
2085 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002086}
2087
Sheng Yang14394422008-04-28 12:24:45 +08002088static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2089{
2090 struct {
2091 u64 eptp, gpa;
2092 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002093 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002094
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002095 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2096 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2097 : "memory");
2098 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002099}
2100
Avi Kivity26bb0982009-09-07 11:14:12 +03002101static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002102{
2103 int i;
2104
Rusty Russell8b9cf982007-07-30 16:31:43 +10002105 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002106 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002107 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002108 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002109}
2110
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111static void vmcs_clear(struct vmcs *vmcs)
2112{
2113 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002114 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002116 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2117 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2118 : "memory");
2119 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2121 vmcs, phys_addr);
2122}
2123
Nadav Har'Eld462b812011-05-24 15:26:10 +03002124static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2125{
2126 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002127 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2128 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002129 loaded_vmcs->cpu = -1;
2130 loaded_vmcs->launched = 0;
2131}
2132
Dongxiao Xu7725b892010-05-11 18:29:38 +08002133static void vmcs_load(struct vmcs *vmcs)
2134{
2135 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002136 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002137
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002138 if (static_branch_unlikely(&enable_evmcs))
2139 return evmcs_load(phys_addr);
2140
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002141 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2142 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2143 : "memory");
2144 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002145 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002146 vmcs, phys_addr);
2147}
2148
Dave Young2965faa2015-09-09 15:38:55 -07002149#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002150/*
2151 * This bitmap is used to indicate whether the vmclear
2152 * operation is enabled on all cpus. All disabled by
2153 * default.
2154 */
2155static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2156
2157static inline void crash_enable_local_vmclear(int cpu)
2158{
2159 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2160}
2161
2162static inline void crash_disable_local_vmclear(int cpu)
2163{
2164 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2165}
2166
2167static inline int crash_local_vmclear_enabled(int cpu)
2168{
2169 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2170}
2171
2172static void crash_vmclear_local_loaded_vmcss(void)
2173{
2174 int cpu = raw_smp_processor_id();
2175 struct loaded_vmcs *v;
2176
2177 if (!crash_local_vmclear_enabled(cpu))
2178 return;
2179
2180 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2181 loaded_vmcss_on_cpu_link)
2182 vmcs_clear(v->vmcs);
2183}
2184#else
2185static inline void crash_enable_local_vmclear(int cpu) { }
2186static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002187#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002188
Nadav Har'Eld462b812011-05-24 15:26:10 +03002189static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002191 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002192 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193
Nadav Har'Eld462b812011-05-24 15:26:10 +03002194 if (loaded_vmcs->cpu != cpu)
2195 return; /* vcpu migration can race with cpu offline */
2196 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002198 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002199 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002200
2201 /*
2202 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2203 * is before setting loaded_vmcs->vcpu to -1 which is done in
2204 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2205 * then adds the vmcs into percpu list before it is deleted.
2206 */
2207 smp_wmb();
2208
Nadav Har'Eld462b812011-05-24 15:26:10 +03002209 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002210 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211}
2212
Nadav Har'Eld462b812011-05-24 15:26:10 +03002213static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002214{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002215 int cpu = loaded_vmcs->cpu;
2216
2217 if (cpu != -1)
2218 smp_call_function_single(cpu,
2219 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002220}
2221
Junaid Shahidfaff8752018-06-29 13:10:05 -07002222static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2223{
2224 if (vpid == 0)
2225 return true;
2226
2227 if (cpu_has_vmx_invvpid_individual_addr()) {
2228 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2229 return true;
2230 }
2231
2232 return false;
2233}
2234
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002235static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002236{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002237 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002238 return;
2239
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002240 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002241 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002242}
2243
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002244static inline void vpid_sync_vcpu_global(void)
2245{
2246 if (cpu_has_vmx_invvpid_global())
2247 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2248}
2249
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002250static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002251{
2252 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002254 else
2255 vpid_sync_vcpu_global();
2256}
2257
Sheng Yang14394422008-04-28 12:24:45 +08002258static inline void ept_sync_global(void)
2259{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002260 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002261}
2262
2263static inline void ept_sync_context(u64 eptp)
2264{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002265 if (cpu_has_vmx_invept_context())
2266 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2267 else
2268 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002269}
2270
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002271static __always_inline void vmcs_check16(unsigned long field)
2272{
2273 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2274 "16-bit accessor invalid for 64-bit field");
2275 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2276 "16-bit accessor invalid for 64-bit high field");
2277 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2278 "16-bit accessor invalid for 32-bit high field");
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2280 "16-bit accessor invalid for natural width field");
2281}
2282
2283static __always_inline void vmcs_check32(unsigned long field)
2284{
2285 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2286 "32-bit accessor invalid for 16-bit field");
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2288 "32-bit accessor invalid for natural width field");
2289}
2290
2291static __always_inline void vmcs_check64(unsigned long field)
2292{
2293 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2294 "64-bit accessor invalid for 16-bit field");
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2296 "64-bit accessor invalid for 64-bit high field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2298 "64-bit accessor invalid for 32-bit field");
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2300 "64-bit accessor invalid for natural width field");
2301}
2302
2303static __always_inline void vmcs_checkl(unsigned long field)
2304{
2305 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2306 "Natural width accessor invalid for 16-bit field");
2307 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2308 "Natural width accessor invalid for 64-bit field");
2309 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2310 "Natural width accessor invalid for 64-bit high field");
2311 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2312 "Natural width accessor invalid for 32-bit field");
2313}
2314
2315static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316{
Avi Kivity5e520e62011-05-15 10:13:12 -04002317 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318
Avi Kivity5e520e62011-05-15 10:13:12 -04002319 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2320 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321 return value;
2322}
2323
Avi Kivity96304212011-05-15 10:13:13 -04002324static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002326 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002327 if (static_branch_unlikely(&enable_evmcs))
2328 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
Avi Kivity96304212011-05-15 10:13:13 -04002332static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002334 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002335 if (static_branch_unlikely(&enable_evmcs))
2336 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002338}
2339
Avi Kivity96304212011-05-15 10:13:13 -04002340static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002342 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002343 if (static_branch_unlikely(&enable_evmcs))
2344 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002345#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002346 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002348 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349#endif
2350}
2351
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002352static __always_inline unsigned long vmcs_readl(unsigned long field)
2353{
2354 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002355 if (static_branch_unlikely(&enable_evmcs))
2356 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002357 return __vmcs_readl(field);
2358}
2359
Avi Kivitye52de1b2007-01-05 16:36:56 -08002360static noinline void vmwrite_error(unsigned long field, unsigned long value)
2361{
2362 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2363 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2364 dump_stack();
2365}
2366
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002367static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002369 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002371 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2372 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002373 if (unlikely(error))
2374 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002377static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002379 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002380 if (static_branch_unlikely(&enable_evmcs))
2381 return evmcs_write16(field, value);
2382
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002383 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384}
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002388 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002389 if (static_branch_unlikely(&enable_evmcs))
2390 return evmcs_write32(field, value);
2391
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002392 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393}
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002398 if (static_branch_unlikely(&enable_evmcs))
2399 return evmcs_write64(field, value);
2400
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002401 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002402#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405#endif
2406}
2407
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002408static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002409{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002410 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002411 if (static_branch_unlikely(&enable_evmcs))
2412 return evmcs_write64(field, value);
2413
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002414 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002415}
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002419 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2420 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002421 if (static_branch_unlikely(&enable_evmcs))
2422 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2423
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002424 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2425}
2426
2427static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2428{
2429 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2430 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002431 if (static_branch_unlikely(&enable_evmcs))
2432 return evmcs_write32(field, evmcs_read32(field) | mask);
2433
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002434 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002435}
2436
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002437static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2438{
2439 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2440}
2441
Gleb Natapov2961e8762013-11-25 15:37:13 +02002442static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2443{
2444 vmcs_write32(VM_ENTRY_CONTROLS, val);
2445 vmx->vm_entry_controls_shadow = val;
2446}
2447
2448static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2449{
2450 if (vmx->vm_entry_controls_shadow != val)
2451 vm_entry_controls_init(vmx, val);
2452}
2453
2454static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2455{
2456 return vmx->vm_entry_controls_shadow;
2457}
2458
2459
2460static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2461{
2462 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2463}
2464
2465static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2466{
2467 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2468}
2469
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002470static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2471{
2472 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2473}
2474
Gleb Natapov2961e8762013-11-25 15:37:13 +02002475static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2476{
2477 vmcs_write32(VM_EXIT_CONTROLS, val);
2478 vmx->vm_exit_controls_shadow = val;
2479}
2480
2481static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2482{
2483 if (vmx->vm_exit_controls_shadow != val)
2484 vm_exit_controls_init(vmx, val);
2485}
2486
2487static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2488{
2489 return vmx->vm_exit_controls_shadow;
2490}
2491
2492
2493static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2494{
2495 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2496}
2497
2498static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2499{
2500 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2501}
2502
Avi Kivity2fb92db2011-04-27 19:42:18 +03002503static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2504{
2505 vmx->segment_cache.bitmask = 0;
2506}
2507
2508static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2509 unsigned field)
2510{
2511 bool ret;
2512 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2513
2514 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2515 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2516 vmx->segment_cache.bitmask = 0;
2517 }
2518 ret = vmx->segment_cache.bitmask & mask;
2519 vmx->segment_cache.bitmask |= mask;
2520 return ret;
2521}
2522
2523static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2524{
2525 u16 *p = &vmx->segment_cache.seg[seg].selector;
2526
2527 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2528 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2529 return *p;
2530}
2531
2532static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2533{
2534 ulong *p = &vmx->segment_cache.seg[seg].base;
2535
2536 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2537 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2538 return *p;
2539}
2540
2541static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2542{
2543 u32 *p = &vmx->segment_cache.seg[seg].limit;
2544
2545 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2546 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2547 return *p;
2548}
2549
2550static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2551{
2552 u32 *p = &vmx->segment_cache.seg[seg].ar;
2553
2554 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2555 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2556 return *p;
2557}
2558
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002559static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2560{
2561 u32 eb;
2562
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002563 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002564 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002565 /*
2566 * Guest access to VMware backdoor ports could legitimately
2567 * trigger #GP because of TSS I/O permission bitmap.
2568 * We intercept those #GP and allow access to them anyway
2569 * as VMware does.
2570 */
2571 if (enable_vmware_backdoor)
2572 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002573 if ((vcpu->guest_debug &
2574 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2575 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2576 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002577 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002578 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002579 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002580 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002581
2582 /* When we are running a nested L2 guest and L1 specified for it a
2583 * certain exception bitmap, we must trap the same exceptions and pass
2584 * them to L1. When running L2, we will only handle the exceptions
2585 * specified above if L1 did not want them.
2586 */
2587 if (is_guest_mode(vcpu))
2588 eb |= get_vmcs12(vcpu)->exception_bitmap;
2589
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002590 vmcs_write32(EXCEPTION_BITMAP, eb);
2591}
2592
Ashok Raj15d45072018-02-01 22:59:43 +01002593/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002594 * Check if MSR is intercepted for currently loaded MSR bitmap.
2595 */
2596static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2597{
2598 unsigned long *msr_bitmap;
2599 int f = sizeof(unsigned long);
2600
2601 if (!cpu_has_vmx_msr_bitmap())
2602 return true;
2603
2604 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2605
2606 if (msr <= 0x1fff) {
2607 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2608 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2609 msr &= 0x1fff;
2610 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2611 }
2612
2613 return true;
2614}
2615
2616/*
Ashok Raj15d45072018-02-01 22:59:43 +01002617 * Check if MSR is intercepted for L01 MSR bitmap.
2618 */
2619static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2620{
2621 unsigned long *msr_bitmap;
2622 int f = sizeof(unsigned long);
2623
2624 if (!cpu_has_vmx_msr_bitmap())
2625 return true;
2626
2627 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2628
2629 if (msr <= 0x1fff) {
2630 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2631 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2632 msr &= 0x1fff;
2633 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2634 }
2635
2636 return true;
2637}
2638
Gleb Natapov2961e8762013-11-25 15:37:13 +02002639static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2640 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002641{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002642 vm_entry_controls_clearbit(vmx, entry);
2643 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002644}
2645
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002646static int find_msr(struct vmx_msrs *m, unsigned int msr)
2647{
2648 unsigned int i;
2649
2650 for (i = 0; i < m->nr; ++i) {
2651 if (m->val[i].index == msr)
2652 return i;
2653 }
2654 return -ENOENT;
2655}
2656
Avi Kivity61d2ef22010-04-28 16:40:38 +03002657static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2658{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002659 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002660 struct msr_autoload *m = &vmx->msr_autoload;
2661
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002662 switch (msr) {
2663 case MSR_EFER:
2664 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002665 clear_atomic_switch_msr_special(vmx,
2666 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002667 VM_EXIT_LOAD_IA32_EFER);
2668 return;
2669 }
2670 break;
2671 case MSR_CORE_PERF_GLOBAL_CTRL:
2672 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002673 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002674 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2675 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2676 return;
2677 }
2678 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002679 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002680 i = find_msr(&m->guest, msr);
2681 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002682 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002683 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002684 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002685 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002686
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002687skip_guest:
2688 i = find_msr(&m->host, msr);
2689 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002690 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002691
2692 --m->host.nr;
2693 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002694 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002695}
2696
Gleb Natapov2961e8762013-11-25 15:37:13 +02002697static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2698 unsigned long entry, unsigned long exit,
2699 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2700 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002701{
2702 vmcs_write64(guest_val_vmcs, guest_val);
2703 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002704 vm_entry_controls_setbit(vmx, entry);
2705 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002706}
2707
Avi Kivity61d2ef22010-04-28 16:40:38 +03002708static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002709 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002710{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002711 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002712 struct msr_autoload *m = &vmx->msr_autoload;
2713
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002714 switch (msr) {
2715 case MSR_EFER:
2716 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002717 add_atomic_switch_msr_special(vmx,
2718 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002719 VM_EXIT_LOAD_IA32_EFER,
2720 GUEST_IA32_EFER,
2721 HOST_IA32_EFER,
2722 guest_val, host_val);
2723 return;
2724 }
2725 break;
2726 case MSR_CORE_PERF_GLOBAL_CTRL:
2727 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002728 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002729 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2730 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2731 GUEST_IA32_PERF_GLOBAL_CTRL,
2732 HOST_IA32_PERF_GLOBAL_CTRL,
2733 guest_val, host_val);
2734 return;
2735 }
2736 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002737 case MSR_IA32_PEBS_ENABLE:
2738 /* PEBS needs a quiescent period after being disabled (to write
2739 * a record). Disabling PEBS through VMX MSR swapping doesn't
2740 * provide that period, so a CPU could write host's record into
2741 * guest's memory.
2742 */
2743 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002744 }
2745
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002746 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002747 if (!entry_only)
2748 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002749
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002750 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002751 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002752 "Can't add msr %x\n", msr);
2753 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002754 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002755 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002756 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002757 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002758 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002759 m->guest.val[i].index = msr;
2760 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002761
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002762 if (entry_only)
2763 return;
2764
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002765 if (j < 0) {
2766 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002767 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002768 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002769 m->host.val[j].index = msr;
2770 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002771}
2772
Avi Kivity92c0d902009-10-29 11:00:16 +02002773static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002774{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002775 u64 guest_efer = vmx->vcpu.arch.efer;
2776 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002777
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002778 if (!enable_ept) {
2779 /*
2780 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2781 * host CPUID is more efficient than testing guest CPUID
2782 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2783 */
2784 if (boot_cpu_has(X86_FEATURE_SMEP))
2785 guest_efer |= EFER_NX;
2786 else if (!(guest_efer & EFER_NX))
2787 ignore_bits |= EFER_NX;
2788 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002789
Avi Kivity51c6cf62007-08-29 03:48:05 +03002790 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002791 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002792 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002793 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002794#ifdef CONFIG_X86_64
2795 ignore_bits |= EFER_LMA | EFER_LME;
2796 /* SCE is meaningful only in long mode on Intel */
2797 if (guest_efer & EFER_LMA)
2798 ignore_bits &= ~(u64)EFER_SCE;
2799#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002800
2801 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002802
2803 /*
2804 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2805 * On CPUs that support "load IA32_EFER", always switch EFER
2806 * atomically, since it's faster than switching it manually.
2807 */
2808 if (cpu_has_load_ia32_efer ||
2809 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002810 if (!(guest_efer & EFER_LMA))
2811 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002812 if (guest_efer != host_efer)
2813 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002814 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002815 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002816 } else {
2817 guest_efer &= ~ignore_bits;
2818 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002819
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002820 vmx->guest_msrs[efer_offset].data = guest_efer;
2821 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2822
2823 return true;
2824 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002825}
2826
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002827#ifdef CONFIG_X86_32
2828/*
2829 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2830 * VMCS rather than the segment table. KVM uses this helper to figure
2831 * out the current bases to poke them into the VMCS before entry.
2832 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833static unsigned long segment_base(u16 selector)
2834{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002835 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836 unsigned long v;
2837
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 return 0;
2840
Thomas Garnier45fc8752017-03-14 10:05:08 -07002841 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002843 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844 u16 ldt_selector = kvm_read_ldt();
2845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 return 0;
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002851 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002852 return v;
2853}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002854#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002856static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002857{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002858 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002859 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002860#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002861 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002862#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002863 unsigned long fs_base, gs_base;
2864 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002865 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002866
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002867 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002868 return;
2869
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002870 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002871 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 /*
2874 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2875 * allow segment selectors with cpl > 0 or ti == 1.
2876 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002877 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002878
2879#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002880 savesegment(ds, host_state->ds_sel);
2881 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002882
2883 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002884 if (likely(is_64bit_mm(current->mm))) {
2885 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002886 fs_sel = current->thread.fsindex;
2887 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002888 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002889 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002890 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002891 savesegment(fs, fs_sel);
2892 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002895 }
2896
Avi Kivityc8770e72010-11-11 12:37:26 +02002897 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002898 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002899#else
Sean Christophersone368b872018-07-23 12:32:41 -07002900 savesegment(fs, fs_sel);
2901 savesegment(gs, gs_sel);
2902 fs_base = segment_base(fs_sel);
2903 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002904#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002905
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002906 if (unlikely(fs_sel != host_state->fs_sel)) {
2907 if (!(fs_sel & 7))
2908 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2909 else
2910 vmcs_write16(HOST_FS_SELECTOR, 0);
2911 host_state->fs_sel = fs_sel;
2912 }
2913 if (unlikely(gs_sel != host_state->gs_sel)) {
2914 if (!(gs_sel & 7))
2915 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2916 else
2917 vmcs_write16(HOST_GS_SELECTOR, 0);
2918 host_state->gs_sel = gs_sel;
2919 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002920 if (unlikely(fs_base != host_state->fs_base)) {
2921 vmcs_writel(HOST_FS_BASE, fs_base);
2922 host_state->fs_base = fs_base;
2923 }
2924 if (unlikely(gs_base != host_state->gs_base)) {
2925 vmcs_writel(HOST_GS_BASE, gs_base);
2926 host_state->gs_base = gs_base;
2927 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002928
Avi Kivity26bb0982009-09-07 11:14:12 +03002929 for (i = 0; i < vmx->save_nmsrs; ++i)
2930 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002931 vmx->guest_msrs[i].data,
2932 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002933}
2934
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002935static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002936{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002937 struct vmcs_host_state *host_state;
2938
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002939 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002940 return;
2941
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002942 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002943 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002944
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002945 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002946 vmx->loaded_cpu_state = NULL;
2947
Avi Kivityc8770e72010-11-11 12:37:26 +02002948#ifdef CONFIG_X86_64
2949 if (is_long_mode(&vmx->vcpu))
2950 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2951#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002952 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2953 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002954#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002956#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002958#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002959 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 if (host_state->fs_sel & 7)
2961 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002962#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002963 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2964 loadsegment(ds, host_state->ds_sel);
2965 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002966 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002967#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002968 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002969#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002970 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002971#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002972 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002973}
2974
Sean Christopherson678e3152018-07-23 12:32:43 -07002975#ifdef CONFIG_X86_64
2976static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002977{
Sean Christopherson678e3152018-07-23 12:32:43 -07002978 if (is_long_mode(&vmx->vcpu)) {
2979 preempt_disable();
2980 if (vmx->loaded_cpu_state)
2981 rdmsrl(MSR_KERNEL_GS_BASE,
2982 vmx->msr_guest_kernel_gs_base);
2983 preempt_enable();
2984 }
2985 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002986}
2987
Sean Christopherson678e3152018-07-23 12:32:43 -07002988static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2989{
2990 if (is_long_mode(&vmx->vcpu)) {
2991 preempt_disable();
2992 if (vmx->loaded_cpu_state)
2993 wrmsrl(MSR_KERNEL_GS_BASE, data);
2994 preempt_enable();
2995 }
2996 vmx->msr_guest_kernel_gs_base = data;
2997}
2998#endif
2999
Feng Wu28b835d2015-09-18 22:29:54 +08003000static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
3001{
3002 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3003 struct pi_desc old, new;
3004 unsigned int dest;
3005
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003006 /*
3007 * In case of hot-plug or hot-unplug, we may have to undo
3008 * vmx_vcpu_pi_put even if there is no assigned device. And we
3009 * always keep PI.NDST up to date for simplicity: it makes the
3010 * code easier, and CPU migration is not a fast path.
3011 */
3012 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003013 return;
3014
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003015 /*
3016 * First handle the simple case where no cmpxchg is necessary; just
3017 * allow posting non-urgent interrupts.
3018 *
3019 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3020 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3021 * expects the VCPU to be on the blocked_vcpu_list that matches
3022 * PI.NDST.
3023 */
3024 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3025 vcpu->cpu == cpu) {
3026 pi_clear_sn(pi_desc);
3027 return;
3028 }
3029
3030 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003031 do {
3032 old.control = new.control = pi_desc->control;
3033
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003034 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003035
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003036 if (x2apic_enabled())
3037 new.ndst = dest;
3038 else
3039 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003040
Feng Wu28b835d2015-09-18 22:29:54 +08003041 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003042 } while (cmpxchg64(&pi_desc->control, old.control,
3043 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003044}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003045
Peter Feinerc95ba922016-08-17 09:36:47 -07003046static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3047{
3048 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3049 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3050}
3051
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052/*
3053 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3054 * vcpu mutex is already taken.
3055 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003056static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003059 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003061 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003062 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003063 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003064 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003065
3066 /*
3067 * Read loaded_vmcs->cpu should be before fetching
3068 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3069 * See the comments in __loaded_vmcs_clear().
3070 */
3071 smp_rmb();
3072
Nadav Har'Eld462b812011-05-24 15:26:10 +03003073 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3074 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003075 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003076 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003077 }
3078
3079 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3080 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3081 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003082 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003083 }
3084
3085 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003086 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003087 unsigned long sysenter_esp;
3088
3089 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003090
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 /*
3092 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003093 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003095 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003096 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003097 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003099 /*
3100 * VM exits change the host TR limit to 0x67 after a VM
3101 * exit. This is okay, since 0x67 covers everything except
3102 * the IO bitmap and have have code to handle the IO bitmap
3103 * being lost after a VM exit.
3104 */
3105 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3106
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3108 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003109
Nadav Har'Eld462b812011-05-24 15:26:10 +03003110 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 }
Feng Wu28b835d2015-09-18 22:29:54 +08003112
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003113 /* Setup TSC multiplier */
3114 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003115 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3116 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003117
Feng Wu28b835d2015-09-18 22:29:54 +08003118 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003119 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003120 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003121}
3122
3123static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3124{
3125 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3126
3127 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003128 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3129 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003130 return;
3131
3132 /* Set SN when the vCPU is preempted */
3133 if (vcpu->preempted)
3134 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135}
3136
3137static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3138{
Feng Wu28b835d2015-09-18 22:29:54 +08003139 vmx_vcpu_pi_put(vcpu);
3140
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003141 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142}
3143
Wanpeng Lif244dee2017-07-20 01:11:54 -07003144static bool emulation_required(struct kvm_vcpu *vcpu)
3145{
3146 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3147}
3148
Avi Kivityedcafe32009-12-30 18:07:40 +02003149static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3150
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003151/*
3152 * Return the cr0 value that a nested guest would read. This is a combination
3153 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3154 * its hypervisor (cr0_read_shadow).
3155 */
3156static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3157{
3158 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3159 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3160}
3161static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3162{
3163 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3164 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3165}
3166
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3168{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003169 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003170
Avi Kivity6de12732011-03-07 12:51:22 +02003171 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3172 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3173 rflags = vmcs_readl(GUEST_RFLAGS);
3174 if (to_vmx(vcpu)->rmode.vm86_active) {
3175 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3176 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3177 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3178 }
3179 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003180 }
Avi Kivity6de12732011-03-07 12:51:22 +02003181 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182}
3183
3184static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3185{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003186 unsigned long old_rflags = vmx_get_rflags(vcpu);
3187
Avi Kivity6de12732011-03-07 12:51:22 +02003188 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3189 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003190 if (to_vmx(vcpu)->rmode.vm86_active) {
3191 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003192 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003193 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003194 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003195
3196 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3197 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198}
3199
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003200static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003201{
3202 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3203 int ret = 0;
3204
3205 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003206 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003207 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003208 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003210 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003211}
3212
3213static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3214{
3215 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3216 u32 interruptibility = interruptibility_old;
3217
3218 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3219
Jan Kiszka48005f62010-02-19 19:38:07 +01003220 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003221 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003222 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003223 interruptibility |= GUEST_INTR_STATE_STI;
3224
3225 if ((interruptibility != interruptibility_old))
3226 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3227}
3228
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3230{
3231 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003233 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003235 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236
Glauber Costa2809f5d2009-05-12 16:21:05 -04003237 /* skipping an emulated instruction also counts */
3238 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239}
3240
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003241static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3242 unsigned long exit_qual)
3243{
3244 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3245 unsigned int nr = vcpu->arch.exception.nr;
3246 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3247
3248 if (vcpu->arch.exception.has_error_code) {
3249 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3250 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3251 }
3252
3253 if (kvm_exception_is_soft(nr))
3254 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3255 else
3256 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3257
3258 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3259 vmx_get_nmi_mask(vcpu))
3260 intr_info |= INTR_INFO_UNBLOCK_NMI;
3261
3262 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3263}
3264
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003265/*
3266 * KVM wants to inject page-faults which it got to the guest. This function
3267 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003268 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003269static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003270{
3271 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003272 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003273
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003274 if (nr == PF_VECTOR) {
3275 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003276 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003277 return 1;
3278 }
3279 /*
3280 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3281 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3282 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3283 * can be written only when inject_pending_event runs. This should be
3284 * conditional on a new capability---if the capability is disabled,
3285 * kvm_multiple_exception would write the ancillary information to
3286 * CR2 or DR6, for backwards ABI-compatibility.
3287 */
3288 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3289 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003290 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003291 return 1;
3292 }
3293 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003294 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003295 if (nr == DB_VECTOR)
3296 *exit_qual = vcpu->arch.dr6;
3297 else
3298 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003299 return 1;
3300 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003301 }
3302
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003303 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003304}
3305
Wanpeng Licaa057a2018-03-12 04:53:03 -07003306static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3307{
3308 /*
3309 * Ensure that we clear the HLT state in the VMCS. We don't need to
3310 * explicitly skip the instruction because if the HLT state is set,
3311 * then the instruction is already executing and RIP has already been
3312 * advanced.
3313 */
3314 if (kvm_hlt_in_guest(vcpu->kvm) &&
3315 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3316 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3317}
3318
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003319static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003320{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003321 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003322 unsigned nr = vcpu->arch.exception.nr;
3323 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003324 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003325 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003326
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003327 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003328 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003329 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3330 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003331
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003332 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003333 int inc_eip = 0;
3334 if (kvm_exception_is_soft(nr))
3335 inc_eip = vcpu->arch.event_exit_inst_len;
3336 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003337 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003338 return;
3339 }
3340
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003341 WARN_ON_ONCE(vmx->emulation_required);
3342
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003343 if (kvm_exception_is_soft(nr)) {
3344 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3345 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003346 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3347 } else
3348 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3349
3350 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003351
3352 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003353}
3354
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003355static bool vmx_rdtscp_supported(void)
3356{
3357 return cpu_has_vmx_rdtscp();
3358}
3359
Mao, Junjiead756a12012-07-02 01:18:48 +00003360static bool vmx_invpcid_supported(void)
3361{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003362 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003363}
3364
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365/*
Eddie Donga75beee2007-05-17 18:55:15 +03003366 * Swap MSR entry in host/guest MSR entry array.
3367 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003368static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003369{
Avi Kivity26bb0982009-09-07 11:14:12 +03003370 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003371
3372 tmp = vmx->guest_msrs[to];
3373 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3374 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003375}
3376
3377/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003378 * Set up the vmcs to automatically save and restore system
3379 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3380 * mode, as fiddling with msrs is very expensive.
3381 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003382static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003383{
Avi Kivity26bb0982009-09-07 11:14:12 +03003384 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003385
Eddie Donga75beee2007-05-17 18:55:15 +03003386 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003387#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003388 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003390 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003391 move_msr_up(vmx, index, save_nmsrs++);
3392 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003393 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003394 move_msr_up(vmx, index, save_nmsrs++);
3395 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003396 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003397 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003398 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003399 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003400 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003401 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003402 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003403 * if efer.sce is enabled.
3404 */
Brian Gerst8c065852010-07-17 09:03:26 -04003405 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003406 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003407 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003408 }
Eddie Donga75beee2007-05-17 18:55:15 +03003409#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003410 index = __find_msr_index(vmx, MSR_EFER);
3411 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003412 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003413
Avi Kivity26bb0982009-09-07 11:14:12 +03003414 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003415
Yang Zhang8d146952013-01-25 10:18:50 +08003416 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003417 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003418}
3419
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003420static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003422 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003424 if (is_guest_mode(vcpu) &&
3425 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3426 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3427
3428 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429}
3430
3431/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003432 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003434static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003436 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003437 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003438 * We're here if L1 chose not to trap WRMSR to TSC. According
3439 * to the spec, this should set L1's TSC; The offset that L1
3440 * set for L2 remains unchanged, and still needs to be added
3441 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003442 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003443 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003444 /* recalculate vmcs02.TSC_OFFSET: */
3445 vmcs12 = get_vmcs12(vcpu);
3446 vmcs_write64(TSC_OFFSET, offset +
3447 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3448 vmcs12->tsc_offset : 0));
3449 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003450 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3451 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003452 vmcs_write64(TSC_OFFSET, offset);
3453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454}
3455
Nadav Har'El801d3422011-05-25 23:02:23 +03003456/*
3457 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3458 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3459 * all guests if the "nested" module option is off, and can also be disabled
3460 * for a single guest by disabling its VMX cpuid bit.
3461 */
3462static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3463{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003464 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003465}
3466
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003468 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3469 * returned for the various VMX controls MSRs when nested VMX is enabled.
3470 * The same values should also be used to verify that vmcs12 control fields are
3471 * valid during nested entry from L1 to L2.
3472 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3473 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3474 * bit in the high half is on if the corresponding bit in the control field
3475 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003476 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003477static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003478{
Paolo Bonzini13893092018-02-26 13:40:09 +01003479 if (!nested) {
3480 memset(msrs, 0, sizeof(*msrs));
3481 return;
3482 }
3483
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003484 /*
3485 * Note that as a general rule, the high half of the MSRs (bits in
3486 * the control fields which may be 1) should be initialized by the
3487 * intersection of the underlying hardware's MSR (i.e., features which
3488 * can be supported) and the list of features we want to expose -
3489 * because they are known to be properly supported in our code.
3490 * Also, usually, the low half of the MSRs (bits which must be 1) can
3491 * be set to 0, meaning that L1 may turn off any of these bits. The
3492 * reason is that if one of these bits is necessary, it will appear
3493 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3494 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003495 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003496 * These rules have exceptions below.
3497 */
3498
3499 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003500 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003501 msrs->pinbased_ctls_low,
3502 msrs->pinbased_ctls_high);
3503 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003504 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003505 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003506 PIN_BASED_EXT_INTR_MASK |
3507 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003508 PIN_BASED_VIRTUAL_NMIS |
3509 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003510 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003511 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003512 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003513
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003514 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003515 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003516 msrs->exit_ctls_low,
3517 msrs->exit_ctls_high);
3518 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003519 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003520
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003521 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003522#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003523 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003524#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003525 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003526 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003527 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003528 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003529 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3530
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003531 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003532 msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003533
Jan Kiszka2996fca2014-06-16 13:59:43 +02003534 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003536
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003537 /* entry controls */
3538 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 msrs->entry_ctls_low,
3540 msrs->entry_ctls_high);
3541 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003542 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003544#ifdef CONFIG_X86_64
3545 VM_ENTRY_IA32E_MODE |
3546#endif
3547 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003548 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003549 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003550 if (kvm_mpx_supported())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02003552
Jan Kiszka2996fca2014-06-16 13:59:43 +02003553 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003555
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003556 /* cpu-based controls */
3557 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003558 msrs->procbased_ctls_low,
3559 msrs->procbased_ctls_high);
3560 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003561 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003562 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003563 CPU_BASED_VIRTUAL_INTR_PENDING |
3564 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003565 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3566 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3567 CPU_BASED_CR3_STORE_EXITING |
3568#ifdef CONFIG_X86_64
3569 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3570#endif
3571 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003572 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3573 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3574 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3575 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003576 /*
3577 * We can allow some features even when not supported by the
3578 * hardware. For example, L1 can specify an MSR bitmap - and we
3579 * can use it to avoid exits to L1 - even when L0 runs L2
3580 * without MSR bitmaps.
3581 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003582 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003583 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003584 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003585
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003586 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003588 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3589
Paolo Bonzini80154d72017-08-24 13:55:35 +02003590 /*
3591 * secondary cpu-based controls. Do not include those that
3592 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3593 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003594 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003595 msrs->secondary_ctls_low,
3596 msrs->secondary_ctls_high);
3597 msrs->secondary_ctls_low = 0;
3598 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003599 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003600 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003601 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003602 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003603 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003604 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003605 /*
3606 * We can emulate "VMCS shadowing," even if the hardware
3607 * doesn't support it.
3608 */
3609 msrs->secondary_ctls_high |=
3610 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003611
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003612 if (enable_ept) {
3613 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003614 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003615 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003616 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003617 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003618 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003620 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 msrs->ept_caps &= vmx_capability.ept;
3622 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003623 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3624 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003625 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003626 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003627 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003628 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003629 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003630 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003631
Bandan Das27c42a12017-08-03 15:54:42 -04003632 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003634 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003635 /*
3636 * Advertise EPTP switching unconditionally
3637 * since we emulate it
3638 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003639 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003640 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003641 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003642 }
3643
Paolo Bonzinief697a72016-03-18 16:58:38 +01003644 /*
3645 * Old versions of KVM use the single-context version without
3646 * checking for support, so declare that it is supported even
3647 * though it is treated as global context. The alternative is
3648 * not failing the single-context invvpid, and it is worse.
3649 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003650 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003652 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003653 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003654 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003655 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003656
Radim Krčmář0790ec12015-03-17 14:02:32 +01003657 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003658 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003659 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3660
Jan Kiszkac18911a2013-03-13 16:06:41 +01003661 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003662 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003663 msrs->misc_low,
3664 msrs->misc_high);
3665 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3666 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003667 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003668 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003669 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003671
3672 /*
3673 * This MSR reports some information about VMX support. We
3674 * should return information about the VMX we emulate for the
3675 * guest, and the VMCS structure we give it - not about the
3676 * VMX support of the underlying hardware.
3677 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003678 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003679 VMCS12_REVISION |
3680 VMX_BASIC_TRUE_CTLS |
3681 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3682 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3683
3684 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003686
3687 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003688 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003689 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3690 * We picked the standard core2 setting.
3691 */
3692#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3693#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3695 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003696
3697 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003698 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3699 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003700
3701 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003702 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703}
3704
David Matlack38991522016-11-29 18:14:08 -08003705/*
3706 * if fixed0[i] == 1: val[i] must be 1
3707 * if fixed1[i] == 0: val[i] must be 0
3708 */
3709static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3710{
3711 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003712}
3713
3714static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3715{
David Matlack38991522016-11-29 18:14:08 -08003716 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003717}
3718
3719static inline u64 vmx_control_msr(u32 low, u32 high)
3720{
3721 return low | ((u64)high << 32);
3722}
3723
David Matlack62cc6b9d2016-11-29 18:14:07 -08003724static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3725{
3726 superset &= mask;
3727 subset &= mask;
3728
3729 return (superset | subset) == superset;
3730}
3731
3732static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3733{
3734 const u64 feature_and_reserved =
3735 /* feature (except bit 48; see below) */
3736 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3737 /* reserved */
3738 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003739 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003740
3741 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3742 return -EINVAL;
3743
3744 /*
3745 * KVM does not emulate a version of VMX that constrains physical
3746 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3747 */
3748 if (data & BIT_ULL(48))
3749 return -EINVAL;
3750
3751 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3752 vmx_basic_vmcs_revision_id(data))
3753 return -EINVAL;
3754
3755 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3756 return -EINVAL;
3757
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003758 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003759 return 0;
3760}
3761
3762static int
3763vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3764{
3765 u64 supported;
3766 u32 *lowp, *highp;
3767
3768 switch (msr_index) {
3769 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3771 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.procbased_ctls_low;
3775 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 lowp = &vmx->nested.msrs.exit_ctls_low;
3779 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003780 break;
3781 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003782 lowp = &vmx->nested.msrs.entry_ctls_low;
3783 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003784 break;
3785 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003786 lowp = &vmx->nested.msrs.secondary_ctls_low;
3787 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003788 break;
3789 default:
3790 BUG();
3791 }
3792
3793 supported = vmx_control_msr(*lowp, *highp);
3794
3795 /* Check must-be-1 bits are still 1. */
3796 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3797 return -EINVAL;
3798
3799 /* Check must-be-0 bits are still 0. */
3800 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3801 return -EINVAL;
3802
3803 *lowp = data;
3804 *highp = data >> 32;
3805 return 0;
3806}
3807
3808static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3809{
3810 const u64 feature_and_reserved_bits =
3811 /* feature */
3812 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3813 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3814 /* reserved */
3815 GENMASK_ULL(13, 9) | BIT_ULL(31);
3816 u64 vmx_misc;
3817
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003818 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3819 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003820
3821 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3822 return -EINVAL;
3823
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003824 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003825 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3826 vmx_misc_preemption_timer_rate(data) !=
3827 vmx_misc_preemption_timer_rate(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3831 return -EINVAL;
3832
3833 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3834 return -EINVAL;
3835
3836 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3837 return -EINVAL;
3838
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003839 vmx->nested.msrs.misc_low = data;
3840 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003841
3842 /*
3843 * If L1 has read-only VM-exit information fields, use the
3844 * less permissive vmx_vmwrite_bitmap to specify write
3845 * permissions for the shadow VMCS.
3846 */
3847 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3848 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3849
David Matlack62cc6b9d2016-11-29 18:14:07 -08003850 return 0;
3851}
3852
3853static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3854{
3855 u64 vmx_ept_vpid_cap;
3856
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003857 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3858 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003859
3860 /* Every bit is either reserved or a feature bit. */
3861 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3862 return -EINVAL;
3863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003864 vmx->nested.msrs.ept_caps = data;
3865 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003866 return 0;
3867}
3868
3869static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3870{
3871 u64 *msr;
3872
3873 switch (msr_index) {
3874 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003876 break;
3877 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003878 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003879 break;
3880 default:
3881 BUG();
3882 }
3883
3884 /*
3885 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3886 * must be 1 in the restored value.
3887 */
3888 if (!is_bitwise_subset(data, *msr, -1ULL))
3889 return -EINVAL;
3890
3891 *msr = data;
3892 return 0;
3893}
3894
3895/*
3896 * Called when userspace is restoring VMX MSRs.
3897 *
3898 * Returns 0 on success, non-0 otherwise.
3899 */
3900static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3901{
3902 struct vcpu_vmx *vmx = to_vmx(vcpu);
3903
Jim Mattsona943ac52018-05-29 09:11:32 -07003904 /*
3905 * Don't allow changes to the VMX capability MSRs while the vCPU
3906 * is in VMX operation.
3907 */
3908 if (vmx->nested.vmxon)
3909 return -EBUSY;
3910
David Matlack62cc6b9d2016-11-29 18:14:07 -08003911 switch (msr_index) {
3912 case MSR_IA32_VMX_BASIC:
3913 return vmx_restore_vmx_basic(vmx, data);
3914 case MSR_IA32_VMX_PINBASED_CTLS:
3915 case MSR_IA32_VMX_PROCBASED_CTLS:
3916 case MSR_IA32_VMX_EXIT_CTLS:
3917 case MSR_IA32_VMX_ENTRY_CTLS:
3918 /*
3919 * The "non-true" VMX capability MSRs are generated from the
3920 * "true" MSRs, so we do not support restoring them directly.
3921 *
3922 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3923 * should restore the "true" MSRs with the must-be-1 bits
3924 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3925 * DEFAULT SETTINGS".
3926 */
3927 return -EINVAL;
3928 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3929 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3930 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3931 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3932 case MSR_IA32_VMX_PROCBASED_CTLS2:
3933 return vmx_restore_control_msr(vmx, msr_index, data);
3934 case MSR_IA32_VMX_MISC:
3935 return vmx_restore_vmx_misc(vmx, data);
3936 case MSR_IA32_VMX_CR0_FIXED0:
3937 case MSR_IA32_VMX_CR4_FIXED0:
3938 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3939 case MSR_IA32_VMX_CR0_FIXED1:
3940 case MSR_IA32_VMX_CR4_FIXED1:
3941 /*
3942 * These MSRs are generated based on the vCPU's CPUID, so we
3943 * do not support restoring them directly.
3944 */
3945 return -EINVAL;
3946 case MSR_IA32_VMX_EPT_VPID_CAP:
3947 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3948 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003949 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003950 return 0;
3951 default:
3952 /*
3953 * The rest of the VMX capability MSRs do not support restore.
3954 */
3955 return -EINVAL;
3956 }
3957}
3958
Jan Kiszkacae50132014-01-04 18:47:22 +01003959/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003960static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003961{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003963 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003964 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003965 break;
3966 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3967 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003968 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003969 msrs->pinbased_ctls_low,
3970 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003971 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3972 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003973 break;
3974 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3975 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003976 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003977 msrs->procbased_ctls_low,
3978 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003979 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3980 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003981 break;
3982 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3983 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003984 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003985 msrs->exit_ctls_low,
3986 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003987 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3988 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003989 break;
3990 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3991 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003992 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 msrs->entry_ctls_low,
3994 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003995 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3996 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003999 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004000 msrs->misc_low,
4001 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 break;
4018 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004019 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004020 msrs->secondary_ctls_low,
4021 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 break;
4023 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004024 *pdata = msrs->ept_caps |
4025 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004027 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004028 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004029 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004030 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004032 }
4033
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004034 return 0;
4035}
4036
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004037static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4038 uint64_t val)
4039{
4040 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4041
4042 return !(val & ~valid_bits);
4043}
4044
Tom Lendacky801e4592018-02-21 13:39:51 -06004045static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4046{
Paolo Bonzini13893092018-02-26 13:40:09 +01004047 switch (msr->index) {
4048 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4049 if (!nested)
4050 return 1;
4051 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4052 default:
4053 return 1;
4054 }
4055
4056 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004057}
4058
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004059/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 * Reads an msr value (of 'msr_index') into 'pdata'.
4061 * Returns 0 on success, non-0 otherwise.
4062 * Assumes vcpu_load() was already called.
4063 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004066 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004067 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004070#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
4074 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004075 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004076 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004077 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004078 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004079 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004080#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004081 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004082 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004083 case MSR_IA32_SPEC_CTRL:
4084 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004085 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4086 return 1;
4087
4088 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4089 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004090 case MSR_IA32_ARCH_CAPABILITIES:
4091 if (!msr_info->host_initiated &&
4092 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4093 return 1;
4094 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4095 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
4099 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 break;
4102 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004103 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004104 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004105 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004106 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004107 (!msr_info->host_initiated &&
4108 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004109 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004110 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004111 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 case MSR_IA32_MCG_EXT_CTL:
4113 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004114 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004115 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004117 msr_info->data = vcpu->arch.mcg_ext_ctl;
4118 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004119 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004120 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004121 break;
4122 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4123 if (!nested_vmx_allowed(vcpu))
4124 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004125 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4126 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004127 case MSR_IA32_XSS:
4128 if (!vmx_xsaves_supported())
4129 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004130 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004131 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004132 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004133 if (!msr_info->host_initiated &&
4134 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004135 return 1;
4136 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004138 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004139 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004141 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004143 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144 }
4145
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146 return 0;
4147}
4148
Jan Kiszkacae50132014-01-04 18:47:22 +01004149static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4150
Avi Kivity6aa8b732006-12-10 02:21:36 -08004151/*
4152 * Writes msr value into into the appropriate "register".
4153 * Returns 0 on success, non-0 otherwise.
4154 * Assumes vcpu_load() was already called.
4155 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004156static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004158 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004159 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004160 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004161 u32 msr_index = msr_info->index;
4162 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004163
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004165 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004166 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004167 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004168#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004170 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171 vmcs_writel(GUEST_FS_BASE, data);
4172 break;
4173 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004174 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 vmcs_writel(GUEST_GS_BASE, data);
4176 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004177 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004178 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004179 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180#endif
4181 case MSR_IA32_SYSENTER_CS:
4182 vmcs_write32(GUEST_SYSENTER_CS, data);
4183 break;
4184 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004185 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 break;
4187 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004188 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004190 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004191 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004192 (!msr_info->host_initiated &&
4193 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004194 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004195 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004196 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004198 vmcs_write64(GUEST_BNDCFGS, data);
4199 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004200 case MSR_IA32_SPEC_CTRL:
4201 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004202 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4203 return 1;
4204
4205 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004206 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004207 return 1;
4208
4209 vmx->spec_ctrl = data;
4210
4211 if (!data)
4212 break;
4213
4214 /*
4215 * For non-nested:
4216 * When it's written (to non-zero) for the first time, pass
4217 * it through.
4218 *
4219 * For nested:
4220 * The handling of the MSR bitmap for L2 guests is done in
4221 * nested_vmx_merge_msr_bitmap. We should not touch the
4222 * vmcs02.msr_bitmap here since it gets completely overwritten
4223 * in the merging. We update the vmcs01 here for L1 as well
4224 * since it will end up touching the MSR anyway now.
4225 */
4226 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4227 MSR_IA32_SPEC_CTRL,
4228 MSR_TYPE_RW);
4229 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004230 case MSR_IA32_PRED_CMD:
4231 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004232 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4233 return 1;
4234
4235 if (data & ~PRED_CMD_IBPB)
4236 return 1;
4237
4238 if (!data)
4239 break;
4240
4241 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4242
4243 /*
4244 * For non-nested:
4245 * When it's written (to non-zero) for the first time, pass
4246 * it through.
4247 *
4248 * For nested:
4249 * The handling of the MSR bitmap for L2 guests is done in
4250 * nested_vmx_merge_msr_bitmap. We should not touch the
4251 * vmcs02.msr_bitmap here since it gets completely overwritten
4252 * in the merging.
4253 */
4254 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4255 MSR_TYPE_W);
4256 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004257 case MSR_IA32_ARCH_CAPABILITIES:
4258 if (!msr_info->host_initiated)
4259 return 1;
4260 vmx->arch_capabilities = data;
4261 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004262 case MSR_IA32_CR_PAT:
4263 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004264 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4265 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004266 vmcs_write64(GUEST_IA32_PAT, data);
4267 vcpu->arch.pat = data;
4268 break;
4269 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004270 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004271 break;
Will Auldba904632012-11-29 12:42:50 -08004272 case MSR_IA32_TSC_ADJUST:
4273 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004274 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004275 case MSR_IA32_MCG_EXT_CTL:
4276 if ((!msr_info->host_initiated &&
4277 !(to_vmx(vcpu)->msr_ia32_feature_control &
4278 FEATURE_CONTROL_LMCE)) ||
4279 (data & ~MCG_EXT_CTL_LMCE_EN))
4280 return 1;
4281 vcpu->arch.mcg_ext_ctl = data;
4282 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004284 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004285 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004286 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4287 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004288 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004289 if (msr_info->host_initiated && data == 0)
4290 vmx_leave_nested(vcpu);
4291 break;
4292 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004293 if (!msr_info->host_initiated)
4294 return 1; /* they are read-only */
4295 if (!nested_vmx_allowed(vcpu))
4296 return 1;
4297 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004298 case MSR_IA32_XSS:
4299 if (!vmx_xsaves_supported())
4300 return 1;
4301 /*
4302 * The only supported bit as of Skylake is bit 8, but
4303 * it is not supported on KVM.
4304 */
4305 if (data != 0)
4306 return 1;
4307 vcpu->arch.ia32_xss = data;
4308 if (vcpu->arch.ia32_xss != host_xss)
4309 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004310 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004311 else
4312 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4313 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004314 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004315 if (!msr_info->host_initiated &&
4316 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004317 return 1;
4318 /* Check reserved bit, higher 32 bits should be zero */
4319 if ((data >> 32) != 0)
4320 return 1;
4321 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004323 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004324 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004325 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004326 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004327 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4328 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004329 ret = kvm_set_shared_msr(msr->index, msr->data,
4330 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004331 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004332 if (ret)
4333 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004334 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004335 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004337 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338 }
4339
Eddie Dong2cc51562007-05-21 07:28:09 +03004340 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341}
4342
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004343static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004345 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4346 switch (reg) {
4347 case VCPU_REGS_RSP:
4348 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4349 break;
4350 case VCPU_REGS_RIP:
4351 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4352 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004353 case VCPU_EXREG_PDPTR:
4354 if (enable_ept)
4355 ept_save_pdptrs(vcpu);
4356 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004357 default:
4358 break;
4359 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360}
4361
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362static __init int cpu_has_kvm_support(void)
4363{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004364 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
4367static __init int vmx_disabled_by_bios(void)
4368{
4369 u64 msr;
4370
4371 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004372 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004374 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4375 && tboot_enabled())
4376 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004377 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004378 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004380 && !tboot_enabled()) {
4381 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004383 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004384 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004385 /* launched w/o TXT and VMX disabled */
4386 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4387 && !tboot_enabled())
4388 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004389 }
4390
4391 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392}
4393
Dongxiao Xu7725b892010-05-11 18:29:38 +08004394static void kvm_cpu_vmxon(u64 addr)
4395{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004396 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004397 intel_pt_handle_vmx(1);
4398
Dongxiao Xu7725b892010-05-11 18:29:38 +08004399 asm volatile (ASM_VMX_VMXON_RAX
4400 : : "a"(&addr), "m"(addr)
4401 : "memory", "cc");
4402}
4403
Radim Krčmář13a34e02014-08-28 15:13:03 +02004404static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004405{
4406 int cpu = raw_smp_processor_id();
4407 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004408 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004410 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004411 return -EBUSY;
4412
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004413 /*
4414 * This can happen if we hot-added a CPU but failed to allocate
4415 * VP assist page for it.
4416 */
4417 if (static_branch_unlikely(&enable_evmcs) &&
4418 !hv_get_vp_assist_page(cpu))
4419 return -EFAULT;
4420
Nadav Har'Eld462b812011-05-24 15:26:10 +03004421 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004422 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4423 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004424
4425 /*
4426 * Now we can enable the vmclear operation in kdump
4427 * since the loaded_vmcss_on_cpu list on this cpu
4428 * has been initialized.
4429 *
4430 * Though the cpu is not in VMX operation now, there
4431 * is no problem to enable the vmclear operation
4432 * for the loaded_vmcss_on_cpu list is empty!
4433 */
4434 crash_enable_local_vmclear(cpu);
4435
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004437
4438 test_bits = FEATURE_CONTROL_LOCKED;
4439 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4440 if (tboot_enabled())
4441 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4442
4443 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004445 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4446 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004447 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004448 if (enable_ept)
4449 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004450
4451 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452}
4453
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004455{
4456 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004457 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004458
Nadav Har'Eld462b812011-05-24 15:26:10 +03004459 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4460 loaded_vmcss_on_cpu_link)
4461 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004462}
4463
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004464
4465/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4466 * tricks.
4467 */
4468static void kvm_cpu_vmxoff(void)
4469{
4470 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004471
4472 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004473 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004474}
4475
Radim Krčmář13a34e02014-08-28 15:13:03 +02004476static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004478 vmclear_local_loaded_vmcss();
4479 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004480}
4481
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004482static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004483 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484{
4485 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004486 u32 ctl = ctl_min | ctl_opt;
4487
4488 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4489
4490 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4491 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4492
4493 /* Ensure minimum (required) set of control bits are supported. */
4494 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004495 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004496
4497 *result = ctl;
4498 return 0;
4499}
4500
Avi Kivity110312c2010-12-21 12:54:20 +02004501static __init bool allow_1_setting(u32 msr, u32 ctl)
4502{
4503 u32 vmx_msr_low, vmx_msr_high;
4504
4505 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4506 return vmx_msr_high & ctl;
4507}
4508
Yang, Sheng002c7f72007-07-31 14:23:01 +03004509static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510{
4511 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004512 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004513 u32 _pin_based_exec_control = 0;
4514 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004515 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004516 u32 _vmexit_control = 0;
4517 u32 _vmentry_control = 0;
4518
Paolo Bonzini13893092018-02-26 13:40:09 +01004519 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304520 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004521#ifdef CONFIG_X86_64
4522 CPU_BASED_CR8_LOAD_EXITING |
4523 CPU_BASED_CR8_STORE_EXITING |
4524#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004525 CPU_BASED_CR3_LOAD_EXITING |
4526 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004527 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004528 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004529 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004530 CPU_BASED_MWAIT_EXITING |
4531 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004532 CPU_BASED_INVLPG_EXITING |
4533 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004534
Sheng Yangf78e0e22007-10-29 09:40:42 +08004535 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004536 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004538 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4539 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004540 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004541#ifdef CONFIG_X86_64
4542 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4543 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4544 ~CPU_BASED_CR8_STORE_EXITING;
4545#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004546 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004547 min2 = 0;
4548 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004549 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004550 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004551 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004552 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004553 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004554 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004555 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004556 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004557 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004558 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004559 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004560 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004561 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004562 SECONDARY_EXEC_RDSEED_EXITING |
4563 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004564 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004565 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004566 SECONDARY_EXEC_ENABLE_VMFUNC |
4567 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004568 if (adjust_vmx_controls(min2, opt2,
4569 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004570 &_cpu_based_2nd_exec_control) < 0)
4571 return -EIO;
4572 }
4573#ifndef CONFIG_X86_64
4574 if (!(_cpu_based_2nd_exec_control &
4575 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4576 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4577#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004578
4579 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4580 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004581 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004582 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4583 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004584
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004585 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4586 &vmx_capability.ept, &vmx_capability.vpid);
4587
Sheng Yangd56f5462008-04-25 10:13:16 +08004588 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004589 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4590 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004591 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4592 CPU_BASED_CR3_STORE_EXITING |
4593 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004594 } else if (vmx_capability.ept) {
4595 vmx_capability.ept = 0;
4596 pr_warn_once("EPT CAP should not exist if not support "
4597 "1-setting enable EPT VM-execution control\n");
4598 }
4599 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4600 vmx_capability.vpid) {
4601 vmx_capability.vpid = 0;
4602 pr_warn_once("VPID CAP should not exist if not support "
4603 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004604 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004605
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004606 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607#ifdef CONFIG_X86_64
4608 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4609#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004610 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004611 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4613 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004614 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004615
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004616 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4617 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4618 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004619 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4620 &_pin_based_exec_control) < 0)
4621 return -EIO;
4622
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004623 if (cpu_has_broken_vmx_preemption_timer())
4624 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004625 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004626 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004627 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4628
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004629 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004630 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4632 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004633 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004634
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004635 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004636
4637 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4638 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004639 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004640
4641#ifdef CONFIG_X86_64
4642 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4643 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004644 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645#endif
4646
4647 /* Require Write-Back (WB) memory type for VMCS accesses. */
4648 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004650
Yang, Sheng002c7f72007-07-31 14:23:01 +03004651 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004652 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004653 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004654
Liran Alon2307af12018-06-29 22:59:04 +03004655 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004656
Yang, Sheng002c7f72007-07-31 14:23:01 +03004657 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4658 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004659 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004660 vmcs_conf->vmexit_ctrl = _vmexit_control;
4661 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004662
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004663 if (static_branch_unlikely(&enable_evmcs))
4664 evmcs_sanitize_exec_ctrls(vmcs_conf);
4665
Avi Kivity110312c2010-12-21 12:54:20 +02004666 cpu_has_load_ia32_efer =
4667 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4668 VM_ENTRY_LOAD_IA32_EFER)
4669 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4670 VM_EXIT_LOAD_IA32_EFER);
4671
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004672 cpu_has_load_perf_global_ctrl =
4673 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4674 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4675 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4676 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4677
4678 /*
4679 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004680 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004681 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4682 *
4683 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4684 *
4685 * AAK155 (model 26)
4686 * AAP115 (model 30)
4687 * AAT100 (model 37)
4688 * BC86,AAY89,BD102 (model 44)
4689 * BA97 (model 46)
4690 *
4691 */
4692 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4693 switch (boot_cpu_data.x86_model) {
4694 case 26:
4695 case 30:
4696 case 37:
4697 case 44:
4698 case 46:
4699 cpu_has_load_perf_global_ctrl = false;
4700 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4701 "does not work properly. Using workaround\n");
4702 break;
4703 default:
4704 break;
4705 }
4706 }
4707
Borislav Petkov782511b2016-04-04 22:25:03 +02004708 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004709 rdmsrl(MSR_IA32_XSS, host_xss);
4710
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004711 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004712}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713
Liran Alon491a6032018-06-23 02:35:12 +03004714static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004715{
4716 int node = cpu_to_node(cpu);
4717 struct page *pages;
4718 struct vmcs *vmcs;
4719
Vlastimil Babka96db8002015-09-08 15:03:50 -07004720 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004721 if (!pages)
4722 return NULL;
4723 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004724 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004725
4726 /* KVM supports Enlightened VMCS v1 only */
4727 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004728 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004729 else
Liran Alon392b2f22018-06-23 02:35:01 +03004730 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004731
Liran Alon491a6032018-06-23 02:35:12 +03004732 if (shadow)
4733 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734 return vmcs;
4735}
4736
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737static void free_vmcs(struct vmcs *vmcs)
4738{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004739 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740}
4741
Nadav Har'Eld462b812011-05-24 15:26:10 +03004742/*
4743 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4744 */
4745static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4746{
4747 if (!loaded_vmcs->vmcs)
4748 return;
4749 loaded_vmcs_clear(loaded_vmcs);
4750 free_vmcs(loaded_vmcs->vmcs);
4751 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004752 if (loaded_vmcs->msr_bitmap)
4753 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004754 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004755}
4756
Liran Alon491a6032018-06-23 02:35:12 +03004757static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004758{
Liran Alon491a6032018-06-23 02:35:12 +03004759 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004760}
4761
4762static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4763{
Liran Alon491a6032018-06-23 02:35:12 +03004764 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004765 if (!loaded_vmcs->vmcs)
4766 return -ENOMEM;
4767
4768 loaded_vmcs->shadow_vmcs = NULL;
4769 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004770
4771 if (cpu_has_vmx_msr_bitmap()) {
4772 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4773 if (!loaded_vmcs->msr_bitmap)
4774 goto out_vmcs;
4775 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004776
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004777 if (IS_ENABLED(CONFIG_HYPERV) &&
4778 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004779 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4780 struct hv_enlightened_vmcs *evmcs =
4781 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4782
4783 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4784 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004785 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004786
4787 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4788
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004789 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004790
4791out_vmcs:
4792 free_loaded_vmcs(loaded_vmcs);
4793 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004794}
4795
Sam Ravnborg39959582007-06-01 00:47:13 -07004796static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797{
4798 int cpu;
4799
Zachary Amsden3230bb42009-09-29 11:38:37 -10004800 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004802 per_cpu(vmxarea, cpu) = NULL;
4803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804}
4805
Jim Mattsond37f4262017-12-22 12:12:16 -08004806enum vmcs_field_width {
4807 VMCS_FIELD_WIDTH_U16 = 0,
4808 VMCS_FIELD_WIDTH_U64 = 1,
4809 VMCS_FIELD_WIDTH_U32 = 2,
4810 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004811};
4812
Jim Mattsond37f4262017-12-22 12:12:16 -08004813static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004814{
4815 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004816 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004817 return (field >> 13) & 0x3 ;
4818}
4819
4820static inline int vmcs_field_readonly(unsigned long field)
4821{
4822 return (((field >> 10) & 0x3) == 1);
4823}
4824
Bandan Dasfe2b2012014-04-21 15:20:14 -04004825static void init_vmcs_shadow_fields(void)
4826{
4827 int i, j;
4828
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004829 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4830 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004831 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004832 (i + 1 == max_shadow_read_only_fields ||
4833 shadow_read_only_fields[i + 1] != field + 1))
4834 pr_err("Missing field from shadow_read_only_field %x\n",
4835 field + 1);
4836
4837 clear_bit(field, vmx_vmread_bitmap);
4838#ifdef CONFIG_X86_64
4839 if (field & 1)
4840 continue;
4841#endif
4842 if (j < i)
4843 shadow_read_only_fields[j] = field;
4844 j++;
4845 }
4846 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004847
4848 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004849 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004850 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004851 (i + 1 == max_shadow_read_write_fields ||
4852 shadow_read_write_fields[i + 1] != field + 1))
4853 pr_err("Missing field from shadow_read_write_field %x\n",
4854 field + 1);
4855
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004856 /*
4857 * PML and the preemption timer can be emulated, but the
4858 * processor cannot vmwrite to fields that don't exist
4859 * on bare metal.
4860 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004861 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004862 case GUEST_PML_INDEX:
4863 if (!cpu_has_vmx_pml())
4864 continue;
4865 break;
4866 case VMX_PREEMPTION_TIMER_VALUE:
4867 if (!cpu_has_vmx_preemption_timer())
4868 continue;
4869 break;
4870 case GUEST_INTR_STATUS:
4871 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004872 continue;
4873 break;
4874 default:
4875 break;
4876 }
4877
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004878 clear_bit(field, vmx_vmwrite_bitmap);
4879 clear_bit(field, vmx_vmread_bitmap);
4880#ifdef CONFIG_X86_64
4881 if (field & 1)
4882 continue;
4883#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004884 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004885 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004886 j++;
4887 }
4888 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004889}
4890
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891static __init int alloc_kvm_area(void)
4892{
4893 int cpu;
4894
Zachary Amsden3230bb42009-09-29 11:38:37 -10004895 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 struct vmcs *vmcs;
4897
Liran Alon491a6032018-06-23 02:35:12 +03004898 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 if (!vmcs) {
4900 free_kvm_area();
4901 return -ENOMEM;
4902 }
4903
Liran Alon2307af12018-06-29 22:59:04 +03004904 /*
4905 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4906 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4907 * revision_id reported by MSR_IA32_VMX_BASIC.
4908 *
4909 * However, even though not explictly documented by
4910 * TLFS, VMXArea passed as VMXON argument should
4911 * still be marked with revision_id reported by
4912 * physical CPU.
4913 */
4914 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004915 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004916
Avi Kivity6aa8b732006-12-10 02:21:36 -08004917 per_cpu(vmxarea, cpu) = vmcs;
4918 }
4919 return 0;
4920}
4921
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004922static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004923 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004924{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004925 if (!emulate_invalid_guest_state) {
4926 /*
4927 * CS and SS RPL should be equal during guest entry according
4928 * to VMX spec, but in reality it is not always so. Since vcpu
4929 * is in the middle of the transition from real mode to
4930 * protected mode it is safe to assume that RPL 0 is a good
4931 * default value.
4932 */
4933 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004934 save->selector &= ~SEGMENT_RPL_MASK;
4935 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004936 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004938 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939}
4940
4941static void enter_pmode(struct kvm_vcpu *vcpu)
4942{
4943 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004945
Gleb Natapovd99e4152012-12-20 16:57:45 +02004946 /*
4947 * Update real mode segment cache. It may be not up-to-date if sement
4948 * register was written while vcpu was in a guest mode.
4949 */
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4952 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4953 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4954 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4955 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4956
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004957 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958
Avi Kivity2fb92db2011-04-27 19:42:18 +03004959 vmx_segment_cache_clear(vmx);
4960
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004961 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962
4963 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004964 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4965 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966 vmcs_writel(GUEST_RFLAGS, flags);
4967
Rusty Russell66aee912007-07-17 23:34:16 +10004968 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4969 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970
4971 update_exception_bitmap(vcpu);
4972
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004973 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4974 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4975 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4976 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4977 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4978 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979}
4980
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004981static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982{
Mathias Krause772e0312012-08-30 01:30:19 +02004983 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004984 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004985
Gleb Natapovd99e4152012-12-20 16:57:45 +02004986 var.dpl = 0x3;
4987 if (seg == VCPU_SREG_CS)
4988 var.type = 0x3;
4989
4990 if (!emulate_invalid_guest_state) {
4991 var.selector = var.base >> 4;
4992 var.base = var.base & 0xffff0;
4993 var.limit = 0xffff;
4994 var.g = 0;
4995 var.db = 0;
4996 var.present = 1;
4997 var.s = 1;
4998 var.l = 0;
4999 var.unusable = 0;
5000 var.type = 0x3;
5001 var.avl = 0;
5002 if (save->base & 0xf)
5003 printk_once(KERN_WARNING "kvm: segment base is not "
5004 "paragraph aligned when entering "
5005 "protected mode (seg=%d)", seg);
5006 }
5007
5008 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005009 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005010 vmcs_write32(sf->limit, var.limit);
5011 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005012}
5013
5014static void enter_rmode(struct kvm_vcpu *vcpu)
5015{
5016 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005017 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005018 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005019
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5022 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5024 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005025 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5026 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005027
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005028 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005029
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 /*
5031 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005032 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005033 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005034 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005035 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5036 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005037
Avi Kivity2fb92db2011-04-27 19:42:18 +03005038 vmx_segment_cache_clear(vmx);
5039
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005040 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5043
5044 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005045 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005046
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005047 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048
5049 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005050 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005051 update_exception_bitmap(vcpu);
5052
Gleb Natapovd99e4152012-12-20 16:57:45 +02005053 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5054 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5055 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5056 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5057 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5058 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005059
Eddie Dong8668a3c2007-10-10 14:26:45 +08005060 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061}
5062
Amit Shah401d10d2009-02-20 22:53:37 +05305063static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5064{
5065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005066 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5067
5068 if (!msr)
5069 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305070
Avi Kivity44ea2b12009-09-06 15:55:37 +03005071 /*
Sean Christopherson678e3152018-07-23 12:32:43 -07005072 * MSR_KERNEL_GS_BASE is not intercepted when the guest is in
5073 * 64-bit mode as a 64-bit kernel may frequently access the
5074 * MSR. This means we need to manually save/restore the MSR
5075 * when switching between guest and host state, but only if
5076 * the guest is in 64-bit mode. Sync our cached value if the
5077 * guest is transitioning to 32-bit mode and the CPU contains
5078 * guest state, i.e. the cache is stale.
Avi Kivity44ea2b12009-09-06 15:55:37 +03005079 */
Sean Christopherson678e3152018-07-23 12:32:43 -07005080#ifdef CONFIG_X86_64
5081 if (!(efer & EFER_LMA))
5082 (void)vmx_read_guest_kernel_gs_base(vmx);
5083#endif
Avi Kivityf6801df2010-01-21 15:31:50 +02005084 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305085 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005086 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305087 msr->data = efer;
5088 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005089 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305090
5091 msr->data = efer & ~EFER_LME;
5092 }
5093 setup_msrs(vmx);
5094}
5095
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005096#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005097
5098static void enter_lmode(struct kvm_vcpu *vcpu)
5099{
5100 u32 guest_tr_ar;
5101
Avi Kivity2fb92db2011-04-27 19:42:18 +03005102 vmx_segment_cache_clear(to_vmx(vcpu));
5103
Avi Kivity6aa8b732006-12-10 02:21:36 -08005104 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005105 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005106 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5107 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005108 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005109 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5110 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005111 }
Avi Kivityda38f432010-07-06 11:30:49 +03005112 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005113}
5114
5115static void exit_lmode(struct kvm_vcpu *vcpu)
5116{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005117 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005118 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005119}
5120
5121#endif
5122
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005123static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5124 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005125{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005126 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005127 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5128 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005129 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005130 } else {
5131 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005132 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005133}
5134
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005135static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005136{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005137 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005138}
5139
Junaid Shahidfaff8752018-06-29 13:10:05 -07005140static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5141{
5142 int vpid = to_vmx(vcpu)->vpid;
5143
5144 if (!vpid_sync_vcpu_addr(vpid, addr))
5145 vpid_sync_context(vpid);
5146
5147 /*
5148 * If VPIDs are not supported or enabled, then the above is a no-op.
5149 * But we don't really need a TLB flush in that case anyway, because
5150 * each VM entry/exit includes an implicit flush when VPID is 0.
5151 */
5152}
5153
Avi Kivitye8467fd2009-12-29 18:43:06 +02005154static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5155{
5156 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5157
5158 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5159 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5160}
5161
Avi Kivityaff48ba2010-12-05 18:56:11 +02005162static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5163{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005164 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005165 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5166 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5167}
5168
Anthony Liguori25c4c272007-04-27 09:29:21 +03005169static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005170{
Avi Kivityfc78f512009-12-07 12:16:48 +02005171 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5172
5173 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5174 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005175}
5176
Sheng Yang14394422008-04-28 12:24:45 +08005177static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5178{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005179 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5180
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005181 if (!test_bit(VCPU_EXREG_PDPTR,
5182 (unsigned long *)&vcpu->arch.regs_dirty))
5183 return;
5184
Sheng Yang14394422008-04-28 12:24:45 +08005185 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005186 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5187 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5188 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5189 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005190 }
5191}
5192
Avi Kivity8f5d5492009-05-31 18:41:29 +03005193static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5194{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005195 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5196
Avi Kivity8f5d5492009-05-31 18:41:29 +03005197 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005198 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5199 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5200 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5201 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005202 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005203
5204 __set_bit(VCPU_EXREG_PDPTR,
5205 (unsigned long *)&vcpu->arch.regs_avail);
5206 __set_bit(VCPU_EXREG_PDPTR,
5207 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005208}
5209
David Matlack38991522016-11-29 18:14:08 -08005210static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5211{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005212 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5213 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005214 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5215
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005216 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005217 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5218 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5219 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5220
5221 return fixed_bits_valid(val, fixed0, fixed1);
5222}
5223
5224static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5225{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005226 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5227 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005228
5229 return fixed_bits_valid(val, fixed0, fixed1);
5230}
5231
5232static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5233{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005234 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5235 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005236
5237 return fixed_bits_valid(val, fixed0, fixed1);
5238}
5239
5240/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5241#define nested_guest_cr4_valid nested_cr4_valid
5242#define nested_host_cr4_valid nested_cr4_valid
5243
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005244static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005245
5246static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5247 unsigned long cr0,
5248 struct kvm_vcpu *vcpu)
5249{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005250 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5251 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005252 if (!(cr0 & X86_CR0_PG)) {
5253 /* From paging/starting to nonpaging */
5254 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005255 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005256 (CPU_BASED_CR3_LOAD_EXITING |
5257 CPU_BASED_CR3_STORE_EXITING));
5258 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005259 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005260 } else if (!is_paging(vcpu)) {
5261 /* From nonpaging to paging */
5262 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005263 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005264 ~(CPU_BASED_CR3_LOAD_EXITING |
5265 CPU_BASED_CR3_STORE_EXITING));
5266 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005267 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005268 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005269
5270 if (!(cr0 & X86_CR0_WP))
5271 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005272}
5273
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5275{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005276 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005277 unsigned long hw_cr0;
5278
Gleb Natapov50378782013-02-04 16:00:28 +02005279 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005280 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005281 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005282 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005283 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005284
Gleb Natapov218e7632013-01-21 15:36:45 +02005285 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5286 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287
Gleb Natapov218e7632013-01-21 15:36:45 +02005288 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5289 enter_rmode(vcpu);
5290 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005291
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005292#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005293 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005294 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005295 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005296 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297 exit_lmode(vcpu);
5298 }
5299#endif
5300
Sean Christophersonb4d18512018-03-05 12:04:40 -08005301 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005302 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5303
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005305 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005306 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005307
5308 /* depends on vcpu->arch.cr0 to be set to a new value */
5309 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310}
5311
Yu Zhang855feb62017-08-24 20:27:55 +08005312static int get_ept_level(struct kvm_vcpu *vcpu)
5313{
5314 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5315 return 5;
5316 return 4;
5317}
5318
Peter Feiner995f00a2017-06-30 17:26:32 -07005319static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005320{
Yu Zhang855feb62017-08-24 20:27:55 +08005321 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005322
Yu Zhang855feb62017-08-24 20:27:55 +08005323 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005324
Peter Feiner995f00a2017-06-30 17:26:32 -07005325 if (enable_ept_ad_bits &&
5326 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005327 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005328 eptp |= (root_hpa & PAGE_MASK);
5329
5330 return eptp;
5331}
5332
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5334{
Tianyu Lan877ad952018-07-19 08:40:23 +00005335 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005336 unsigned long guest_cr3;
5337 u64 eptp;
5338
5339 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005340 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005341 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005342 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005343
5344 if (kvm_x86_ops->tlb_remote_flush) {
5345 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5346 to_vmx(vcpu)->ept_pointer = eptp;
5347 to_kvm_vmx(kvm)->ept_pointers_match
5348 = EPT_POINTERS_CHECK;
5349 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5350 }
5351
Sean Christophersone90008d2018-03-05 12:04:37 -08005352 if (enable_unrestricted_guest || is_paging(vcpu) ||
5353 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005354 guest_cr3 = kvm_read_cr3(vcpu);
5355 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005356 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005357 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005358 }
5359
Sheng Yang14394422008-04-28 12:24:45 +08005360 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005361}
5362
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005363static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005364{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005365 /*
5366 * Pass through host's Machine Check Enable value to hw_cr4, which
5367 * is in force while we are in guest mode. Do not let guests control
5368 * this bit, even if host CR4.MCE == 0.
5369 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005370 unsigned long hw_cr4;
5371
5372 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5373 if (enable_unrestricted_guest)
5374 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5375 else if (to_vmx(vcpu)->rmode.vm86_active)
5376 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5377 else
5378 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005379
Sean Christopherson64f7a112018-04-30 10:01:06 -07005380 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5381 if (cr4 & X86_CR4_UMIP) {
5382 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005383 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005384 hw_cr4 &= ~X86_CR4_UMIP;
5385 } else if (!is_guest_mode(vcpu) ||
5386 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5387 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5388 SECONDARY_EXEC_DESC);
5389 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005390
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005391 if (cr4 & X86_CR4_VMXE) {
5392 /*
5393 * To use VMXON (and later other VMX instructions), a guest
5394 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5395 * So basically the check on whether to allow nested VMX
5396 * is here.
5397 */
5398 if (!nested_vmx_allowed(vcpu))
5399 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005400 }
David Matlack38991522016-11-29 18:14:08 -08005401
5402 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005403 return 1;
5404
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005405 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005406
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005407 if (!enable_unrestricted_guest) {
5408 if (enable_ept) {
5409 if (!is_paging(vcpu)) {
5410 hw_cr4 &= ~X86_CR4_PAE;
5411 hw_cr4 |= X86_CR4_PSE;
5412 } else if (!(cr4 & X86_CR4_PAE)) {
5413 hw_cr4 &= ~X86_CR4_PAE;
5414 }
5415 }
5416
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005417 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005418 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5419 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5420 * to be manually disabled when guest switches to non-paging
5421 * mode.
5422 *
5423 * If !enable_unrestricted_guest, the CPU is always running
5424 * with CR0.PG=1 and CR4 needs to be modified.
5425 * If enable_unrestricted_guest, the CPU automatically
5426 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005427 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005428 if (!is_paging(vcpu))
5429 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5430 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005431
Sheng Yang14394422008-04-28 12:24:45 +08005432 vmcs_writel(CR4_READ_SHADOW, cr4);
5433 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005434 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005435}
5436
Avi Kivity6aa8b732006-12-10 02:21:36 -08005437static void vmx_get_segment(struct kvm_vcpu *vcpu,
5438 struct kvm_segment *var, int seg)
5439{
Avi Kivitya9179492011-01-03 14:28:52 +02005440 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005441 u32 ar;
5442
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005443 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005444 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005445 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005446 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005447 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005448 var->base = vmx_read_guest_seg_base(vmx, seg);
5449 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5450 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005451 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005452 var->base = vmx_read_guest_seg_base(vmx, seg);
5453 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5454 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5455 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005456 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457 var->type = ar & 15;
5458 var->s = (ar >> 4) & 1;
5459 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005460 /*
5461 * Some userspaces do not preserve unusable property. Since usable
5462 * segment has to be present according to VMX spec we can use present
5463 * property to amend userspace bug by making unusable segment always
5464 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5465 * segment as unusable.
5466 */
5467 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005468 var->avl = (ar >> 12) & 1;
5469 var->l = (ar >> 13) & 1;
5470 var->db = (ar >> 14) & 1;
5471 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005472}
5473
Avi Kivitya9179492011-01-03 14:28:52 +02005474static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5475{
Avi Kivitya9179492011-01-03 14:28:52 +02005476 struct kvm_segment s;
5477
5478 if (to_vmx(vcpu)->rmode.vm86_active) {
5479 vmx_get_segment(vcpu, &s, seg);
5480 return s.base;
5481 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005482 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005483}
5484
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005485static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005486{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005487 struct vcpu_vmx *vmx = to_vmx(vcpu);
5488
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005489 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005490 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005491 else {
5492 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005493 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005494 }
Avi Kivity69c73022011-03-07 15:26:44 +02005495}
5496
Avi Kivity653e3102007-05-07 10:55:37 +03005497static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005498{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499 u32 ar;
5500
Avi Kivityf0495f92012-06-07 17:06:10 +03005501 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005502 ar = 1 << 16;
5503 else {
5504 ar = var->type & 15;
5505 ar |= (var->s & 1) << 4;
5506 ar |= (var->dpl & 3) << 5;
5507 ar |= (var->present & 1) << 7;
5508 ar |= (var->avl & 1) << 12;
5509 ar |= (var->l & 1) << 13;
5510 ar |= (var->db & 1) << 14;
5511 ar |= (var->g & 1) << 15;
5512 }
Avi Kivity653e3102007-05-07 10:55:37 +03005513
5514 return ar;
5515}
5516
5517static void vmx_set_segment(struct kvm_vcpu *vcpu,
5518 struct kvm_segment *var, int seg)
5519{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005521 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005522
Avi Kivity2fb92db2011-04-27 19:42:18 +03005523 vmx_segment_cache_clear(vmx);
5524
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005525 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5526 vmx->rmode.segs[seg] = *var;
5527 if (seg == VCPU_SREG_TR)
5528 vmcs_write16(sf->selector, var->selector);
5529 else if (var->s)
5530 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005531 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005532 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005533
Avi Kivity653e3102007-05-07 10:55:37 +03005534 vmcs_writel(sf->base, var->base);
5535 vmcs_write32(sf->limit, var->limit);
5536 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005537
5538 /*
5539 * Fix the "Accessed" bit in AR field of segment registers for older
5540 * qemu binaries.
5541 * IA32 arch specifies that at the time of processor reset the
5542 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005543 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005544 * state vmexit when "unrestricted guest" mode is turned on.
5545 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5546 * tree. Newer qemu binaries with that qemu fix would not need this
5547 * kvm hack.
5548 */
5549 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005550 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005551
Gleb Natapovf924d662012-12-12 19:10:55 +02005552 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005553
5554out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005555 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556}
5557
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5559{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005560 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561
5562 *db = (ar >> 14) & 1;
5563 *l = (ar >> 13) & 1;
5564}
5565
Gleb Natapov89a27f42010-02-16 10:51:48 +02005566static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005568 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5569 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570}
5571
Gleb Natapov89a27f42010-02-16 10:51:48 +02005572static void vmx_set_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 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5575 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005576}
5577
Gleb Natapov89a27f42010-02-16 10:51:48 +02005578static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005579{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005580 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5581 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005582}
5583
Gleb Natapov89a27f42010-02-16 10:51:48 +02005584static void vmx_set_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 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5587 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005588}
5589
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005590static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5591{
5592 struct kvm_segment var;
5593 u32 ar;
5594
5595 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005596 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005597 if (seg == VCPU_SREG_CS)
5598 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005599 ar = vmx_segment_access_rights(&var);
5600
5601 if (var.base != (var.selector << 4))
5602 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005603 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005604 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005605 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005606 return false;
5607
5608 return true;
5609}
5610
5611static bool code_segment_valid(struct kvm_vcpu *vcpu)
5612{
5613 struct kvm_segment cs;
5614 unsigned int cs_rpl;
5615
5616 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005617 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005618
Avi Kivity1872a3f2009-01-04 23:26:52 +02005619 if (cs.unusable)
5620 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005621 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005622 return false;
5623 if (!cs.s)
5624 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005625 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005626 if (cs.dpl > cs_rpl)
5627 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005628 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005629 if (cs.dpl != cs_rpl)
5630 return false;
5631 }
5632 if (!cs.present)
5633 return false;
5634
5635 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5636 return true;
5637}
5638
5639static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5640{
5641 struct kvm_segment ss;
5642 unsigned int ss_rpl;
5643
5644 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005645 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005646
Avi Kivity1872a3f2009-01-04 23:26:52 +02005647 if (ss.unusable)
5648 return true;
5649 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005650 return false;
5651 if (!ss.s)
5652 return false;
5653 if (ss.dpl != ss_rpl) /* DPL != RPL */
5654 return false;
5655 if (!ss.present)
5656 return false;
5657
5658 return true;
5659}
5660
5661static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5662{
5663 struct kvm_segment var;
5664 unsigned int rpl;
5665
5666 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005667 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005668
Avi Kivity1872a3f2009-01-04 23:26:52 +02005669 if (var.unusable)
5670 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005671 if (!var.s)
5672 return false;
5673 if (!var.present)
5674 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005675 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005676 if (var.dpl < rpl) /* DPL < RPL */
5677 return false;
5678 }
5679
5680 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5681 * rights flags
5682 */
5683 return true;
5684}
5685
5686static bool tr_valid(struct kvm_vcpu *vcpu)
5687{
5688 struct kvm_segment tr;
5689
5690 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5691
Avi Kivity1872a3f2009-01-04 23:26:52 +02005692 if (tr.unusable)
5693 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005694 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005695 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005696 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005697 return false;
5698 if (!tr.present)
5699 return false;
5700
5701 return true;
5702}
5703
5704static bool ldtr_valid(struct kvm_vcpu *vcpu)
5705{
5706 struct kvm_segment ldtr;
5707
5708 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5709
Avi Kivity1872a3f2009-01-04 23:26:52 +02005710 if (ldtr.unusable)
5711 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005712 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005713 return false;
5714 if (ldtr.type != 2)
5715 return false;
5716 if (!ldtr.present)
5717 return false;
5718
5719 return true;
5720}
5721
5722static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5723{
5724 struct kvm_segment cs, ss;
5725
5726 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5727 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5728
Nadav Amitb32a9912015-03-29 16:33:04 +03005729 return ((cs.selector & SEGMENT_RPL_MASK) ==
5730 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005731}
5732
5733/*
5734 * Check if guest state is valid. Returns true if valid, false if
5735 * not.
5736 * We assume that registers are always usable
5737 */
5738static bool guest_state_valid(struct kvm_vcpu *vcpu)
5739{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005740 if (enable_unrestricted_guest)
5741 return true;
5742
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005743 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005744 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005745 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5746 return false;
5747 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5748 return false;
5749 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5750 return false;
5751 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5752 return false;
5753 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5754 return false;
5755 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5756 return false;
5757 } else {
5758 /* protected mode guest state checks */
5759 if (!cs_ss_rpl_check(vcpu))
5760 return false;
5761 if (!code_segment_valid(vcpu))
5762 return false;
5763 if (!stack_segment_valid(vcpu))
5764 return false;
5765 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5766 return false;
5767 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5768 return false;
5769 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5770 return false;
5771 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5772 return false;
5773 if (!tr_valid(vcpu))
5774 return false;
5775 if (!ldtr_valid(vcpu))
5776 return false;
5777 }
5778 /* TODO:
5779 * - Add checks on RIP
5780 * - Add checks on RFLAGS
5781 */
5782
5783 return true;
5784}
5785
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005786static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5787{
5788 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5789}
5790
Mike Dayd77c26f2007-10-08 09:02:08 -04005791static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005792{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005793 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005794 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005795 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005796
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005797 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005798 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005799 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5800 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005801 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005802 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005803 r = kvm_write_guest_page(kvm, fn++, &data,
5804 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005805 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005806 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005807 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5808 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005809 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005810 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5811 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005812 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005813 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005814 r = kvm_write_guest_page(kvm, fn, &data,
5815 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5816 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005817out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005818 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005819 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005820}
5821
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005822static int init_rmode_identity_map(struct kvm *kvm)
5823{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005824 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005825 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005826 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005827 u32 tmp;
5828
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005829 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005830 mutex_lock(&kvm->slots_lock);
5831
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005832 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005833 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005834
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005835 if (!kvm_vmx->ept_identity_map_addr)
5836 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5837 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005838
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005839 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005840 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005841 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005842 goto out2;
5843
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005844 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005845 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5846 if (r < 0)
5847 goto out;
5848 /* Set up identity-mapping pagetable for EPT in real mode */
5849 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5850 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5851 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5852 r = kvm_write_guest_page(kvm, identity_map_pfn,
5853 &tmp, i * sizeof(tmp), sizeof(tmp));
5854 if (r < 0)
5855 goto out;
5856 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005857 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005858
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005859out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005860 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005861
5862out2:
5863 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005864 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005865}
5866
Avi Kivity6aa8b732006-12-10 02:21:36 -08005867static void seg_setup(int seg)
5868{
Mathias Krause772e0312012-08-30 01:30:19 +02005869 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005870 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005871
5872 vmcs_write16(sf->selector, 0);
5873 vmcs_writel(sf->base, 0);
5874 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005875 ar = 0x93;
5876 if (seg == VCPU_SREG_CS)
5877 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005878
5879 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005880}
5881
Sheng Yangf78e0e22007-10-29 09:40:42 +08005882static int alloc_apic_access_page(struct kvm *kvm)
5883{
Xiao Guangrong44841412012-09-07 14:14:20 +08005884 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005885 int r = 0;
5886
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005887 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005888 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005889 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005890 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5891 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005892 if (r)
5893 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005894
Tang Chen73a6d942014-09-11 13:38:00 +08005895 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005896 if (is_error_page(page)) {
5897 r = -EFAULT;
5898 goto out;
5899 }
5900
Tang Chenc24ae0d2014-09-24 15:57:58 +08005901 /*
5902 * Do not pin the page in memory, so that memory hot-unplug
5903 * is able to migrate it.
5904 */
5905 put_page(page);
5906 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005907out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005908 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005909 return r;
5910}
5911
Wanpeng Li991e7a02015-09-16 17:30:05 +08005912static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005913{
5914 int vpid;
5915
Avi Kivity919818a2009-03-23 18:01:29 +02005916 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005917 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005918 spin_lock(&vmx_vpid_lock);
5919 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005920 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005921 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005922 else
5923 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005924 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005925 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005926}
5927
Wanpeng Li991e7a02015-09-16 17:30:05 +08005928static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005929{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005930 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005931 return;
5932 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005933 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005934 spin_unlock(&vmx_vpid_lock);
5935}
5936
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005937static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5938 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005939{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005940 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005941
5942 if (!cpu_has_vmx_msr_bitmap())
5943 return;
5944
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005945 if (static_branch_unlikely(&enable_evmcs))
5946 evmcs_touch_msr_bitmap();
5947
Sheng Yang25c5f222008-03-28 13:18:56 +08005948 /*
5949 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5950 * have the write-low and read-high bitmap offsets the wrong way round.
5951 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5952 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005953 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005954 if (type & MSR_TYPE_R)
5955 /* read-low */
5956 __clear_bit(msr, msr_bitmap + 0x000 / f);
5957
5958 if (type & MSR_TYPE_W)
5959 /* write-low */
5960 __clear_bit(msr, msr_bitmap + 0x800 / f);
5961
Sheng Yang25c5f222008-03-28 13:18:56 +08005962 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5963 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005964 if (type & MSR_TYPE_R)
5965 /* read-high */
5966 __clear_bit(msr, msr_bitmap + 0x400 / f);
5967
5968 if (type & MSR_TYPE_W)
5969 /* write-high */
5970 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5971
5972 }
5973}
5974
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005975static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5976 u32 msr, int type)
5977{
5978 int f = sizeof(unsigned long);
5979
5980 if (!cpu_has_vmx_msr_bitmap())
5981 return;
5982
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005983 if (static_branch_unlikely(&enable_evmcs))
5984 evmcs_touch_msr_bitmap();
5985
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005986 /*
5987 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5988 * have the write-low and read-high bitmap offsets the wrong way round.
5989 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5990 */
5991 if (msr <= 0x1fff) {
5992 if (type & MSR_TYPE_R)
5993 /* read-low */
5994 __set_bit(msr, msr_bitmap + 0x000 / f);
5995
5996 if (type & MSR_TYPE_W)
5997 /* write-low */
5998 __set_bit(msr, msr_bitmap + 0x800 / f);
5999
6000 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6001 msr &= 0x1fff;
6002 if (type & MSR_TYPE_R)
6003 /* read-high */
6004 __set_bit(msr, msr_bitmap + 0x400 / f);
6005
6006 if (type & MSR_TYPE_W)
6007 /* write-high */
6008 __set_bit(msr, msr_bitmap + 0xc00 / f);
6009
6010 }
6011}
6012
6013static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
6014 u32 msr, int type, bool value)
6015{
6016 if (value)
6017 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6018 else
6019 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6020}
6021
Wincy Vanf2b93282015-02-03 23:56:03 +08006022/*
6023 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6024 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6025 */
6026static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6027 unsigned long *msr_bitmap_nested,
6028 u32 msr, int type)
6029{
6030 int f = sizeof(unsigned long);
6031
Wincy Vanf2b93282015-02-03 23:56:03 +08006032 /*
6033 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6034 * have the write-low and read-high bitmap offsets the wrong way round.
6035 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6036 */
6037 if (msr <= 0x1fff) {
6038 if (type & MSR_TYPE_R &&
6039 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6040 /* read-low */
6041 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6042
6043 if (type & MSR_TYPE_W &&
6044 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6045 /* write-low */
6046 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6047
6048 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6049 msr &= 0x1fff;
6050 if (type & MSR_TYPE_R &&
6051 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6052 /* read-high */
6053 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6054
6055 if (type & MSR_TYPE_W &&
6056 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6057 /* write-high */
6058 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6059
6060 }
6061}
6062
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006063static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006064{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006065 u8 mode = 0;
6066
6067 if (cpu_has_secondary_exec_ctrls() &&
6068 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6069 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6070 mode |= MSR_BITMAP_MODE_X2APIC;
6071 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6072 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6073 }
6074
6075 if (is_long_mode(vcpu))
6076 mode |= MSR_BITMAP_MODE_LM;
6077
6078 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006079}
6080
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006081#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6082
6083static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6084 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006085{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006086 int msr;
6087
6088 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6089 unsigned word = msr / BITS_PER_LONG;
6090 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6091 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006092 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006093
6094 if (mode & MSR_BITMAP_MODE_X2APIC) {
6095 /*
6096 * TPR reads and writes can be virtualized even if virtual interrupt
6097 * delivery is not in use.
6098 */
6099 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6100 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6101 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6102 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6103 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6104 }
6105 }
6106}
6107
6108static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6109{
6110 struct vcpu_vmx *vmx = to_vmx(vcpu);
6111 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6112 u8 mode = vmx_msr_bitmap_mode(vcpu);
6113 u8 changed = mode ^ vmx->msr_bitmap_mode;
6114
6115 if (!changed)
6116 return;
6117
6118 vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW,
6119 !(mode & MSR_BITMAP_MODE_LM));
6120
6121 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6122 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6123
6124 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006125}
6126
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006127static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006128{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006129 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006130}
6131
David Matlackc9f04402017-08-01 14:00:40 -07006132static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6133{
6134 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6135 gfn_t gfn;
6136
6137 /*
6138 * Don't need to mark the APIC access page dirty; it is never
6139 * written to by the CPU during APIC virtualization.
6140 */
6141
6142 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6143 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6144 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6145 }
6146
6147 if (nested_cpu_has_posted_intr(vmcs12)) {
6148 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6149 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6150 }
6151}
6152
6153
David Hildenbrand6342c502017-01-25 11:58:58 +01006154static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006155{
6156 struct vcpu_vmx *vmx = to_vmx(vcpu);
6157 int max_irr;
6158 void *vapic_page;
6159 u16 status;
6160
David Matlackc9f04402017-08-01 14:00:40 -07006161 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6162 return;
Wincy Van705699a2015-02-03 23:58:17 +08006163
David Matlackc9f04402017-08-01 14:00:40 -07006164 vmx->nested.pi_pending = false;
6165 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6166 return;
Wincy Van705699a2015-02-03 23:58:17 +08006167
David Matlackc9f04402017-08-01 14:00:40 -07006168 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6169 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006170 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006171 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6172 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006173 kunmap(vmx->nested.virtual_apic_page);
6174
6175 status = vmcs_read16(GUEST_INTR_STATUS);
6176 if ((u8)max_irr > ((u8)status & 0xff)) {
6177 status &= ~0xff;
6178 status |= (u8)max_irr;
6179 vmcs_write16(GUEST_INTR_STATUS, status);
6180 }
6181 }
David Matlackc9f04402017-08-01 14:00:40 -07006182
6183 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006184}
6185
Wincy Van06a55242017-04-28 13:13:59 +08006186static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6187 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006188{
6189#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006190 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6191
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006192 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006193 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006194 * The vector of interrupt to be delivered to vcpu had
6195 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006196 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006197 * Following cases will be reached in this block, and
6198 * we always send a notification event in all cases as
6199 * explained below.
6200 *
6201 * Case 1: vcpu keeps in non-root mode. Sending a
6202 * notification event posts the interrupt to vcpu.
6203 *
6204 * Case 2: vcpu exits to root mode and is still
6205 * runnable. PIR will be synced to vIRR before the
6206 * next vcpu entry. Sending a notification event in
6207 * this case has no effect, as vcpu is not in root
6208 * mode.
6209 *
6210 * Case 3: vcpu exits to root mode and is blocked.
6211 * vcpu_block() has already synced PIR to vIRR and
6212 * never blocks vcpu if vIRR is not cleared. Therefore,
6213 * a blocked vcpu here does not wait for any requested
6214 * interrupts in PIR, and sending a notification event
6215 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006216 */
Feng Wu28b835d2015-09-18 22:29:54 +08006217
Wincy Van06a55242017-04-28 13:13:59 +08006218 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006219 return true;
6220 }
6221#endif
6222 return false;
6223}
6224
Wincy Van705699a2015-02-03 23:58:17 +08006225static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6226 int vector)
6227{
6228 struct vcpu_vmx *vmx = to_vmx(vcpu);
6229
6230 if (is_guest_mode(vcpu) &&
6231 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006232 /*
6233 * If a posted intr is not recognized by hardware,
6234 * we will accomplish it in the next vmentry.
6235 */
6236 vmx->nested.pi_pending = true;
6237 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006238 /* the PIR and ON have been set by L1. */
6239 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6240 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006241 return 0;
6242 }
6243 return -1;
6244}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006245/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006246 * Send interrupt to vcpu via posted interrupt way.
6247 * 1. If target vcpu is running(non-root mode), send posted interrupt
6248 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6249 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6250 * interrupt from PIR in next vmentry.
6251 */
6252static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6253{
6254 struct vcpu_vmx *vmx = to_vmx(vcpu);
6255 int r;
6256
Wincy Van705699a2015-02-03 23:58:17 +08006257 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6258 if (!r)
6259 return;
6260
Yang Zhanga20ed542013-04-11 19:25:15 +08006261 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6262 return;
6263
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006264 /* If a previous notification has sent the IPI, nothing to do. */
6265 if (pi_test_and_set_on(&vmx->pi_desc))
6266 return;
6267
Wincy Van06a55242017-04-28 13:13:59 +08006268 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006269 kvm_vcpu_kick(vcpu);
6270}
6271
Avi Kivity6aa8b732006-12-10 02:21:36 -08006272/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006273 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6274 * will not change in the lifetime of the guest.
6275 * Note that host-state that does change is set elsewhere. E.g., host-state
6276 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6277 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006278static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006279{
6280 u32 low32, high32;
6281 unsigned long tmpl;
6282 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006283 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006284
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006285 cr0 = read_cr0();
6286 WARN_ON(cr0 & X86_CR0_TS);
6287 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006288
6289 /*
6290 * Save the most likely value for this task's CR3 in the VMCS.
6291 * We can't use __get_current_cr3_fast() because we're not atomic.
6292 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006293 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006294 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006295 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006296
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006297 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006298 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006299 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006300 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006301
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006302 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006303#ifdef CONFIG_X86_64
6304 /*
6305 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006306 * vmx_prepare_switch_to_host(), in case userspace uses
6307 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006308 */
6309 vmcs_write16(HOST_DS_SELECTOR, 0);
6310 vmcs_write16(HOST_ES_SELECTOR, 0);
6311#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006312 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6313 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006314#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006315 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6316 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6317
Juergen Gross87930012017-09-04 12:25:27 +02006318 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006319 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006320 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006321
Avi Kivity83287ea422012-09-16 15:10:57 +03006322 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006323
6324 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6325 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6326 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6327 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6328
6329 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6330 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6331 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6332 }
6333}
6334
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006335static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6336{
6337 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6338 if (enable_ept)
6339 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006340 if (is_guest_mode(&vmx->vcpu))
6341 vmx->vcpu.arch.cr4_guest_owned_bits &=
6342 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006343 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6344}
6345
Yang Zhang01e439b2013-04-11 19:25:12 +08006346static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6347{
6348 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6349
Andrey Smetanind62caab2015-11-10 15:36:33 +03006350 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006351 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006352
6353 if (!enable_vnmi)
6354 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6355
Yunhong Jiang64672c92016-06-13 14:19:59 -07006356 /* Enable the preemption timer dynamically */
6357 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006358 return pin_based_exec_ctrl;
6359}
6360
Andrey Smetanind62caab2015-11-10 15:36:33 +03006361static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6362{
6363 struct vcpu_vmx *vmx = to_vmx(vcpu);
6364
6365 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006366 if (cpu_has_secondary_exec_ctrls()) {
6367 if (kvm_vcpu_apicv_active(vcpu))
6368 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6369 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6370 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6371 else
6372 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6373 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6374 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6375 }
6376
6377 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006378 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006379}
6380
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006381static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6382{
6383 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006384
6385 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6386 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6387
Paolo Bonzini35754c92015-07-29 12:05:37 +02006388 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006389 exec_control &= ~CPU_BASED_TPR_SHADOW;
6390#ifdef CONFIG_X86_64
6391 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6392 CPU_BASED_CR8_LOAD_EXITING;
6393#endif
6394 }
6395 if (!enable_ept)
6396 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6397 CPU_BASED_CR3_LOAD_EXITING |
6398 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006399 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6400 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6401 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006402 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6403 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006404 return exec_control;
6405}
6406
Jim Mattson45ec3682017-08-23 16:32:04 -07006407static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006408{
Jim Mattson45ec3682017-08-23 16:32:04 -07006409 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006410 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006411}
6412
Jim Mattson75f4fc82017-08-23 16:32:03 -07006413static bool vmx_rdseed_supported(void)
6414{
6415 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006416 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006417}
6418
Paolo Bonzini80154d72017-08-24 13:55:35 +02006419static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006420{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006421 struct kvm_vcpu *vcpu = &vmx->vcpu;
6422
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006424
Paolo Bonzini80154d72017-08-24 13:55:35 +02006425 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006426 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6427 if (vmx->vpid == 0)
6428 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6429 if (!enable_ept) {
6430 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6431 enable_unrestricted_guest = 0;
6432 }
6433 if (!enable_unrestricted_guest)
6434 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006435 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006436 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006437 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006438 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6439 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006440 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006441
6442 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6443 * in vmx_set_cr4. */
6444 exec_control &= ~SECONDARY_EXEC_DESC;
6445
Abel Gordonabc4fc52013-04-18 14:35:25 +03006446 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6447 (handle_vmptrld).
6448 We can NOT enable shadow_vmcs here because we don't have yet
6449 a current VMCS12
6450 */
6451 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006452
6453 if (!enable_pml)
6454 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006455
Paolo Bonzini3db13482017-08-24 14:48:03 +02006456 if (vmx_xsaves_supported()) {
6457 /* Exposing XSAVES only when XSAVE is exposed */
6458 bool xsaves_enabled =
6459 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6460 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6461
6462 if (!xsaves_enabled)
6463 exec_control &= ~SECONDARY_EXEC_XSAVES;
6464
6465 if (nested) {
6466 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006467 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006468 SECONDARY_EXEC_XSAVES;
6469 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006470 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006471 ~SECONDARY_EXEC_XSAVES;
6472 }
6473 }
6474
Paolo Bonzini80154d72017-08-24 13:55:35 +02006475 if (vmx_rdtscp_supported()) {
6476 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6477 if (!rdtscp_enabled)
6478 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6479
6480 if (nested) {
6481 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006482 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006483 SECONDARY_EXEC_RDTSCP;
6484 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006485 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006486 ~SECONDARY_EXEC_RDTSCP;
6487 }
6488 }
6489
6490 if (vmx_invpcid_supported()) {
6491 /* Exposing INVPCID only when PCID is exposed */
6492 bool invpcid_enabled =
6493 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6494 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6495
6496 if (!invpcid_enabled) {
6497 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6498 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6499 }
6500
6501 if (nested) {
6502 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006503 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006504 SECONDARY_EXEC_ENABLE_INVPCID;
6505 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006506 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006507 ~SECONDARY_EXEC_ENABLE_INVPCID;
6508 }
6509 }
6510
Jim Mattson45ec3682017-08-23 16:32:04 -07006511 if (vmx_rdrand_supported()) {
6512 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6513 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006514 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006515
6516 if (nested) {
6517 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006518 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006519 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006520 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006521 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006522 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006523 }
6524 }
6525
Jim Mattson75f4fc82017-08-23 16:32:03 -07006526 if (vmx_rdseed_supported()) {
6527 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6528 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006529 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006530
6531 if (nested) {
6532 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006533 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006534 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006535 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006536 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006537 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006538 }
6539 }
6540
Paolo Bonzini80154d72017-08-24 13:55:35 +02006541 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006542}
6543
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006544static void ept_set_mmio_spte_mask(void)
6545{
6546 /*
6547 * EPT Misconfigurations can be generated if the value of bits 2:0
6548 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006549 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006550 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6551 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006552}
6553
Wanpeng Lif53cd632014-12-02 19:14:58 +08006554#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006555/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006556 * Sets up the vmcs for emulated real mode.
6557 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006558static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006559{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006560 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006561
Abel Gordon4607c2d2013-04-18 14:35:55 +03006562 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006563 /*
6564 * At vCPU creation, "VMWRITE to any supported field
6565 * in the VMCS" is supported, so use the more
6566 * permissive vmx_vmread_bitmap to specify both read
6567 * and write permissions for the shadow VMCS.
6568 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006569 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006570 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006571 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006572 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006573 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006574
Avi Kivity6aa8b732006-12-10 02:21:36 -08006575 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6576
Avi Kivity6aa8b732006-12-10 02:21:36 -08006577 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006578 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006579 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006580
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006581 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582
Dan Williamsdfa169b2016-06-02 11:17:24 -07006583 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006584 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006585 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006586 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006587 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006588
Andrey Smetanind62caab2015-11-10 15:36:33 +03006589 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006590 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6591 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6592 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6593 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6594
6595 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006596
Li RongQing0bcf2612015-12-03 13:29:34 +08006597 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006598 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006599 }
6600
Wanpeng Lib31c1142018-03-12 04:53:04 -07006601 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006602 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006603 vmx->ple_window = ple_window;
6604 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006605 }
6606
Xiao Guangrongc3707952011-07-12 03:28:04 +08006607 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6608 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6610
Avi Kivity9581d442010-10-19 16:46:55 +02006611 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6612 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006613 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006614 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6615 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006616
Bandan Das2a499e42017-08-03 15:54:41 -04006617 if (cpu_has_vmx_vmfunc())
6618 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6619
Eddie Dong2cc51562007-05-21 07:28:09 +03006620 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6621 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006622 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006623 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006624 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006625
Radim Krčmář74545702015-04-27 15:11:25 +02006626 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6627 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006628
Paolo Bonzini03916db2014-07-24 14:21:57 +02006629 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006630 u32 index = vmx_msr_index[i];
6631 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006632 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006633
6634 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6635 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006636 if (wrmsr_safe(index, data_low, data_high) < 0)
6637 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006638 vmx->guest_msrs[j].index = i;
6639 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006640 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006641 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006644 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006645
6646 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006647
6648 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006649 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006650
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006651 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6652 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6653
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006654 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006655
Wanpeng Lif53cd632014-12-02 19:14:58 +08006656 if (vmx_xsaves_supported())
6657 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6658
Peter Feiner4e595162016-07-07 14:49:58 -07006659 if (enable_pml) {
6660 ASSERT(vmx->pml_pg);
6661 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6662 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6663 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006664
6665 if (cpu_has_vmx_encls_vmexit())
6666 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006667}
6668
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006669static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006670{
6671 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006672 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006673 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006674
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006675 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006676 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006677
Wanpeng Li518e7b92018-02-28 14:03:31 +08006678 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006679 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006680 kvm_set_cr8(vcpu, 0);
6681
6682 if (!init_event) {
6683 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6684 MSR_IA32_APICBASE_ENABLE;
6685 if (kvm_vcpu_is_reset_bsp(vcpu))
6686 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6687 apic_base_msr.host_initiated = true;
6688 kvm_set_apic_base(vcpu, &apic_base_msr);
6689 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006690
Avi Kivity2fb92db2011-04-27 19:42:18 +03006691 vmx_segment_cache_clear(vmx);
6692
Avi Kivity5706be02008-08-20 15:07:31 +03006693 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006694 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006695 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006696
6697 seg_setup(VCPU_SREG_DS);
6698 seg_setup(VCPU_SREG_ES);
6699 seg_setup(VCPU_SREG_FS);
6700 seg_setup(VCPU_SREG_GS);
6701 seg_setup(VCPU_SREG_SS);
6702
6703 vmcs_write16(GUEST_TR_SELECTOR, 0);
6704 vmcs_writel(GUEST_TR_BASE, 0);
6705 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6706 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6707
6708 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6709 vmcs_writel(GUEST_LDTR_BASE, 0);
6710 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6711 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6712
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006713 if (!init_event) {
6714 vmcs_write32(GUEST_SYSENTER_CS, 0);
6715 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6716 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6717 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6718 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006719
Wanpeng Lic37c2872017-11-20 14:52:21 -08006720 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006721 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006722
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006723 vmcs_writel(GUEST_GDTR_BASE, 0);
6724 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6725
6726 vmcs_writel(GUEST_IDTR_BASE, 0);
6727 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6728
Anthony Liguori443381a2010-12-06 10:53:38 -06006729 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006730 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006731 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006732 if (kvm_mpx_supported())
6733 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006734
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006735 setup_msrs(vmx);
6736
Avi Kivity6aa8b732006-12-10 02:21:36 -08006737 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6738
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006739 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006740 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006741 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006742 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006743 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006744 vmcs_write32(TPR_THRESHOLD, 0);
6745 }
6746
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006747 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006748
Sheng Yang2384d2b2008-01-17 15:14:33 +08006749 if (vmx->vpid != 0)
6750 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6751
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006752 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006753 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006754 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006755 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006756 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006757
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006758 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006759
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006760 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006761 if (init_event)
6762 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006763}
6764
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006765/*
6766 * In nested virtualization, check if L1 asked to exit on external interrupts.
6767 * For most existing hypervisors, this will always return true.
6768 */
6769static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6770{
6771 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6772 PIN_BASED_EXT_INTR_MASK;
6773}
6774
Bandan Das77b0f5d2014-04-19 18:17:45 -04006775/*
6776 * In nested virtualization, check if L1 has set
6777 * VM_EXIT_ACK_INTR_ON_EXIT
6778 */
6779static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6780{
6781 return get_vmcs12(vcpu)->vm_exit_controls &
6782 VM_EXIT_ACK_INTR_ON_EXIT;
6783}
6784
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006785static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6786{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006787 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006788}
6789
Jan Kiszkac9a79532014-03-07 20:03:15 +01006790static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006791{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006792 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6793 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006794}
6795
Jan Kiszkac9a79532014-03-07 20:03:15 +01006796static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006797{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006798 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006799 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006800 enable_irq_window(vcpu);
6801 return;
6802 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006803
Paolo Bonzini47c01522016-12-19 11:44:07 +01006804 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6805 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006806}
6807
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006808static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006809{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006810 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006811 uint32_t intr;
6812 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006813
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006814 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006815
Avi Kivityfa89a812008-09-01 15:57:51 +03006816 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006817 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006818 int inc_eip = 0;
6819 if (vcpu->arch.interrupt.soft)
6820 inc_eip = vcpu->arch.event_exit_inst_len;
6821 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006822 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006823 return;
6824 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006825 intr = irq | INTR_INFO_VALID_MASK;
6826 if (vcpu->arch.interrupt.soft) {
6827 intr |= INTR_TYPE_SOFT_INTR;
6828 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6829 vmx->vcpu.arch.event_exit_inst_len);
6830 } else
6831 intr |= INTR_TYPE_EXT_INTR;
6832 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006833
6834 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006835}
6836
Sheng Yangf08864b2008-05-15 18:23:25 +08006837static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6838{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006839 struct vcpu_vmx *vmx = to_vmx(vcpu);
6840
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006841 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006842 /*
6843 * Tracking the NMI-blocked state in software is built upon
6844 * finding the next open IRQ window. This, in turn, depends on
6845 * well-behaving guests: They have to keep IRQs disabled at
6846 * least as long as the NMI handler runs. Otherwise we may
6847 * cause NMI nesting, maybe breaking the guest. But as this is
6848 * highly unlikely, we can live with the residual risk.
6849 */
6850 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6851 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6852 }
6853
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006854 ++vcpu->stat.nmi_injections;
6855 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006856
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006857 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006858 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006859 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006860 return;
6861 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006862
Sheng Yangf08864b2008-05-15 18:23:25 +08006863 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6864 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006865
6866 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006867}
6868
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006869static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6870{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006871 struct vcpu_vmx *vmx = to_vmx(vcpu);
6872 bool masked;
6873
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006874 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006875 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006876 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006877 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006878 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6879 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6880 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006881}
6882
6883static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6884{
6885 struct vcpu_vmx *vmx = to_vmx(vcpu);
6886
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006887 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006888 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6889 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6890 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6891 }
6892 } else {
6893 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6894 if (masked)
6895 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6896 GUEST_INTR_STATE_NMI);
6897 else
6898 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6899 GUEST_INTR_STATE_NMI);
6900 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006901}
6902
Jan Kiszka2505dc92013-04-14 12:12:47 +02006903static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6904{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006905 if (to_vmx(vcpu)->nested.nested_run_pending)
6906 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006907
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006908 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006909 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6910 return 0;
6911
Jan Kiszka2505dc92013-04-14 12:12:47 +02006912 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6913 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6914 | GUEST_INTR_STATE_NMI));
6915}
6916
Gleb Natapov78646122009-03-23 12:12:11 +02006917static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6918{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006919 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6920 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006921 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6922 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006923}
6924
Izik Eiduscbc94022007-10-25 00:29:55 +02006925static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6926{
6927 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006928
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006929 if (enable_unrestricted_guest)
6930 return 0;
6931
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006932 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6933 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006934 if (ret)
6935 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006936 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006937 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006938}
6939
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006940static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6941{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006942 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006943 return 0;
6944}
6945
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006946static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006947{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006948 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006949 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006950 /*
6951 * Update instruction length as we may reinject the exception
6952 * from user space while in guest debugging mode.
6953 */
6954 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6955 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006956 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006957 return false;
6958 /* fall through */
6959 case DB_VECTOR:
6960 if (vcpu->guest_debug &
6961 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6962 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006963 /* fall through */
6964 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006965 case OF_VECTOR:
6966 case BR_VECTOR:
6967 case UD_VECTOR:
6968 case DF_VECTOR:
6969 case SS_VECTOR:
6970 case GP_VECTOR:
6971 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006972 return true;
6973 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006974 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006975 return false;
6976}
6977
6978static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6979 int vec, u32 err_code)
6980{
6981 /*
6982 * Instruction with address size override prefix opcode 0x67
6983 * Cause the #SS fault with 0 error code in VM86 mode.
6984 */
6985 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006986 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006987 if (vcpu->arch.halt_request) {
6988 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006989 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006990 }
6991 return 1;
6992 }
6993 return 0;
6994 }
6995
6996 /*
6997 * Forward all other exceptions that are valid in real mode.
6998 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6999 * the required debugging infrastructure rework.
7000 */
7001 kvm_queue_exception(vcpu, vec);
7002 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007003}
7004
Andi Kleena0861c02009-06-08 17:37:09 +08007005/*
7006 * Trigger machine check on the host. We assume all the MSRs are already set up
7007 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7008 * We pass a fake environment to the machine check handler because we want
7009 * the guest to be always treated like user space, no matter what context
7010 * it used internally.
7011 */
7012static void kvm_machine_check(void)
7013{
7014#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7015 struct pt_regs regs = {
7016 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7017 .flags = X86_EFLAGS_IF,
7018 };
7019
7020 do_machine_check(&regs, 0);
7021#endif
7022}
7023
Avi Kivity851ba692009-08-24 11:10:17 +03007024static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007025{
7026 /* already handled by vcpu_run */
7027 return 1;
7028}
7029
Avi Kivity851ba692009-08-24 11:10:17 +03007030static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031{
Avi Kivity1155f762007-11-22 11:30:47 +02007032 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007033 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007034 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007035 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036 u32 vect_info;
7037 enum emulation_result er;
7038
Avi Kivity1155f762007-11-22 11:30:47 +02007039 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007040 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041
Andi Kleena0861c02009-06-08 17:37:09 +08007042 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007043 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007044
Jim Mattsonef85b672016-12-12 11:01:37 -08007045 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007046 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007047
Wanpeng Li082d06e2018-04-03 16:28:48 -07007048 if (is_invalid_opcode(intr_info))
7049 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007050
Avi Kivity6aa8b732006-12-10 02:21:36 -08007051 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007052 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007053 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007054
Liran Alon9e869482018-03-12 13:12:51 +02007055 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7056 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007057 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007058 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7059 if (er == EMULATE_USER_EXIT)
7060 return 0;
7061 else if (er != EMULATE_DONE)
7062 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7063 return 1;
7064 }
7065
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007066 /*
7067 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7068 * MMIO, it is better to report an internal error.
7069 * See the comments in vmx_handle_exit.
7070 */
7071 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7072 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7073 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7074 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007075 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007076 vcpu->run->internal.data[0] = vect_info;
7077 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007078 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007079 return 0;
7080 }
7081
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 if (is_page_fault(intr_info)) {
7083 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007084 /* EPT won't cause page fault directly */
7085 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007086 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087 }
7088
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007089 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007090
7091 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7092 return handle_rmode_exception(vcpu, ex_no, error_code);
7093
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007094 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007095 case AC_VECTOR:
7096 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7097 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007098 case DB_VECTOR:
7099 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7100 if (!(vcpu->guest_debug &
7101 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007102 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007103 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007104 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007105 skip_emulated_instruction(vcpu);
7106
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007107 kvm_queue_exception(vcpu, DB_VECTOR);
7108 return 1;
7109 }
7110 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7111 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7112 /* fall through */
7113 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007114 /*
7115 * Update instruction length as we may reinject #BP from
7116 * user space while in guest debugging mode. Reading it for
7117 * #DB as well causes no harm, it is not used in that case.
7118 */
7119 vmx->vcpu.arch.event_exit_inst_len =
7120 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007121 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007122 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007123 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7124 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 break;
7126 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007127 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7128 kvm_run->ex.exception = ex_no;
7129 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007132 return 0;
7133}
7134
Avi Kivity851ba692009-08-24 11:10:17 +03007135static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007137 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007138 return 1;
7139}
7140
Avi Kivity851ba692009-08-24 11:10:17 +03007141static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007142{
Avi Kivity851ba692009-08-24 11:10:17 +03007143 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007144 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007145 return 0;
7146}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147
Avi Kivity851ba692009-08-24 11:10:17 +03007148static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007149{
He, Qingbfdaab02007-09-12 14:18:28 +08007150 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007151 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007152 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007153
He, Qingbfdaab02007-09-12 14:18:28 +08007154 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007155 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007156
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007157 ++vcpu->stat.io_exits;
7158
Sean Christopherson432baf62018-03-08 08:57:26 -08007159 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007160 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007161
7162 port = exit_qualification >> 16;
7163 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007164 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007165
Sean Christophersondca7f122018-03-08 08:57:27 -08007166 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007167}
7168
Ingo Molnar102d8322007-02-19 14:37:47 +02007169static void
7170vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7171{
7172 /*
7173 * Patch in the VMCALL instruction:
7174 */
7175 hypercall[0] = 0x0f;
7176 hypercall[1] = 0x01;
7177 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007178}
7179
Guo Chao0fa06072012-06-28 15:16:19 +08007180/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007181static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7182{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007183 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007184 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7185 unsigned long orig_val = val;
7186
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007187 /*
7188 * We get here when L2 changed cr0 in a way that did not change
7189 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007190 * but did change L0 shadowed bits. So we first calculate the
7191 * effective cr0 value that L1 would like to write into the
7192 * hardware. It consists of the L2-owned bits from the new
7193 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007194 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195 val = (val & ~vmcs12->cr0_guest_host_mask) |
7196 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7197
David Matlack38991522016-11-29 18:14:08 -08007198 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007199 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007200
7201 if (kvm_set_cr0(vcpu, val))
7202 return 1;
7203 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007204 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007205 } else {
7206 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007207 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007208 return 1;
David Matlack38991522016-11-29 18:14:08 -08007209
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007210 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007211 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007212}
7213
7214static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7215{
7216 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007217 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7218 unsigned long orig_val = val;
7219
7220 /* analogously to handle_set_cr0 */
7221 val = (val & ~vmcs12->cr4_guest_host_mask) |
7222 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7223 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007224 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007225 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007226 return 0;
7227 } else
7228 return kvm_set_cr4(vcpu, val);
7229}
7230
Paolo Bonzini0367f202016-07-12 10:44:55 +02007231static int handle_desc(struct kvm_vcpu *vcpu)
7232{
7233 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007234 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007235}
7236
Avi Kivity851ba692009-08-24 11:10:17 +03007237static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007238{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007239 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 int cr;
7241 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007242 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007243 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007244
He, Qingbfdaab02007-09-12 14:18:28 +08007245 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246 cr = exit_qualification & 15;
7247 reg = (exit_qualification >> 8) & 15;
7248 switch ((exit_qualification >> 4) & 3) {
7249 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007250 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007251 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007252 switch (cr) {
7253 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007254 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007255 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007256 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007257 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007258 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007259 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007260 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007261 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007262 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007263 case 8: {
7264 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007265 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007266 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007267 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007268 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007269 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007270 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007271 return ret;
7272 /*
7273 * TODO: we might be squashing a
7274 * KVM_GUESTDBG_SINGLESTEP-triggered
7275 * KVM_EXIT_DEBUG here.
7276 */
Avi Kivity851ba692009-08-24 11:10:17 +03007277 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007278 return 0;
7279 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007280 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007281 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007282 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007283 WARN_ONCE(1, "Guest should always own CR0.TS");
7284 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007285 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007286 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 case 1: /*mov from cr*/
7288 switch (cr) {
7289 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007290 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007291 val = kvm_read_cr3(vcpu);
7292 kvm_register_write(vcpu, reg, val);
7293 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007294 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007295 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007296 val = kvm_get_cr8(vcpu);
7297 kvm_register_write(vcpu, reg, val);
7298 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007299 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007300 }
7301 break;
7302 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007303 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007304 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007305 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007306
Kyle Huey6affcbe2016-11-29 12:40:40 -08007307 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 default:
7309 break;
7310 }
Avi Kivity851ba692009-08-24 11:10:17 +03007311 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007312 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007313 (int)(exit_qualification >> 4) & 3, cr);
7314 return 0;
7315}
7316
Avi Kivity851ba692009-08-24 11:10:17 +03007317static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007318{
He, Qingbfdaab02007-09-12 14:18:28 +08007319 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007320 int dr, dr7, reg;
7321
7322 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7323 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7324
7325 /* First, if DR does not exist, trigger UD */
7326 if (!kvm_require_dr(vcpu, dr))
7327 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007328
Jan Kiszkaf2483412010-01-20 18:20:20 +01007329 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007330 if (!kvm_require_cpl(vcpu, 0))
7331 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007332 dr7 = vmcs_readl(GUEST_DR7);
7333 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007334 /*
7335 * As the vm-exit takes precedence over the debug trap, we
7336 * need to emulate the latter, either for the host or the
7337 * guest debugging itself.
7338 */
7339 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007340 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007341 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007342 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007343 vcpu->run->debug.arch.exception = DB_VECTOR;
7344 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007345 return 0;
7346 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007347 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007348 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007349 kvm_queue_exception(vcpu, DB_VECTOR);
7350 return 1;
7351 }
7352 }
7353
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007354 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007355 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7356 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007357
7358 /*
7359 * No more DR vmexits; force a reload of the debug registers
7360 * and reenter on this instruction. The next vmexit will
7361 * retrieve the full state of the debug registers.
7362 */
7363 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7364 return 1;
7365 }
7366
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007367 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7368 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007369 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007370
7371 if (kvm_get_dr(vcpu, dr, &val))
7372 return 1;
7373 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007374 } else
Nadav Amit57773922014-06-18 17:19:23 +03007375 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007376 return 1;
7377
Kyle Huey6affcbe2016-11-29 12:40:40 -08007378 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007379}
7380
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007381static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7382{
7383 return vcpu->arch.dr6;
7384}
7385
7386static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7387{
7388}
7389
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007390static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7391{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007392 get_debugreg(vcpu->arch.db[0], 0);
7393 get_debugreg(vcpu->arch.db[1], 1);
7394 get_debugreg(vcpu->arch.db[2], 2);
7395 get_debugreg(vcpu->arch.db[3], 3);
7396 get_debugreg(vcpu->arch.dr6, 6);
7397 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7398
7399 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007400 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007401}
7402
Gleb Natapov020df072010-04-13 10:05:23 +03007403static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7404{
7405 vmcs_writel(GUEST_DR7, val);
7406}
7407
Avi Kivity851ba692009-08-24 11:10:17 +03007408static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007409{
Kyle Huey6a908b62016-11-29 12:40:37 -08007410 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007411}
7412
Avi Kivity851ba692009-08-24 11:10:17 +03007413static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007414{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007415 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007416 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007417
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007418 msr_info.index = ecx;
7419 msr_info.host_initiated = false;
7420 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007421 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007422 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007423 return 1;
7424 }
7425
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007426 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007427
Avi Kivity6aa8b732006-12-10 02:21:36 -08007428 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007429 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7430 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007431 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007432}
7433
Avi Kivity851ba692009-08-24 11:10:17 +03007434static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007435{
Will Auld8fe8ab42012-11-29 12:42:12 -08007436 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007437 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7438 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7439 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007440
Will Auld8fe8ab42012-11-29 12:42:12 -08007441 msr.data = data;
7442 msr.index = ecx;
7443 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007444 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007445 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007446 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447 return 1;
7448 }
7449
Avi Kivity59200272010-01-25 19:47:02 +02007450 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007451 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007452}
7453
Avi Kivity851ba692009-08-24 11:10:17 +03007454static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007455{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007456 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007457 return 1;
7458}
7459
Avi Kivity851ba692009-08-24 11:10:17 +03007460static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007461{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007462 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7463 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007464
Avi Kivity3842d132010-07-27 12:30:24 +03007465 kvm_make_request(KVM_REQ_EVENT, vcpu);
7466
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007467 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007468 return 1;
7469}
7470
Avi Kivity851ba692009-08-24 11:10:17 +03007471static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007472{
Avi Kivityd3bef152007-06-05 15:53:05 +03007473 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007474}
7475
Avi Kivity851ba692009-08-24 11:10:17 +03007476static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007477{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007478 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007479}
7480
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007481static int handle_invd(struct kvm_vcpu *vcpu)
7482{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007483 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007484}
7485
Avi Kivity851ba692009-08-24 11:10:17 +03007486static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007487{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007488 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007489
7490 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007491 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007492}
7493
Avi Kivityfee84b02011-11-10 14:57:25 +02007494static int handle_rdpmc(struct kvm_vcpu *vcpu)
7495{
7496 int err;
7497
7498 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007499 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007500}
7501
Avi Kivity851ba692009-08-24 11:10:17 +03007502static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007503{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007504 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007505}
7506
Dexuan Cui2acf9232010-06-10 11:27:12 +08007507static int handle_xsetbv(struct kvm_vcpu *vcpu)
7508{
7509 u64 new_bv = kvm_read_edx_eax(vcpu);
7510 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7511
7512 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007513 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007514 return 1;
7515}
7516
Wanpeng Lif53cd632014-12-02 19:14:58 +08007517static int handle_xsaves(struct kvm_vcpu *vcpu)
7518{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007519 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007520 WARN(1, "this should never happen\n");
7521 return 1;
7522}
7523
7524static int handle_xrstors(struct kvm_vcpu *vcpu)
7525{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007526 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007527 WARN(1, "this should never happen\n");
7528 return 1;
7529}
7530
Avi Kivity851ba692009-08-24 11:10:17 +03007531static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007532{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007533 if (likely(fasteoi)) {
7534 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7535 int access_type, offset;
7536
7537 access_type = exit_qualification & APIC_ACCESS_TYPE;
7538 offset = exit_qualification & APIC_ACCESS_OFFSET;
7539 /*
7540 * Sane guest uses MOV to write EOI, with written value
7541 * not cared. So make a short-circuit here by avoiding
7542 * heavy instruction emulation.
7543 */
7544 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7545 (offset == APIC_EOI)) {
7546 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007547 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007548 }
7549 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007550 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007551}
7552
Yang Zhangc7c9c562013-01-25 10:18:51 +08007553static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7554{
7555 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7556 int vector = exit_qualification & 0xff;
7557
7558 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7559 kvm_apic_set_eoi_accelerated(vcpu, vector);
7560 return 1;
7561}
7562
Yang Zhang83d4c282013-01-25 10:18:49 +08007563static int handle_apic_write(struct kvm_vcpu *vcpu)
7564{
7565 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7566 u32 offset = exit_qualification & 0xfff;
7567
7568 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7569 kvm_apic_write_nodecode(vcpu, offset);
7570 return 1;
7571}
7572
Avi Kivity851ba692009-08-24 11:10:17 +03007573static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007574{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007575 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007576 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007577 bool has_error_code = false;
7578 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007579 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007580 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007581
7582 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007583 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007584 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007585
7586 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7587
7588 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589 if (reason == TASK_SWITCH_GATE && idt_v) {
7590 switch (type) {
7591 case INTR_TYPE_NMI_INTR:
7592 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007593 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007594 break;
7595 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007596 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007597 kvm_clear_interrupt_queue(vcpu);
7598 break;
7599 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007600 if (vmx->idt_vectoring_info &
7601 VECTORING_INFO_DELIVER_CODE_MASK) {
7602 has_error_code = true;
7603 error_code =
7604 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7605 }
7606 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007607 case INTR_TYPE_SOFT_EXCEPTION:
7608 kvm_clear_exception_queue(vcpu);
7609 break;
7610 default:
7611 break;
7612 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007613 }
Izik Eidus37817f22008-03-24 23:14:53 +02007614 tss_selector = exit_qualification;
7615
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007616 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7617 type != INTR_TYPE_EXT_INTR &&
7618 type != INTR_TYPE_NMI_INTR))
7619 skip_emulated_instruction(vcpu);
7620
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007621 if (kvm_task_switch(vcpu, tss_selector,
7622 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7623 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007624 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7625 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7626 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007627 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007628 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007629
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007630 /*
7631 * TODO: What about debug traps on tss switch?
7632 * Are we supposed to inject them and update dr6?
7633 */
7634
7635 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007636}
7637
Avi Kivity851ba692009-08-24 11:10:17 +03007638static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007639{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007640 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007641 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007642 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007643
Sheng Yangf9c617f2009-03-25 10:08:52 +08007644 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007645
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007646 /*
7647 * EPT violation happened while executing iret from NMI,
7648 * "blocked by NMI" bit has to be set before next VM entry.
7649 * There are errata that may cause this bit to not be set:
7650 * AAK134, BY25.
7651 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007652 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007653 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007654 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007655 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7656
Sheng Yang14394422008-04-28 12:24:45 +08007657 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007658 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007659
Junaid Shahid27959a42016-12-06 16:46:10 -08007660 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007661 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007662 ? PFERR_USER_MASK : 0;
7663 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007664 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007665 ? PFERR_WRITE_MASK : 0;
7666 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007667 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007668 ? PFERR_FETCH_MASK : 0;
7669 /* ept page table entry is present? */
7670 error_code |= (exit_qualification &
7671 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7672 EPT_VIOLATION_EXECUTABLE))
7673 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007674
Paolo Bonzinieebed242016-11-28 14:39:58 +01007675 error_code |= (exit_qualification & 0x100) != 0 ?
7676 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007677
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007678 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007679 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007680}
7681
Avi Kivity851ba692009-08-24 11:10:17 +03007682static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007683{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007684 gpa_t gpa;
7685
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007686 /*
7687 * A nested guest cannot optimize MMIO vmexits, because we have an
7688 * nGPA here instead of the required GPA.
7689 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007690 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007691 if (!is_guest_mode(vcpu) &&
7692 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007693 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007694 /*
7695 * Doing kvm_skip_emulated_instruction() depends on undefined
7696 * behavior: Intel's manual doesn't mandate
7697 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7698 * occurs and while on real hardware it was observed to be set,
7699 * other hypervisors (namely Hyper-V) don't set it, we end up
7700 * advancing IP with some random value. Disable fast mmio when
7701 * running nested and keep it for real hardware in hope that
7702 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7703 */
7704 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7705 return kvm_skip_emulated_instruction(vcpu);
7706 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007707 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007708 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007709 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007710
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007711 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007712}
7713
Avi Kivity851ba692009-08-24 11:10:17 +03007714static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007715{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007716 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007717 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7718 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007719 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007720 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007721
7722 return 1;
7723}
7724
Mohammed Gamal80ced182009-09-01 12:48:18 +02007725static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007726{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007727 struct vcpu_vmx *vmx = to_vmx(vcpu);
7728 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007729 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007730 u32 cpu_exec_ctrl;
7731 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007732 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007733
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007734 /*
7735 * We should never reach the point where we are emulating L2
7736 * due to invalid guest state as that means we incorrectly
7737 * allowed a nested VMEntry with an invalid vmcs12.
7738 */
7739 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7740
Avi Kivity49e9d552010-09-19 14:34:08 +02007741 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7742 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007743
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007744 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007745 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007746 return handle_interrupt_window(&vmx->vcpu);
7747
Radim Krčmář72875d82017-04-26 22:32:19 +02007748 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007749 return 1;
7750
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007751 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007752
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007753 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007754 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007755 ret = 0;
7756 goto out;
7757 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007758
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007759 if (err != EMULATE_DONE)
7760 goto emulation_error;
7761
7762 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7763 vcpu->arch.exception.pending)
7764 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007765
Gleb Natapov8d76c492013-05-08 18:38:44 +03007766 if (vcpu->arch.halt_request) {
7767 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007768 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007769 goto out;
7770 }
7771
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007772 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007773 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007774 if (need_resched())
7775 schedule();
7776 }
7777
Mohammed Gamal80ced182009-09-01 12:48:18 +02007778out:
7779 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007780
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007781emulation_error:
7782 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7783 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7784 vcpu->run->internal.ndata = 0;
7785 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007786}
7787
7788static void grow_ple_window(struct kvm_vcpu *vcpu)
7789{
7790 struct vcpu_vmx *vmx = to_vmx(vcpu);
7791 int old = vmx->ple_window;
7792
Babu Mogerc8e88712018-03-16 16:37:24 -04007793 vmx->ple_window = __grow_ple_window(old, ple_window,
7794 ple_window_grow,
7795 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007796
7797 if (vmx->ple_window != old)
7798 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007799
7800 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007801}
7802
7803static void shrink_ple_window(struct kvm_vcpu *vcpu)
7804{
7805 struct vcpu_vmx *vmx = to_vmx(vcpu);
7806 int old = vmx->ple_window;
7807
Babu Mogerc8e88712018-03-16 16:37:24 -04007808 vmx->ple_window = __shrink_ple_window(old, ple_window,
7809 ple_window_shrink,
7810 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007811
7812 if (vmx->ple_window != old)
7813 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007814
7815 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007816}
7817
7818/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007819 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7820 */
7821static void wakeup_handler(void)
7822{
7823 struct kvm_vcpu *vcpu;
7824 int cpu = smp_processor_id();
7825
7826 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7827 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7828 blocked_vcpu_list) {
7829 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7830
7831 if (pi_test_on(pi_desc) == 1)
7832 kvm_vcpu_kick(vcpu);
7833 }
7834 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7835}
7836
Peng Haoe01bca22018-04-07 05:47:32 +08007837static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007838{
7839 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7840 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7841 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7842 0ull, VMX_EPT_EXECUTABLE_MASK,
7843 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007844 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007845
7846 ept_set_mmio_spte_mask();
7847 kvm_enable_tdp();
7848}
7849
Tiejun Chenf2c76482014-10-28 10:14:47 +08007850static __init int hardware_setup(void)
7851{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007852 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007853 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007854
7855 rdmsrl_safe(MSR_EFER, &host_efer);
7856
7857 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7858 kvm_define_shared_msr(i, vmx_msr_index[i]);
7859
Radim Krčmář23611332016-09-29 22:41:33 +02007860 for (i = 0; i < VMX_BITMAP_NR; i++) {
7861 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7862 if (!vmx_bitmap[i])
7863 goto out;
7864 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007865
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007866 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7867 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7868
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007869 if (setup_vmcs_config(&vmcs_config) < 0) {
7870 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007871 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007872 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007873
7874 if (boot_cpu_has(X86_FEATURE_NX))
7875 kvm_enable_efer_bits(EFER_NX);
7876
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007877 if (boot_cpu_has(X86_FEATURE_MPX)) {
7878 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7879 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7880 }
7881
Wanpeng Li08d839c2017-03-23 05:30:08 -07007882 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7883 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007884 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007885
Tiejun Chenf2c76482014-10-28 10:14:47 +08007886 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007887 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007888 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007889 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007890 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007892 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007893 enable_ept_ad_bits = 0;
7894
Wanpeng Li8ad81822017-10-09 15:51:53 -07007895 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007896 enable_unrestricted_guest = 0;
7897
Paolo Bonziniad15a292015-01-30 16:18:49 +01007898 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007899 flexpriority_enabled = 0;
7900
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007901 if (!cpu_has_virtual_nmis())
7902 enable_vnmi = 0;
7903
Paolo Bonziniad15a292015-01-30 16:18:49 +01007904 /*
7905 * set_apic_access_page_addr() is used to reload apic access
7906 * page upon invalidation. No need to do anything if not
7907 * using the APIC_ACCESS_ADDR VMCS field.
7908 */
7909 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007910 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007911
7912 if (!cpu_has_vmx_tpr_shadow())
7913 kvm_x86_ops->update_cr8_intercept = NULL;
7914
7915 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7916 kvm_disable_largepages();
7917
Tianyu Lan877ad952018-07-19 08:40:23 +00007918#if IS_ENABLED(CONFIG_HYPERV)
7919 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7920 && enable_ept)
7921 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7922#endif
7923
Wanpeng Li0f107682017-09-28 18:06:24 -07007924 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007925 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007926 ple_window = 0;
7927 ple_window_grow = 0;
7928 ple_window_max = 0;
7929 ple_window_shrink = 0;
7930 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007932 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007933 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007934 kvm_x86_ops->sync_pir_to_irr = NULL;
7935 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007936
Haozhong Zhang64903d62015-10-20 15:39:09 +08007937 if (cpu_has_vmx_tsc_scaling()) {
7938 kvm_has_tsc_control = true;
7939 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7940 kvm_tsc_scaling_ratio_frac_bits = 48;
7941 }
7942
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007943 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7944
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007945 if (enable_ept)
7946 vmx_enable_tdp();
7947 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007948 kvm_disable_tdp();
7949
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007950 if (!nested) {
7951 kvm_x86_ops->get_nested_state = NULL;
7952 kvm_x86_ops->set_nested_state = NULL;
7953 }
7954
Kai Huang843e4332015-01-28 10:54:28 +08007955 /*
7956 * Only enable PML when hardware supports PML feature, and both EPT
7957 * and EPT A/D bit features are enabled -- PML depends on them to work.
7958 */
7959 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7960 enable_pml = 0;
7961
7962 if (!enable_pml) {
7963 kvm_x86_ops->slot_enable_log_dirty = NULL;
7964 kvm_x86_ops->slot_disable_log_dirty = NULL;
7965 kvm_x86_ops->flush_log_dirty = NULL;
7966 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7967 }
7968
Yunhong Jiang64672c92016-06-13 14:19:59 -07007969 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7970 u64 vmx_msr;
7971
7972 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7973 cpu_preemption_timer_multi =
7974 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7975 } else {
7976 kvm_x86_ops->set_hv_timer = NULL;
7977 kvm_x86_ops->cancel_hv_timer = NULL;
7978 }
7979
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007980 if (!cpu_has_vmx_shadow_vmcs())
7981 enable_shadow_vmcs = 0;
7982 if (enable_shadow_vmcs)
7983 init_vmcs_shadow_fields();
7984
Feng Wubf9f6ac2015-09-18 22:29:55 +08007985 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007986 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007987
Ashok Rajc45dcc72016-06-22 14:59:56 +08007988 kvm_mce_cap_supported |= MCG_LMCE_P;
7989
Tiejun Chenf2c76482014-10-28 10:14:47 +08007990 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007991
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007992out:
Radim Krčmář23611332016-09-29 22:41:33 +02007993 for (i = 0; i < VMX_BITMAP_NR; i++)
7994 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007995
7996 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007997}
7998
7999static __exit void hardware_unsetup(void)
8000{
Radim Krčmář23611332016-09-29 22:41:33 +02008001 int i;
8002
8003 for (i = 0; i < VMX_BITMAP_NR; i++)
8004 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008005
Tiejun Chenf2c76482014-10-28 10:14:47 +08008006 free_kvm_area();
8007}
8008
Avi Kivity6aa8b732006-12-10 02:21:36 -08008009/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008010 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8011 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8012 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008013static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008014{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008015 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008016 grow_ple_window(vcpu);
8017
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008018 /*
8019 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8020 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8021 * never set PAUSE_EXITING and just set PLE if supported,
8022 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8023 */
8024 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008025 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008026}
8027
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008028static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008029{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008030 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008031}
8032
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008033static int handle_mwait(struct kvm_vcpu *vcpu)
8034{
8035 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8036 return handle_nop(vcpu);
8037}
8038
Jim Mattson45ec3682017-08-23 16:32:04 -07008039static int handle_invalid_op(struct kvm_vcpu *vcpu)
8040{
8041 kvm_queue_exception(vcpu, UD_VECTOR);
8042 return 1;
8043}
8044
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008045static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8046{
8047 return 1;
8048}
8049
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008050static int handle_monitor(struct kvm_vcpu *vcpu)
8051{
8052 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8053 return handle_nop(vcpu);
8054}
8055
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008056/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008057 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8058 * set the success or error code of an emulated VMX instruction, as specified
8059 * by Vol 2B, VMX Instruction Reference, "Conventions".
8060 */
8061static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8062{
8063 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8064 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8065 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8066}
8067
8068static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8069{
8070 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8071 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8072 X86_EFLAGS_SF | X86_EFLAGS_OF))
8073 | X86_EFLAGS_CF);
8074}
8075
Abel Gordon145c28d2013-04-18 14:36:55 +03008076static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008077 u32 vm_instruction_error)
8078{
8079 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8080 /*
8081 * failValid writes the error number to the current VMCS, which
8082 * can't be done there isn't a current VMCS.
8083 */
8084 nested_vmx_failInvalid(vcpu);
8085 return;
8086 }
8087 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8088 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8089 X86_EFLAGS_SF | X86_EFLAGS_OF))
8090 | X86_EFLAGS_ZF);
8091 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8092 /*
8093 * We don't need to force a shadow sync because
8094 * VM_INSTRUCTION_ERROR is not shadowed
8095 */
8096}
Abel Gordon145c28d2013-04-18 14:36:55 +03008097
Wincy Vanff651cb2014-12-11 08:52:58 +03008098static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8099{
8100 /* TODO: not to reset guest simply here. */
8101 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008102 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008103}
8104
Jan Kiszkaf41245002014-03-07 20:03:13 +01008105static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8106{
8107 struct vcpu_vmx *vmx =
8108 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8109
8110 vmx->nested.preemption_timer_expired = true;
8111 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8112 kvm_vcpu_kick(&vmx->vcpu);
8113
8114 return HRTIMER_NORESTART;
8115}
8116
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008117/*
Bandan Das19677e32014-05-06 02:19:15 -04008118 * Decode the memory-address operand of a vmx instruction, as recorded on an
8119 * exit caused by such an instruction (run by a guest hypervisor).
8120 * On success, returns 0. When the operand is invalid, returns 1 and throws
8121 * #UD or #GP.
8122 */
8123static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8124 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008125 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008126{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008127 gva_t off;
8128 bool exn;
8129 struct kvm_segment s;
8130
Bandan Das19677e32014-05-06 02:19:15 -04008131 /*
8132 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8133 * Execution", on an exit, vmx_instruction_info holds most of the
8134 * addressing components of the operand. Only the displacement part
8135 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8136 * For how an actual address is calculated from all these components,
8137 * refer to Vol. 1, "Operand Addressing".
8138 */
8139 int scaling = vmx_instruction_info & 3;
8140 int addr_size = (vmx_instruction_info >> 7) & 7;
8141 bool is_reg = vmx_instruction_info & (1u << 10);
8142 int seg_reg = (vmx_instruction_info >> 15) & 7;
8143 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8144 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8145 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8146 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8147
8148 if (is_reg) {
8149 kvm_queue_exception(vcpu, UD_VECTOR);
8150 return 1;
8151 }
8152
8153 /* Addr = segment_base + offset */
8154 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008156 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008158 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008159 off += kvm_register_read(vcpu, index_reg)<<scaling;
8160 vmx_get_segment(vcpu, &s, seg_reg);
8161 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008162
8163 if (addr_size == 1) /* 32 bit */
8164 *ret &= 0xffffffff;
8165
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008166 /* Checks for #GP/#SS exceptions. */
8167 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008168 if (is_long_mode(vcpu)) {
8169 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8170 * non-canonical form. This is the only check on the memory
8171 * destination for long mode!
8172 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008173 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008174 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008175 /* Protected mode: apply checks for segment validity in the
8176 * following order:
8177 * - segment type check (#GP(0) may be thrown)
8178 * - usability check (#GP(0)/#SS(0))
8179 * - limit check (#GP(0)/#SS(0))
8180 */
8181 if (wr)
8182 /* #GP(0) if the destination operand is located in a
8183 * read-only data segment or any code segment.
8184 */
8185 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8186 else
8187 /* #GP(0) if the source operand is located in an
8188 * execute-only code segment
8189 */
8190 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008191 if (exn) {
8192 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8193 return 1;
8194 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008195 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8196 */
8197 exn = (s.unusable != 0);
8198 /* Protected mode: #GP(0)/#SS(0) if the memory
8199 * operand is outside the segment limit.
8200 */
8201 exn = exn || (off + sizeof(u64) > s.limit);
8202 }
8203 if (exn) {
8204 kvm_queue_exception_e(vcpu,
8205 seg_reg == VCPU_SREG_SS ?
8206 SS_VECTOR : GP_VECTOR,
8207 0);
8208 return 1;
8209 }
8210
Bandan Das19677e32014-05-06 02:19:15 -04008211 return 0;
8212}
8213
Radim Krčmářcbf71272017-05-19 15:48:51 +02008214static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008215{
8216 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008217 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008218
8219 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008220 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008221 return 1;
8222
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008223 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008224 kvm_inject_page_fault(vcpu, &e);
8225 return 1;
8226 }
8227
Bandan Das3573e222014-05-06 02:19:16 -04008228 return 0;
8229}
8230
Liran Alonabfc52c2018-06-23 02:35:13 +03008231/*
8232 * Allocate a shadow VMCS and associate it with the currently loaded
8233 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8234 * VMCS is also VMCLEARed, so that it is ready for use.
8235 */
8236static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8237{
8238 struct vcpu_vmx *vmx = to_vmx(vcpu);
8239 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8240
8241 /*
8242 * We should allocate a shadow vmcs for vmcs01 only when L1
8243 * executes VMXON and free it when L1 executes VMXOFF.
8244 * As it is invalid to execute VMXON twice, we shouldn't reach
8245 * here when vmcs01 already have an allocated shadow vmcs.
8246 */
8247 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8248
8249 if (!loaded_vmcs->shadow_vmcs) {
8250 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8251 if (loaded_vmcs->shadow_vmcs)
8252 vmcs_clear(loaded_vmcs->shadow_vmcs);
8253 }
8254 return loaded_vmcs->shadow_vmcs;
8255}
8256
Jim Mattsone29acc52016-11-30 12:03:43 -08008257static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8258{
8259 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008260 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008261
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008262 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8263 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008264 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008265
8266 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8267 if (!vmx->nested.cached_vmcs12)
8268 goto out_cached_vmcs12;
8269
Liran Alon61ada742018-06-23 02:35:08 +03008270 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8271 if (!vmx->nested.cached_shadow_vmcs12)
8272 goto out_cached_shadow_vmcs12;
8273
Liran Alonabfc52c2018-06-23 02:35:13 +03008274 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8275 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008276
Jim Mattsone29acc52016-11-30 12:03:43 -08008277 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8278 HRTIMER_MODE_REL_PINNED);
8279 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8280
Roman Kagan63aff652018-07-19 21:59:07 +03008281 vmx->nested.vpid02 = allocate_vpid();
8282
Jim Mattsone29acc52016-11-30 12:03:43 -08008283 vmx->nested.vmxon = true;
8284 return 0;
8285
8286out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008287 kfree(vmx->nested.cached_shadow_vmcs12);
8288
8289out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008290 kfree(vmx->nested.cached_vmcs12);
8291
8292out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008293 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008294
Jim Mattsonde3a0022017-11-27 17:22:25 -06008295out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008296 return -ENOMEM;
8297}
8298
Bandan Das3573e222014-05-06 02:19:16 -04008299/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008300 * Emulate the VMXON instruction.
8301 * Currently, we just remember that VMX is active, and do not save or even
8302 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8303 * do not currently need to store anything in that guest-allocated memory
8304 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8305 * argument is different from the VMXON pointer (which the spec says they do).
8306 */
8307static int handle_vmon(struct kvm_vcpu *vcpu)
8308{
Jim Mattsone29acc52016-11-30 12:03:43 -08008309 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008310 gpa_t vmptr;
8311 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008312 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008313 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8314 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008315
Jim Mattson70f3aac2017-04-26 08:53:46 -07008316 /*
8317 * The Intel VMX Instruction Reference lists a bunch of bits that are
8318 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8319 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8320 * Otherwise, we should fail with #UD. But most faulting conditions
8321 * have already been checked by hardware, prior to the VM-exit for
8322 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8323 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008324 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008325 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008326 kvm_queue_exception(vcpu, UD_VECTOR);
8327 return 1;
8328 }
8329
Felix Wilhelm727ba742018-06-11 09:43:44 +02008330 /* CPL=0 must be checked manually. */
8331 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008332 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008333 return 1;
8334 }
8335
Abel Gordon145c28d2013-04-18 14:36:55 +03008336 if (vmx->nested.vmxon) {
8337 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008338 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008339 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008340
Haozhong Zhang3b840802016-06-22 14:59:54 +08008341 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008342 != VMXON_NEEDED_FEATURES) {
8343 kvm_inject_gp(vcpu, 0);
8344 return 1;
8345 }
8346
Radim Krčmářcbf71272017-05-19 15:48:51 +02008347 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008348 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008349
8350 /*
8351 * SDM 3: 24.11.5
8352 * The first 4 bytes of VMXON region contain the supported
8353 * VMCS revision identifier
8354 *
8355 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8356 * which replaces physical address width with 32
8357 */
8358 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8359 nested_vmx_failInvalid(vcpu);
8360 return kvm_skip_emulated_instruction(vcpu);
8361 }
8362
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008363 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8364 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008365 nested_vmx_failInvalid(vcpu);
8366 return kvm_skip_emulated_instruction(vcpu);
8367 }
8368 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8369 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008370 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008371 nested_vmx_failInvalid(vcpu);
8372 return kvm_skip_emulated_instruction(vcpu);
8373 }
8374 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008375 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008376
8377 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008378 ret = enter_vmx_operation(vcpu);
8379 if (ret)
8380 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008381
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008382 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008383 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008384}
8385
8386/*
8387 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8388 * for running VMX instructions (except VMXON, whose prerequisites are
8389 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008390 * Note that many of these exceptions have priority over VM exits, so they
8391 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008392 */
8393static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8394{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008395 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008396 kvm_queue_exception(vcpu, UD_VECTOR);
8397 return 0;
8398 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008399
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008400 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008401 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008402 return 0;
8403 }
8404
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008405 return 1;
8406}
8407
David Matlack8ca44e82017-08-01 14:00:39 -07008408static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8409{
8410 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8411 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8412}
8413
Abel Gordone7953d72013-04-18 14:37:55 +03008414static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8415{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008416 if (vmx->nested.current_vmptr == -1ull)
8417 return;
8418
Abel Gordon012f83c2013-04-18 14:39:25 +03008419 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008420 /* copy to memory all shadowed fields in case
8421 they were modified */
8422 copy_shadow_to_vmcs12(vmx);
8423 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008424 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008425 }
Wincy Van705699a2015-02-03 23:58:17 +08008426 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008427
8428 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008429 kvm_vcpu_write_guest_page(&vmx->vcpu,
8430 vmx->nested.current_vmptr >> PAGE_SHIFT,
8431 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008432
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008433 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008434}
8435
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008436/*
8437 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8438 * just stops using VMX.
8439 */
8440static void free_nested(struct vcpu_vmx *vmx)
8441{
Wanpeng Lib7455822017-11-22 14:04:00 -08008442 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008443 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008444
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008445 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008446 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008447 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008448 vmx->nested.posted_intr_nv = -1;
8449 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008450 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008451 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008452 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8453 free_vmcs(vmx->vmcs01.shadow_vmcs);
8454 vmx->vmcs01.shadow_vmcs = NULL;
8455 }
David Matlack4f2777b2016-07-13 17:16:37 -07008456 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008457 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008458 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008459 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008460 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008461 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008462 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008463 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008464 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008465 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008466 }
Wincy Van705699a2015-02-03 23:58:17 +08008467 if (vmx->nested.pi_desc_page) {
8468 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008469 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008470 vmx->nested.pi_desc_page = NULL;
8471 vmx->nested.pi_desc = NULL;
8472 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008473
Jim Mattsonde3a0022017-11-27 17:22:25 -06008474 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008475}
8476
8477/* Emulate the VMXOFF instruction */
8478static int handle_vmoff(struct kvm_vcpu *vcpu)
8479{
8480 if (!nested_vmx_check_permission(vcpu))
8481 return 1;
8482 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008483 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008484 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008485}
8486
Nadav Har'El27d6c862011-05-25 23:06:59 +03008487/* Emulate the VMCLEAR instruction */
8488static int handle_vmclear(struct kvm_vcpu *vcpu)
8489{
8490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008491 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008492 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008493
8494 if (!nested_vmx_check_permission(vcpu))
8495 return 1;
8496
Radim Krčmářcbf71272017-05-19 15:48:51 +02008497 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498 return 1;
8499
Radim Krčmářcbf71272017-05-19 15:48:51 +02008500 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8501 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8502 return kvm_skip_emulated_instruction(vcpu);
8503 }
8504
8505 if (vmptr == vmx->nested.vmxon_ptr) {
8506 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8507 return kvm_skip_emulated_instruction(vcpu);
8508 }
8509
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008510 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008511 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008512
Jim Mattson587d7e722017-03-02 12:41:48 -08008513 kvm_vcpu_write_guest(vcpu,
8514 vmptr + offsetof(struct vmcs12, launch_state),
8515 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008516
Nadav Har'El27d6c862011-05-25 23:06:59 +03008517 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008518 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008519}
8520
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008521static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8522
8523/* Emulate the VMLAUNCH instruction */
8524static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8525{
8526 return nested_vmx_run(vcpu, true);
8527}
8528
8529/* Emulate the VMRESUME instruction */
8530static int handle_vmresume(struct kvm_vcpu *vcpu)
8531{
8532
8533 return nested_vmx_run(vcpu, false);
8534}
8535
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536/*
8537 * Read a vmcs12 field. Since these can have varying lengths and we return
8538 * one type, we chose the biggest type (u64) and zero-extend the return value
8539 * to that size. Note that the caller, handle_vmread, might need to use only
8540 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8541 * 64-bit fields are to be returned).
8542 */
Liran Alone2536742018-06-23 02:35:02 +03008543static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008544 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008545{
8546 short offset = vmcs_field_to_offset(field);
8547 char *p;
8548
8549 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008550 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551
Liran Alone2536742018-06-23 02:35:02 +03008552 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553
Jim Mattsond37f4262017-12-22 12:12:16 -08008554 switch (vmcs_field_width(field)) {
8555 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008557 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008558 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008560 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008561 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008563 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008564 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008566 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 WARN_ON(1);
8569 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 }
8571}
8572
Abel Gordon20b97fe2013-04-18 14:36:25 +03008573
Liran Alone2536742018-06-23 02:35:02 +03008574static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008575 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008576 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008577 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008579 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008580
Jim Mattsond37f4262017-12-22 12:12:16 -08008581 switch (vmcs_field_width(field)) {
8582 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008583 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008584 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008585 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008588 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008591 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008592 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 WARN_ON(1);
8596 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 }
8598
8599}
8600
Jim Mattsonf4160e42018-05-29 09:11:33 -07008601/*
8602 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8603 * they have been modified by the L1 guest. Note that the "read-only"
8604 * VM-exit information fields are actually writable if the vCPU is
8605 * configured to support "VMWRITE to any supported field in the VMCS."
8606 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008607static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8608{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008609 const u16 *fields[] = {
8610 shadow_read_write_fields,
8611 shadow_read_only_fields
8612 };
8613 const int max_fields[] = {
8614 max_shadow_read_write_fields,
8615 max_shadow_read_only_fields
8616 };
8617 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008618 unsigned long field;
8619 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008620 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008621
Jan Kiszka282da872014-10-08 18:05:39 +02008622 preempt_disable();
8623
Abel Gordon16f5b902013-04-18 14:38:25 +03008624 vmcs_load(shadow_vmcs);
8625
Jim Mattsonf4160e42018-05-29 09:11:33 -07008626 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8627 for (i = 0; i < max_fields[q]; i++) {
8628 field = fields[q][i];
8629 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008630 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008631 }
8632 /*
8633 * Skip the VM-exit information fields if they are read-only.
8634 */
8635 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8636 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008637 }
8638
8639 vmcs_clear(shadow_vmcs);
8640 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008641
8642 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008643}
8644
Abel Gordonc3114422013-04-18 14:38:55 +03008645static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8646{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008647 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008648 shadow_read_write_fields,
8649 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008650 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008651 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008652 max_shadow_read_write_fields,
8653 max_shadow_read_only_fields
8654 };
8655 int i, q;
8656 unsigned long field;
8657 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008658 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008659
8660 vmcs_load(shadow_vmcs);
8661
Mathias Krausec2bae892013-06-26 20:36:21 +02008662 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008663 for (i = 0; i < max_fields[q]; i++) {
8664 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008665 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008666 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008667 }
8668 }
8669
8670 vmcs_clear(shadow_vmcs);
8671 vmcs_load(vmx->loaded_vmcs->vmcs);
8672}
8673
Nadav Har'El49f705c2011-05-25 23:08:30 +03008674/*
8675 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8676 * used before) all generate the same failure when it is missing.
8677 */
8678static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8679{
8680 struct vcpu_vmx *vmx = to_vmx(vcpu);
8681 if (vmx->nested.current_vmptr == -1ull) {
8682 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008683 return 0;
8684 }
8685 return 1;
8686}
8687
8688static int handle_vmread(struct kvm_vcpu *vcpu)
8689{
8690 unsigned long field;
8691 u64 field_value;
8692 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8693 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8694 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008695 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008696
Kyle Hueyeb277562016-11-29 12:40:39 -08008697 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008698 return 1;
8699
Kyle Huey6affcbe2016-11-29 12:40:40 -08008700 if (!nested_vmx_check_vmcs12(vcpu))
8701 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008702
Liran Alon6d894f42018-06-23 02:35:09 +03008703 if (!is_guest_mode(vcpu))
8704 vmcs12 = get_vmcs12(vcpu);
8705 else {
8706 /*
8707 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8708 * to shadowed-field sets the ALU flags for VMfailInvalid.
8709 */
8710 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8711 nested_vmx_failInvalid(vcpu);
8712 return kvm_skip_emulated_instruction(vcpu);
8713 }
8714 vmcs12 = get_shadow_vmcs12(vcpu);
8715 }
8716
Nadav Har'El49f705c2011-05-25 23:08:30 +03008717 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008718 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008720 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008721 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008722 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 }
8724 /*
8725 * Now copy part of this value to register or memory, as requested.
8726 * Note that the number of bits actually copied is 32 or 64 depending
8727 * on the guest's mode (32 or 64 bit), not on the given field's length.
8728 */
8729 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008730 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008731 field_value);
8732 } else {
8733 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008734 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008735 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008736 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008737 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8738 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008739 }
8740
8741 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008742 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008743}
8744
8745
8746static int handle_vmwrite(struct kvm_vcpu *vcpu)
8747{
8748 unsigned long field;
8749 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008750 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008751 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8752 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008753
Nadav Har'El49f705c2011-05-25 23:08:30 +03008754 /* The value to write might be 32 or 64 bits, depending on L1's long
8755 * mode, and eventually we need to write that into a field of several
8756 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008757 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008758 * bits into the vmcs12 field.
8759 */
8760 u64 field_value = 0;
8761 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008762 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763
Kyle Hueyeb277562016-11-29 12:40:39 -08008764 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008765 return 1;
8766
Kyle Huey6affcbe2016-11-29 12:40:40 -08008767 if (!nested_vmx_check_vmcs12(vcpu))
8768 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008769
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008771 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008772 (((vmx_instruction_info) >> 3) & 0xf));
8773 else {
8774 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008775 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008776 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008777 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8778 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008779 kvm_inject_page_fault(vcpu, &e);
8780 return 1;
8781 }
8782 }
8783
8784
Nadav Amit27e6fb52014-06-18 17:19:26 +03008785 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008786 /*
8787 * If the vCPU supports "VMWRITE to any supported field in the
8788 * VMCS," then the "read-only" fields are actually read/write.
8789 */
8790 if (vmcs_field_readonly(field) &&
8791 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008792 nested_vmx_failValid(vcpu,
8793 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008794 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008795 }
8796
Liran Alon6d894f42018-06-23 02:35:09 +03008797 if (!is_guest_mode(vcpu))
8798 vmcs12 = get_vmcs12(vcpu);
8799 else {
8800 /*
8801 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8802 * to shadowed-field sets the ALU flags for VMfailInvalid.
8803 */
8804 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8805 nested_vmx_failInvalid(vcpu);
8806 return kvm_skip_emulated_instruction(vcpu);
8807 }
8808 vmcs12 = get_shadow_vmcs12(vcpu);
8809
8810 }
8811
8812 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008813 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008814 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008815 }
8816
Liran Alon6d894f42018-06-23 02:35:09 +03008817 /*
8818 * Do not track vmcs12 dirty-state if in guest-mode
8819 * as we actually dirty shadow vmcs12 instead of vmcs12.
8820 */
8821 if (!is_guest_mode(vcpu)) {
8822 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008823#define SHADOW_FIELD_RW(x) case x:
8824#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008825 /*
8826 * The fields that can be updated by L1 without a vmexit are
8827 * always updated in the vmcs02, the others go down the slow
8828 * path of prepare_vmcs02.
8829 */
8830 break;
8831 default:
8832 vmx->nested.dirty_vmcs12 = true;
8833 break;
8834 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008835 }
8836
Nadav Har'El49f705c2011-05-25 23:08:30 +03008837 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008838 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008839}
8840
Jim Mattsona8bc2842016-11-30 12:03:44 -08008841static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8842{
8843 vmx->nested.current_vmptr = vmptr;
8844 if (enable_shadow_vmcs) {
8845 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8846 SECONDARY_EXEC_SHADOW_VMCS);
8847 vmcs_write64(VMCS_LINK_POINTER,
8848 __pa(vmx->vmcs01.shadow_vmcs));
8849 vmx->nested.sync_shadow_vmcs = true;
8850 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008851 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008852}
8853
Nadav Har'El63846662011-05-25 23:07:29 +03008854/* Emulate the VMPTRLD instruction */
8855static int handle_vmptrld(struct kvm_vcpu *vcpu)
8856{
8857 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008858 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008859
8860 if (!nested_vmx_check_permission(vcpu))
8861 return 1;
8862
Radim Krčmářcbf71272017-05-19 15:48:51 +02008863 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008864 return 1;
8865
Radim Krčmářcbf71272017-05-19 15:48:51 +02008866 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8867 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8868 return kvm_skip_emulated_instruction(vcpu);
8869 }
8870
8871 if (vmptr == vmx->nested.vmxon_ptr) {
8872 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8873 return kvm_skip_emulated_instruction(vcpu);
8874 }
8875
Nadav Har'El63846662011-05-25 23:07:29 +03008876 if (vmx->nested.current_vmptr != vmptr) {
8877 struct vmcs12 *new_vmcs12;
8878 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008879 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8880 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008881 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008882 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008883 }
8884 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008885 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008886 (new_vmcs12->hdr.shadow_vmcs &&
8887 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008888 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008889 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008890 nested_vmx_failValid(vcpu,
8891 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008892 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008893 }
Nadav Har'El63846662011-05-25 23:07:29 +03008894
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008895 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008896 /*
8897 * Load VMCS12 from guest memory since it is not already
8898 * cached.
8899 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008900 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8901 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008902 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008903
Jim Mattsona8bc2842016-11-30 12:03:44 -08008904 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008905 }
8906
8907 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008908 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008909}
8910
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008911/* Emulate the VMPTRST instruction */
8912static int handle_vmptrst(struct kvm_vcpu *vcpu)
8913{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008914 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8915 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8916 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008918 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008919
8920 if (!nested_vmx_check_permission(vcpu))
8921 return 1;
8922
Sean Christopherson0a06d422018-07-19 10:31:00 -07008923 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008924 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008925 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008926 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8927 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008928 kvm_inject_page_fault(vcpu, &e);
8929 return 1;
8930 }
8931 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008932 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008933}
8934
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008935/* Emulate the INVEPT instruction */
8936static int handle_invept(struct kvm_vcpu *vcpu)
8937{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008938 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939 u32 vmx_instruction_info, types;
8940 unsigned long type;
8941 gva_t gva;
8942 struct x86_exception e;
8943 struct {
8944 u64 eptp, gpa;
8945 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008946
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008947 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008948 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008949 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008950 kvm_queue_exception(vcpu, UD_VECTOR);
8951 return 1;
8952 }
8953
8954 if (!nested_vmx_check_permission(vcpu))
8955 return 1;
8956
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008958 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008960 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961
Jim Mattson85c856b2016-10-26 08:38:38 -07008962 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008963 nested_vmx_failValid(vcpu,
8964 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008965 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008966 }
8967
8968 /* According to the Intel VMX instruction reference, the memory
8969 * operand is read even if it isn't needed (e.g., for type==global)
8970 */
8971 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008972 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008973 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008974 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008975 kvm_inject_page_fault(vcpu, &e);
8976 return 1;
8977 }
8978
8979 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008980 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008981 /*
8982 * TODO: track mappings and invalidate
8983 * single context requests appropriately
8984 */
8985 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008987 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008988 nested_vmx_succeed(vcpu);
8989 break;
8990 default:
8991 BUG_ON(1);
8992 break;
8993 }
8994
Kyle Huey6affcbe2016-11-29 12:40:40 -08008995 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008996}
8997
Petr Matouseka642fc32014-09-23 20:22:30 +02008998static int handle_invvpid(struct kvm_vcpu *vcpu)
8999{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009000 struct vcpu_vmx *vmx = to_vmx(vcpu);
9001 u32 vmx_instruction_info;
9002 unsigned long type, types;
9003 gva_t gva;
9004 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009005 struct {
9006 u64 vpid;
9007 u64 gla;
9008 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009009
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009010 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009011 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009012 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009013 kvm_queue_exception(vcpu, UD_VECTOR);
9014 return 1;
9015 }
9016
9017 if (!nested_vmx_check_permission(vcpu))
9018 return 1;
9019
9020 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9021 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9022
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009023 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009024 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009025
Jim Mattson85c856b2016-10-26 08:38:38 -07009026 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009027 nested_vmx_failValid(vcpu,
9028 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009029 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009030 }
9031
9032 /* according to the intel vmx instruction reference, the memory
9033 * operand is read even if it isn't needed (e.g., for type==global)
9034 */
9035 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9036 vmx_instruction_info, false, &gva))
9037 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009038 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009039 kvm_inject_page_fault(vcpu, &e);
9040 return 1;
9041 }
Jim Mattson40352602017-06-28 09:37:37 -07009042 if (operand.vpid >> 16) {
9043 nested_vmx_failValid(vcpu,
9044 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9045 return kvm_skip_emulated_instruction(vcpu);
9046 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009047
9048 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009049 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009050 if (!operand.vpid ||
9051 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009052 nested_vmx_failValid(vcpu,
9053 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9054 return kvm_skip_emulated_instruction(vcpu);
9055 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009056 if (cpu_has_vmx_invvpid_individual_addr() &&
9057 vmx->nested.vpid02) {
9058 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9059 vmx->nested.vpid02, operand.gla);
9060 } else
9061 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9062 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009063 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009064 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009065 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009066 nested_vmx_failValid(vcpu,
9067 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009068 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009069 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009070 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009071 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009072 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03009073 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009074 break;
9075 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009076 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009077 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009078 }
9079
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009080 nested_vmx_succeed(vcpu);
9081
Kyle Huey6affcbe2016-11-29 12:40:40 -08009082 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009083}
9084
Junaid Shahideb4b2482018-06-27 14:59:14 -07009085static int handle_invpcid(struct kvm_vcpu *vcpu)
9086{
9087 u32 vmx_instruction_info;
9088 unsigned long type;
9089 bool pcid_enabled;
9090 gva_t gva;
9091 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009092 unsigned i;
9093 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009094 struct {
9095 u64 pcid;
9096 u64 gla;
9097 } operand;
9098
9099 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9100 kvm_queue_exception(vcpu, UD_VECTOR);
9101 return 1;
9102 }
9103
9104 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9105 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9106
9107 if (type > 3) {
9108 kvm_inject_gp(vcpu, 0);
9109 return 1;
9110 }
9111
9112 /* According to the Intel instruction reference, the memory operand
9113 * is read even if it isn't needed (e.g., for type==all)
9114 */
9115 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9116 vmx_instruction_info, false, &gva))
9117 return 1;
9118
9119 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9120 kvm_inject_page_fault(vcpu, &e);
9121 return 1;
9122 }
9123
9124 if (operand.pcid >> 12 != 0) {
9125 kvm_inject_gp(vcpu, 0);
9126 return 1;
9127 }
9128
9129 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9130
9131 switch (type) {
9132 case INVPCID_TYPE_INDIV_ADDR:
9133 if ((!pcid_enabled && (operand.pcid != 0)) ||
9134 is_noncanonical_address(operand.gla, vcpu)) {
9135 kvm_inject_gp(vcpu, 0);
9136 return 1;
9137 }
9138 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9139 return kvm_skip_emulated_instruction(vcpu);
9140
9141 case INVPCID_TYPE_SINGLE_CTXT:
9142 if (!pcid_enabled && (operand.pcid != 0)) {
9143 kvm_inject_gp(vcpu, 0);
9144 return 1;
9145 }
9146
9147 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9148 kvm_mmu_sync_roots(vcpu);
9149 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9150 }
9151
Junaid Shahidb94742c2018-06-27 14:59:20 -07009152 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9153 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9154 == operand.pcid)
9155 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009156
Junaid Shahidb94742c2018-06-27 14:59:20 -07009157 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009158 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009159 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009160 * given PCID, then nothing needs to be done here because a
9161 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009162 */
9163
9164 return kvm_skip_emulated_instruction(vcpu);
9165
9166 case INVPCID_TYPE_ALL_NON_GLOBAL:
9167 /*
9168 * Currently, KVM doesn't mark global entries in the shadow
9169 * page tables, so a non-global flush just degenerates to a
9170 * global flush. If needed, we could optimize this later by
9171 * keeping track of global entries in shadow page tables.
9172 */
9173
9174 /* fall-through */
9175 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9176 kvm_mmu_unload(vcpu);
9177 return kvm_skip_emulated_instruction(vcpu);
9178
9179 default:
9180 BUG(); /* We have already checked above that type <= 3 */
9181 }
9182}
9183
Kai Huang843e4332015-01-28 10:54:28 +08009184static int handle_pml_full(struct kvm_vcpu *vcpu)
9185{
9186 unsigned long exit_qualification;
9187
9188 trace_kvm_pml_full(vcpu->vcpu_id);
9189
9190 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9191
9192 /*
9193 * PML buffer FULL happened while executing iret from NMI,
9194 * "blocked by NMI" bit has to be set before next VM entry.
9195 */
9196 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009197 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009198 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9199 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9200 GUEST_INTR_STATE_NMI);
9201
9202 /*
9203 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9204 * here.., and there's no userspace involvement needed for PML.
9205 */
9206 return 1;
9207}
9208
Yunhong Jiang64672c92016-06-13 14:19:59 -07009209static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9210{
9211 kvm_lapic_expired_hv_timer(vcpu);
9212 return 1;
9213}
9214
Bandan Das41ab9372017-08-03 15:54:43 -04009215static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9216{
9217 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009218 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9219
9220 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009221 switch (address & VMX_EPTP_MT_MASK) {
9222 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009223 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009224 return false;
9225 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009226 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009227 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009228 return false;
9229 break;
9230 default:
9231 return false;
9232 }
9233
David Hildenbrandbb97a012017-08-10 23:15:28 +02009234 /* only 4 levels page-walk length are valid */
9235 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009236 return false;
9237
9238 /* Reserved bits should not be set */
9239 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9240 return false;
9241
9242 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009243 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009244 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009245 return false;
9246 }
9247
9248 return true;
9249}
9250
9251static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9252 struct vmcs12 *vmcs12)
9253{
9254 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9255 u64 address;
9256 bool accessed_dirty;
9257 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9258
9259 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9260 !nested_cpu_has_ept(vmcs12))
9261 return 1;
9262
9263 if (index >= VMFUNC_EPTP_ENTRIES)
9264 return 1;
9265
9266
9267 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9268 &address, index * 8, 8))
9269 return 1;
9270
David Hildenbrandbb97a012017-08-10 23:15:28 +02009271 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009272
9273 /*
9274 * If the (L2) guest does a vmfunc to the currently
9275 * active ept pointer, we don't have to do anything else
9276 */
9277 if (vmcs12->ept_pointer != address) {
9278 if (!valid_ept_address(vcpu, address))
9279 return 1;
9280
9281 kvm_mmu_unload(vcpu);
9282 mmu->ept_ad = accessed_dirty;
9283 mmu->base_role.ad_disabled = !accessed_dirty;
9284 vmcs12->ept_pointer = address;
9285 /*
9286 * TODO: Check what's the correct approach in case
9287 * mmu reload fails. Currently, we just let the next
9288 * reload potentially fail
9289 */
9290 kvm_mmu_reload(vcpu);
9291 }
9292
9293 return 0;
9294}
9295
Bandan Das2a499e42017-08-03 15:54:41 -04009296static int handle_vmfunc(struct kvm_vcpu *vcpu)
9297{
Bandan Das27c42a12017-08-03 15:54:42 -04009298 struct vcpu_vmx *vmx = to_vmx(vcpu);
9299 struct vmcs12 *vmcs12;
9300 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9301
9302 /*
9303 * VMFUNC is only supported for nested guests, but we always enable the
9304 * secondary control for simplicity; for non-nested mode, fake that we
9305 * didn't by injecting #UD.
9306 */
9307 if (!is_guest_mode(vcpu)) {
9308 kvm_queue_exception(vcpu, UD_VECTOR);
9309 return 1;
9310 }
9311
9312 vmcs12 = get_vmcs12(vcpu);
9313 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9314 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009315
9316 switch (function) {
9317 case 0:
9318 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9319 goto fail;
9320 break;
9321 default:
9322 goto fail;
9323 }
9324 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009325
9326fail:
9327 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9328 vmcs_read32(VM_EXIT_INTR_INFO),
9329 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009330 return 1;
9331}
9332
Sean Christopherson0b665d32018-08-14 09:33:34 -07009333static int handle_encls(struct kvm_vcpu *vcpu)
9334{
9335 /*
9336 * SGX virtualization is not yet supported. There is no software
9337 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9338 * to prevent the guest from executing ENCLS.
9339 */
9340 kvm_queue_exception(vcpu, UD_VECTOR);
9341 return 1;
9342}
9343
Nadav Har'El0140cae2011-05-25 23:06:28 +03009344/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009345 * The exit handlers return 1 if the exit was handled fully and guest execution
9346 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9347 * to be done to userspace and return 0.
9348 */
Mathias Krause772e0312012-08-30 01:30:19 +02009349static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009350 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9351 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009352 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009353 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009354 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009355 [EXIT_REASON_CR_ACCESS] = handle_cr,
9356 [EXIT_REASON_DR_ACCESS] = handle_dr,
9357 [EXIT_REASON_CPUID] = handle_cpuid,
9358 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9359 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9360 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9361 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009362 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009363 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009364 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009365 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009366 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009367 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009368 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009369 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009370 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009371 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009372 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009373 [EXIT_REASON_VMOFF] = handle_vmoff,
9374 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009375 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9376 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009377 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009378 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009379 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009380 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009381 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009382 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009383 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9384 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009385 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9386 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009387 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009388 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009389 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009390 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009391 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009392 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009393 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009394 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009395 [EXIT_REASON_XSAVES] = handle_xsaves,
9396 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009397 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009398 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009399 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009400 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009401 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009402};
9403
9404static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009405 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009406
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009407static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9408 struct vmcs12 *vmcs12)
9409{
9410 unsigned long exit_qualification;
9411 gpa_t bitmap, last_bitmap;
9412 unsigned int port;
9413 int size;
9414 u8 b;
9415
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009416 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009417 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009418
9419 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9420
9421 port = exit_qualification >> 16;
9422 size = (exit_qualification & 7) + 1;
9423
9424 last_bitmap = (gpa_t)-1;
9425 b = -1;
9426
9427 while (size > 0) {
9428 if (port < 0x8000)
9429 bitmap = vmcs12->io_bitmap_a;
9430 else if (port < 0x10000)
9431 bitmap = vmcs12->io_bitmap_b;
9432 else
Joe Perches1d804d02015-03-30 16:46:09 -07009433 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009434 bitmap += (port & 0x7fff) / 8;
9435
9436 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009437 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009438 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009439 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009440 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009441
9442 port++;
9443 size--;
9444 last_bitmap = bitmap;
9445 }
9446
Joe Perches1d804d02015-03-30 16:46:09 -07009447 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009448}
9449
Nadav Har'El644d7112011-05-25 23:12:35 +03009450/*
9451 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9452 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9453 * disinterest in the current event (read or write a specific MSR) by using an
9454 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9455 */
9456static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9457 struct vmcs12 *vmcs12, u32 exit_reason)
9458{
9459 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9460 gpa_t bitmap;
9461
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009462 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009463 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009464
9465 /*
9466 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9467 * for the four combinations of read/write and low/high MSR numbers.
9468 * First we need to figure out which of the four to use:
9469 */
9470 bitmap = vmcs12->msr_bitmap;
9471 if (exit_reason == EXIT_REASON_MSR_WRITE)
9472 bitmap += 2048;
9473 if (msr_index >= 0xc0000000) {
9474 msr_index -= 0xc0000000;
9475 bitmap += 1024;
9476 }
9477
9478 /* Then read the msr_index'th bit from this bitmap: */
9479 if (msr_index < 1024*8) {
9480 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009481 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009482 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009483 return 1 & (b >> (msr_index & 7));
9484 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009485 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009486}
9487
9488/*
9489 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9490 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9491 * intercept (via guest_host_mask etc.) the current event.
9492 */
9493static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9494 struct vmcs12 *vmcs12)
9495{
9496 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9497 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009498 int reg;
9499 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009500
9501 switch ((exit_qualification >> 4) & 3) {
9502 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009503 reg = (exit_qualification >> 8) & 15;
9504 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009505 switch (cr) {
9506 case 0:
9507 if (vmcs12->cr0_guest_host_mask &
9508 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009509 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009510 break;
9511 case 3:
9512 if ((vmcs12->cr3_target_count >= 1 &&
9513 vmcs12->cr3_target_value0 == val) ||
9514 (vmcs12->cr3_target_count >= 2 &&
9515 vmcs12->cr3_target_value1 == val) ||
9516 (vmcs12->cr3_target_count >= 3 &&
9517 vmcs12->cr3_target_value2 == val) ||
9518 (vmcs12->cr3_target_count >= 4 &&
9519 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009520 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009521 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009522 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009523 break;
9524 case 4:
9525 if (vmcs12->cr4_guest_host_mask &
9526 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009527 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009528 break;
9529 case 8:
9530 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009531 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009532 break;
9533 }
9534 break;
9535 case 2: /* clts */
9536 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9537 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009538 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009539 break;
9540 case 1: /* mov from cr */
9541 switch (cr) {
9542 case 3:
9543 if (vmcs12->cpu_based_vm_exec_control &
9544 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009545 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009546 break;
9547 case 8:
9548 if (vmcs12->cpu_based_vm_exec_control &
9549 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009550 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009551 break;
9552 }
9553 break;
9554 case 3: /* lmsw */
9555 /*
9556 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9557 * cr0. Other attempted changes are ignored, with no exit.
9558 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009559 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009560 if (vmcs12->cr0_guest_host_mask & 0xe &
9561 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009562 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009563 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9564 !(vmcs12->cr0_read_shadow & 0x1) &&
9565 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009566 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009567 break;
9568 }
Joe Perches1d804d02015-03-30 16:46:09 -07009569 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009570}
9571
Liran Alona7cde482018-06-23 02:35:10 +03009572static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9573 struct vmcs12 *vmcs12, gpa_t bitmap)
9574{
9575 u32 vmx_instruction_info;
9576 unsigned long field;
9577 u8 b;
9578
9579 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9580 return true;
9581
9582 /* Decode instruction info and find the field to access */
9583 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9584 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9585
9586 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9587 if (field >> 15)
9588 return true;
9589
9590 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9591 return true;
9592
9593 return 1 & (b >> (field & 7));
9594}
9595
Nadav Har'El644d7112011-05-25 23:12:35 +03009596/*
9597 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9598 * should handle it ourselves in L0 (and then continue L2). Only call this
9599 * when in is_guest_mode (L2).
9600 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009601static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009602{
Nadav Har'El644d7112011-05-25 23:12:35 +03009603 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9604 struct vcpu_vmx *vmx = to_vmx(vcpu);
9605 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9606
Jim Mattson4f350c62017-09-14 16:31:44 -07009607 if (vmx->nested.nested_run_pending)
9608 return false;
9609
9610 if (unlikely(vmx->fail)) {
9611 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9612 vmcs_read32(VM_INSTRUCTION_ERROR));
9613 return true;
9614 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009615
David Matlackc9f04402017-08-01 14:00:40 -07009616 /*
9617 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009618 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9619 * Page). The CPU may write to these pages via their host
9620 * physical address while L2 is running, bypassing any
9621 * address-translation-based dirty tracking (e.g. EPT write
9622 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009623 *
9624 * Mark them dirty on every exit from L2 to prevent them from
9625 * getting out of sync with dirty tracking.
9626 */
9627 nested_mark_vmcs12_pages_dirty(vcpu);
9628
Jim Mattson4f350c62017-09-14 16:31:44 -07009629 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9630 vmcs_readl(EXIT_QUALIFICATION),
9631 vmx->idt_vectoring_info,
9632 intr_info,
9633 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9634 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009635
9636 switch (exit_reason) {
9637 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009638 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009639 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009640 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009641 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009642 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009643 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009644 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009645 else if (is_debug(intr_info) &&
9646 vcpu->guest_debug &
9647 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9648 return false;
9649 else if (is_breakpoint(intr_info) &&
9650 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9651 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009652 return vmcs12->exception_bitmap &
9653 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9654 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009655 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009656 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009657 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009658 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009659 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009661 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009662 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009663 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009665 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 case EXIT_REASON_HLT:
9667 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9668 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_INVLPG:
9671 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9672 case EXIT_REASON_RDPMC:
9673 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009674 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009675 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009676 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009677 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009678 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009679 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009680 case EXIT_REASON_VMREAD:
9681 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9682 vmcs12->vmread_bitmap);
9683 case EXIT_REASON_VMWRITE:
9684 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9685 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009686 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9687 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009688 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009689 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009690 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009691 /*
9692 * VMX instructions trap unconditionally. This allows L1 to
9693 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9694 */
Joe Perches1d804d02015-03-30 16:46:09 -07009695 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009696 case EXIT_REASON_CR_ACCESS:
9697 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9698 case EXIT_REASON_DR_ACCESS:
9699 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9700 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009701 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009702 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9703 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009704 case EXIT_REASON_MSR_READ:
9705 case EXIT_REASON_MSR_WRITE:
9706 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9707 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009708 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009709 case EXIT_REASON_MWAIT_INSTRUCTION:
9710 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009711 case EXIT_REASON_MONITOR_TRAP_FLAG:
9712 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009713 case EXIT_REASON_MONITOR_INSTRUCTION:
9714 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9715 case EXIT_REASON_PAUSE_INSTRUCTION:
9716 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9717 nested_cpu_has2(vmcs12,
9718 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9719 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009720 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009721 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009722 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009723 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009724 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009725 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009726 /*
9727 * The controls for "virtualize APIC accesses," "APIC-
9728 * register virtualization," and "virtual-interrupt
9729 * delivery" only come from vmcs12.
9730 */
Joe Perches1d804d02015-03-30 16:46:09 -07009731 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009732 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009733 /*
9734 * L0 always deals with the EPT violation. If nested EPT is
9735 * used, and the nested mmu code discovers that the address is
9736 * missing in the guest EPT table (EPT12), the EPT violation
9737 * will be injected with nested_ept_inject_page_fault()
9738 */
Joe Perches1d804d02015-03-30 16:46:09 -07009739 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009740 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009741 /*
9742 * L2 never uses directly L1's EPT, but rather L0's own EPT
9743 * table (shadow on EPT) or a merged EPT table that L0 built
9744 * (EPT on EPT). So any problems with the structure of the
9745 * table is L0's fault.
9746 */
Joe Perches1d804d02015-03-30 16:46:09 -07009747 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009748 case EXIT_REASON_INVPCID:
9749 return
9750 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9751 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009752 case EXIT_REASON_WBINVD:
9753 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9754 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009755 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009756 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9757 /*
9758 * This should never happen, since it is not possible to
9759 * set XSS to a non-zero value---neither in L1 nor in L2.
9760 * If if it were, XSS would have to be checked against
9761 * the XSS exit bitmap in vmcs12.
9762 */
9763 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009764 case EXIT_REASON_PREEMPTION_TIMER:
9765 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009766 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009767 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009768 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009769 case EXIT_REASON_VMFUNC:
9770 /* VM functions are emulated through L2->L0 vmexits. */
9771 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009772 case EXIT_REASON_ENCLS:
9773 /* SGX is never exposed to L1 */
9774 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009775 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009776 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009777 }
9778}
9779
Paolo Bonzini7313c692017-07-27 10:31:25 +02009780static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9781{
9782 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9783
9784 /*
9785 * At this point, the exit interruption info in exit_intr_info
9786 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9787 * we need to query the in-kernel LAPIC.
9788 */
9789 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9790 if ((exit_intr_info &
9791 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9792 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9793 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9794 vmcs12->vm_exit_intr_error_code =
9795 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9796 }
9797
9798 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9799 vmcs_readl(EXIT_QUALIFICATION));
9800 return 1;
9801}
9802
Avi Kivity586f9602010-11-18 13:09:54 +02009803static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9804{
9805 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9806 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9807}
9808
Kai Huanga3eaa862015-11-04 13:46:05 +08009809static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009810{
Kai Huanga3eaa862015-11-04 13:46:05 +08009811 if (vmx->pml_pg) {
9812 __free_page(vmx->pml_pg);
9813 vmx->pml_pg = NULL;
9814 }
Kai Huang843e4332015-01-28 10:54:28 +08009815}
9816
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009817static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009818{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009819 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009820 u64 *pml_buf;
9821 u16 pml_idx;
9822
9823 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9824
9825 /* Do nothing if PML buffer is empty */
9826 if (pml_idx == (PML_ENTITY_NUM - 1))
9827 return;
9828
9829 /* PML index always points to next available PML buffer entity */
9830 if (pml_idx >= PML_ENTITY_NUM)
9831 pml_idx = 0;
9832 else
9833 pml_idx++;
9834
9835 pml_buf = page_address(vmx->pml_pg);
9836 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9837 u64 gpa;
9838
9839 gpa = pml_buf[pml_idx];
9840 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009841 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009842 }
9843
9844 /* reset PML index */
9845 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9846}
9847
9848/*
9849 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9850 * Called before reporting dirty_bitmap to userspace.
9851 */
9852static void kvm_flush_pml_buffers(struct kvm *kvm)
9853{
9854 int i;
9855 struct kvm_vcpu *vcpu;
9856 /*
9857 * We only need to kick vcpu out of guest mode here, as PML buffer
9858 * is flushed at beginning of all VMEXITs, and it's obvious that only
9859 * vcpus running in guest are possible to have unflushed GPAs in PML
9860 * buffer.
9861 */
9862 kvm_for_each_vcpu(i, vcpu, kvm)
9863 kvm_vcpu_kick(vcpu);
9864}
9865
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009866static void vmx_dump_sel(char *name, uint32_t sel)
9867{
9868 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009869 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009870 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9871 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9872 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9873}
9874
9875static void vmx_dump_dtsel(char *name, uint32_t limit)
9876{
9877 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9878 name, vmcs_read32(limit),
9879 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9880}
9881
9882static void dump_vmcs(void)
9883{
9884 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9885 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9886 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9887 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9888 u32 secondary_exec_control = 0;
9889 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009890 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009891 int i, n;
9892
9893 if (cpu_has_secondary_exec_ctrls())
9894 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9895
9896 pr_err("*** Guest State ***\n");
9897 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9898 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9899 vmcs_readl(CR0_GUEST_HOST_MASK));
9900 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9901 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9902 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9903 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9904 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9905 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009906 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9907 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9908 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9909 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009910 }
9911 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9912 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9913 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9914 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9915 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9916 vmcs_readl(GUEST_SYSENTER_ESP),
9917 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9918 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9919 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9920 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9921 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9922 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9923 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9924 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9925 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9926 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9927 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9928 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9929 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009930 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9931 efer, vmcs_read64(GUEST_IA32_PAT));
9932 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9933 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009934 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009935 if (cpu_has_load_perf_global_ctrl &&
9936 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009937 pr_err("PerfGlobCtl = 0x%016llx\n",
9938 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009939 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009940 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009941 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9942 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9943 vmcs_read32(GUEST_ACTIVITY_STATE));
9944 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9945 pr_err("InterruptStatus = %04x\n",
9946 vmcs_read16(GUEST_INTR_STATUS));
9947
9948 pr_err("*** Host State ***\n");
9949 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9950 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9951 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9952 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9953 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9954 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9955 vmcs_read16(HOST_TR_SELECTOR));
9956 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9957 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9958 vmcs_readl(HOST_TR_BASE));
9959 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9960 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9961 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9962 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9963 vmcs_readl(HOST_CR4));
9964 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9965 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9966 vmcs_read32(HOST_IA32_SYSENTER_CS),
9967 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9968 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009969 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9970 vmcs_read64(HOST_IA32_EFER),
9971 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009972 if (cpu_has_load_perf_global_ctrl &&
9973 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009974 pr_err("PerfGlobCtl = 0x%016llx\n",
9975 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009976
9977 pr_err("*** Control State ***\n");
9978 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9979 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9980 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9981 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9982 vmcs_read32(EXCEPTION_BITMAP),
9983 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9984 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9985 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9986 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9987 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9988 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9989 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9990 vmcs_read32(VM_EXIT_INTR_INFO),
9991 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9992 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9993 pr_err(" reason=%08x qualification=%016lx\n",
9994 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9995 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9996 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9997 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009998 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009999 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010000 pr_err("TSC Multiplier = 0x%016llx\n",
10001 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010002 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10003 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10004 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10005 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10006 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010007 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010008 n = vmcs_read32(CR3_TARGET_COUNT);
10009 for (i = 0; i + 1 < n; i += 4)
10010 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10011 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10012 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10013 if (i < n)
10014 pr_err("CR3 target%u=%016lx\n",
10015 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10016 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10017 pr_err("PLE Gap=%08x Window=%08x\n",
10018 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10019 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10020 pr_err("Virtual processor ID = 0x%04x\n",
10021 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10022}
10023
Avi Kivity6aa8b732006-12-10 02:21:36 -080010024/*
10025 * The guest has exited. See if we can fix it or if we need userspace
10026 * assistance.
10027 */
Avi Kivity851ba692009-08-24 11:10:17 +030010028static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010029{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010030 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010031 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010032 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010033
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010034 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10035
Kai Huang843e4332015-01-28 10:54:28 +080010036 /*
10037 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10038 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10039 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10040 * mode as if vcpus is in root mode, the PML buffer must has been
10041 * flushed already.
10042 */
10043 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010044 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010045
Mohammed Gamal80ced182009-09-01 12:48:18 +020010046 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010047 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010048 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010049
Paolo Bonzini7313c692017-07-27 10:31:25 +020010050 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10051 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010052
Mohammed Gamal51207022010-05-31 22:40:54 +030010053 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010054 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010055 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10056 vcpu->run->fail_entry.hardware_entry_failure_reason
10057 = exit_reason;
10058 return 0;
10059 }
10060
Avi Kivity29bd8a72007-09-10 17:27:03 +030010061 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010062 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10063 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010064 = vmcs_read32(VM_INSTRUCTION_ERROR);
10065 return 0;
10066 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010067
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010068 /*
10069 * Note:
10070 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10071 * delivery event since it indicates guest is accessing MMIO.
10072 * The vm-exit can be triggered again after return to guest that
10073 * will cause infinite loop.
10074 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010075 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010076 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010077 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010078 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010079 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10080 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10081 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010082 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010083 vcpu->run->internal.data[0] = vectoring_info;
10084 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010085 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10086 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10087 vcpu->run->internal.ndata++;
10088 vcpu->run->internal.data[3] =
10089 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10090 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010091 return 0;
10092 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010093
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010094 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010095 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10096 if (vmx_interrupt_allowed(vcpu)) {
10097 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10098 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10099 vcpu->arch.nmi_pending) {
10100 /*
10101 * This CPU don't support us in finding the end of an
10102 * NMI-blocked window if the guest runs with IRQs
10103 * disabled. So we pull the trigger after 1 s of
10104 * futile waiting, but inform the user about this.
10105 */
10106 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10107 "state on VCPU %d after 1 s timeout\n",
10108 __func__, vcpu->vcpu_id);
10109 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10110 }
10111 }
10112
Avi Kivity6aa8b732006-12-10 02:21:36 -080010113 if (exit_reason < kvm_vmx_max_exit_handlers
10114 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010115 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010116 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010117 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10118 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010119 kvm_queue_exception(vcpu, UD_VECTOR);
10120 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010121 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010122}
10123
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010124/*
10125 * Software based L1D cache flush which is used when microcode providing
10126 * the cache control MSR is not loaded.
10127 *
10128 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10129 * flush it is required to read in 64 KiB because the replacement algorithm
10130 * is not exactly LRU. This could be sized at runtime via topology
10131 * information but as all relevant affected CPUs have 32KiB L1D cache size
10132 * there is no point in doing so.
10133 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010134static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010135{
10136 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010137
10138 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010139 * This code is only executed when the the flush mode is 'cond' or
10140 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010141 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010142 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010143 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010144
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010145 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010146 * Clear the per-vcpu flush bit, it gets set again
10147 * either from vcpu_run() or from one of the unsafe
10148 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010149 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010150 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010151 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010152
10153 /*
10154 * Clear the per-cpu flush bit, it gets set again from
10155 * the interrupt handlers.
10156 */
10157 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10158 kvm_clear_cpu_l1tf_flush_l1d();
10159
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010160 if (!flush_l1d)
10161 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010162 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010163
10164 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010165
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010166 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10167 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10168 return;
10169 }
10170
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010171 asm volatile(
10172 /* First ensure the pages are in the TLB */
10173 "xorl %%eax, %%eax\n"
10174 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010175 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010176 "addl $4096, %%eax\n\t"
10177 "cmpl %%eax, %[size]\n\t"
10178 "jne .Lpopulate_tlb\n\t"
10179 "xorl %%eax, %%eax\n\t"
10180 "cpuid\n\t"
10181 /* Now fill the cache */
10182 "xorl %%eax, %%eax\n"
10183 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010184 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010185 "addl $64, %%eax\n\t"
10186 "cmpl %%eax, %[size]\n\t"
10187 "jne .Lfill_cache\n\t"
10188 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010189 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010190 [size] "r" (size)
10191 : "eax", "ebx", "ecx", "edx");
10192}
10193
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010194static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010195{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010196 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10197
10198 if (is_guest_mode(vcpu) &&
10199 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10200 return;
10201
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010202 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010203 vmcs_write32(TPR_THRESHOLD, 0);
10204 return;
10205 }
10206
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010207 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010208}
10209
Jim Mattson8d860bb2018-05-09 16:56:05 -040010210static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010211{
10212 u32 sec_exec_control;
10213
Jim Mattson8d860bb2018-05-09 16:56:05 -040010214 if (!lapic_in_kernel(vcpu))
10215 return;
10216
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010217 /* Postpone execution until vmcs01 is the current VMCS. */
10218 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010219 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010220 return;
10221 }
10222
Paolo Bonzini35754c92015-07-29 12:05:37 +020010223 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +080010224 return;
10225
10226 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010227 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10228 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010229
Jim Mattson8d860bb2018-05-09 16:56:05 -040010230 switch (kvm_get_apic_mode(vcpu)) {
10231 case LAPIC_MODE_INVALID:
10232 WARN_ONCE(true, "Invalid local APIC state");
10233 case LAPIC_MODE_DISABLED:
10234 break;
10235 case LAPIC_MODE_XAPIC:
10236 if (flexpriority_enabled) {
10237 sec_exec_control |=
10238 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10239 vmx_flush_tlb(vcpu, true);
10240 }
10241 break;
10242 case LAPIC_MODE_X2APIC:
10243 if (cpu_has_vmx_virtualize_x2apic_mode())
10244 sec_exec_control |=
10245 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10246 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010247 }
10248 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10249
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010250 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010251}
10252
Tang Chen38b99172014-09-24 15:57:54 +080010253static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10254{
Jim Mattsonab5df312018-05-09 17:02:03 -040010255 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010256 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010257 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010258 }
Tang Chen38b99172014-09-24 15:57:54 +080010259}
10260
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010261static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010262{
10263 u16 status;
10264 u8 old;
10265
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010266 if (max_isr == -1)
10267 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010268
10269 status = vmcs_read16(GUEST_INTR_STATUS);
10270 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010271 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010272 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010273 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010274 vmcs_write16(GUEST_INTR_STATUS, status);
10275 }
10276}
10277
10278static void vmx_set_rvi(int vector)
10279{
10280 u16 status;
10281 u8 old;
10282
Wei Wang4114c272014-11-05 10:53:43 +080010283 if (vector == -1)
10284 vector = 0;
10285
Yang Zhangc7c9c562013-01-25 10:18:51 +080010286 status = vmcs_read16(GUEST_INTR_STATUS);
10287 old = (u8)status & 0xff;
10288 if ((u8)vector != old) {
10289 status &= ~0xff;
10290 status |= (u8)vector;
10291 vmcs_write16(GUEST_INTR_STATUS, status);
10292 }
10293}
10294
10295static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10296{
Liran Alon851c1a182017-12-24 18:12:56 +020010297 /*
10298 * When running L2, updating RVI is only relevant when
10299 * vmcs12 virtual-interrupt-delivery enabled.
10300 * However, it can be enabled only when L1 also
10301 * intercepts external-interrupts and in that case
10302 * we should not update vmcs02 RVI but instead intercept
10303 * interrupt. Therefore, do nothing when running L2.
10304 */
10305 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010306 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010307}
10308
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010309static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010310{
10311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010312 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010313 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010314
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010315 WARN_ON(!vcpu->arch.apicv_active);
10316 if (pi_test_on(&vmx->pi_desc)) {
10317 pi_clear_on(&vmx->pi_desc);
10318 /*
10319 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10320 * But on x86 this is just a compiler barrier anyway.
10321 */
10322 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010323 max_irr_updated =
10324 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10325
10326 /*
10327 * If we are running L2 and L1 has a new pending interrupt
10328 * which can be injected, we should re-evaluate
10329 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010330 * If L1 intercepts external-interrupts, we should
10331 * exit from L2 to L1. Otherwise, interrupt should be
10332 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010333 */
Liran Alon851c1a182017-12-24 18:12:56 +020010334 if (is_guest_mode(vcpu) && max_irr_updated) {
10335 if (nested_exit_on_intr(vcpu))
10336 kvm_vcpu_exiting_guest_mode(vcpu);
10337 else
10338 kvm_make_request(KVM_REQ_EVENT, vcpu);
10339 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010340 } else {
10341 max_irr = kvm_lapic_find_highest_irr(vcpu);
10342 }
10343 vmx_hwapic_irr_update(vcpu, max_irr);
10344 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010345}
10346
Andrey Smetanin63086302015-11-10 15:36:32 +030010347static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010348{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010349 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010350 return;
10351
Yang Zhangc7c9c562013-01-25 10:18:51 +080010352 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10353 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10354 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10355 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10356}
10357
Paolo Bonzini967235d2016-12-19 14:03:45 +010010358static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10359{
10360 struct vcpu_vmx *vmx = to_vmx(vcpu);
10361
10362 pi_clear_on(&vmx->pi_desc);
10363 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10364}
10365
Avi Kivity51aa01d2010-07-20 14:31:20 +030010366static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010367{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010368 u32 exit_intr_info = 0;
10369 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010370
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010371 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10372 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010373 return;
10374
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010375 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10376 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10377 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010378
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010379 /* if exit due to PF check for async PF */
10380 if (is_page_fault(exit_intr_info))
10381 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10382
Andi Kleena0861c02009-06-08 17:37:09 +080010383 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010384 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10385 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010386 kvm_machine_check();
10387
Gleb Natapov20f65982009-05-11 13:35:55 +030010388 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010389 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010390 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010391 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010392 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010393 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010394}
Gleb Natapov20f65982009-05-11 13:35:55 +030010395
Yang Zhanga547c6d2013-04-11 19:25:10 +080010396static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10397{
10398 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10399
Yang Zhanga547c6d2013-04-11 19:25:10 +080010400 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10401 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10402 unsigned int vector;
10403 unsigned long entry;
10404 gate_desc *desc;
10405 struct vcpu_vmx *vmx = to_vmx(vcpu);
10406#ifdef CONFIG_X86_64
10407 unsigned long tmp;
10408#endif
10409
10410 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10411 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010412 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010413 asm volatile(
10414#ifdef CONFIG_X86_64
10415 "mov %%" _ASM_SP ", %[sp]\n\t"
10416 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10417 "push $%c[ss]\n\t"
10418 "push %[sp]\n\t"
10419#endif
10420 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010421 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010422 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010423 :
10424#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010425 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010426#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010427 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010428 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010429 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010430 [ss]"i"(__KERNEL_DS),
10431 [cs]"i"(__KERNEL_CS)
10432 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010433 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010434}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010435STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010436
Tom Lendackybc226f02018-05-10 22:06:39 +020010437static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010438{
Tom Lendackybc226f02018-05-10 22:06:39 +020010439 switch (index) {
10440 case MSR_IA32_SMBASE:
10441 /*
10442 * We cannot do SMM unless we can run the guest in big
10443 * real mode.
10444 */
10445 return enable_unrestricted_guest || emulate_invalid_guest_state;
10446 case MSR_AMD64_VIRT_SPEC_CTRL:
10447 /* This is AMD only. */
10448 return false;
10449 default:
10450 return true;
10451 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010452}
10453
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010454static bool vmx_mpx_supported(void)
10455{
10456 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10457 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10458}
10459
Wanpeng Li55412b22014-12-02 19:21:30 +080010460static bool vmx_xsaves_supported(void)
10461{
10462 return vmcs_config.cpu_based_2nd_exec_ctrl &
10463 SECONDARY_EXEC_XSAVES;
10464}
10465
Avi Kivity51aa01d2010-07-20 14:31:20 +030010466static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10467{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010468 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010469 bool unblock_nmi;
10470 u8 vector;
10471 bool idtv_info_valid;
10472
10473 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010474
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010475 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010476 if (vmx->loaded_vmcs->nmi_known_unmasked)
10477 return;
10478 /*
10479 * Can't use vmx->exit_intr_info since we're not sure what
10480 * the exit reason is.
10481 */
10482 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10483 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10484 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10485 /*
10486 * SDM 3: 27.7.1.2 (September 2008)
10487 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10488 * a guest IRET fault.
10489 * SDM 3: 23.2.2 (September 2008)
10490 * Bit 12 is undefined in any of the following cases:
10491 * If the VM exit sets the valid bit in the IDT-vectoring
10492 * information field.
10493 * If the VM exit is due to a double fault.
10494 */
10495 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10496 vector != DF_VECTOR && !idtv_info_valid)
10497 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10498 GUEST_INTR_STATE_NMI);
10499 else
10500 vmx->loaded_vmcs->nmi_known_unmasked =
10501 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10502 & GUEST_INTR_STATE_NMI);
10503 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10504 vmx->loaded_vmcs->vnmi_blocked_time +=
10505 ktime_to_ns(ktime_sub(ktime_get(),
10506 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010507}
10508
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010509static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010510 u32 idt_vectoring_info,
10511 int instr_len_field,
10512 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010513{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010514 u8 vector;
10515 int type;
10516 bool idtv_info_valid;
10517
10518 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010519
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010520 vcpu->arch.nmi_injected = false;
10521 kvm_clear_exception_queue(vcpu);
10522 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010523
10524 if (!idtv_info_valid)
10525 return;
10526
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010527 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010528
Avi Kivity668f6122008-07-02 09:28:55 +030010529 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10530 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010531
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010532 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010533 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010534 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010535 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010536 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010537 * Clear bit "block by NMI" before VM entry if a NMI
10538 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010539 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010540 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010541 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010542 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010543 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010544 /* fall through */
10545 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010546 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010547 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010548 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010549 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010550 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010551 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010552 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010553 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010554 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010555 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010556 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010557 break;
10558 default:
10559 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010560 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010561}
10562
Avi Kivity83422e12010-07-20 14:43:23 +030010563static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10564{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010565 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010566 VM_EXIT_INSTRUCTION_LEN,
10567 IDT_VECTORING_ERROR_CODE);
10568}
10569
Avi Kivityb463a6f2010-07-20 15:06:17 +030010570static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10571{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010572 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010573 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10574 VM_ENTRY_INSTRUCTION_LEN,
10575 VM_ENTRY_EXCEPTION_ERROR_CODE);
10576
10577 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10578}
10579
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010580static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10581{
10582 int i, nr_msrs;
10583 struct perf_guest_switch_msr *msrs;
10584
10585 msrs = perf_guest_get_msrs(&nr_msrs);
10586
10587 if (!msrs)
10588 return;
10589
10590 for (i = 0; i < nr_msrs; i++)
10591 if (msrs[i].host == msrs[i].guest)
10592 clear_atomic_switch_msr(vmx, msrs[i].msr);
10593 else
10594 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010595 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010596}
10597
Jiang Biao33365e72016-11-03 15:03:37 +080010598static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010599{
10600 struct vcpu_vmx *vmx = to_vmx(vcpu);
10601 u64 tscl;
10602 u32 delta_tsc;
10603
10604 if (vmx->hv_deadline_tsc == -1)
10605 return;
10606
10607 tscl = rdtsc();
10608 if (vmx->hv_deadline_tsc > tscl)
10609 /* sure to be 32 bit only because checked on set_hv_timer */
10610 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10611 cpu_preemption_timer_multi);
10612 else
10613 delta_tsc = 0;
10614
10615 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
10616}
10617
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010618static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010619{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010620 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010621 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010622
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010623 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010624 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010625 vmx->loaded_vmcs->soft_vnmi_blocked))
10626 vmx->loaded_vmcs->entry_time = ktime_get();
10627
Avi Kivity104f2262010-11-18 13:12:52 +020010628 /* Don't enter VMX if guest state is invalid, let the exit handler
10629 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010630 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010631 return;
10632
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010633 if (vmx->ple_window_dirty) {
10634 vmx->ple_window_dirty = false;
10635 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10636 }
10637
Abel Gordon012f83c2013-04-18 14:39:25 +030010638 if (vmx->nested.sync_shadow_vmcs) {
10639 copy_vmcs12_to_shadow(vmx);
10640 vmx->nested.sync_shadow_vmcs = false;
10641 }
10642
Avi Kivity104f2262010-11-18 13:12:52 +020010643 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10644 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10645 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10646 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10647
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010648 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010649 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010650 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010651 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010652 }
10653
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010654 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010655 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010656 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010657 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010658 }
10659
Avi Kivity104f2262010-11-18 13:12:52 +020010660 /* When single-stepping over STI and MOV SS, we must clear the
10661 * corresponding interruptibility bits in the guest state. Otherwise
10662 * vmentry fails as it then expects bit 14 (BS) in pending debug
10663 * exceptions being set, but that's not correct for the guest debugging
10664 * case. */
10665 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10666 vmx_set_interrupt_shadow(vcpu, 0);
10667
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010668 if (static_cpu_has(X86_FEATURE_PKU) &&
10669 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10670 vcpu->arch.pkru != vmx->host_pkru)
10671 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010672
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010673 atomic_switch_perf_msrs(vmx);
10674
Yunhong Jiang64672c92016-06-13 14:19:59 -070010675 vmx_arm_hv_timer(vcpu);
10676
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010677 /*
10678 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10679 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10680 * is no need to worry about the conditional branch over the wrmsr
10681 * being speculatively taken.
10682 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010683 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010684
Nadav Har'Eld462b812011-05-24 15:26:10 +030010685 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010686
10687 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10688 (unsigned long)&current_evmcs->host_rsp : 0;
10689
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010690 if (static_branch_unlikely(&vmx_l1d_should_flush))
10691 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010692
Avi Kivity104f2262010-11-18 13:12:52 +020010693 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010694 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010695 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10696 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10697 "push %%" _ASM_CX " \n\t"
10698 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010699 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010700 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010701 /* Avoid VMWRITE when Enlightened VMCS is in use */
10702 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10703 "jz 2f \n\t"
10704 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10705 "jmp 1f \n\t"
10706 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010707 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010708 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010709 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010710 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10711 "mov %%cr2, %%" _ASM_DX " \n\t"
10712 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010713 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010714 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010715 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010716 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010717 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010718 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010719 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10720 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10721 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10722 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10723 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10724 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010725#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010726 "mov %c[r8](%0), %%r8 \n\t"
10727 "mov %c[r9](%0), %%r9 \n\t"
10728 "mov %c[r10](%0), %%r10 \n\t"
10729 "mov %c[r11](%0), %%r11 \n\t"
10730 "mov %c[r12](%0), %%r12 \n\t"
10731 "mov %c[r13](%0), %%r13 \n\t"
10732 "mov %c[r14](%0), %%r14 \n\t"
10733 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010734#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010735 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010736
Avi Kivity6aa8b732006-12-10 02:21:36 -080010737 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010738 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010739 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010740 "jmp 2f \n\t"
10741 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10742 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010743 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010744 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010745 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010746 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010747 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10748 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10749 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10750 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10751 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10752 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10753 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010754#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010755 "mov %%r8, %c[r8](%0) \n\t"
10756 "mov %%r9, %c[r9](%0) \n\t"
10757 "mov %%r10, %c[r10](%0) \n\t"
10758 "mov %%r11, %c[r11](%0) \n\t"
10759 "mov %%r12, %c[r12](%0) \n\t"
10760 "mov %%r13, %c[r13](%0) \n\t"
10761 "mov %%r14, %c[r14](%0) \n\t"
10762 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010763 "xor %%r8d, %%r8d \n\t"
10764 "xor %%r9d, %%r9d \n\t"
10765 "xor %%r10d, %%r10d \n\t"
10766 "xor %%r11d, %%r11d \n\t"
10767 "xor %%r12d, %%r12d \n\t"
10768 "xor %%r13d, %%r13d \n\t"
10769 "xor %%r14d, %%r14d \n\t"
10770 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010771#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010772 "mov %%cr2, %%" _ASM_AX " \n\t"
10773 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010774
Jim Mattson0cb5b302018-01-03 14:31:38 -080010775 "xor %%eax, %%eax \n\t"
10776 "xor %%ebx, %%ebx \n\t"
10777 "xor %%esi, %%esi \n\t"
10778 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010779 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010780 ".pushsection .rodata \n\t"
10781 ".global vmx_return \n\t"
10782 "vmx_return: " _ASM_PTR " 2b \n\t"
10783 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010784 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010785 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010786 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010787 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010788 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10789 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10790 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10791 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10792 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10793 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10794 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010795#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010796 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10797 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10798 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10799 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10800 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10801 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10802 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10803 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010804#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010805 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10806 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010807 : "cc", "memory"
10808#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010809 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010810 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010811#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010812 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010813#endif
10814 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010815
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010816 /*
10817 * We do not use IBRS in the kernel. If this vCPU has used the
10818 * SPEC_CTRL MSR it may have left it on; save the value and
10819 * turn it off. This is much more efficient than blindly adding
10820 * it to the atomic save/restore list. Especially as the former
10821 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10822 *
10823 * For non-nested case:
10824 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10825 * save it.
10826 *
10827 * For nested case:
10828 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10829 * save it.
10830 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010831 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010832 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010833
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010834 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010835
David Woodhouse117cc7a2018-01-12 11:11:27 +000010836 /* Eliminate branch target predictions from guest mode */
10837 vmexit_fill_RSB();
10838
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010839 /* All fields are clean at this point */
10840 if (static_branch_unlikely(&enable_evmcs))
10841 current_evmcs->hv_clean_fields |=
10842 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10843
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010844 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010845 if (vmx->host_debugctlmsr)
10846 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010847
Avi Kivityaa67f602012-08-01 16:48:03 +030010848#ifndef CONFIG_X86_64
10849 /*
10850 * The sysexit path does not restore ds/es, so we must set them to
10851 * a reasonable value ourselves.
10852 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010853 * We can't defer this to vmx_prepare_switch_to_host() since that
10854 * function may be executed in interrupt context, which saves and
10855 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010856 */
10857 loadsegment(ds, __USER_DS);
10858 loadsegment(es, __USER_DS);
10859#endif
10860
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010861 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010862 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010863 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010864 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010865 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010866 vcpu->arch.regs_dirty = 0;
10867
Gleb Natapove0b890d2013-09-25 12:51:33 +030010868 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010869 * eager fpu is enabled if PKEY is supported and CR4 is switched
10870 * back on host, so it is safe to read guest PKRU from current
10871 * XSAVE.
10872 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010873 if (static_cpu_has(X86_FEATURE_PKU) &&
10874 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10875 vcpu->arch.pkru = __read_pkru();
10876 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010877 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010878 }
10879
Gleb Natapove0b890d2013-09-25 12:51:33 +030010880 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010881 vmx->idt_vectoring_info = 0;
10882
10883 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10884 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10885 return;
10886
10887 vmx->loaded_vmcs->launched = 1;
10888 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010889
Avi Kivity51aa01d2010-07-20 14:31:20 +030010890 vmx_complete_atomic_exit(vmx);
10891 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010892 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010893}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010894STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010895
Sean Christopherson434a1e92018-03-20 12:17:18 -070010896static struct kvm *vmx_vm_alloc(void)
10897{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010898 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010899 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010900}
10901
10902static void vmx_vm_free(struct kvm *kvm)
10903{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010904 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010905}
10906
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010907static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010908{
10909 struct vcpu_vmx *vmx = to_vmx(vcpu);
10910 int cpu;
10911
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010912 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010913 return;
10914
10915 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010916 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010917 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010918 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010919 put_cpu();
10920}
10921
Jim Mattson2f1fe812016-07-08 15:36:06 -070010922/*
10923 * Ensure that the current vmcs of the logical processor is the
10924 * vmcs01 of the vcpu before calling free_nested().
10925 */
10926static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10927{
10928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010929
Christoffer Dallec7660c2017-12-04 21:35:23 +010010930 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010931 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010932 free_nested(vmx);
10933 vcpu_put(vcpu);
10934}
10935
Avi Kivity6aa8b732006-12-10 02:21:36 -080010936static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10937{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010938 struct vcpu_vmx *vmx = to_vmx(vcpu);
10939
Kai Huang843e4332015-01-28 10:54:28 +080010940 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010941 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010942 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010943 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010944 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010945 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010946 kfree(vmx->guest_msrs);
10947 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010948 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010949}
10950
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010951static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010952{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010953 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010954 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010955 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010956 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010957
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010958 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010959 return ERR_PTR(-ENOMEM);
10960
Wanpeng Li991e7a02015-09-16 17:30:05 +080010961 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010962
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010963 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10964 if (err)
10965 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010966
Peter Feiner4e595162016-07-07 14:49:58 -070010967 err = -ENOMEM;
10968
10969 /*
10970 * If PML is turned on, failure on enabling PML just results in failure
10971 * of creating the vcpu, therefore we can simplify PML logic (by
10972 * avoiding dealing with cases, such as enabling PML partially on vcpus
10973 * for the guest, etc.
10974 */
10975 if (enable_pml) {
10976 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10977 if (!vmx->pml_pg)
10978 goto uninit_vcpu;
10979 }
10980
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010981 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020010982 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
10983 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030010984
Peter Feiner4e595162016-07-07 14:49:58 -070010985 if (!vmx->guest_msrs)
10986 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010987
Paolo Bonzinif21f1652018-01-11 12:16:15 +010010988 err = alloc_loaded_vmcs(&vmx->vmcs01);
10989 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010990 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010991
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010992 msr_bitmap = vmx->vmcs01.msr_bitmap;
10993 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
10994 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
10995 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
10996 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
10997 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
10998 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
10999 vmx->msr_bitmap_mode = 0;
11000
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011001 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011002 cpu = get_cpu();
11003 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011004 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011005 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011006 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011007 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011008 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011009 err = alloc_apic_access_page(kvm);
11010 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011011 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011012 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011013
Sean Christophersone90008d2018-03-05 12:04:37 -080011014 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011015 err = init_rmode_identity_map(kvm);
11016 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011017 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011018 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011019
Roman Kagan63aff652018-07-19 21:59:07 +030011020 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011021 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11022 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011023
Wincy Van705699a2015-02-03 23:58:17 +080011024 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011025 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011026
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011027 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11028
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011029 /*
11030 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11031 * or POSTED_INTR_WAKEUP_VECTOR.
11032 */
11033 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11034 vmx->pi_desc.sn = 1;
11035
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011036 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011037
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011038free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011039 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011040free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011041 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011042free_pml:
11043 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011044uninit_vcpu:
11045 kvm_vcpu_uninit(&vmx->vcpu);
11046free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011047 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011048 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011049 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011050}
11051
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011052#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"
11053#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 -040011054
Wanpeng Lib31c1142018-03-12 04:53:04 -070011055static int vmx_vm_init(struct kvm *kvm)
11056{
Tianyu Lan877ad952018-07-19 08:40:23 +000011057 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11058
Wanpeng Lib31c1142018-03-12 04:53:04 -070011059 if (!ple_gap)
11060 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011061
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011062 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11063 switch (l1tf_mitigation) {
11064 case L1TF_MITIGATION_OFF:
11065 case L1TF_MITIGATION_FLUSH_NOWARN:
11066 /* 'I explicitly don't care' is set */
11067 break;
11068 case L1TF_MITIGATION_FLUSH:
11069 case L1TF_MITIGATION_FLUSH_NOSMT:
11070 case L1TF_MITIGATION_FULL:
11071 /*
11072 * Warn upon starting the first VM in a potentially
11073 * insecure environment.
11074 */
11075 if (cpu_smt_control == CPU_SMT_ENABLED)
11076 pr_warn_once(L1TF_MSG_SMT);
11077 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11078 pr_warn_once(L1TF_MSG_L1D);
11079 break;
11080 case L1TF_MITIGATION_FULL_FORCE:
11081 /* Flush is enforced */
11082 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011083 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011084 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011085 return 0;
11086}
11087
Yang, Sheng002c7f72007-07-31 14:23:01 +030011088static void __init vmx_check_processor_compat(void *rtn)
11089{
11090 struct vmcs_config vmcs_conf;
11091
11092 *(int *)rtn = 0;
11093 if (setup_vmcs_config(&vmcs_conf) < 0)
11094 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011095 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011096 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11097 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11098 smp_processor_id());
11099 *(int *)rtn = -EIO;
11100 }
11101}
11102
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011103static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011104{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011105 u8 cache;
11106 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011107
Sheng Yang522c68c2009-04-27 20:35:43 +080011108 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011109 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011110 * 2. EPT with VT-d:
11111 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011112 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011113 * b. VT-d with snooping control feature: snooping control feature of
11114 * VT-d engine can guarantee the cache correctness. Just set it
11115 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011116 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011117 * consistent with host MTRR
11118 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011119 if (is_mmio) {
11120 cache = MTRR_TYPE_UNCACHABLE;
11121 goto exit;
11122 }
11123
11124 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011125 ipat = VMX_EPT_IPAT_BIT;
11126 cache = MTRR_TYPE_WRBACK;
11127 goto exit;
11128 }
11129
11130 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11131 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011132 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011133 cache = MTRR_TYPE_WRBACK;
11134 else
11135 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011136 goto exit;
11137 }
11138
Xiao Guangrongff536042015-06-15 16:55:22 +080011139 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011140
11141exit:
11142 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011143}
11144
Sheng Yang17cc3932010-01-05 19:02:27 +080011145static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011146{
Sheng Yang878403b2010-01-05 19:02:29 +080011147 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11148 return PT_DIRECTORY_LEVEL;
11149 else
11150 /* For shadow and EPT supported 1GB page */
11151 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011152}
11153
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011154static void vmcs_set_secondary_exec_control(u32 new_ctl)
11155{
11156 /*
11157 * These bits in the secondary execution controls field
11158 * are dynamic, the others are mostly based on the hypervisor
11159 * architecture and the guest's CPUID. Do not touch the
11160 * dynamic bits.
11161 */
11162 u32 mask =
11163 SECONDARY_EXEC_SHADOW_VMCS |
11164 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011165 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11166 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011167
11168 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11169
11170 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11171 (new_ctl & ~mask) | (cur_ctl & mask));
11172}
11173
David Matlack8322ebb2016-11-29 18:14:09 -080011174/*
11175 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11176 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11177 */
11178static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11179{
11180 struct vcpu_vmx *vmx = to_vmx(vcpu);
11181 struct kvm_cpuid_entry2 *entry;
11182
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011183 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11184 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011185
11186#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11187 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011188 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011189} while (0)
11190
11191 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11192 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11193 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11194 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11195 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11196 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11197 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11198 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11199 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11200 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11201 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11202 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11203 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11204 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11205 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11206
11207 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11208 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11209 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11210 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11211 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011212 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011213
11214#undef cr4_fixed1_update
11215}
11216
Sheng Yang0e851882009-12-18 16:48:46 +080011217static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11218{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011220
Paolo Bonzini80154d72017-08-24 13:55:35 +020011221 if (cpu_has_secondary_exec_ctrls()) {
11222 vmx_compute_secondary_exec_control(vmx);
11223 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011224 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011225
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011226 if (nested_vmx_allowed(vcpu))
11227 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11228 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11229 else
11230 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11231 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011232
11233 if (nested_vmx_allowed(vcpu))
11234 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +080011235}
11236
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011237static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11238{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011239 if (func == 1 && nested)
11240 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011241}
11242
Yang Zhang25d92082013-08-06 12:00:32 +030011243static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11244 struct x86_exception *fault)
11245{
Jan Kiszka533558b2014-01-04 18:47:20 +010011246 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011247 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011248 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011249 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011250
Bandan Dasc5f983f2017-05-05 15:25:14 -040011251 if (vmx->nested.pml_full) {
11252 exit_reason = EXIT_REASON_PML_FULL;
11253 vmx->nested.pml_full = false;
11254 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11255 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011256 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011257 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011258 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011259
11260 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011261 vmcs12->guest_physical_address = fault->address;
11262}
11263
Peter Feiner995f00a2017-06-30 17:26:32 -070011264static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11265{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011266 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011267}
11268
Nadav Har'El155a97a2013-08-05 11:07:16 +030011269/* Callbacks for nested_ept_init_mmu_context: */
11270
11271static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11272{
11273 /* return the page table to be shadowed - in our case, EPT12 */
11274 return get_vmcs12(vcpu)->ept_pointer;
11275}
11276
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011277static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011278{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011279 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011280 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011281 return 1;
11282
Paolo Bonziniad896af2013-10-02 16:56:14 +020011283 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011284 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011285 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011286 nested_ept_ad_enabled(vcpu),
11287 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011288 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11289 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11290 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11291
11292 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011293 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011294}
11295
11296static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11297{
11298 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11299}
11300
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011301static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11302 u16 error_code)
11303{
11304 bool inequality, bit;
11305
11306 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11307 inequality =
11308 (error_code & vmcs12->page_fault_error_code_mask) !=
11309 vmcs12->page_fault_error_code_match;
11310 return inequality ^ bit;
11311}
11312
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011313static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11314 struct x86_exception *fault)
11315{
11316 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11317
11318 WARN_ON(!is_guest_mode(vcpu));
11319
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011320 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11321 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011322 vmcs12->vm_exit_intr_error_code = fault->error_code;
11323 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11324 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11325 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11326 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011327 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011328 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011329 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011330}
11331
Paolo Bonzinic9923842017-12-13 14:16:30 +010011332static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11333 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011334
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011335static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011336{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011337 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011338 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011339 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011340 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011341
11342 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011343 /*
11344 * Translate L1 physical address to host physical
11345 * address for vmcs02. Keep the page pinned, so this
11346 * physical address remains valid. We keep a reference
11347 * to it so we can release it later.
11348 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011349 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011350 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011351 vmx->nested.apic_access_page = NULL;
11352 }
11353 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011354 /*
11355 * If translation failed, no matter: This feature asks
11356 * to exit when accessing the given address, and if it
11357 * can never be accessed, this feature won't do
11358 * anything anyway.
11359 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011360 if (!is_error_page(page)) {
11361 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011362 hpa = page_to_phys(vmx->nested.apic_access_page);
11363 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11364 } else {
11365 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11366 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11367 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011368 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011369
11370 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011371 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011372 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011373 vmx->nested.virtual_apic_page = NULL;
11374 }
11375 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011376
11377 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011378 * If translation failed, VM entry will fail because
11379 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11380 * Failing the vm entry is _not_ what the processor
11381 * does but it's basically the only possibility we
11382 * have. We could still enter the guest if CR8 load
11383 * exits are enabled, CR8 store exits are enabled, and
11384 * virtualize APIC access is disabled; in this case
11385 * the processor would never use the TPR shadow and we
11386 * could simply clear the bit from the execution
11387 * control. But such a configuration is useless, so
11388 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011389 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011390 if (!is_error_page(page)) {
11391 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011392 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11393 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11394 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011395 }
11396
Wincy Van705699a2015-02-03 23:58:17 +080011397 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011398 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11399 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011400 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011401 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011402 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011403 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11404 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011405 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011406 vmx->nested.pi_desc_page = page;
11407 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011408 vmx->nested.pi_desc =
11409 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11410 (unsigned long)(vmcs12->posted_intr_desc_addr &
11411 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011412 vmcs_write64(POSTED_INTR_DESC_ADDR,
11413 page_to_phys(vmx->nested.pi_desc_page) +
11414 (unsigned long)(vmcs12->posted_intr_desc_addr &
11415 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011416 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011417 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011418 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11419 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011420 else
11421 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11422 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011423}
11424
Jan Kiszkaf41245002014-03-07 20:03:13 +010011425static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11426{
11427 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11428 struct vcpu_vmx *vmx = to_vmx(vcpu);
11429
11430 if (vcpu->arch.virtual_tsc_khz == 0)
11431 return;
11432
11433 /* Make sure short timeouts reliably trigger an immediate vmexit.
11434 * hrtimer_start does not guarantee this. */
11435 if (preemption_timeout <= 1) {
11436 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11437 return;
11438 }
11439
11440 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11441 preemption_timeout *= 1000000;
11442 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11443 hrtimer_start(&vmx->nested.preemption_timer,
11444 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11445}
11446
Jim Mattson56a20512017-07-06 16:33:06 -070011447static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11448 struct vmcs12 *vmcs12)
11449{
11450 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11451 return 0;
11452
11453 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11454 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11455 return -EINVAL;
11456
11457 return 0;
11458}
11459
Wincy Van3af18d92015-02-03 23:49:31 +080011460static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11461 struct vmcs12 *vmcs12)
11462{
Wincy Van3af18d92015-02-03 23:49:31 +080011463 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11464 return 0;
11465
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011466 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011467 return -EINVAL;
11468
11469 return 0;
11470}
11471
Jim Mattson712b12d2017-08-24 13:24:47 -070011472static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11473 struct vmcs12 *vmcs12)
11474{
11475 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11476 return 0;
11477
11478 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11479 return -EINVAL;
11480
11481 return 0;
11482}
11483
Wincy Van3af18d92015-02-03 23:49:31 +080011484/*
11485 * Merge L0's and L1's MSR bitmap, return false to indicate that
11486 * we do not use the hardware.
11487 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011488static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11489 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011490{
Wincy Van82f0dd42015-02-03 23:57:18 +080011491 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011492 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011493 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011494 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011495 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011496 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011497 *
11498 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11499 * ensures that we do not accidentally generate an L02 MSR bitmap
11500 * from the L12 MSR bitmap that is too permissive.
11501 * 2. That L1 or L2s have actually used the MSR. This avoids
11502 * unnecessarily merging of the bitmap if the MSR is unused. This
11503 * works properly because we only update the L01 MSR bitmap lazily.
11504 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11505 * updated to reflect this when L1 (or its L2s) actually write to
11506 * the MSR.
11507 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011508 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11509 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011510
Paolo Bonzinic9923842017-12-13 14:16:30 +010011511 /* Nothing to do if the MSR bitmap is not in use. */
11512 if (!cpu_has_vmx_msr_bitmap() ||
11513 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11514 return false;
11515
Ashok Raj15d45072018-02-01 22:59:43 +010011516 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011517 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011518 return false;
11519
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011520 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11521 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011522 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011523
Radim Krčmářd048c092016-08-08 20:16:22 +020011524 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011525 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11526 /*
11527 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11528 * just lets the processor take the value from the virtual-APIC page;
11529 * take those 256 bits directly from the L1 bitmap.
11530 */
11531 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11532 unsigned word = msr / BITS_PER_LONG;
11533 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11534 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011535 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011536 } else {
11537 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11538 unsigned word = msr / BITS_PER_LONG;
11539 msr_bitmap_l0[word] = ~0;
11540 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11541 }
11542 }
11543
11544 nested_vmx_disable_intercept_for_msr(
11545 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011546 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011547 MSR_TYPE_W);
11548
11549 if (nested_cpu_has_vid(vmcs12)) {
11550 nested_vmx_disable_intercept_for_msr(
11551 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011552 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011553 MSR_TYPE_W);
11554 nested_vmx_disable_intercept_for_msr(
11555 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011556 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011557 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011558 }
Ashok Raj15d45072018-02-01 22:59:43 +010011559
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011560 if (spec_ctrl)
11561 nested_vmx_disable_intercept_for_msr(
11562 msr_bitmap_l1, msr_bitmap_l0,
11563 MSR_IA32_SPEC_CTRL,
11564 MSR_TYPE_R | MSR_TYPE_W);
11565
Ashok Raj15d45072018-02-01 22:59:43 +010011566 if (pred_cmd)
11567 nested_vmx_disable_intercept_for_msr(
11568 msr_bitmap_l1, msr_bitmap_l0,
11569 MSR_IA32_PRED_CMD,
11570 MSR_TYPE_W);
11571
Wincy Vanf2b93282015-02-03 23:56:03 +080011572 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011573 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011574
11575 return true;
11576}
11577
Liran Alon61ada742018-06-23 02:35:08 +030011578static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11579 struct vmcs12 *vmcs12)
11580{
11581 struct vmcs12 *shadow;
11582 struct page *page;
11583
11584 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11585 vmcs12->vmcs_link_pointer == -1ull)
11586 return;
11587
11588 shadow = get_shadow_vmcs12(vcpu);
11589 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11590
11591 memcpy(shadow, kmap(page), VMCS12_SIZE);
11592
11593 kunmap(page);
11594 kvm_release_page_clean(page);
11595}
11596
11597static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11598 struct vmcs12 *vmcs12)
11599{
11600 struct vcpu_vmx *vmx = to_vmx(vcpu);
11601
11602 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11603 vmcs12->vmcs_link_pointer == -1ull)
11604 return;
11605
11606 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11607 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11608}
11609
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011610static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11611 struct vmcs12 *vmcs12)
11612{
11613 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11614 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11615 return -EINVAL;
11616 else
11617 return 0;
11618}
11619
Wincy Vanf2b93282015-02-03 23:56:03 +080011620static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11621 struct vmcs12 *vmcs12)
11622{
Wincy Van82f0dd42015-02-03 23:57:18 +080011623 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011624 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011625 !nested_cpu_has_vid(vmcs12) &&
11626 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011627 return 0;
11628
11629 /*
11630 * If virtualize x2apic mode is enabled,
11631 * virtualize apic access must be disabled.
11632 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011633 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11634 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011635 return -EINVAL;
11636
Wincy Van608406e2015-02-03 23:57:51 +080011637 /*
11638 * If virtual interrupt delivery is enabled,
11639 * we must exit on external interrupts.
11640 */
11641 if (nested_cpu_has_vid(vmcs12) &&
11642 !nested_exit_on_intr(vcpu))
11643 return -EINVAL;
11644
Wincy Van705699a2015-02-03 23:58:17 +080011645 /*
11646 * bits 15:8 should be zero in posted_intr_nv,
11647 * the descriptor address has been already checked
11648 * in nested_get_vmcs12_pages.
11649 */
11650 if (nested_cpu_has_posted_intr(vmcs12) &&
11651 (!nested_cpu_has_vid(vmcs12) ||
11652 !nested_exit_intr_ack_set(vcpu) ||
11653 vmcs12->posted_intr_nv & 0xff00))
11654 return -EINVAL;
11655
Wincy Vanf2b93282015-02-03 23:56:03 +080011656 /* tpr shadow is needed by all apicv features. */
11657 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11658 return -EINVAL;
11659
11660 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011661}
11662
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011663static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11664 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011665 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011666{
Liran Alone2536742018-06-23 02:35:02 +030011667 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011668 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011669 u64 count, addr;
11670
Liran Alone2536742018-06-23 02:35:02 +030011671 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11672 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011673 WARN_ON(1);
11674 return -EINVAL;
11675 }
11676 if (count == 0)
11677 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011678 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011679 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11680 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011681 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011682 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11683 addr_field, maxphyaddr, count, addr);
11684 return -EINVAL;
11685 }
11686 return 0;
11687}
11688
11689static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11690 struct vmcs12 *vmcs12)
11691{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011692 if (vmcs12->vm_exit_msr_load_count == 0 &&
11693 vmcs12->vm_exit_msr_store_count == 0 &&
11694 vmcs12->vm_entry_msr_load_count == 0)
11695 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011696 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011697 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011698 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011699 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011700 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011701 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011702 return -EINVAL;
11703 return 0;
11704}
11705
Bandan Dasc5f983f2017-05-05 15:25:14 -040011706static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11707 struct vmcs12 *vmcs12)
11708{
11709 u64 address = vmcs12->pml_address;
11710 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11711
11712 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11713 if (!nested_cpu_has_ept(vmcs12) ||
11714 !IS_ALIGNED(address, 4096) ||
11715 address >> maxphyaddr)
11716 return -EINVAL;
11717 }
11718
11719 return 0;
11720}
11721
Liran Alona8a7c022018-06-23 02:35:06 +030011722static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11723 struct vmcs12 *vmcs12)
11724{
11725 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11726 return 0;
11727
11728 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11729 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11730 return -EINVAL;
11731
11732 return 0;
11733}
11734
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11736 struct vmx_msr_entry *e)
11737{
11738 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011739 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011740 return -EINVAL;
11741 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11742 e->index == MSR_IA32_UCODE_REV)
11743 return -EINVAL;
11744 if (e->reserved != 0)
11745 return -EINVAL;
11746 return 0;
11747}
11748
11749static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11750 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011751{
11752 if (e->index == MSR_FS_BASE ||
11753 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011754 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11755 nested_vmx_msr_check_common(vcpu, e))
11756 return -EINVAL;
11757 return 0;
11758}
11759
11760static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11761 struct vmx_msr_entry *e)
11762{
11763 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11764 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011765 return -EINVAL;
11766 return 0;
11767}
11768
11769/*
11770 * Load guest's/host's msr at nested entry/exit.
11771 * return 0 for success, entry index for failure.
11772 */
11773static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11774{
11775 u32 i;
11776 struct vmx_msr_entry e;
11777 struct msr_data msr;
11778
11779 msr.host_initiated = false;
11780 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011781 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11782 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011783 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011784 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11785 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011786 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011787 }
11788 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011789 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011790 "%s check failed (%u, 0x%x, 0x%x)\n",
11791 __func__, i, e.index, e.reserved);
11792 goto fail;
11793 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011794 msr.index = e.index;
11795 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011796 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011797 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011798 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11799 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011800 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011801 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011802 }
11803 return 0;
11804fail:
11805 return i + 1;
11806}
11807
11808static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11809{
11810 u32 i;
11811 struct vmx_msr_entry e;
11812
11813 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011814 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011815 if (kvm_vcpu_read_guest(vcpu,
11816 gpa + i * sizeof(e),
11817 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011818 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011819 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11820 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011821 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011822 }
11823 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011824 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011825 "%s check failed (%u, 0x%x, 0x%x)\n",
11826 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011827 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011828 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011829 msr_info.host_initiated = false;
11830 msr_info.index = e.index;
11831 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011832 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011833 "%s cannot read MSR (%u, 0x%x)\n",
11834 __func__, i, e.index);
11835 return -EINVAL;
11836 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011837 if (kvm_vcpu_write_guest(vcpu,
11838 gpa + i * sizeof(e) +
11839 offsetof(struct vmx_msr_entry, value),
11840 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011841 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011842 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011843 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011844 return -EINVAL;
11845 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011846 }
11847 return 0;
11848}
11849
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011850static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11851{
11852 unsigned long invalid_mask;
11853
11854 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11855 return (val & invalid_mask) == 0;
11856}
11857
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011858/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011859 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11860 * emulating VM entry into a guest with EPT enabled.
11861 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11862 * is assigned to entry_failure_code on failure.
11863 */
11864static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011865 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011866{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011867 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011868 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011869 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11870 return 1;
11871 }
11872
11873 /*
11874 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11875 * must not be dereferenced.
11876 */
11877 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11878 !nested_ept) {
11879 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11880 *entry_failure_code = ENTRY_FAIL_PDPTE;
11881 return 1;
11882 }
11883 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011884 }
11885
Junaid Shahid50c28f22018-06-27 14:59:11 -070011886 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011887 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011888
11889 vcpu->arch.cr3 = cr3;
11890 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11891
11892 kvm_init_mmu(vcpu, false);
11893
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011894 return 0;
11895}
11896
Jim Mattson6514dc32018-04-26 16:09:12 -070011897static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011898{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011899 struct vcpu_vmx *vmx = to_vmx(vcpu);
11900
11901 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11902 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11903 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11904 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11905 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11906 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11907 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11908 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11909 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11910 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11911 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11912 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11913 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11914 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11915 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11916 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11917 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11918 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11919 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11920 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11921 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11922 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11923 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11924 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11925 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11926 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11927 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11928 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11929 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11930 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11931 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011932
11933 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11934 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11935 vmcs12->guest_pending_dbg_exceptions);
11936 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11937 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11938
11939 if (nested_cpu_has_xsaves(vmcs12))
11940 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11941 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11942
11943 if (cpu_has_vmx_posted_intr())
11944 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11945
11946 /*
11947 * Whether page-faults are trapped is determined by a combination of
11948 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11949 * If enable_ept, L0 doesn't care about page faults and we should
11950 * set all of these to L1's desires. However, if !enable_ept, L0 does
11951 * care about (at least some) page faults, and because it is not easy
11952 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11953 * to exit on each and every L2 page fault. This is done by setting
11954 * MASK=MATCH=0 and (see below) EB.PF=1.
11955 * Note that below we don't need special code to set EB.PF beyond the
11956 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
11957 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
11958 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
11959 */
11960 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
11961 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
11962 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
11963 enable_ept ? vmcs12->page_fault_error_code_match : 0);
11964
11965 /* All VMFUNCs are currently emulated through L0 vmexits. */
11966 if (cpu_has_vmx_vmfunc())
11967 vmcs_write64(VM_FUNCTION_CONTROL, 0);
11968
11969 if (cpu_has_vmx_apicv()) {
11970 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
11971 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
11972 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
11973 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
11974 }
11975
11976 /*
11977 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
11978 * Some constant fields are set here by vmx_set_constant_host_state().
11979 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070011980 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
11981 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011982 */
11983 vmx_set_constant_host_state(vmx);
11984
11985 /*
11986 * Set the MSR load/store lists to match L0's settings.
11987 */
11988 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040011989 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
11990 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
11991 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
11992 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011993
11994 set_cr4_guest_host_mask(vmx);
11995
11996 if (vmx_mpx_supported())
11997 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
11998
11999 if (enable_vpid) {
12000 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12001 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12002 else
12003 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12004 }
12005
12006 /*
12007 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12008 */
12009 if (enable_ept) {
12010 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12011 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12012 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12013 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12014 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012015
12016 if (cpu_has_vmx_msr_bitmap())
12017 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012018}
12019
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012020/*
12021 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12022 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012023 * 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 +030012024 * guest in a way that will both be appropriate to L1's requests, and our
12025 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12026 * function also has additional necessary side-effects, like setting various
12027 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012028 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12029 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012030 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012031static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012032 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012033{
12034 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012035 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012036
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012037 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012038 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012039 vmx->nested.dirty_vmcs12 = false;
12040 }
12041
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012042 /*
12043 * First, the fields that are shadowed. This must be kept in sync
12044 * with vmx_shadow_fields.h.
12045 */
12046
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012047 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012048 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012049 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012050 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12051 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012052
Jim Mattson6514dc32018-04-26 16:09:12 -070012053 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012054 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012055 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12056 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12057 } else {
12058 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12059 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12060 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012061 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012062 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12063 vmcs12->vm_entry_intr_info_field);
12064 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12065 vmcs12->vm_entry_exception_error_code);
12066 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12067 vmcs12->vm_entry_instruction_len);
12068 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12069 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012070 vmx->loaded_vmcs->nmi_known_unmasked =
12071 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012072 } else {
12073 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12074 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012075 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012076
Jan Kiszkaf41245002014-03-07 20:03:13 +010012077 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012078
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012079 /* Preemption timer setting is only taken from vmcs01. */
12080 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12081 exec_control |= vmcs_config.pin_based_exec_ctrl;
12082 if (vmx->hv_deadline_tsc == -1)
12083 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12084
12085 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012086 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012087 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12088 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012089 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012090 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012091 }
Wincy Van705699a2015-02-03 23:58:17 +080012092
Jan Kiszkaf41245002014-03-07 20:03:13 +010012093 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012094
Jan Kiszkaf41245002014-03-07 20:03:13 +010012095 vmx->nested.preemption_timer_expired = false;
12096 if (nested_cpu_has_preemption_timer(vmcs12))
12097 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012098
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012099 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012100 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012101
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012102 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012103 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012104 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012105 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012106 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012107 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012108 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12109 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012110 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012111 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12112 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12113 ~SECONDARY_EXEC_ENABLE_PML;
12114 exec_control |= vmcs12_exec_ctrl;
12115 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012116
Liran Alon32c7acf2018-06-23 02:35:11 +030012117 /* VMCS shadowing for L2 is emulated for now */
12118 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12119
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012120 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012121 vmcs_write16(GUEST_INTR_STATUS,
12122 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012123
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012124 /*
12125 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12126 * nested_get_vmcs12_pages will either fix it up or
12127 * remove the VM execution control.
12128 */
12129 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12130 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12131
Sean Christopherson0b665d32018-08-14 09:33:34 -070012132 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12133 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12134
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012135 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12136 }
12137
Jim Mattson83bafef2016-10-04 10:48:38 -070012138 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012139 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12140 * entry, but only if the current (host) sp changed from the value
12141 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12142 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12143 * here we just force the write to happen on entry.
12144 */
12145 vmx->host_rsp = 0;
12146
12147 exec_control = vmx_exec_control(vmx); /* L0's desires */
12148 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12149 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12150 exec_control &= ~CPU_BASED_TPR_SHADOW;
12151 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012152
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012153 /*
12154 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12155 * nested_get_vmcs12_pages can't fix it up, the illegal value
12156 * will result in a VM entry failure.
12157 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012158 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012159 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012160 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012161 } else {
12162#ifdef CONFIG_X86_64
12163 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12164 CPU_BASED_CR8_STORE_EXITING;
12165#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012166 }
12167
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012168 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012169 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12170 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012171 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012172 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12173 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12174
12175 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12176
12177 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12178 * bitwise-or of what L1 wants to trap for L2, and what we want to
12179 * trap. Note that CR0.TS also needs updating - we do this later.
12180 */
12181 update_exception_bitmap(vcpu);
12182 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12183 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12184
Nadav Har'El8049d652013-08-05 11:07:06 +030012185 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12186 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12187 * bits are further modified by vmx_set_efer() below.
12188 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012189 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012190
12191 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12192 * emulated by vmx_set_efer(), below.
12193 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012194 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012195 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12196 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012197 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12198
Jim Mattson6514dc32018-04-26 16:09:12 -070012199 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012200 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012201 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012202 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012203 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012204 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012205 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012206
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012207 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12208
Peter Feinerc95ba922016-08-17 09:36:47 -070012209 if (kvm_has_tsc_control)
12210 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012211
12212 if (enable_vpid) {
12213 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012214 * There is no direct mapping between vpid02 and vpid12, the
12215 * vpid02 is per-vCPU for L0 and reused while the value of
12216 * vpid12 is changed w/ one invvpid during nested vmentry.
12217 * The vpid12 is allocated by L1 for L2, so it will not
12218 * influence global bitmap(for vpid01 and vpid02 allocation)
12219 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012220 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012221 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012222 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12223 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012224 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012225 }
12226 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012227 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012228 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012229 }
12230
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012231 if (enable_pml) {
12232 /*
12233 * Conceptually we want to copy the PML address and index from
12234 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12235 * since we always flush the log on each vmexit, this happens
12236 * to be equivalent to simply resetting the fields in vmcs02.
12237 */
12238 ASSERT(vmx->pml_pg);
12239 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12240 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12241 }
12242
Nadav Har'El155a97a2013-08-05 11:07:16 +030012243 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012244 if (nested_ept_init_mmu_context(vcpu)) {
12245 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12246 return 1;
12247 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012248 } else if (nested_cpu_has2(vmcs12,
12249 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012250 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012251 }
12252
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012253 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012254 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12255 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012256 * The CR0_READ_SHADOW is what L2 should have expected to read given
12257 * the specifications by L1; It's not enough to take
12258 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12259 * have more bits than L1 expected.
12260 */
12261 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12262 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12263
12264 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12265 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12266
Jim Mattson6514dc32018-04-26 16:09:12 -070012267 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012268 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012269 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12270 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12271 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12272 else
12273 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12274 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12275 vmx_set_efer(vcpu, vcpu->arch.efer);
12276
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012277 /*
12278 * Guest state is invalid and unrestricted guest is disabled,
12279 * which means L1 attempted VMEntry to L2 with invalid state.
12280 * Fail the VMEntry.
12281 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012282 if (vmx->emulation_required) {
12283 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012284 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012285 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012286
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012287 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012288 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012289 entry_failure_code))
12290 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012291
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012292 if (!enable_ept)
12293 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12294
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012295 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12296 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012297 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012298}
12299
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012300static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12301{
12302 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12303 nested_cpu_has_virtual_nmis(vmcs12))
12304 return -EINVAL;
12305
12306 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12307 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12308 return -EINVAL;
12309
12310 return 0;
12311}
12312
Jim Mattsonca0bde22016-11-30 12:03:46 -080012313static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12314{
12315 struct vcpu_vmx *vmx = to_vmx(vcpu);
12316
12317 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12318 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12319 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12320
Jim Mattson56a20512017-07-06 16:33:06 -070012321 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12322 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12323
Jim Mattsonca0bde22016-11-30 12:03:46 -080012324 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12325 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12326
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012327 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12328 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12329
Jim Mattson712b12d2017-08-24 13:24:47 -070012330 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12331 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12332
Jim Mattsonca0bde22016-11-30 12:03:46 -080012333 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12334 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12335
12336 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12337 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12338
Bandan Dasc5f983f2017-05-05 15:25:14 -040012339 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12340 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12341
Liran Alona8a7c022018-06-23 02:35:06 +030012342 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12343 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12344
Jim Mattsonca0bde22016-11-30 12:03:46 -080012345 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012346 vmx->nested.msrs.procbased_ctls_low,
12347 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012348 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12349 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012350 vmx->nested.msrs.secondary_ctls_low,
12351 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012352 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012353 vmx->nested.msrs.pinbased_ctls_low,
12354 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012355 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012356 vmx->nested.msrs.exit_ctls_low,
12357 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012358 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012359 vmx->nested.msrs.entry_ctls_low,
12360 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012361 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12362
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012363 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012364 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12365
Bandan Das41ab9372017-08-03 15:54:43 -040012366 if (nested_cpu_has_vmfunc(vmcs12)) {
12367 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012368 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012369 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12370
12371 if (nested_cpu_has_eptp_switching(vmcs12)) {
12372 if (!nested_cpu_has_ept(vmcs12) ||
12373 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12374 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12375 }
12376 }
Bandan Das27c42a12017-08-03 15:54:42 -040012377
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012378 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12379 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12380
Jim Mattsonca0bde22016-11-30 12:03:46 -080012381 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12382 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12383 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12384 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12385
Marc Orr04473782018-06-20 17:21:29 -070012386 /*
12387 * From the Intel SDM, volume 3:
12388 * Fields relevant to VM-entry event injection must be set properly.
12389 * These fields are the VM-entry interruption-information field, the
12390 * VM-entry exception error code, and the VM-entry instruction length.
12391 */
12392 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12393 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12394 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12395 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12396 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12397 bool should_have_error_code;
12398 bool urg = nested_cpu_has2(vmcs12,
12399 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12400 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12401
12402 /* VM-entry interruption-info field: interruption type */
12403 if (intr_type == INTR_TYPE_RESERVED ||
12404 (intr_type == INTR_TYPE_OTHER_EVENT &&
12405 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12406 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12407
12408 /* VM-entry interruption-info field: vector */
12409 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12410 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12411 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12412 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12413
12414 /* VM-entry interruption-info field: deliver error code */
12415 should_have_error_code =
12416 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12417 x86_exception_has_error_code(vector);
12418 if (has_error_code != should_have_error_code)
12419 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12420
12421 /* VM-entry exception error code */
12422 if (has_error_code &&
12423 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12424 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12425
12426 /* VM-entry interruption-info field: reserved bits */
12427 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12428 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12429
12430 /* VM-entry instruction length */
12431 switch (intr_type) {
12432 case INTR_TYPE_SOFT_EXCEPTION:
12433 case INTR_TYPE_SOFT_INTR:
12434 case INTR_TYPE_PRIV_SW_EXCEPTION:
12435 if ((vmcs12->vm_entry_instruction_len > 15) ||
12436 (vmcs12->vm_entry_instruction_len == 0 &&
12437 !nested_cpu_has_zero_length_injection(vcpu)))
12438 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12439 }
12440 }
12441
Jim Mattsonca0bde22016-11-30 12:03:46 -080012442 return 0;
12443}
12444
Liran Alonf145d902018-06-23 02:35:07 +030012445static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12446 struct vmcs12 *vmcs12)
12447{
12448 int r;
12449 struct page *page;
12450 struct vmcs12 *shadow;
12451
12452 if (vmcs12->vmcs_link_pointer == -1ull)
12453 return 0;
12454
12455 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12456 return -EINVAL;
12457
12458 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12459 if (is_error_page(page))
12460 return -EINVAL;
12461
12462 r = 0;
12463 shadow = kmap(page);
12464 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12465 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12466 r = -EINVAL;
12467 kunmap(page);
12468 kvm_release_page_clean(page);
12469 return r;
12470}
12471
Jim Mattsonca0bde22016-11-30 12:03:46 -080012472static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12473 u32 *exit_qual)
12474{
12475 bool ia32e;
12476
12477 *exit_qual = ENTRY_FAIL_DEFAULT;
12478
12479 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12480 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12481 return 1;
12482
Liran Alonf145d902018-06-23 02:35:07 +030012483 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012484 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12485 return 1;
12486 }
12487
12488 /*
12489 * If the load IA32_EFER VM-entry control is 1, the following checks
12490 * are performed on the field for the IA32_EFER MSR:
12491 * - Bits reserved in the IA32_EFER MSR must be 0.
12492 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12493 * the IA-32e mode guest VM-exit control. It must also be identical
12494 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12495 * CR0.PG) is 1.
12496 */
12497 if (to_vmx(vcpu)->nested.nested_run_pending &&
12498 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12499 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12500 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12501 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12502 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12503 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12504 return 1;
12505 }
12506
12507 /*
12508 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12509 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12510 * the values of the LMA and LME bits in the field must each be that of
12511 * the host address-space size VM-exit control.
12512 */
12513 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12514 ia32e = (vmcs12->vm_exit_controls &
12515 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12516 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12517 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12518 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12519 return 1;
12520 }
12521
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012522 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12523 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12524 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12525 return 1;
12526
Jim Mattsonca0bde22016-11-30 12:03:46 -080012527 return 0;
12528}
12529
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012530/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012531 * If exit_qual is NULL, this is being called from state restore (either RSM
12532 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012533 */
12534static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012535{
12536 struct vcpu_vmx *vmx = to_vmx(vcpu);
12537 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012538 bool from_vmentry = !!exit_qual;
12539 u32 dummy_exit_qual;
Liran Alonb5861e52018-09-03 15:20:22 +030012540 u32 vmcs01_cpu_exec_ctrl;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012541 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012542
Liran Alonb5861e52018-09-03 15:20:22 +030012543 vmcs01_cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
12544
Jim Mattson858e25c2016-11-30 12:03:47 -080012545 enter_guest_mode(vcpu);
12546
12547 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12548 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12549
Jim Mattsonde3a0022017-11-27 17:22:25 -060012550 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012551 vmx_segment_cache_clear(vmx);
12552
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012553 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12554 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12555
12556 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012557 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012558 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012559
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012560 if (from_vmentry) {
12561 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012562
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012563 r = EXIT_REASON_MSR_LOAD_FAIL;
12564 *exit_qual = nested_vmx_load_msr(vcpu,
12565 vmcs12->vm_entry_msr_load_addr,
12566 vmcs12->vm_entry_msr_load_count);
12567 if (*exit_qual)
12568 goto fail;
12569 } else {
12570 /*
12571 * The MMU is not initialized to point at the right entities yet and
12572 * "get pages" would need to read data from the guest (i.e. we will
12573 * need to perform gpa to hpa translation). Request a call
12574 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12575 * have already been set at vmentry time and should not be reset.
12576 */
12577 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12578 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012579
Jim Mattson858e25c2016-11-30 12:03:47 -080012580 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012581 * If L1 had a pending IRQ/NMI until it executed
12582 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12583 * disallowed (e.g. interrupts disabled), L0 needs to
12584 * evaluate if this pending event should cause an exit from L2
12585 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12586 * intercept EXTERNAL_INTERRUPT).
12587 *
12588 * Usually this would be handled by L0 requesting a
12589 * IRQ/NMI window by setting VMCS accordingly. However,
12590 * this setting was done on VMCS01 and now VMCS02 is active
12591 * instead. Thus, we force L0 to perform pending event
12592 * evaluation by requesting a KVM_REQ_EVENT.
12593 */
12594 if (vmcs01_cpu_exec_ctrl &
12595 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING)) {
12596 kvm_make_request(KVM_REQ_EVENT, vcpu);
12597 }
12598
12599 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012600 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12601 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12602 * returned as far as L1 is concerned. It will only return (and set
12603 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12604 */
12605 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012606
12607fail:
12608 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12609 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12610 leave_guest_mode(vcpu);
12611 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012612 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012613}
12614
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012615/*
12616 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12617 * for running an L2 nested guest.
12618 */
12619static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12620{
12621 struct vmcs12 *vmcs12;
12622 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012623 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012624 u32 exit_qual;
12625 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012626
Kyle Hueyeb277562016-11-29 12:40:39 -080012627 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012628 return 1;
12629
Kyle Hueyeb277562016-11-29 12:40:39 -080012630 if (!nested_vmx_check_vmcs12(vcpu))
12631 goto out;
12632
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012633 vmcs12 = get_vmcs12(vcpu);
12634
Liran Alona6192d42018-06-23 02:35:04 +030012635 /*
12636 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12637 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12638 * rather than RFLAGS.ZF, and no error number is stored to the
12639 * VM-instruction error field.
12640 */
12641 if (vmcs12->hdr.shadow_vmcs) {
12642 nested_vmx_failInvalid(vcpu);
12643 goto out;
12644 }
12645
Abel Gordon012f83c2013-04-18 14:39:25 +030012646 if (enable_shadow_vmcs)
12647 copy_shadow_to_vmcs12(vmx);
12648
Nadav Har'El7c177932011-05-25 23:12:04 +030012649 /*
12650 * The nested entry process starts with enforcing various prerequisites
12651 * on vmcs12 as required by the Intel SDM, and act appropriately when
12652 * they fail: As the SDM explains, some conditions should cause the
12653 * instruction to fail, while others will cause the instruction to seem
12654 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12655 * To speed up the normal (success) code path, we should avoid checking
12656 * for misconfigurations which will anyway be caught by the processor
12657 * when using the merged vmcs02.
12658 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012659 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12660 nested_vmx_failValid(vcpu,
12661 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12662 goto out;
12663 }
12664
Nadav Har'El7c177932011-05-25 23:12:04 +030012665 if (vmcs12->launch_state == launch) {
12666 nested_vmx_failValid(vcpu,
12667 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12668 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012669 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012670 }
12671
Jim Mattsonca0bde22016-11-30 12:03:46 -080012672 ret = check_vmentry_prereqs(vcpu, vmcs12);
12673 if (ret) {
12674 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012675 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012676 }
12677
Nadav Har'El7c177932011-05-25 23:12:04 +030012678 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012679 * After this point, the trap flag no longer triggers a singlestep trap
12680 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12681 * This is not 100% correct; for performance reasons, we delegate most
12682 * of the checks on host state to the processor. If those fail,
12683 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012684 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012685 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012686
Jim Mattsonca0bde22016-11-30 12:03:46 -080012687 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12688 if (ret) {
12689 nested_vmx_entry_failure(vcpu, vmcs12,
12690 EXIT_REASON_INVALID_STATE, exit_qual);
12691 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012692 }
12693
12694 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012695 * We're finally done with prerequisite checking, and can start with
12696 * the nested entry.
12697 */
12698
Jim Mattson6514dc32018-04-26 16:09:12 -070012699 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012700 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012701 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012702 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012703 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012704 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012705 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012706
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012707 /* Hide L1D cache contents from the nested guest. */
12708 vmx->vcpu.arch.l1tf_flush_l1d = true;
12709
Chao Gao135a06c2018-02-11 10:06:30 +080012710 /*
Liran Alon61ada742018-06-23 02:35:08 +030012711 * Must happen outside of enter_vmx_non_root_mode() as it will
12712 * also be used as part of restoring nVMX state for
12713 * snapshot restore (migration).
12714 *
12715 * In this flow, it is assumed that vmcs12 cache was
12716 * trasferred as part of captured nVMX state and should
12717 * therefore not be read from guest memory (which may not
12718 * exist on destination host yet).
12719 */
12720 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12721
12722 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012723 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12724 * by event injection, halt vcpu.
12725 */
12726 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012727 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12728 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012729 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012730 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012731 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012732
12733out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012734 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012735}
12736
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012737/*
12738 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12739 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12740 * This function returns the new value we should put in vmcs12.guest_cr0.
12741 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12742 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12743 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12744 * didn't trap the bit, because if L1 did, so would L0).
12745 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12746 * been modified by L2, and L1 knows it. So just leave the old value of
12747 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12748 * isn't relevant, because if L0 traps this bit it can set it to anything.
12749 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12750 * changed these bits, and therefore they need to be updated, but L0
12751 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12752 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12753 */
12754static inline unsigned long
12755vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12756{
12757 return
12758 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12759 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12760 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12761 vcpu->arch.cr0_guest_owned_bits));
12762}
12763
12764static inline unsigned long
12765vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12766{
12767 return
12768 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12769 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12770 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12771 vcpu->arch.cr4_guest_owned_bits));
12772}
12773
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012774static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12775 struct vmcs12 *vmcs12)
12776{
12777 u32 idt_vectoring;
12778 unsigned int nr;
12779
Wanpeng Li664f8e22017-08-24 03:35:09 -070012780 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012781 nr = vcpu->arch.exception.nr;
12782 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12783
12784 if (kvm_exception_is_soft(nr)) {
12785 vmcs12->vm_exit_instruction_len =
12786 vcpu->arch.event_exit_inst_len;
12787 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12788 } else
12789 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12790
12791 if (vcpu->arch.exception.has_error_code) {
12792 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12793 vmcs12->idt_vectoring_error_code =
12794 vcpu->arch.exception.error_code;
12795 }
12796
12797 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012798 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012799 vmcs12->idt_vectoring_info_field =
12800 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012801 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012802 nr = vcpu->arch.interrupt.nr;
12803 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12804
12805 if (vcpu->arch.interrupt.soft) {
12806 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12807 vmcs12->vm_entry_instruction_len =
12808 vcpu->arch.event_exit_inst_len;
12809 } else
12810 idt_vectoring |= INTR_TYPE_EXT_INTR;
12811
12812 vmcs12->idt_vectoring_info_field = idt_vectoring;
12813 }
12814}
12815
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012816static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12817{
12818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012819 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012820 bool block_nested_events =
12821 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012822
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012823 if (vcpu->arch.exception.pending &&
12824 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012825 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012826 return -EBUSY;
12827 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012828 return 0;
12829 }
12830
Jan Kiszkaf41245002014-03-07 20:03:13 +010012831 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12832 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012833 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012834 return -EBUSY;
12835 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12836 return 0;
12837 }
12838
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012839 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012840 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012841 return -EBUSY;
12842 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12843 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12844 INTR_INFO_VALID_MASK, 0);
12845 /*
12846 * The NMI-triggered VM exit counts as injection:
12847 * clear this one and block further NMIs.
12848 */
12849 vcpu->arch.nmi_pending = 0;
12850 vmx_set_nmi_mask(vcpu, true);
12851 return 0;
12852 }
12853
12854 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12855 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012856 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012857 return -EBUSY;
12858 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012859 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012860 }
12861
David Hildenbrand6342c502017-01-25 11:58:58 +010012862 vmx_complete_nested_posted_interrupt(vcpu);
12863 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012864}
12865
Jan Kiszkaf41245002014-03-07 20:03:13 +010012866static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12867{
12868 ktime_t remaining =
12869 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12870 u64 value;
12871
12872 if (ktime_to_ns(remaining) <= 0)
12873 return 0;
12874
12875 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12876 do_div(value, 1000000);
12877 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12878}
12879
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012880/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012881 * Update the guest state fields of vmcs12 to reflect changes that
12882 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12883 * VM-entry controls is also updated, since this is really a guest
12884 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012885 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012886static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012887{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012888 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12889 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12890
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012891 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12892 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12893 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12894
12895 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12896 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12897 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12898 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12899 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12900 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12901 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12902 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12903 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12904 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12905 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12906 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12907 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12908 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12909 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12910 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12911 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12912 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12913 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12914 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12915 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12916 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12917 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12918 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12919 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12920 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12921 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12922 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12923 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12924 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12925 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12926 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12927 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12928 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12929 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12930 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12931
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012932 vmcs12->guest_interruptibility_info =
12933 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12934 vmcs12->guest_pending_dbg_exceptions =
12935 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012936 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12937 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12938 else
12939 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012940
Jan Kiszkaf41245002014-03-07 20:03:13 +010012941 if (nested_cpu_has_preemption_timer(vmcs12)) {
12942 if (vmcs12->vm_exit_controls &
12943 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12944 vmcs12->vmx_preemption_timer_value =
12945 vmx_get_preemption_timer_value(vcpu);
12946 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12947 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012948
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012949 /*
12950 * In some cases (usually, nested EPT), L2 is allowed to change its
12951 * own CR3 without exiting. If it has changed it, we must keep it.
12952 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
12953 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
12954 *
12955 * Additionally, restore L2's PDPTR to vmcs12.
12956 */
12957 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010012958 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012959 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
12960 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
12961 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
12962 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
12963 }
12964
Jim Mattsond281e132017-06-01 12:44:46 -070012965 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030012966
Wincy Van608406e2015-02-03 23:57:51 +080012967 if (nested_cpu_has_vid(vmcs12))
12968 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
12969
Jan Kiszkac18911a2013-03-13 16:06:41 +010012970 vmcs12->vm_entry_controls =
12971 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020012972 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010012973
Jan Kiszka2996fca2014-06-16 13:59:43 +020012974 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
12975 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
12976 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12977 }
12978
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012979 /* TODO: These cannot have changed unless we have MSR bitmaps and
12980 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020012981 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012982 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020012983 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
12984 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012985 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
12986 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
12987 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010012988 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010012989 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012990}
12991
12992/*
12993 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
12994 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
12995 * and this function updates it to reflect the changes to the guest state while
12996 * L2 was running (and perhaps made some exits which were handled directly by L0
12997 * without going back to L1), and to reflect the exit reason.
12998 * Note that we do not have to copy here all VMCS fields, just those that
12999 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13000 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13001 * which already writes to vmcs12 directly.
13002 */
13003static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13004 u32 exit_reason, u32 exit_intr_info,
13005 unsigned long exit_qualification)
13006{
13007 /* update guest state fields: */
13008 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013009
13010 /* update exit information fields: */
13011
Jan Kiszka533558b2014-01-04 18:47:20 +010013012 vmcs12->vm_exit_reason = exit_reason;
13013 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013014 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013015
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013016 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013017 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13018 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13019
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013020 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013021 vmcs12->launch_state = 1;
13022
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013023 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13024 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013025 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013026
13027 /*
13028 * Transfer the event that L0 or L1 may wanted to inject into
13029 * L2 to IDT_VECTORING_INFO_FIELD.
13030 */
13031 vmcs12_save_pending_event(vcpu, vmcs12);
13032 }
13033
13034 /*
13035 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13036 * preserved above and would only end up incorrectly in L1.
13037 */
13038 vcpu->arch.nmi_injected = false;
13039 kvm_clear_exception_queue(vcpu);
13040 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013041}
13042
Wanpeng Li5af41572017-11-05 16:54:49 -080013043static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
13044 struct vmcs12 *vmcs12)
13045{
13046 u32 entry_failure_code;
13047
13048 nested_ept_uninit_mmu_context(vcpu);
13049
13050 /*
13051 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13052 * couldn't have changed.
13053 */
13054 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13055 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13056
13057 if (!enable_ept)
13058 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13059}
13060
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013061/*
13062 * A part of what we need to when the nested L2 guest exits and we want to
13063 * run its L1 parent, is to reset L1's guest state to the host state specified
13064 * in vmcs12.
13065 * This function is to be called not only on normal nested exit, but also on
13066 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13067 * Failures During or After Loading Guest State").
13068 * This function should be called when the active VMCS is L1's (vmcs01).
13069 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013070static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13071 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013072{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013073 struct kvm_segment seg;
13074
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013075 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13076 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013077 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013078 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13079 else
13080 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13081 vmx_set_efer(vcpu, vcpu->arch.efer);
13082
13083 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13084 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013085 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013086 /*
13087 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013088 * actually changed, because vmx_set_cr0 refers to efer set above.
13089 *
13090 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13091 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013092 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013093 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013094 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013095
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013096 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013097 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013098 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013099
Wanpeng Li5af41572017-11-05 16:54:49 -080013100 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013101
Liran Alon6f1e03b2018-05-22 17:16:14 +030013102 /*
13103 * If vmcs01 don't use VPID, CPU flushes TLB on every
13104 * VMEntry/VMExit. Thus, no need to flush TLB.
13105 *
13106 * If vmcs12 uses VPID, TLB entries populated by L2 are
13107 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13108 * with vmx->vpid. Thus, no need to flush TLB.
13109 *
13110 * Therefore, flush TLB only in case vmcs01 uses VPID and
13111 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13112 * are both tagged with vmx->vpid.
13113 */
13114 if (enable_vpid &&
13115 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080013116 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013117 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013118
13119 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13120 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13121 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13122 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13123 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d5512017-10-11 16:54:42 +020013124 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13125 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013126
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013127 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13128 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13129 vmcs_write64(GUEST_BNDCFGS, 0);
13130
Jan Kiszka44811c02013-08-04 17:17:27 +020013131 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013132 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013133 vcpu->arch.pat = vmcs12->host_ia32_pat;
13134 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013135 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13136 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13137 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013138
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013139 /* Set L1 segment info according to Intel SDM
13140 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13141 seg = (struct kvm_segment) {
13142 .base = 0,
13143 .limit = 0xFFFFFFFF,
13144 .selector = vmcs12->host_cs_selector,
13145 .type = 11,
13146 .present = 1,
13147 .s = 1,
13148 .g = 1
13149 };
13150 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13151 seg.l = 1;
13152 else
13153 seg.db = 1;
13154 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13155 seg = (struct kvm_segment) {
13156 .base = 0,
13157 .limit = 0xFFFFFFFF,
13158 .type = 3,
13159 .present = 1,
13160 .s = 1,
13161 .db = 1,
13162 .g = 1
13163 };
13164 seg.selector = vmcs12->host_ds_selector;
13165 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13166 seg.selector = vmcs12->host_es_selector;
13167 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13168 seg.selector = vmcs12->host_ss_selector;
13169 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13170 seg.selector = vmcs12->host_fs_selector;
13171 seg.base = vmcs12->host_fs_base;
13172 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13173 seg.selector = vmcs12->host_gs_selector;
13174 seg.base = vmcs12->host_gs_base;
13175 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13176 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013177 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013178 .limit = 0x67,
13179 .selector = vmcs12->host_tr_selector,
13180 .type = 11,
13181 .present = 1
13182 };
13183 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13184
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013185 kvm_set_dr(vcpu, 7, 0x400);
13186 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013187
Wincy Van3af18d92015-02-03 23:49:31 +080013188 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013189 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013190
Wincy Vanff651cb2014-12-11 08:52:58 +030013191 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13192 vmcs12->vm_exit_msr_load_count))
13193 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013194}
13195
13196/*
13197 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13198 * and modify vmcs12 to make it see what it would expect to see there if
13199 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13200 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013201static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13202 u32 exit_intr_info,
13203 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013204{
13205 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013206 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13207
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013208 /* trying to cancel vmlaunch/vmresume is a bug */
13209 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13210
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013211 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013212 * The only expected VM-instruction error is "VM entry with
13213 * invalid control field(s)." Anything else indicates a
13214 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013215 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013216 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13217 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13218
13219 leave_guest_mode(vcpu);
13220
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013221 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13222 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13223
Jim Mattson4f350c62017-09-14 16:31:44 -070013224 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013225 if (exit_reason == -1)
13226 sync_vmcs12(vcpu, vmcs12);
13227 else
13228 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13229 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013230
Liran Alon61ada742018-06-23 02:35:08 +030013231 /*
13232 * Must happen outside of sync_vmcs12() as it will
13233 * also be used to capture vmcs12 cache as part of
13234 * capturing nVMX state for snapshot (migration).
13235 *
13236 * Otherwise, this flush will dirty guest memory at a
13237 * point it is already assumed by user-space to be
13238 * immutable.
13239 */
13240 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13241
Jim Mattson4f350c62017-09-14 16:31:44 -070013242 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13243 vmcs12->vm_exit_msr_store_count))
13244 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013245 }
13246
Jim Mattson4f350c62017-09-14 16:31:44 -070013247 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013248 vm_entry_controls_reset_shadow(vmx);
13249 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013250 vmx_segment_cache_clear(vmx);
13251
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013252 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013253 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13254 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013255 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013256 if (vmx->hv_deadline_tsc == -1)
13257 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13258 PIN_BASED_VMX_PREEMPTION_TIMER);
13259 else
13260 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13261 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070013262 if (kvm_has_tsc_control)
13263 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013264
Jim Mattson8d860bb2018-05-09 16:56:05 -040013265 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13266 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13267 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013268 } else if (!nested_cpu_has_ept(vmcs12) &&
13269 nested_cpu_has2(vmcs12,
13270 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013271 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013272 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013273
13274 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13275 vmx->host_rsp = 0;
13276
13277 /* Unpin physical memory we referred to in vmcs02 */
13278 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013279 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013280 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013281 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013282 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013283 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013284 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013285 }
Wincy Van705699a2015-02-03 23:58:17 +080013286 if (vmx->nested.pi_desc_page) {
13287 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013288 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013289 vmx->nested.pi_desc_page = NULL;
13290 vmx->nested.pi_desc = NULL;
13291 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013292
13293 /*
Tang Chen38b99172014-09-24 15:57:54 +080013294 * We are now running in L2, mmu_notifier will force to reload the
13295 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13296 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013297 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013298
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013299 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013300 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013301
13302 /* in case we halted in L2 */
13303 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013304
13305 if (likely(!vmx->fail)) {
13306 /*
13307 * TODO: SDM says that with acknowledge interrupt on
13308 * exit, bit 31 of the VM-exit interrupt information
13309 * (valid interrupt) is always set to 1 on
13310 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13311 * need kvm_cpu_has_interrupt(). See the commit
13312 * message for details.
13313 */
13314 if (nested_exit_intr_ack_set(vcpu) &&
13315 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13316 kvm_cpu_has_interrupt(vcpu)) {
13317 int irq = kvm_cpu_get_interrupt(vcpu);
13318 WARN_ON(irq < 0);
13319 vmcs12->vm_exit_intr_info = irq |
13320 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13321 }
13322
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013323 if (exit_reason != -1)
13324 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13325 vmcs12->exit_qualification,
13326 vmcs12->idt_vectoring_info_field,
13327 vmcs12->vm_exit_intr_info,
13328 vmcs12->vm_exit_intr_error_code,
13329 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013330
13331 load_vmcs12_host_state(vcpu, vmcs12);
13332
13333 return;
13334 }
13335
13336 /*
13337 * After an early L2 VM-entry failure, we're now back
13338 * in L1 which thinks it just finished a VMLAUNCH or
13339 * VMRESUME instruction, so we need to set the failure
13340 * flag and the VM-instruction error field of the VMCS
13341 * accordingly.
13342 */
13343 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013344
13345 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13346
Jim Mattson4f350c62017-09-14 16:31:44 -070013347 /*
13348 * The emulated instruction was already skipped in
13349 * nested_vmx_run, but the updated RIP was never
13350 * written back to the vmcs01.
13351 */
13352 skip_emulated_instruction(vcpu);
13353 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013354}
13355
Nadav Har'El7c177932011-05-25 23:12:04 +030013356/*
Jan Kiszka42124922014-01-04 18:47:19 +010013357 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13358 */
13359static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13360{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013361 if (is_guest_mode(vcpu)) {
13362 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013363 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013364 }
Jan Kiszka42124922014-01-04 18:47:19 +010013365 free_nested(to_vmx(vcpu));
13366}
13367
13368/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013369 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13370 * 23.7 "VM-entry failures during or after loading guest state" (this also
13371 * lists the acceptable exit-reason and exit-qualification parameters).
13372 * It should only be called before L2 actually succeeded to run, and when
13373 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13374 */
13375static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13376 struct vmcs12 *vmcs12,
13377 u32 reason, unsigned long qualification)
13378{
13379 load_vmcs12_host_state(vcpu, vmcs12);
13380 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13381 vmcs12->exit_qualification = qualification;
13382 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013383 if (enable_shadow_vmcs)
13384 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013385}
13386
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013387static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13388 struct x86_instruction_info *info,
13389 enum x86_intercept_stage stage)
13390{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013391 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13392 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13393
13394 /*
13395 * RDPID causes #UD if disabled through secondary execution controls.
13396 * Because it is marked as EmulateOnUD, we need to intercept it here.
13397 */
13398 if (info->intercept == x86_intercept_rdtscp &&
13399 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13400 ctxt->exception.vector = UD_VECTOR;
13401 ctxt->exception.error_code_valid = false;
13402 return X86EMUL_PROPAGATE_FAULT;
13403 }
13404
13405 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013406 return X86EMUL_CONTINUE;
13407}
13408
Yunhong Jiang64672c92016-06-13 14:19:59 -070013409#ifdef CONFIG_X86_64
13410/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13411static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13412 u64 divisor, u64 *result)
13413{
13414 u64 low = a << shift, high = a >> (64 - shift);
13415
13416 /* To avoid the overflow on divq */
13417 if (high >= divisor)
13418 return 1;
13419
13420 /* Low hold the result, high hold rem which is discarded */
13421 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13422 "rm" (divisor), "0" (low), "1" (high));
13423 *result = low;
13424
13425 return 0;
13426}
13427
13428static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13429{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013430 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013431 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013432
13433 if (kvm_mwait_in_guest(vcpu->kvm))
13434 return -EOPNOTSUPP;
13435
13436 vmx = to_vmx(vcpu);
13437 tscl = rdtsc();
13438 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13439 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013440 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13441
13442 if (delta_tsc > lapic_timer_advance_cycles)
13443 delta_tsc -= lapic_timer_advance_cycles;
13444 else
13445 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013446
13447 /* Convert to host delta tsc if tsc scaling is enabled */
13448 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13449 u64_shl_div_u64(delta_tsc,
13450 kvm_tsc_scaling_ratio_frac_bits,
13451 vcpu->arch.tsc_scaling_ratio,
13452 &delta_tsc))
13453 return -ERANGE;
13454
13455 /*
13456 * If the delta tsc can't fit in the 32 bit after the multi shift,
13457 * we can't use the preemption timer.
13458 * It's possible that it fits on later vmentries, but checking
13459 * on every vmentry is costly so we just use an hrtimer.
13460 */
13461 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13462 return -ERANGE;
13463
13464 vmx->hv_deadline_tsc = tscl + delta_tsc;
13465 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
13466 PIN_BASED_VMX_PREEMPTION_TIMER);
Wanpeng Lic8533542017-06-29 06:28:09 -070013467
13468 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013469}
13470
13471static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13472{
13473 struct vcpu_vmx *vmx = to_vmx(vcpu);
13474 vmx->hv_deadline_tsc = -1;
13475 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
13476 PIN_BASED_VMX_PREEMPTION_TIMER);
13477}
13478#endif
13479
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013480static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013481{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013482 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013483 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013484}
13485
Kai Huang843e4332015-01-28 10:54:28 +080013486static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13487 struct kvm_memory_slot *slot)
13488{
13489 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13490 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13491}
13492
13493static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13494 struct kvm_memory_slot *slot)
13495{
13496 kvm_mmu_slot_set_dirty(kvm, slot);
13497}
13498
13499static void vmx_flush_log_dirty(struct kvm *kvm)
13500{
13501 kvm_flush_pml_buffers(kvm);
13502}
13503
Bandan Dasc5f983f2017-05-05 15:25:14 -040013504static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13505{
13506 struct vmcs12 *vmcs12;
13507 struct vcpu_vmx *vmx = to_vmx(vcpu);
13508 gpa_t gpa;
13509 struct page *page = NULL;
13510 u64 *pml_address;
13511
13512 if (is_guest_mode(vcpu)) {
13513 WARN_ON_ONCE(vmx->nested.pml_full);
13514
13515 /*
13516 * Check if PML is enabled for the nested guest.
13517 * Whether eptp bit 6 is set is already checked
13518 * as part of A/D emulation.
13519 */
13520 vmcs12 = get_vmcs12(vcpu);
13521 if (!nested_cpu_has_pml(vmcs12))
13522 return 0;
13523
Dan Carpenter47698862017-05-10 22:43:17 +030013524 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013525 vmx->nested.pml_full = true;
13526 return 1;
13527 }
13528
13529 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13530
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013531 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13532 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013533 return 0;
13534
13535 pml_address = kmap(page);
13536 pml_address[vmcs12->guest_pml_index--] = gpa;
13537 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013538 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013539 }
13540
13541 return 0;
13542}
13543
Kai Huang843e4332015-01-28 10:54:28 +080013544static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13545 struct kvm_memory_slot *memslot,
13546 gfn_t offset, unsigned long mask)
13547{
13548 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13549}
13550
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013551static void __pi_post_block(struct kvm_vcpu *vcpu)
13552{
13553 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13554 struct pi_desc old, new;
13555 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013556
13557 do {
13558 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013559 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13560 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013561
13562 dest = cpu_physical_id(vcpu->cpu);
13563
13564 if (x2apic_enabled())
13565 new.ndst = dest;
13566 else
13567 new.ndst = (dest << 8) & 0xFF00;
13568
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013569 /* set 'NV' to 'notification vector' */
13570 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013571 } while (cmpxchg64(&pi_desc->control, old.control,
13572 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013573
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013574 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13575 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013576 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013577 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013578 vcpu->pre_pcpu = -1;
13579 }
13580}
13581
Feng Wuefc64402015-09-18 22:29:51 +080013582/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013583 * This routine does the following things for vCPU which is going
13584 * to be blocked if VT-d PI is enabled.
13585 * - Store the vCPU to the wakeup list, so when interrupts happen
13586 * we can find the right vCPU to wake up.
13587 * - Change the Posted-interrupt descriptor as below:
13588 * 'NDST' <-- vcpu->pre_pcpu
13589 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13590 * - If 'ON' is set during this process, which means at least one
13591 * interrupt is posted for this vCPU, we cannot block it, in
13592 * this case, return 1, otherwise, return 0.
13593 *
13594 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013595static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013596{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013597 unsigned int dest;
13598 struct pi_desc old, new;
13599 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13600
13601 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013602 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13603 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013604 return 0;
13605
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013606 WARN_ON(irqs_disabled());
13607 local_irq_disable();
13608 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13609 vcpu->pre_pcpu = vcpu->cpu;
13610 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13611 list_add_tail(&vcpu->blocked_vcpu_list,
13612 &per_cpu(blocked_vcpu_on_cpu,
13613 vcpu->pre_pcpu));
13614 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13615 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013616
13617 do {
13618 old.control = new.control = pi_desc->control;
13619
Feng Wubf9f6ac2015-09-18 22:29:55 +080013620 WARN((pi_desc->sn == 1),
13621 "Warning: SN field of posted-interrupts "
13622 "is set before blocking\n");
13623
13624 /*
13625 * Since vCPU can be preempted during this process,
13626 * vcpu->cpu could be different with pre_pcpu, we
13627 * need to set pre_pcpu as the destination of wakeup
13628 * notification event, then we can find the right vCPU
13629 * to wakeup in wakeup handler if interrupts happen
13630 * when the vCPU is in blocked state.
13631 */
13632 dest = cpu_physical_id(vcpu->pre_pcpu);
13633
13634 if (x2apic_enabled())
13635 new.ndst = dest;
13636 else
13637 new.ndst = (dest << 8) & 0xFF00;
13638
13639 /* set 'NV' to 'wakeup vector' */
13640 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013641 } while (cmpxchg64(&pi_desc->control, old.control,
13642 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013643
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013644 /* We should not block the vCPU if an interrupt is posted for it. */
13645 if (pi_test_on(pi_desc) == 1)
13646 __pi_post_block(vcpu);
13647
13648 local_irq_enable();
13649 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013650}
13651
Yunhong Jiangbc225122016-06-13 14:19:58 -070013652static int vmx_pre_block(struct kvm_vcpu *vcpu)
13653{
13654 if (pi_pre_block(vcpu))
13655 return 1;
13656
Yunhong Jiang64672c92016-06-13 14:19:59 -070013657 if (kvm_lapic_hv_timer_in_use(vcpu))
13658 kvm_lapic_switch_to_sw_timer(vcpu);
13659
Yunhong Jiangbc225122016-06-13 14:19:58 -070013660 return 0;
13661}
13662
13663static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013664{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013665 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013666 return;
13667
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013668 WARN_ON(irqs_disabled());
13669 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013670 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013671 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013672}
13673
Yunhong Jiangbc225122016-06-13 14:19:58 -070013674static void vmx_post_block(struct kvm_vcpu *vcpu)
13675{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013676 if (kvm_x86_ops->set_hv_timer)
13677 kvm_lapic_switch_to_hv_timer(vcpu);
13678
Yunhong Jiangbc225122016-06-13 14:19:58 -070013679 pi_post_block(vcpu);
13680}
13681
Feng Wubf9f6ac2015-09-18 22:29:55 +080013682/*
Feng Wuefc64402015-09-18 22:29:51 +080013683 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13684 *
13685 * @kvm: kvm
13686 * @host_irq: host irq of the interrupt
13687 * @guest_irq: gsi of the interrupt
13688 * @set: set or unset PI
13689 * returns 0 on success, < 0 on failure
13690 */
13691static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13692 uint32_t guest_irq, bool set)
13693{
13694 struct kvm_kernel_irq_routing_entry *e;
13695 struct kvm_irq_routing_table *irq_rt;
13696 struct kvm_lapic_irq irq;
13697 struct kvm_vcpu *vcpu;
13698 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013699 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013700
13701 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013702 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13703 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013704 return 0;
13705
13706 idx = srcu_read_lock(&kvm->irq_srcu);
13707 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013708 if (guest_irq >= irq_rt->nr_rt_entries ||
13709 hlist_empty(&irq_rt->map[guest_irq])) {
13710 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13711 guest_irq, irq_rt->nr_rt_entries);
13712 goto out;
13713 }
Feng Wuefc64402015-09-18 22:29:51 +080013714
13715 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13716 if (e->type != KVM_IRQ_ROUTING_MSI)
13717 continue;
13718 /*
13719 * VT-d PI cannot support posting multicast/broadcast
13720 * interrupts to a vCPU, we still use interrupt remapping
13721 * for these kind of interrupts.
13722 *
13723 * For lowest-priority interrupts, we only support
13724 * those with single CPU as the destination, e.g. user
13725 * configures the interrupts via /proc/irq or uses
13726 * irqbalance to make the interrupts single-CPU.
13727 *
13728 * We will support full lowest-priority interrupt later.
13729 */
13730
Radim Krčmář371313132016-07-12 22:09:27 +020013731 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013732 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13733 /*
13734 * Make sure the IRTE is in remapped mode if
13735 * we don't handle it in posted mode.
13736 */
13737 ret = irq_set_vcpu_affinity(host_irq, NULL);
13738 if (ret < 0) {
13739 printk(KERN_INFO
13740 "failed to back to remapped mode, irq: %u\n",
13741 host_irq);
13742 goto out;
13743 }
13744
Feng Wuefc64402015-09-18 22:29:51 +080013745 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013746 }
Feng Wuefc64402015-09-18 22:29:51 +080013747
13748 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13749 vcpu_info.vector = irq.vector;
13750
hu huajun2698d822018-04-11 15:16:40 +080013751 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013752 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13753
13754 if (set)
13755 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2eb2017-09-18 09:56:49 +080013756 else
Feng Wuefc64402015-09-18 22:29:51 +080013757 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013758
13759 if (ret < 0) {
13760 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13761 __func__);
13762 goto out;
13763 }
13764 }
13765
13766 ret = 0;
13767out:
13768 srcu_read_unlock(&kvm->irq_srcu, idx);
13769 return ret;
13770}
13771
Ashok Rajc45dcc72016-06-22 14:59:56 +080013772static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13773{
13774 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13775 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13776 FEATURE_CONTROL_LMCE;
13777 else
13778 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13779 ~FEATURE_CONTROL_LMCE;
13780}
13781
Ladi Prosek72d7b372017-10-11 16:54:41 +020013782static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13783{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013784 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13785 if (to_vmx(vcpu)->nested.nested_run_pending)
13786 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013787 return 1;
13788}
13789
Ladi Prosek0234bf82017-10-11 16:54:40 +020013790static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13791{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013792 struct vcpu_vmx *vmx = to_vmx(vcpu);
13793
13794 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13795 if (vmx->nested.smm.guest_mode)
13796 nested_vmx_vmexit(vcpu, -1, 0, 0);
13797
13798 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13799 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013800 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013801 return 0;
13802}
13803
13804static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13805{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013806 struct vcpu_vmx *vmx = to_vmx(vcpu);
13807 int ret;
13808
13809 if (vmx->nested.smm.vmxon) {
13810 vmx->nested.vmxon = true;
13811 vmx->nested.smm.vmxon = false;
13812 }
13813
13814 if (vmx->nested.smm.guest_mode) {
13815 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013816 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013817 vcpu->arch.hflags |= HF_SMM_MASK;
13818 if (ret)
13819 return ret;
13820
13821 vmx->nested.smm.guest_mode = false;
13822 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013823 return 0;
13824}
13825
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013826static int enable_smi_window(struct kvm_vcpu *vcpu)
13827{
13828 return 0;
13829}
13830
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013831static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13832 struct kvm_nested_state __user *user_kvm_nested_state,
13833 u32 user_data_size)
13834{
13835 struct vcpu_vmx *vmx;
13836 struct vmcs12 *vmcs12;
13837 struct kvm_nested_state kvm_state = {
13838 .flags = 0,
13839 .format = 0,
13840 .size = sizeof(kvm_state),
13841 .vmx.vmxon_pa = -1ull,
13842 .vmx.vmcs_pa = -1ull,
13843 };
13844
13845 if (!vcpu)
13846 return kvm_state.size + 2 * VMCS12_SIZE;
13847
13848 vmx = to_vmx(vcpu);
13849 vmcs12 = get_vmcs12(vcpu);
13850 if (nested_vmx_allowed(vcpu) &&
13851 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13852 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13853 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13854
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013855 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013856 kvm_state.size += VMCS12_SIZE;
13857
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013858 if (is_guest_mode(vcpu) &&
13859 nested_cpu_has_shadow_vmcs(vmcs12) &&
13860 vmcs12->vmcs_link_pointer != -1ull)
13861 kvm_state.size += VMCS12_SIZE;
13862 }
13863
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013864 if (vmx->nested.smm.vmxon)
13865 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13866
13867 if (vmx->nested.smm.guest_mode)
13868 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13869
13870 if (is_guest_mode(vcpu)) {
13871 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13872
13873 if (vmx->nested.nested_run_pending)
13874 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13875 }
13876 }
13877
13878 if (user_data_size < kvm_state.size)
13879 goto out;
13880
13881 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13882 return -EFAULT;
13883
13884 if (vmx->nested.current_vmptr == -1ull)
13885 goto out;
13886
13887 /*
13888 * When running L2, the authoritative vmcs12 state is in the
13889 * vmcs02. When running L1, the authoritative vmcs12 state is
13890 * in the shadow vmcs linked to vmcs01, unless
13891 * sync_shadow_vmcs is set, in which case, the authoritative
13892 * vmcs12 state is in the vmcs12 already.
13893 */
13894 if (is_guest_mode(vcpu))
13895 sync_vmcs12(vcpu, vmcs12);
13896 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13897 copy_shadow_to_vmcs12(vmx);
13898
13899 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13900 return -EFAULT;
13901
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013902 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13903 vmcs12->vmcs_link_pointer != -1ull) {
13904 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13905 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13906 return -EFAULT;
13907 }
13908
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013909out:
13910 return kvm_state.size;
13911}
13912
13913static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13914 struct kvm_nested_state __user *user_kvm_nested_state,
13915 struct kvm_nested_state *kvm_state)
13916{
13917 struct vcpu_vmx *vmx = to_vmx(vcpu);
13918 struct vmcs12 *vmcs12;
13919 u32 exit_qual;
13920 int ret;
13921
13922 if (kvm_state->format != 0)
13923 return -EINVAL;
13924
13925 if (!nested_vmx_allowed(vcpu))
13926 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13927
13928 if (kvm_state->vmx.vmxon_pa == -1ull) {
13929 if (kvm_state->vmx.smm.flags)
13930 return -EINVAL;
13931
13932 if (kvm_state->vmx.vmcs_pa != -1ull)
13933 return -EINVAL;
13934
13935 vmx_leave_nested(vcpu);
13936 return 0;
13937 }
13938
13939 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13940 return -EINVAL;
13941
13942 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13943 return -EINVAL;
13944
13945 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13946 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13947 return -EINVAL;
13948
13949 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13950 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13951 return -EINVAL;
13952
13953 if (kvm_state->vmx.smm.flags &
13954 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13955 return -EINVAL;
13956
13957 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13958 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
13959 return -EINVAL;
13960
13961 vmx_leave_nested(vcpu);
13962 if (kvm_state->vmx.vmxon_pa == -1ull)
13963 return 0;
13964
13965 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
13966 ret = enter_vmx_operation(vcpu);
13967 if (ret)
13968 return ret;
13969
13970 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
13971
13972 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
13973 vmx->nested.smm.vmxon = true;
13974 vmx->nested.vmxon = false;
13975
13976 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
13977 vmx->nested.smm.guest_mode = true;
13978 }
13979
13980 vmcs12 = get_vmcs12(vcpu);
13981 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
13982 return -EFAULT;
13983
Liran Alon392b2f22018-06-23 02:35:01 +030013984 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013985 return -EINVAL;
13986
13987 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13988 return 0;
13989
13990 vmx->nested.nested_run_pending =
13991 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
13992
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013993 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13994 vmcs12->vmcs_link_pointer != -1ull) {
13995 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
13996 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
13997 return -EINVAL;
13998
13999 if (copy_from_user(shadow_vmcs12,
14000 user_kvm_nested_state->data + VMCS12_SIZE,
14001 sizeof(*vmcs12)))
14002 return -EFAULT;
14003
14004 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14005 !shadow_vmcs12->hdr.shadow_vmcs)
14006 return -EINVAL;
14007 }
14008
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014009 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14010 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14011 return -EINVAL;
14012
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014013 vmx->nested.dirty_vmcs12 = true;
14014 ret = enter_vmx_non_root_mode(vcpu, NULL);
14015 if (ret)
14016 return -EINVAL;
14017
14018 return 0;
14019}
14020
Kees Cook404f6aa2016-08-08 16:29:06 -070014021static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014022 .cpu_has_kvm_support = cpu_has_kvm_support,
14023 .disabled_by_bios = vmx_disabled_by_bios,
14024 .hardware_setup = hardware_setup,
14025 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014026 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014027 .hardware_enable = hardware_enable,
14028 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014029 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014030 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014031
Wanpeng Lib31c1142018-03-12 04:53:04 -070014032 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014033 .vm_alloc = vmx_vm_alloc,
14034 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014035
Avi Kivity6aa8b732006-12-10 02:21:36 -080014036 .vcpu_create = vmx_create_vcpu,
14037 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014038 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014039
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014040 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014041 .vcpu_load = vmx_vcpu_load,
14042 .vcpu_put = vmx_vcpu_put,
14043
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014044 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014045 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014046 .get_msr = vmx_get_msr,
14047 .set_msr = vmx_set_msr,
14048 .get_segment_base = vmx_get_segment_base,
14049 .get_segment = vmx_get_segment,
14050 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014051 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014052 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014053 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014054 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014055 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014056 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014057 .set_cr3 = vmx_set_cr3,
14058 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014059 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014060 .get_idt = vmx_get_idt,
14061 .set_idt = vmx_set_idt,
14062 .get_gdt = vmx_get_gdt,
14063 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014064 .get_dr6 = vmx_get_dr6,
14065 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014066 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014067 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014068 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014069 .get_rflags = vmx_get_rflags,
14070 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014071
Avi Kivity6aa8b732006-12-10 02:21:36 -080014072 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014073 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014074
Avi Kivity6aa8b732006-12-10 02:21:36 -080014075 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014076 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014077 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014078 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14079 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014080 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014081 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014082 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014083 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014084 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014085 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014086 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014087 .get_nmi_mask = vmx_get_nmi_mask,
14088 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014089 .enable_nmi_window = enable_nmi_window,
14090 .enable_irq_window = enable_irq_window,
14091 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014092 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014093 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014094 .get_enable_apicv = vmx_get_enable_apicv,
14095 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014096 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014097 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014098 .hwapic_irr_update = vmx_hwapic_irr_update,
14099 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080014100 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14101 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014102
Izik Eiduscbc94022007-10-25 00:29:55 +020014103 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014104 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014105 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014106 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014107
Avi Kivity586f9602010-11-18 13:09:54 +020014108 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014109
Sheng Yang17cc3932010-01-05 19:02:27 +080014110 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014111
14112 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014113
14114 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014115 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014116
14117 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014118
14119 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014120
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014121 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014122 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014123
14124 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014125
14126 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014127 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014128 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014129 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014130 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014131
14132 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014133
14134 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014135
14136 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14137 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14138 .flush_log_dirty = vmx_flush_log_dirty,
14139 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014140 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014141
Feng Wubf9f6ac2015-09-18 22:29:55 +080014142 .pre_block = vmx_pre_block,
14143 .post_block = vmx_post_block,
14144
Wei Huang25462f72015-06-19 15:45:05 +020014145 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014146
14147 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014148
14149#ifdef CONFIG_X86_64
14150 .set_hv_timer = vmx_set_hv_timer,
14151 .cancel_hv_timer = vmx_cancel_hv_timer,
14152#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014153
14154 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014155
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014156 .get_nested_state = vmx_get_nested_state,
14157 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014158 .get_vmcs12_pages = nested_get_vmcs12_pages,
14159
Ladi Prosek72d7b372017-10-11 16:54:41 +020014160 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014161 .pre_enter_smm = vmx_pre_enter_smm,
14162 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014163 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014164};
14165
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014166static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014167{
14168 if (vmx_l1d_flush_pages) {
14169 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14170 vmx_l1d_flush_pages = NULL;
14171 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014172 /* Restore state so sysfs ignores VMX */
14173 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014174}
14175
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014176static void vmx_exit(void)
14177{
14178#ifdef CONFIG_KEXEC_CORE
14179 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14180 synchronize_rcu();
14181#endif
14182
14183 kvm_exit();
14184
14185#if IS_ENABLED(CONFIG_HYPERV)
14186 if (static_branch_unlikely(&enable_evmcs)) {
14187 int cpu;
14188 struct hv_vp_assist_page *vp_ap;
14189 /*
14190 * Reset everything to support using non-enlightened VMCS
14191 * access later (e.g. when we reload the module with
14192 * enlightened_vmcs=0)
14193 */
14194 for_each_online_cpu(cpu) {
14195 vp_ap = hv_get_vp_assist_page(cpu);
14196
14197 if (!vp_ap)
14198 continue;
14199
14200 vp_ap->current_nested_vmcs = 0;
14201 vp_ap->enlighten_vmentry = 0;
14202 }
14203
14204 static_branch_disable(&enable_evmcs);
14205 }
14206#endif
14207 vmx_cleanup_l1d_flush();
14208}
14209module_exit(vmx_exit);
14210
Avi Kivity6aa8b732006-12-10 02:21:36 -080014211static int __init vmx_init(void)
14212{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014213 int r;
14214
14215#if IS_ENABLED(CONFIG_HYPERV)
14216 /*
14217 * Enlightened VMCS usage should be recommended and the host needs
14218 * to support eVMCS v1 or above. We can also disable eVMCS support
14219 * with module parameter.
14220 */
14221 if (enlightened_vmcs &&
14222 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14223 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14224 KVM_EVMCS_VERSION) {
14225 int cpu;
14226
14227 /* Check that we have assist pages on all online CPUs */
14228 for_each_online_cpu(cpu) {
14229 if (!hv_get_vp_assist_page(cpu)) {
14230 enlightened_vmcs = false;
14231 break;
14232 }
14233 }
14234
14235 if (enlightened_vmcs) {
14236 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14237 static_branch_enable(&enable_evmcs);
14238 }
14239 } else {
14240 enlightened_vmcs = false;
14241 }
14242#endif
14243
14244 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014245 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014246 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014247 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014248
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014249 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014250 * Must be called after kvm_init() so enable_ept is properly set
14251 * up. Hand the parameter mitigation value in which was stored in
14252 * the pre module init parser. If no parameter was given, it will
14253 * contain 'auto' which will be turned into the default 'cond'
14254 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014255 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014256 if (boot_cpu_has(X86_BUG_L1TF)) {
14257 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14258 if (r) {
14259 vmx_exit();
14260 return r;
14261 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014262 }
14263
Dave Young2965faa2015-09-09 15:38:55 -070014264#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014265 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14266 crash_vmclear_local_loaded_vmcss);
14267#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014268 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014269
He, Qingfdef3ad2007-04-30 09:45:24 +030014270 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014271}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014272module_init(vmx_init);