blob: d32e9c5e743b437f0e3eeb414042fcd8e068bf19 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf4124502014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100124
Haozhong Zhang64903d62015-10-20 15:39:09 +0800125#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
126
Yunhong Jiang64672c92016-06-13 14:19:59 -0700127/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
128static int __read_mostly cpu_preemption_timer_multi;
129static bool __read_mostly enable_preemption_timer = 1;
130#ifdef CONFIG_X86_64
131module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
132#endif
133
Gleb Natapov50378782013-02-04 16:00:28 +0200134#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
Sean Christopherson1706bd02018-03-05 12:04:38 -0800135#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
136#define KVM_VM_CR0_ALWAYS_ON \
137 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
138 X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200139#define KVM_CR4_GUEST_OWNED_BITS \
140 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Yu Zhangfd8cb432017-08-24 20:27:56 +0800141 | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200142
Sean Christopherson5dc1f042018-03-05 12:04:39 -0800143#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
Avi Kivitycdc0e242009-12-06 17:21:14 +0200144#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
145#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
146
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
148
Jan Kiszkaf4124502014-03-07 20:03:13 +0100149#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
150
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800151/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300152 * Hyper-V requires all of these, so mark them as supported even though
153 * they are just treated the same as all-context.
154 */
155#define VMX_VPID_EXTENT_SUPPORTED_MASK \
156 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
157 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
158 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
159 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
160
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800161/*
162 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
163 * ple_gap: upper bound on the amount of time between two successive
164 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500165 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800166 * ple_window: upper bound on the amount of time a guest is allowed to execute
167 * in a PAUSE loop. Tests indicate that most spinlocks are held for
168 * less than 2^12 cycles
169 * Time is measured based on a counter that runs at the same rate as the TSC,
170 * refer SDM volume 3b section 21.6.13 & 22.1.3.
171 */
Babu Mogerc8e88712018-03-16 16:37:24 -0400172static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200173
Babu Moger7fbc85a2018-03-16 16:37:22 -0400174static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
175module_param(ple_window, uint, 0444);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800176
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200177/* Default doubles per-vcpu window every exit. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400178static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400179module_param(ple_window_grow, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200180
181/* Default resets per-vcpu window every exit to ple_window. */
Babu Mogerc8e88712018-03-16 16:37:24 -0400182static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
Babu Moger7fbc85a2018-03-16 16:37:22 -0400183module_param(ple_window_shrink, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200184
185/* Default is to compute the maximum so we can never overflow. */
Babu Moger7fbc85a2018-03-16 16:37:22 -0400186static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
187module_param(ple_window_max, uint, 0444);
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200188
Avi Kivity83287ea422012-09-16 15:10:57 +0300189extern const ulong vmx_return;
190
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200191static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
Nicolai Stange427362a2018-07-21 22:25:00 +0200192static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200193static DEFINE_MUTEX(vmx_l1d_flush_mutex);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200194
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200195/* Storage for pre module init parameter parsing */
196static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200197
198static const struct {
199 const char *option;
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200200 bool for_parse;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200201} vmentry_l1d_param[] = {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200202 [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
203 [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
204 [VMENTER_L1D_FLUSH_COND] = {"cond", true},
205 [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
206 [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
207 [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200208};
209
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200210#define L1D_CACHE_ORDER 4
211static void *vmx_l1d_flush_pages;
212
213static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
214{
215 struct page *page;
Nicolai Stange288d1522018-07-18 19:07:38 +0200216 unsigned int i;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200217
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200218 if (!enable_ept) {
219 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
220 return 0;
221 }
222
Yi Wangd806afa2018-08-16 13:42:39 +0800223 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
224 u64 msr;
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200225
Yi Wangd806afa2018-08-16 13:42:39 +0800226 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
227 if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
228 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
229 return 0;
230 }
231 }
Paolo Bonzini8e0b2b92018-08-05 16:07:46 +0200232
Jiri Kosinad90a7a02018-07-13 16:23:25 +0200233 /* If set to auto use the default l1tf mitigation method */
234 if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
235 switch (l1tf_mitigation) {
236 case L1TF_MITIGATION_OFF:
237 l1tf = VMENTER_L1D_FLUSH_NEVER;
238 break;
239 case L1TF_MITIGATION_FLUSH_NOWARN:
240 case L1TF_MITIGATION_FLUSH:
241 case L1TF_MITIGATION_FLUSH_NOSMT:
242 l1tf = VMENTER_L1D_FLUSH_COND;
243 break;
244 case L1TF_MITIGATION_FULL:
245 case L1TF_MITIGATION_FULL_FORCE:
246 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
247 break;
248 }
249 } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
250 l1tf = VMENTER_L1D_FLUSH_ALWAYS;
251 }
252
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200253 if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
254 !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
255 page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
256 if (!page)
257 return -ENOMEM;
258 vmx_l1d_flush_pages = page_address(page);
Nicolai Stange288d1522018-07-18 19:07:38 +0200259
260 /*
261 * Initialize each page with a different pattern in
262 * order to protect against KSM in the nested
263 * virtualization case.
264 */
265 for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
266 memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
267 PAGE_SIZE);
268 }
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200269 }
270
271 l1tf_vmx_mitigation = l1tf;
272
Thomas Gleixner895ae472018-07-13 16:23:22 +0200273 if (l1tf != VMENTER_L1D_FLUSH_NEVER)
274 static_branch_enable(&vmx_l1d_should_flush);
275 else
276 static_branch_disable(&vmx_l1d_should_flush);
Thomas Gleixner4c6523e2018-07-13 16:23:20 +0200277
Nicolai Stange427362a2018-07-21 22:25:00 +0200278 if (l1tf == VMENTER_L1D_FLUSH_COND)
279 static_branch_enable(&vmx_l1d_flush_cond);
Thomas Gleixner895ae472018-07-13 16:23:22 +0200280 else
Nicolai Stange427362a2018-07-21 22:25:00 +0200281 static_branch_disable(&vmx_l1d_flush_cond);
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200282 return 0;
283}
284
285static int vmentry_l1d_flush_parse(const char *s)
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200286{
287 unsigned int i;
288
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200289 if (s) {
290 for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200291 if (vmentry_l1d_param[i].for_parse &&
292 sysfs_streq(s, vmentry_l1d_param[i].option))
293 return i;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200294 }
295 }
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200296 return -EINVAL;
297}
298
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200299static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
300{
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200301 int l1tf, ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200302
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200303 l1tf = vmentry_l1d_flush_parse(s);
304 if (l1tf < 0)
305 return l1tf;
306
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200307 if (!boot_cpu_has(X86_BUG_L1TF))
308 return 0;
309
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200310 /*
311 * Has vmx_init() run already? If not then this is the pre init
312 * parameter parsing. In that case just store the value and let
313 * vmx_init() do the proper setup after enable_ept has been
314 * established.
315 */
316 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
317 vmentry_l1d_flush_param = l1tf;
318 return 0;
319 }
320
Thomas Gleixnerdd4bfa72018-07-13 16:23:21 +0200321 mutex_lock(&vmx_l1d_flush_mutex);
322 ret = vmx_setup_l1d_flush(l1tf);
323 mutex_unlock(&vmx_l1d_flush_mutex);
324 return ret;
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200325}
326
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200327static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
328{
Paolo Bonzini0027ff22018-08-22 16:43:39 +0200329 if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
330 return sprintf(s, "???\n");
331
Thomas Gleixner7db92e12018-07-13 16:23:19 +0200332 return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200333}
334
335static const struct kernel_param_ops vmentry_l1d_flush_ops = {
336 .set = vmentry_l1d_flush_set,
337 .get = vmentry_l1d_flush_get,
338};
Thomas Gleixner895ae472018-07-13 16:23:22 +0200339module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +0200340
Tianyu Lan877ad952018-07-19 08:40:23 +0000341enum ept_pointers_status {
342 EPT_POINTERS_CHECK = 0,
343 EPT_POINTERS_MATCH = 1,
344 EPT_POINTERS_MISMATCH = 2
345};
346
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700347struct kvm_vmx {
348 struct kvm kvm;
349
350 unsigned int tss_addr;
351 bool ept_identity_pagetable_done;
352 gpa_t ept_identity_map_addr;
Tianyu Lan877ad952018-07-19 08:40:23 +0000353
354 enum ept_pointers_status ept_pointers_match;
355 spinlock_t ept_pointer_lock;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -0700356};
357
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200358#define NR_AUTOLOAD_MSRS 8
Avi Kivity61d2ef22010-04-28 16:40:38 +0300359
Liran Alon392b2f22018-06-23 02:35:01 +0300360struct vmcs_hdr {
361 u32 revision_id:31;
362 u32 shadow_vmcs:1;
363};
364
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400365struct vmcs {
Liran Alon392b2f22018-06-23 02:35:01 +0300366 struct vmcs_hdr hdr;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367 u32 abort;
368 char data[0];
369};
370
Nadav Har'Eld462b812011-05-24 15:26:10 +0300371/*
Sean Christophersond7ee0392018-07-23 12:32:47 -0700372 * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT
373 * and whose values change infrequently, but are not constant. I.e. this is
374 * used as a write-through cache of the corresponding VMCS fields.
375 */
376struct vmcs_host_state {
377 unsigned long cr3; /* May not match real cr3 */
378 unsigned long cr4; /* May not match real cr4 */
Sean Christopherson5e079c72018-07-23 12:32:50 -0700379 unsigned long gs_base;
380 unsigned long fs_base;
Sean Christophersond7ee0392018-07-23 12:32:47 -0700381
382 u16 fs_sel, gs_sel, ldt_sel;
383#ifdef CONFIG_X86_64
384 u16 ds_sel, es_sel;
385#endif
386};
387
388/*
Nadav Har'Eld462b812011-05-24 15:26:10 +0300389 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
390 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
391 * loaded on this CPU (so we can clear them if the CPU goes down).
392 */
393struct loaded_vmcs {
394 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700395 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300396 int cpu;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +0200397 bool launched;
398 bool nmi_known_unmasked;
Sean Christophersonf459a702018-08-27 15:21:11 -0700399 bool hv_timer_armed;
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 Kiszkaf4124502014-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
Sean Christophersond264ee02018-08-27 15:21:12 -07001022 bool req_immediate_exit;
1023
Kai Huang843e4332015-01-28 10:54:28 +08001024 /* Support for PML */
1025#define PML_ENTITY_NUM 512
1026 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001027
Yunhong Jiang64672c92016-06-13 14:19:59 -07001028 /* apic deadline value in host tsc */
1029 u64 hv_deadline_tsc;
1030
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001031 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001032
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001033 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001034
Wanpeng Li74c55932017-11-29 01:31:20 -08001035 unsigned long host_debugctlmsr;
1036
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001037 /*
1038 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1039 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1040 * in msr_ia32_feature_control_valid_bits.
1041 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001042 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001043 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001044 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001045};
1046
Avi Kivity2fb92db2011-04-27 19:42:18 +03001047enum segment_cache_field {
1048 SEG_FIELD_SEL = 0,
1049 SEG_FIELD_BASE = 1,
1050 SEG_FIELD_LIMIT = 2,
1051 SEG_FIELD_AR = 3,
1052
1053 SEG_FIELD_NR = 4
1054};
1055
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001056static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1057{
1058 return container_of(kvm, struct kvm_vmx, kvm);
1059}
1060
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001061static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1062{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001063 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001064}
1065
Feng Wuefc64402015-09-18 22:29:51 +08001066static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1067{
1068 return &(to_vmx(vcpu)->pi_desc);
1069}
1070
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001071#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001072#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001073#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1074#define FIELD64(number, name) \
1075 FIELD(number, name), \
1076 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001077
Abel Gordon4607c2d2013-04-18 14:35:55 +03001078
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001079static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001080#define SHADOW_FIELD_RO(x) x,
1081#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001082};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001083static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001084 ARRAY_SIZE(shadow_read_only_fields);
1085
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001086static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001087#define SHADOW_FIELD_RW(x) x,
1088#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001089};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001090static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001091 ARRAY_SIZE(shadow_read_write_fields);
1092
Mathias Krause772e0312012-08-30 01:30:19 +02001093static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001094 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001095 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001096 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1097 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1098 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1099 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1100 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1101 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1102 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1103 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001104 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001105 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001106 FIELD(HOST_ES_SELECTOR, host_es_selector),
1107 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1108 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1109 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1110 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1111 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1112 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1113 FIELD64(IO_BITMAP_A, io_bitmap_a),
1114 FIELD64(IO_BITMAP_B, io_bitmap_b),
1115 FIELD64(MSR_BITMAP, msr_bitmap),
1116 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1117 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1118 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001119 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001120 FIELD64(TSC_OFFSET, tsc_offset),
1121 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1122 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001123 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001124 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001125 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001126 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1127 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1128 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1129 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001130 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001131 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1132 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001133 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001134 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1135 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1136 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1137 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1138 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1139 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1140 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1141 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1142 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1143 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001144 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001145 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1146 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1147 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1148 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1149 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1150 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1151 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1152 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1153 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1154 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1155 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1156 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1157 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1158 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1159 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1160 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1161 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1162 FIELD(TPR_THRESHOLD, tpr_threshold),
1163 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1164 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1165 FIELD(VM_EXIT_REASON, vm_exit_reason),
1166 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1167 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1168 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1169 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1170 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1171 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1172 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1173 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1174 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1175 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1176 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1177 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1178 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1179 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1180 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1181 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1182 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1183 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1184 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1185 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1186 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1187 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1188 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1189 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1190 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1191 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1192 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1193 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001194 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001195 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1196 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1197 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1198 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1199 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1200 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1201 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1202 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1203 FIELD(EXIT_QUALIFICATION, exit_qualification),
1204 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1205 FIELD(GUEST_CR0, guest_cr0),
1206 FIELD(GUEST_CR3, guest_cr3),
1207 FIELD(GUEST_CR4, guest_cr4),
1208 FIELD(GUEST_ES_BASE, guest_es_base),
1209 FIELD(GUEST_CS_BASE, guest_cs_base),
1210 FIELD(GUEST_SS_BASE, guest_ss_base),
1211 FIELD(GUEST_DS_BASE, guest_ds_base),
1212 FIELD(GUEST_FS_BASE, guest_fs_base),
1213 FIELD(GUEST_GS_BASE, guest_gs_base),
1214 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1215 FIELD(GUEST_TR_BASE, guest_tr_base),
1216 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1217 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1218 FIELD(GUEST_DR7, guest_dr7),
1219 FIELD(GUEST_RSP, guest_rsp),
1220 FIELD(GUEST_RIP, guest_rip),
1221 FIELD(GUEST_RFLAGS, guest_rflags),
1222 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1223 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1224 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1225 FIELD(HOST_CR0, host_cr0),
1226 FIELD(HOST_CR3, host_cr3),
1227 FIELD(HOST_CR4, host_cr4),
1228 FIELD(HOST_FS_BASE, host_fs_base),
1229 FIELD(HOST_GS_BASE, host_gs_base),
1230 FIELD(HOST_TR_BASE, host_tr_base),
1231 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1232 FIELD(HOST_IDTR_BASE, host_idtr_base),
1233 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1234 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1235 FIELD(HOST_RSP, host_rsp),
1236 FIELD(HOST_RIP, host_rip),
1237};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001238
1239static inline short vmcs_field_to_offset(unsigned long field)
1240{
Dan Williams085331d2018-01-31 17:47:03 -08001241 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1242 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001243 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001244
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001245 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001246 return -ENOENT;
1247
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001248 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001249 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001250 return -ENOENT;
1251
Linus Torvalds15303ba2018-02-10 13:16:35 -08001252 index = array_index_nospec(index, size);
1253 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001254 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001255 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001256 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001257}
1258
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001259static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1260{
David Matlack4f2777b2016-07-13 17:16:37 -07001261 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001262}
1263
Liran Alon61ada742018-06-23 02:35:08 +03001264static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1265{
1266 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1267}
1268
Peter Feiner995f00a2017-06-30 17:26:32 -07001269static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001270static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001271static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001272static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001273static void vmx_set_segment(struct kvm_vcpu *vcpu,
1274 struct kvm_segment *var, int seg);
1275static void vmx_get_segment(struct kvm_vcpu *vcpu,
1276 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001277static bool guest_state_valid(struct kvm_vcpu *vcpu);
1278static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001279static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001280static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1281static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1282static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1283 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001284static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001285static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1286 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001287
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1289static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001290/*
1291 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1292 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1293 */
1294static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295
Feng Wubf9f6ac2015-09-18 22:29:55 +08001296/*
1297 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1298 * can find which vCPU should be waken up.
1299 */
1300static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1301static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1302
Radim Krčmář23611332016-09-29 22:41:33 +02001303enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001304 VMX_VMREAD_BITMAP,
1305 VMX_VMWRITE_BITMAP,
1306 VMX_BITMAP_NR
1307};
1308
1309static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1310
Radim Krčmář23611332016-09-29 22:41:33 +02001311#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1312#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001313
Avi Kivity110312c2010-12-21 12:54:20 +02001314static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001315static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001316
Sheng Yang2384d2b2008-01-17 15:14:33 +08001317static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1318static DEFINE_SPINLOCK(vmx_vpid_lock);
1319
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001320static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 int size;
1322 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001323 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001325 u32 pin_based_exec_ctrl;
1326 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001327 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001328 u32 vmexit_ctrl;
1329 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001330 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001331} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332
Hannes Ederefff9e52008-11-28 17:02:06 +01001333static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001334 u32 ept;
1335 u32 vpid;
1336} vmx_capability;
1337
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338#define VMX_SEGMENT_FIELD(seg) \
1339 [VCPU_SREG_##seg] = { \
1340 .selector = GUEST_##seg##_SELECTOR, \
1341 .base = GUEST_##seg##_BASE, \
1342 .limit = GUEST_##seg##_LIMIT, \
1343 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1344 }
1345
Mathias Krause772e0312012-08-30 01:30:19 +02001346static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 unsigned selector;
1348 unsigned base;
1349 unsigned limit;
1350 unsigned ar_bytes;
1351} kvm_vmx_segment_fields[] = {
1352 VMX_SEGMENT_FIELD(CS),
1353 VMX_SEGMENT_FIELD(DS),
1354 VMX_SEGMENT_FIELD(ES),
1355 VMX_SEGMENT_FIELD(FS),
1356 VMX_SEGMENT_FIELD(GS),
1357 VMX_SEGMENT_FIELD(SS),
1358 VMX_SEGMENT_FIELD(TR),
1359 VMX_SEGMENT_FIELD(LDTR),
1360};
1361
Avi Kivity26bb0982009-09-07 11:14:12 +03001362static u64 host_efer;
1363
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001364static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1365
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001366/*
Brian Gerst8c065852010-07-17 09:03:26 -04001367 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001368 * away by decrementing the array size.
1369 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001371#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001372 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001374 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001377DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1378
1379#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1380
1381#define KVM_EVMCS_VERSION 1
1382
1383#if IS_ENABLED(CONFIG_HYPERV)
1384static bool __read_mostly enlightened_vmcs = true;
1385module_param(enlightened_vmcs, bool, 0444);
1386
1387static inline void evmcs_write64(unsigned long field, u64 value)
1388{
1389 u16 clean_field;
1390 int offset = get_evmcs_offset(field, &clean_field);
1391
1392 if (offset < 0)
1393 return;
1394
1395 *(u64 *)((char *)current_evmcs + offset) = value;
1396
1397 current_evmcs->hv_clean_fields &= ~clean_field;
1398}
1399
1400static inline void evmcs_write32(unsigned long field, u32 value)
1401{
1402 u16 clean_field;
1403 int offset = get_evmcs_offset(field, &clean_field);
1404
1405 if (offset < 0)
1406 return;
1407
1408 *(u32 *)((char *)current_evmcs + offset) = value;
1409 current_evmcs->hv_clean_fields &= ~clean_field;
1410}
1411
1412static inline void evmcs_write16(unsigned long field, u16 value)
1413{
1414 u16 clean_field;
1415 int offset = get_evmcs_offset(field, &clean_field);
1416
1417 if (offset < 0)
1418 return;
1419
1420 *(u16 *)((char *)current_evmcs + offset) = value;
1421 current_evmcs->hv_clean_fields &= ~clean_field;
1422}
1423
1424static inline u64 evmcs_read64(unsigned long field)
1425{
1426 int offset = get_evmcs_offset(field, NULL);
1427
1428 if (offset < 0)
1429 return 0;
1430
1431 return *(u64 *)((char *)current_evmcs + offset);
1432}
1433
1434static inline u32 evmcs_read32(unsigned long field)
1435{
1436 int offset = get_evmcs_offset(field, NULL);
1437
1438 if (offset < 0)
1439 return 0;
1440
1441 return *(u32 *)((char *)current_evmcs + offset);
1442}
1443
1444static inline u16 evmcs_read16(unsigned long field)
1445{
1446 int offset = get_evmcs_offset(field, NULL);
1447
1448 if (offset < 0)
1449 return 0;
1450
1451 return *(u16 *)((char *)current_evmcs + offset);
1452}
1453
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001454static inline void evmcs_touch_msr_bitmap(void)
1455{
1456 if (unlikely(!current_evmcs))
1457 return;
1458
1459 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1460 current_evmcs->hv_clean_fields &=
1461 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1462}
1463
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001464static void evmcs_load(u64 phys_addr)
1465{
1466 struct hv_vp_assist_page *vp_ap =
1467 hv_get_vp_assist_page(smp_processor_id());
1468
1469 vp_ap->current_nested_vmcs = phys_addr;
1470 vp_ap->enlighten_vmentry = 1;
1471}
1472
1473static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1474{
1475 /*
1476 * Enlightened VMCSv1 doesn't support these:
1477 *
1478 * POSTED_INTR_NV = 0x00000002,
1479 * GUEST_INTR_STATUS = 0x00000810,
1480 * APIC_ACCESS_ADDR = 0x00002014,
1481 * POSTED_INTR_DESC_ADDR = 0x00002016,
1482 * EOI_EXIT_BITMAP0 = 0x0000201c,
1483 * EOI_EXIT_BITMAP1 = 0x0000201e,
1484 * EOI_EXIT_BITMAP2 = 0x00002020,
1485 * EOI_EXIT_BITMAP3 = 0x00002022,
1486 */
1487 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1488 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1489 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1490 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1491 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1492 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1493 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1494
1495 /*
1496 * GUEST_PML_INDEX = 0x00000812,
1497 * PML_ADDRESS = 0x0000200e,
1498 */
1499 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1500
1501 /* VM_FUNCTION_CONTROL = 0x00002018, */
1502 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1503
1504 /*
1505 * EPTP_LIST_ADDRESS = 0x00002024,
1506 * VMREAD_BITMAP = 0x00002026,
1507 * VMWRITE_BITMAP = 0x00002028,
1508 */
1509 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1510
1511 /*
1512 * TSC_MULTIPLIER = 0x00002032,
1513 */
1514 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1515
1516 /*
1517 * PLE_GAP = 0x00004020,
1518 * PLE_WINDOW = 0x00004022,
1519 */
1520 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1521
1522 /*
1523 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1524 */
1525 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1526
1527 /*
1528 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1529 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1530 */
1531 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1532 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1533
1534 /*
1535 * Currently unsupported in KVM:
1536 * GUEST_IA32_RTIT_CTL = 0x00002814,
1537 */
1538}
Tianyu Lan877ad952018-07-19 08:40:23 +00001539
1540/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1541static void check_ept_pointer_match(struct kvm *kvm)
1542{
1543 struct kvm_vcpu *vcpu;
1544 u64 tmp_eptp = INVALID_PAGE;
1545 int i;
1546
1547 kvm_for_each_vcpu(i, vcpu, kvm) {
1548 if (!VALID_PAGE(tmp_eptp)) {
1549 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1550 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1551 to_kvm_vmx(kvm)->ept_pointers_match
1552 = EPT_POINTERS_MISMATCH;
1553 return;
1554 }
1555 }
1556
1557 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1558}
1559
1560static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1561{
1562 int ret;
1563
1564 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1565
1566 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1567 check_ept_pointer_match(kvm);
1568
1569 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1570 ret = -ENOTSUPP;
1571 goto out;
1572 }
1573
1574 ret = hyperv_flush_guest_mapping(
1575 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1576
1577out:
1578 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1579 return ret;
1580}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001581#else /* !IS_ENABLED(CONFIG_HYPERV) */
1582static inline void evmcs_write64(unsigned long field, u64 value) {}
1583static inline void evmcs_write32(unsigned long field, u32 value) {}
1584static inline void evmcs_write16(unsigned long field, u16 value) {}
1585static inline u64 evmcs_read64(unsigned long field) { return 0; }
1586static inline u32 evmcs_read32(unsigned long field) { return 0; }
1587static inline u16 evmcs_read16(unsigned long field) { return 0; }
1588static inline void evmcs_load(u64 phys_addr) {}
1589static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001590static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001591#endif /* IS_ENABLED(CONFIG_HYPERV) */
1592
Jan Kiszka5bb16012016-02-09 20:14:21 +01001593static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594{
1595 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1596 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001597 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1598}
1599
Jan Kiszka6f054852016-02-09 20:15:18 +01001600static inline bool is_debug(u32 intr_info)
1601{
1602 return is_exception_n(intr_info, DB_VECTOR);
1603}
1604
1605static inline bool is_breakpoint(u32 intr_info)
1606{
1607 return is_exception_n(intr_info, BP_VECTOR);
1608}
1609
Jan Kiszka5bb16012016-02-09 20:14:21 +01001610static inline bool is_page_fault(u32 intr_info)
1611{
1612 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001613}
1614
Gui Jianfeng31299942010-03-15 17:29:09 +08001615static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001616{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001617 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001618}
1619
Gui Jianfeng31299942010-03-15 17:29:09 +08001620static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001621{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001622 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001623}
1624
Liran Alon9e869482018-03-12 13:12:51 +02001625static inline bool is_gp_fault(u32 intr_info)
1626{
1627 return is_exception_n(intr_info, GP_VECTOR);
1628}
1629
Gui Jianfeng31299942010-03-15 17:29:09 +08001630static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001631{
1632 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1633 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1634}
1635
Gui Jianfeng31299942010-03-15 17:29:09 +08001636static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001637{
1638 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1639 INTR_INFO_VALID_MASK)) ==
1640 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1641}
1642
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001643/* Undocumented: icebp/int1 */
1644static inline bool is_icebp(u32 intr_info)
1645{
1646 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1647 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1648}
1649
Gui Jianfeng31299942010-03-15 17:29:09 +08001650static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001651{
Sheng Yang04547152009-04-01 15:52:31 +08001652 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001653}
1654
Gui Jianfeng31299942010-03-15 17:29:09 +08001655static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001656{
Sheng Yang04547152009-04-01 15:52:31 +08001657 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001658}
1659
Paolo Bonzini35754c92015-07-29 12:05:37 +02001660static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001661{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001662 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001663}
1664
Gui Jianfeng31299942010-03-15 17:29:09 +08001665static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001666{
Sheng Yang04547152009-04-01 15:52:31 +08001667 return vmcs_config.cpu_based_exec_ctrl &
1668 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001669}
1670
Avi Kivity774ead32007-12-26 13:57:04 +02001671static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001672{
Sheng Yang04547152009-04-01 15:52:31 +08001673 return vmcs_config.cpu_based_2nd_exec_ctrl &
1674 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1675}
1676
Yang Zhang8d146952013-01-25 10:18:50 +08001677static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1678{
1679 return vmcs_config.cpu_based_2nd_exec_ctrl &
1680 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1681}
1682
Yang Zhang83d4c282013-01-25 10:18:49 +08001683static inline bool cpu_has_vmx_apic_register_virt(void)
1684{
1685 return vmcs_config.cpu_based_2nd_exec_ctrl &
1686 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1687}
1688
Yang Zhangc7c9c562013-01-25 10:18:51 +08001689static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1690{
1691 return vmcs_config.cpu_based_2nd_exec_ctrl &
1692 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1693}
1694
Sean Christopherson0b665d32018-08-14 09:33:34 -07001695static inline bool cpu_has_vmx_encls_vmexit(void)
1696{
1697 return vmcs_config.cpu_based_2nd_exec_ctrl &
1698 SECONDARY_EXEC_ENCLS_EXITING;
1699}
1700
Yunhong Jiang64672c92016-06-13 14:19:59 -07001701/*
1702 * Comment's format: document - errata name - stepping - processor name.
1703 * Refer from
1704 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1705 */
1706static u32 vmx_preemption_cpu_tfms[] = {
1707/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17080x000206E6,
1709/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1710/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1711/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17120x00020652,
1713/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17140x00020655,
1715/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1716/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1717/*
1718 * 320767.pdf - AAP86 - B1 -
1719 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1720 */
17210x000106E5,
1722/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17230x000106A0,
1724/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17250x000106A1,
1726/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17270x000106A4,
1728 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1729 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1730 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17310x000106A5,
1732};
1733
1734static inline bool cpu_has_broken_vmx_preemption_timer(void)
1735{
1736 u32 eax = cpuid_eax(0x00000001), i;
1737
1738 /* Clear the reserved bits */
1739 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001740 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001741 if (eax == vmx_preemption_cpu_tfms[i])
1742 return true;
1743
1744 return false;
1745}
1746
1747static inline bool cpu_has_vmx_preemption_timer(void)
1748{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001749 return vmcs_config.pin_based_exec_ctrl &
1750 PIN_BASED_VMX_PREEMPTION_TIMER;
1751}
1752
Yang Zhang01e439b2013-04-11 19:25:12 +08001753static inline bool cpu_has_vmx_posted_intr(void)
1754{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001755 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1756 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001757}
1758
1759static inline bool cpu_has_vmx_apicv(void)
1760{
1761 return cpu_has_vmx_apic_register_virt() &&
1762 cpu_has_vmx_virtual_intr_delivery() &&
1763 cpu_has_vmx_posted_intr();
1764}
1765
Sheng Yang04547152009-04-01 15:52:31 +08001766static inline bool cpu_has_vmx_flexpriority(void)
1767{
1768 return cpu_has_vmx_tpr_shadow() &&
1769 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001770}
1771
Marcelo Tosattie7997942009-06-11 12:07:40 -03001772static inline bool cpu_has_vmx_ept_execute_only(void)
1773{
Gui Jianfeng31299942010-03-15 17:29:09 +08001774 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001775}
1776
Marcelo Tosattie7997942009-06-11 12:07:40 -03001777static inline bool cpu_has_vmx_ept_2m_page(void)
1778{
Gui Jianfeng31299942010-03-15 17:29:09 +08001779 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001780}
1781
Sheng Yang878403b2010-01-05 19:02:29 +08001782static inline bool cpu_has_vmx_ept_1g_page(void)
1783{
Gui Jianfeng31299942010-03-15 17:29:09 +08001784 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001785}
1786
Sheng Yang4bc9b982010-06-02 14:05:24 +08001787static inline bool cpu_has_vmx_ept_4levels(void)
1788{
1789 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1790}
1791
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001792static inline bool cpu_has_vmx_ept_mt_wb(void)
1793{
1794 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1795}
1796
Yu Zhang855feb62017-08-24 20:27:55 +08001797static inline bool cpu_has_vmx_ept_5levels(void)
1798{
1799 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1800}
1801
Xudong Hao83c3a332012-05-28 19:33:35 +08001802static inline bool cpu_has_vmx_ept_ad_bits(void)
1803{
1804 return vmx_capability.ept & VMX_EPT_AD_BIT;
1805}
1806
Gui Jianfeng31299942010-03-15 17:29:09 +08001807static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001808{
Gui Jianfeng31299942010-03-15 17:29:09 +08001809 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001810}
1811
Gui Jianfeng31299942010-03-15 17:29:09 +08001812static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001813{
Gui Jianfeng31299942010-03-15 17:29:09 +08001814 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001815}
1816
Liran Aloncd9a4912018-05-22 17:16:15 +03001817static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1818{
1819 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1820}
1821
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001822static inline bool cpu_has_vmx_invvpid_single(void)
1823{
1824 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1825}
1826
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001827static inline bool cpu_has_vmx_invvpid_global(void)
1828{
1829 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1830}
1831
Wanpeng Li08d839c2017-03-23 05:30:08 -07001832static inline bool cpu_has_vmx_invvpid(void)
1833{
1834 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1835}
1836
Gui Jianfeng31299942010-03-15 17:29:09 +08001837static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001838{
Sheng Yang04547152009-04-01 15:52:31 +08001839 return vmcs_config.cpu_based_2nd_exec_ctrl &
1840 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001841}
1842
Gui Jianfeng31299942010-03-15 17:29:09 +08001843static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001844{
1845 return vmcs_config.cpu_based_2nd_exec_ctrl &
1846 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1847}
1848
Gui Jianfeng31299942010-03-15 17:29:09 +08001849static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001850{
1851 return vmcs_config.cpu_based_2nd_exec_ctrl &
1852 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1853}
1854
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001855static inline bool cpu_has_vmx_basic_inout(void)
1856{
1857 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1858}
1859
Paolo Bonzini35754c92015-07-29 12:05:37 +02001860static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001861{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001862 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001863}
1864
Gui Jianfeng31299942010-03-15 17:29:09 +08001865static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001866{
Sheng Yang04547152009-04-01 15:52:31 +08001867 return vmcs_config.cpu_based_2nd_exec_ctrl &
1868 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001869}
1870
Gui Jianfeng31299942010-03-15 17:29:09 +08001871static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001872{
1873 return vmcs_config.cpu_based_2nd_exec_ctrl &
1874 SECONDARY_EXEC_RDTSCP;
1875}
1876
Mao, Junjiead756a12012-07-02 01:18:48 +00001877static inline bool cpu_has_vmx_invpcid(void)
1878{
1879 return vmcs_config.cpu_based_2nd_exec_ctrl &
1880 SECONDARY_EXEC_ENABLE_INVPCID;
1881}
1882
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001883static inline bool cpu_has_virtual_nmis(void)
1884{
1885 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1886}
1887
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001888static inline bool cpu_has_vmx_wbinvd_exit(void)
1889{
1890 return vmcs_config.cpu_based_2nd_exec_ctrl &
1891 SECONDARY_EXEC_WBINVD_EXITING;
1892}
1893
Abel Gordonabc4fc52013-04-18 14:35:25 +03001894static inline bool cpu_has_vmx_shadow_vmcs(void)
1895{
1896 u64 vmx_msr;
1897 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1898 /* check if the cpu supports writing r/o exit information fields */
1899 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1900 return false;
1901
1902 return vmcs_config.cpu_based_2nd_exec_ctrl &
1903 SECONDARY_EXEC_SHADOW_VMCS;
1904}
1905
Kai Huang843e4332015-01-28 10:54:28 +08001906static inline bool cpu_has_vmx_pml(void)
1907{
1908 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1909}
1910
Haozhong Zhang64903d62015-10-20 15:39:09 +08001911static inline bool cpu_has_vmx_tsc_scaling(void)
1912{
1913 return vmcs_config.cpu_based_2nd_exec_ctrl &
1914 SECONDARY_EXEC_TSC_SCALING;
1915}
1916
Bandan Das2a499e42017-08-03 15:54:41 -04001917static inline bool cpu_has_vmx_vmfunc(void)
1918{
1919 return vmcs_config.cpu_based_2nd_exec_ctrl &
1920 SECONDARY_EXEC_ENABLE_VMFUNC;
1921}
1922
Sean Christopherson64f7a112018-04-30 10:01:06 -07001923static bool vmx_umip_emulated(void)
1924{
1925 return vmcs_config.cpu_based_2nd_exec_ctrl &
1926 SECONDARY_EXEC_DESC;
1927}
1928
Sheng Yang04547152009-04-01 15:52:31 +08001929static inline bool report_flexpriority(void)
1930{
1931 return flexpriority_enabled;
1932}
1933
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001934static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1935{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001936 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001937}
1938
Jim Mattsonf4160e42018-05-29 09:11:33 -07001939/*
1940 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1941 * to modify any valid field of the VMCS, or are the VM-exit
1942 * information fields read-only?
1943 */
1944static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1945{
1946 return to_vmx(vcpu)->nested.msrs.misc_low &
1947 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1948}
1949
Marc Orr04473782018-06-20 17:21:29 -07001950static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1951{
1952 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1953}
1954
1955static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1956{
1957 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1958 CPU_BASED_MONITOR_TRAP_FLAG;
1959}
1960
Liran Alonfa97d7d2018-07-18 14:07:59 +02001961static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1962{
1963 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1964 SECONDARY_EXEC_SHADOW_VMCS;
1965}
1966
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001967static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1968{
1969 return vmcs12->cpu_based_vm_exec_control & bit;
1970}
1971
1972static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1973{
1974 return (vmcs12->cpu_based_vm_exec_control &
1975 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1976 (vmcs12->secondary_vm_exec_control & bit);
1977}
1978
Jan Kiszkaf4124502014-03-07 20:03:13 +01001979static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1980{
1981 return vmcs12->pin_based_vm_exec_control &
1982 PIN_BASED_VMX_PREEMPTION_TIMER;
1983}
1984
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001985static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1986{
1987 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1988}
1989
1990static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1991{
1992 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1993}
1994
Nadav Har'El155a97a2013-08-05 11:07:16 +03001995static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1996{
1997 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1998}
1999
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002000static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2001{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002002 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002003}
2004
Bandan Dasc5f983f2017-05-05 15:25:14 -04002005static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2006{
2007 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2008}
2009
Wincy Vanf2b93282015-02-03 23:56:03 +08002010static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2011{
2012 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2013}
2014
Wanpeng Li5c614b32015-10-13 09:18:36 -07002015static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2016{
2017 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2018}
2019
Wincy Van82f0dd42015-02-03 23:57:18 +08002020static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2021{
2022 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2023}
2024
Wincy Van608406e2015-02-03 23:57:51 +08002025static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2026{
2027 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2028}
2029
Wincy Van705699a2015-02-03 23:58:17 +08002030static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2031{
2032 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2033}
2034
Bandan Das27c42a12017-08-03 15:54:42 -04002035static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2036{
2037 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2038}
2039
Bandan Das41ab9372017-08-03 15:54:43 -04002040static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2041{
2042 return nested_cpu_has_vmfunc(vmcs12) &&
2043 (vmcs12->vm_function_control &
2044 VMX_VMFUNC_EPTP_SWITCHING);
2045}
2046
Liran Alonf792d272018-06-23 02:35:05 +03002047static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2048{
2049 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2050}
2051
Jim Mattsonef85b672016-12-12 11:01:37 -08002052static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002053{
2054 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002055 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002056}
2057
Jan Kiszka533558b2014-01-04 18:47:20 +01002058static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2059 u32 exit_intr_info,
2060 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002061static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2062 struct vmcs12 *vmcs12,
2063 u32 reason, unsigned long qualification);
2064
Rusty Russell8b9cf982007-07-30 16:31:43 +10002065static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002066{
2067 int i;
2068
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002069 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002070 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002071 return i;
2072 return -1;
2073}
2074
Sheng Yang2384d2b2008-01-17 15:14:33 +08002075static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2076{
2077 struct {
2078 u64 vpid : 16;
2079 u64 rsvd : 48;
2080 u64 gva;
2081 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002082 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002083
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002084 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2085 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2086 : "memory");
2087 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002088}
2089
Sheng Yang14394422008-04-28 12:24:45 +08002090static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2091{
2092 struct {
2093 u64 eptp, gpa;
2094 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002095 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002096
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002097 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2098 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2099 : "memory");
2100 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002101}
2102
Avi Kivity26bb0982009-09-07 11:14:12 +03002103static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002104{
2105 int i;
2106
Rusty Russell8b9cf982007-07-30 16:31:43 +10002107 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002108 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002109 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002110 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002111}
2112
Avi Kivity6aa8b732006-12-10 02:21:36 -08002113static void vmcs_clear(struct vmcs *vmcs)
2114{
2115 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002116 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002118 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2119 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2120 : "memory");
2121 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2123 vmcs, phys_addr);
2124}
2125
Nadav Har'Eld462b812011-05-24 15:26:10 +03002126static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2127{
2128 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002129 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2130 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002131 loaded_vmcs->cpu = -1;
2132 loaded_vmcs->launched = 0;
2133}
2134
Dongxiao Xu7725b892010-05-11 18:29:38 +08002135static void vmcs_load(struct vmcs *vmcs)
2136{
2137 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002138 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002139
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002140 if (static_branch_unlikely(&enable_evmcs))
2141 return evmcs_load(phys_addr);
2142
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002143 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2144 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2145 : "memory");
2146 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002147 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002148 vmcs, phys_addr);
2149}
2150
Dave Young2965faa2015-09-09 15:38:55 -07002151#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002152/*
2153 * This bitmap is used to indicate whether the vmclear
2154 * operation is enabled on all cpus. All disabled by
2155 * default.
2156 */
2157static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2158
2159static inline void crash_enable_local_vmclear(int cpu)
2160{
2161 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2162}
2163
2164static inline void crash_disable_local_vmclear(int cpu)
2165{
2166 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2167}
2168
2169static inline int crash_local_vmclear_enabled(int cpu)
2170{
2171 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2172}
2173
2174static void crash_vmclear_local_loaded_vmcss(void)
2175{
2176 int cpu = raw_smp_processor_id();
2177 struct loaded_vmcs *v;
2178
2179 if (!crash_local_vmclear_enabled(cpu))
2180 return;
2181
2182 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2183 loaded_vmcss_on_cpu_link)
2184 vmcs_clear(v->vmcs);
2185}
2186#else
2187static inline void crash_enable_local_vmclear(int cpu) { }
2188static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002189#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002190
Nadav Har'Eld462b812011-05-24 15:26:10 +03002191static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002193 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002194 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195
Nadav Har'Eld462b812011-05-24 15:26:10 +03002196 if (loaded_vmcs->cpu != cpu)
2197 return; /* vcpu migration can race with cpu offline */
2198 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002200 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002201 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002202
2203 /*
2204 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2205 * is before setting loaded_vmcs->vcpu to -1 which is done in
2206 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2207 * then adds the vmcs into percpu list before it is deleted.
2208 */
2209 smp_wmb();
2210
Nadav Har'Eld462b812011-05-24 15:26:10 +03002211 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002212 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002213}
2214
Nadav Har'Eld462b812011-05-24 15:26:10 +03002215static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002216{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002217 int cpu = loaded_vmcs->cpu;
2218
2219 if (cpu != -1)
2220 smp_call_function_single(cpu,
2221 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002222}
2223
Junaid Shahidfaff8752018-06-29 13:10:05 -07002224static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2225{
2226 if (vpid == 0)
2227 return true;
2228
2229 if (cpu_has_vmx_invvpid_individual_addr()) {
2230 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2231 return true;
2232 }
2233
2234 return false;
2235}
2236
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002237static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002238{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002239 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002240 return;
2241
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002242 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002243 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002244}
2245
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002246static inline void vpid_sync_vcpu_global(void)
2247{
2248 if (cpu_has_vmx_invvpid_global())
2249 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2250}
2251
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002252static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002253{
2254 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002255 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002256 else
2257 vpid_sync_vcpu_global();
2258}
2259
Sheng Yang14394422008-04-28 12:24:45 +08002260static inline void ept_sync_global(void)
2261{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002262 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002263}
2264
2265static inline void ept_sync_context(u64 eptp)
2266{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002267 if (cpu_has_vmx_invept_context())
2268 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2269 else
2270 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002271}
2272
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002273static __always_inline void vmcs_check16(unsigned long field)
2274{
2275 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2276 "16-bit accessor invalid for 64-bit field");
2277 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2278 "16-bit accessor invalid for 64-bit high field");
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2280 "16-bit accessor invalid for 32-bit high field");
2281 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2282 "16-bit accessor invalid for natural width field");
2283}
2284
2285static __always_inline void vmcs_check32(unsigned long field)
2286{
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2288 "32-bit accessor invalid for 16-bit field");
2289 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2290 "32-bit accessor invalid for natural width field");
2291}
2292
2293static __always_inline void vmcs_check64(unsigned long field)
2294{
2295 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2296 "64-bit accessor invalid for 16-bit field");
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2298 "64-bit accessor invalid for 64-bit high field");
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2300 "64-bit accessor invalid for 32-bit field");
2301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2302 "64-bit accessor invalid for natural width field");
2303}
2304
2305static __always_inline void vmcs_checkl(unsigned long field)
2306{
2307 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2308 "Natural width accessor invalid for 16-bit field");
2309 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2310 "Natural width accessor invalid for 64-bit field");
2311 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2312 "Natural width accessor invalid for 64-bit high field");
2313 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2314 "Natural width accessor invalid for 32-bit field");
2315}
2316
2317static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318{
Avi Kivity5e520e62011-05-15 10:13:12 -04002319 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320
Avi Kivity5e520e62011-05-15 10:13:12 -04002321 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2322 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323 return value;
2324}
2325
Avi Kivity96304212011-05-15 10:13:13 -04002326static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002328 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002329 if (static_branch_unlikely(&enable_evmcs))
2330 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002331 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332}
2333
Avi Kivity96304212011-05-15 10:13:13 -04002334static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002336 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002337 if (static_branch_unlikely(&enable_evmcs))
2338 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002339 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340}
2341
Avi Kivity96304212011-05-15 10:13:13 -04002342static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002344 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002345 if (static_branch_unlikely(&enable_evmcs))
2346 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002347#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002348 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002350 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351#endif
2352}
2353
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002354static __always_inline unsigned long vmcs_readl(unsigned long field)
2355{
2356 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002357 if (static_branch_unlikely(&enable_evmcs))
2358 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002359 return __vmcs_readl(field);
2360}
2361
Avi Kivitye52de1b2007-01-05 16:36:56 -08002362static noinline void vmwrite_error(unsigned long field, unsigned long value)
2363{
2364 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2365 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2366 dump_stack();
2367}
2368
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002369static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002371 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002372
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002373 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2374 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002375 if (unlikely(error))
2376 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377}
2378
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002379static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002381 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002382 if (static_branch_unlikely(&enable_evmcs))
2383 return evmcs_write16(field, value);
2384
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002385 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002388static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002390 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002391 if (static_branch_unlikely(&enable_evmcs))
2392 return evmcs_write32(field, value);
2393
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002394 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002397static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002399 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002400 if (static_branch_unlikely(&enable_evmcs))
2401 return evmcs_write64(field, value);
2402
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002403 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002404#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002405 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407#endif
2408}
2409
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002410static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002411{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002412 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002413 if (static_branch_unlikely(&enable_evmcs))
2414 return evmcs_write64(field, value);
2415
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002416 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002417}
2418
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002419static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002420{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002421 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2422 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002423 if (static_branch_unlikely(&enable_evmcs))
2424 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2425
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002426 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2427}
2428
2429static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2430{
2431 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2432 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002433 if (static_branch_unlikely(&enable_evmcs))
2434 return evmcs_write32(field, evmcs_read32(field) | mask);
2435
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002436 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002437}
2438
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002439static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2440{
2441 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2442}
2443
Gleb Natapov2961e8762013-11-25 15:37:13 +02002444static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2445{
2446 vmcs_write32(VM_ENTRY_CONTROLS, val);
2447 vmx->vm_entry_controls_shadow = val;
2448}
2449
2450static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2451{
2452 if (vmx->vm_entry_controls_shadow != val)
2453 vm_entry_controls_init(vmx, val);
2454}
2455
2456static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2457{
2458 return vmx->vm_entry_controls_shadow;
2459}
2460
2461
2462static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2463{
2464 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2465}
2466
2467static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2468{
2469 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2470}
2471
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002472static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2473{
2474 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2475}
2476
Gleb Natapov2961e8762013-11-25 15:37:13 +02002477static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2478{
2479 vmcs_write32(VM_EXIT_CONTROLS, val);
2480 vmx->vm_exit_controls_shadow = val;
2481}
2482
2483static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2484{
2485 if (vmx->vm_exit_controls_shadow != val)
2486 vm_exit_controls_init(vmx, val);
2487}
2488
2489static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2490{
2491 return vmx->vm_exit_controls_shadow;
2492}
2493
2494
2495static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2496{
2497 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2498}
2499
2500static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2501{
2502 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2503}
2504
Avi Kivity2fb92db2011-04-27 19:42:18 +03002505static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2506{
2507 vmx->segment_cache.bitmask = 0;
2508}
2509
2510static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2511 unsigned field)
2512{
2513 bool ret;
2514 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2515
2516 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2517 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2518 vmx->segment_cache.bitmask = 0;
2519 }
2520 ret = vmx->segment_cache.bitmask & mask;
2521 vmx->segment_cache.bitmask |= mask;
2522 return ret;
2523}
2524
2525static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2526{
2527 u16 *p = &vmx->segment_cache.seg[seg].selector;
2528
2529 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2530 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2531 return *p;
2532}
2533
2534static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2535{
2536 ulong *p = &vmx->segment_cache.seg[seg].base;
2537
2538 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2539 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2540 return *p;
2541}
2542
2543static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2544{
2545 u32 *p = &vmx->segment_cache.seg[seg].limit;
2546
2547 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2548 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2549 return *p;
2550}
2551
2552static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2553{
2554 u32 *p = &vmx->segment_cache.seg[seg].ar;
2555
2556 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2557 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2558 return *p;
2559}
2560
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002561static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2562{
2563 u32 eb;
2564
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002565 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002566 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002567 /*
2568 * Guest access to VMware backdoor ports could legitimately
2569 * trigger #GP because of TSS I/O permission bitmap.
2570 * We intercept those #GP and allow access to them anyway
2571 * as VMware does.
2572 */
2573 if (enable_vmware_backdoor)
2574 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002575 if ((vcpu->guest_debug &
2576 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2577 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2578 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002579 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002580 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002581 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002582 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002583
2584 /* When we are running a nested L2 guest and L1 specified for it a
2585 * certain exception bitmap, we must trap the same exceptions and pass
2586 * them to L1. When running L2, we will only handle the exceptions
2587 * specified above if L1 did not want them.
2588 */
2589 if (is_guest_mode(vcpu))
2590 eb |= get_vmcs12(vcpu)->exception_bitmap;
2591
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002592 vmcs_write32(EXCEPTION_BITMAP, eb);
2593}
2594
Ashok Raj15d45072018-02-01 22:59:43 +01002595/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002596 * Check if MSR is intercepted for currently loaded MSR bitmap.
2597 */
2598static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2599{
2600 unsigned long *msr_bitmap;
2601 int f = sizeof(unsigned long);
2602
2603 if (!cpu_has_vmx_msr_bitmap())
2604 return true;
2605
2606 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2607
2608 if (msr <= 0x1fff) {
2609 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2610 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2611 msr &= 0x1fff;
2612 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2613 }
2614
2615 return true;
2616}
2617
2618/*
Ashok Raj15d45072018-02-01 22:59:43 +01002619 * Check if MSR is intercepted for L01 MSR bitmap.
2620 */
2621static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2622{
2623 unsigned long *msr_bitmap;
2624 int f = sizeof(unsigned long);
2625
2626 if (!cpu_has_vmx_msr_bitmap())
2627 return true;
2628
2629 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2630
2631 if (msr <= 0x1fff) {
2632 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2633 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2634 msr &= 0x1fff;
2635 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2636 }
2637
2638 return true;
2639}
2640
Gleb Natapov2961e8762013-11-25 15:37:13 +02002641static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2642 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002643{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002644 vm_entry_controls_clearbit(vmx, entry);
2645 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002646}
2647
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002648static int find_msr(struct vmx_msrs *m, unsigned int msr)
2649{
2650 unsigned int i;
2651
2652 for (i = 0; i < m->nr; ++i) {
2653 if (m->val[i].index == msr)
2654 return i;
2655 }
2656 return -ENOENT;
2657}
2658
Avi Kivity61d2ef22010-04-28 16:40:38 +03002659static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2660{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002661 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002662 struct msr_autoload *m = &vmx->msr_autoload;
2663
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002664 switch (msr) {
2665 case MSR_EFER:
2666 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002667 clear_atomic_switch_msr_special(vmx,
2668 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002669 VM_EXIT_LOAD_IA32_EFER);
2670 return;
2671 }
2672 break;
2673 case MSR_CORE_PERF_GLOBAL_CTRL:
2674 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002675 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002676 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2677 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2678 return;
2679 }
2680 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002681 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002682 i = find_msr(&m->guest, msr);
2683 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002684 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002685 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002686 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002687 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002688
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002689skip_guest:
2690 i = find_msr(&m->host, msr);
2691 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002692 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002693
2694 --m->host.nr;
2695 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002696 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002697}
2698
Gleb Natapov2961e8762013-11-25 15:37:13 +02002699static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2700 unsigned long entry, unsigned long exit,
2701 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2702 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002703{
2704 vmcs_write64(guest_val_vmcs, guest_val);
2705 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002706 vm_entry_controls_setbit(vmx, entry);
2707 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002708}
2709
Avi Kivity61d2ef22010-04-28 16:40:38 +03002710static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002711 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002712{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002713 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002714 struct msr_autoload *m = &vmx->msr_autoload;
2715
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002716 switch (msr) {
2717 case MSR_EFER:
2718 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002719 add_atomic_switch_msr_special(vmx,
2720 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002721 VM_EXIT_LOAD_IA32_EFER,
2722 GUEST_IA32_EFER,
2723 HOST_IA32_EFER,
2724 guest_val, host_val);
2725 return;
2726 }
2727 break;
2728 case MSR_CORE_PERF_GLOBAL_CTRL:
2729 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002730 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002731 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2732 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2733 GUEST_IA32_PERF_GLOBAL_CTRL,
2734 HOST_IA32_PERF_GLOBAL_CTRL,
2735 guest_val, host_val);
2736 return;
2737 }
2738 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002739 case MSR_IA32_PEBS_ENABLE:
2740 /* PEBS needs a quiescent period after being disabled (to write
2741 * a record). Disabling PEBS through VMX MSR swapping doesn't
2742 * provide that period, so a CPU could write host's record into
2743 * guest's memory.
2744 */
2745 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002746 }
2747
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002748 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002749 if (!entry_only)
2750 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002751
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002752 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002753 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002754 "Can't add msr %x\n", msr);
2755 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002756 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002757 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002758 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002759 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002760 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002761 m->guest.val[i].index = msr;
2762 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002763
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002764 if (entry_only)
2765 return;
2766
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002767 if (j < 0) {
2768 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002769 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002770 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002771 m->host.val[j].index = msr;
2772 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002773}
2774
Avi Kivity92c0d902009-10-29 11:00:16 +02002775static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002776{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002777 u64 guest_efer = vmx->vcpu.arch.efer;
2778 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002779
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002780 if (!enable_ept) {
2781 /*
2782 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2783 * host CPUID is more efficient than testing guest CPUID
2784 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2785 */
2786 if (boot_cpu_has(X86_FEATURE_SMEP))
2787 guest_efer |= EFER_NX;
2788 else if (!(guest_efer & EFER_NX))
2789 ignore_bits |= EFER_NX;
2790 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002791
Avi Kivity51c6cf62007-08-29 03:48:05 +03002792 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002793 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002794 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002795 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002796#ifdef CONFIG_X86_64
2797 ignore_bits |= EFER_LMA | EFER_LME;
2798 /* SCE is meaningful only in long mode on Intel */
2799 if (guest_efer & EFER_LMA)
2800 ignore_bits &= ~(u64)EFER_SCE;
2801#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002802
2803 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002804
2805 /*
2806 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2807 * On CPUs that support "load IA32_EFER", always switch EFER
2808 * atomically, since it's faster than switching it manually.
2809 */
2810 if (cpu_has_load_ia32_efer ||
2811 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002812 if (!(guest_efer & EFER_LMA))
2813 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002814 if (guest_efer != host_efer)
2815 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002816 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002817 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002818 } else {
2819 guest_efer &= ~ignore_bits;
2820 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002821
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002822 vmx->guest_msrs[efer_offset].data = guest_efer;
2823 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2824
2825 return true;
2826 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002827}
2828
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002829#ifdef CONFIG_X86_32
2830/*
2831 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2832 * VMCS rather than the segment table. KVM uses this helper to figure
2833 * out the current bases to poke them into the VMCS before entry.
2834 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002835static unsigned long segment_base(u16 selector)
2836{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 unsigned long v;
2839
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002840 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002841 return 0;
2842
Thomas Garnier45fc8752017-03-14 10:05:08 -07002843 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002845 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002846 u16 ldt_selector = kvm_read_ldt();
2847
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002848 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002849 return 0;
2850
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002851 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002852 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002853 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002854 return v;
2855}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002856#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002857
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002858static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002859{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002860 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002861 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002862#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002863 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002864#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002865 unsigned long fs_base, gs_base;
2866 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002867 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002868
Sean Christophersond264ee02018-08-27 15:21:12 -07002869 vmx->req_immediate_exit = false;
2870
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002871 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002872 return;
2873
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002874 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002875 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002876
Avi Kivity33ed6322007-05-02 16:54:03 +03002877 /*
2878 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2879 * allow segment selectors with cpl > 0 or ti == 1.
2880 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002881 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002882
2883#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002884 savesegment(ds, host_state->ds_sel);
2885 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002886
2887 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002888 if (likely(is_64bit_mm(current->mm))) {
2889 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002890 fs_sel = current->thread.fsindex;
2891 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002892 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002893 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002894 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002895 savesegment(fs, fs_sel);
2896 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002897 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002898 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002899 }
2900
Paolo Bonzini4679b612018-09-24 17:23:01 +02002901 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002902#else
Sean Christophersone368b872018-07-23 12:32:41 -07002903 savesegment(fs, fs_sel);
2904 savesegment(gs, gs_sel);
2905 fs_base = segment_base(fs_sel);
2906 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002907#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002908
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002909 if (unlikely(fs_sel != host_state->fs_sel)) {
2910 if (!(fs_sel & 7))
2911 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2912 else
2913 vmcs_write16(HOST_FS_SELECTOR, 0);
2914 host_state->fs_sel = fs_sel;
2915 }
2916 if (unlikely(gs_sel != host_state->gs_sel)) {
2917 if (!(gs_sel & 7))
2918 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2919 else
2920 vmcs_write16(HOST_GS_SELECTOR, 0);
2921 host_state->gs_sel = gs_sel;
2922 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002923 if (unlikely(fs_base != host_state->fs_base)) {
2924 vmcs_writel(HOST_FS_BASE, fs_base);
2925 host_state->fs_base = fs_base;
2926 }
2927 if (unlikely(gs_base != host_state->gs_base)) {
2928 vmcs_writel(HOST_GS_BASE, gs_base);
2929 host_state->gs_base = gs_base;
2930 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002931
Avi Kivity26bb0982009-09-07 11:14:12 +03002932 for (i = 0; i < vmx->save_nmsrs; ++i)
2933 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002934 vmx->guest_msrs[i].data,
2935 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002936}
2937
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002938static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002939{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002940 struct vmcs_host_state *host_state;
2941
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002942 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002943 return;
2944
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002945 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002946 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002947
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002948 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002949 vmx->loaded_cpu_state = NULL;
2950
Avi Kivityc8770e72010-11-11 12:37:26 +02002951#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002952 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002953#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002954 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2955 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002956#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002958#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002959 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002960#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002961 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002962 if (host_state->fs_sel & 7)
2963 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002964#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002965 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2966 loadsegment(ds, host_state->ds_sel);
2967 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002968 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002969#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002970 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002971#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002972 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002973#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002974 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002975}
2976
Sean Christopherson678e3152018-07-23 12:32:43 -07002977#ifdef CONFIG_X86_64
2978static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002979{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002980 preempt_disable();
2981 if (vmx->loaded_cpu_state)
2982 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2983 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002984 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002985}
2986
Sean Christopherson678e3152018-07-23 12:32:43 -07002987static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2988{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002989 preempt_disable();
2990 if (vmx->loaded_cpu_state)
2991 wrmsrl(MSR_KERNEL_GS_BASE, data);
2992 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002993 vmx->msr_guest_kernel_gs_base = data;
2994}
2995#endif
2996
Feng Wu28b835d2015-09-18 22:29:54 +08002997static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2998{
2999 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3000 struct pi_desc old, new;
3001 unsigned int dest;
3002
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003003 /*
3004 * In case of hot-plug or hot-unplug, we may have to undo
3005 * vmx_vcpu_pi_put even if there is no assigned device. And we
3006 * always keep PI.NDST up to date for simplicity: it makes the
3007 * code easier, and CPU migration is not a fast path.
3008 */
3009 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003010 return;
3011
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003012 /*
3013 * First handle the simple case where no cmpxchg is necessary; just
3014 * allow posting non-urgent interrupts.
3015 *
3016 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3017 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3018 * expects the VCPU to be on the blocked_vcpu_list that matches
3019 * PI.NDST.
3020 */
3021 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3022 vcpu->cpu == cpu) {
3023 pi_clear_sn(pi_desc);
3024 return;
3025 }
3026
3027 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003028 do {
3029 old.control = new.control = pi_desc->control;
3030
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003031 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003032
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003033 if (x2apic_enabled())
3034 new.ndst = dest;
3035 else
3036 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003037
Feng Wu28b835d2015-09-18 22:29:54 +08003038 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003039 } while (cmpxchg64(&pi_desc->control, old.control,
3040 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003041}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003042
Peter Feinerc95ba922016-08-17 09:36:47 -07003043static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3044{
3045 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3046 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3047}
3048
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049/*
3050 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3051 * vcpu mutex is already taken.
3052 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003053static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003054{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003056 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003058 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003059 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003060 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003061 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003062
3063 /*
3064 * Read loaded_vmcs->cpu should be before fetching
3065 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3066 * See the comments in __loaded_vmcs_clear().
3067 */
3068 smp_rmb();
3069
Nadav Har'Eld462b812011-05-24 15:26:10 +03003070 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3071 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003072 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003073 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003074 }
3075
3076 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3077 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3078 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003079 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003080 }
3081
3082 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003083 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003084 unsigned long sysenter_esp;
3085
3086 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003087
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 /*
3089 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003090 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003092 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003093 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003094 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003096 /*
3097 * VM exits change the host TR limit to 0x67 after a VM
3098 * exit. This is okay, since 0x67 covers everything except
3099 * the IO bitmap and have have code to handle the IO bitmap
3100 * being lost after a VM exit.
3101 */
3102 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3103
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3105 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003106
Nadav Har'Eld462b812011-05-24 15:26:10 +03003107 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108 }
Feng Wu28b835d2015-09-18 22:29:54 +08003109
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003110 /* Setup TSC multiplier */
3111 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003112 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3113 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003114
Feng Wu28b835d2015-09-18 22:29:54 +08003115 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003116 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003117 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003118}
3119
3120static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3121{
3122 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3123
3124 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003125 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3126 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003127 return;
3128
3129 /* Set SN when the vCPU is preempted */
3130 if (vcpu->preempted)
3131 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132}
3133
3134static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3135{
Feng Wu28b835d2015-09-18 22:29:54 +08003136 vmx_vcpu_pi_put(vcpu);
3137
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003138 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139}
3140
Wanpeng Lif244dee2017-07-20 01:11:54 -07003141static bool emulation_required(struct kvm_vcpu *vcpu)
3142{
3143 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3144}
3145
Avi Kivityedcafe32009-12-30 18:07:40 +02003146static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3147
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003148/*
3149 * Return the cr0 value that a nested guest would read. This is a combination
3150 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3151 * its hypervisor (cr0_read_shadow).
3152 */
3153static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3154{
3155 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3156 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3157}
3158static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3159{
3160 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3161 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3162}
3163
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3165{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003166 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003167
Avi Kivity6de12732011-03-07 12:51:22 +02003168 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3169 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3170 rflags = vmcs_readl(GUEST_RFLAGS);
3171 if (to_vmx(vcpu)->rmode.vm86_active) {
3172 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3173 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3174 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3175 }
3176 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003177 }
Avi Kivity6de12732011-03-07 12:51:22 +02003178 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179}
3180
3181static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3182{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003183 unsigned long old_rflags = vmx_get_rflags(vcpu);
3184
Avi Kivity6de12732011-03-07 12:51:22 +02003185 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3186 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003187 if (to_vmx(vcpu)->rmode.vm86_active) {
3188 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003189 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003190 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003192
3193 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3194 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195}
3196
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003197static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003198{
3199 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3200 int ret = 0;
3201
3202 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003203 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003204 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003205 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003206
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003207 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003208}
3209
3210static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3211{
3212 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3213 u32 interruptibility = interruptibility_old;
3214
3215 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3216
Jan Kiszka48005f62010-02-19 19:38:07 +01003217 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003218 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003219 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003220 interruptibility |= GUEST_INTR_STATE_STI;
3221
3222 if ((interruptibility != interruptibility_old))
3223 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3224}
3225
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3227{
3228 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003230 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003232 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003233
Glauber Costa2809f5d2009-05-12 16:21:05 -04003234 /* skipping an emulated instruction also counts */
3235 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236}
3237
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003238static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3239 unsigned long exit_qual)
3240{
3241 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3242 unsigned int nr = vcpu->arch.exception.nr;
3243 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3244
3245 if (vcpu->arch.exception.has_error_code) {
3246 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3247 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3248 }
3249
3250 if (kvm_exception_is_soft(nr))
3251 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3252 else
3253 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3254
3255 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3256 vmx_get_nmi_mask(vcpu))
3257 intr_info |= INTR_INFO_UNBLOCK_NMI;
3258
3259 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3260}
3261
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003262/*
3263 * KVM wants to inject page-faults which it got to the guest. This function
3264 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003265 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003266static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003267{
3268 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003269 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003270
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003271 if (nr == PF_VECTOR) {
3272 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003273 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003274 return 1;
3275 }
3276 /*
3277 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3278 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3279 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3280 * can be written only when inject_pending_event runs. This should be
3281 * conditional on a new capability---if the capability is disabled,
3282 * kvm_multiple_exception would write the ancillary information to
3283 * CR2 or DR6, for backwards ABI-compatibility.
3284 */
3285 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3286 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003287 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003288 return 1;
3289 }
3290 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003291 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003292 if (nr == DB_VECTOR)
3293 *exit_qual = vcpu->arch.dr6;
3294 else
3295 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003296 return 1;
3297 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003298 }
3299
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003300 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003301}
3302
Wanpeng Licaa057a2018-03-12 04:53:03 -07003303static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3304{
3305 /*
3306 * Ensure that we clear the HLT state in the VMCS. We don't need to
3307 * explicitly skip the instruction because if the HLT state is set,
3308 * then the instruction is already executing and RIP has already been
3309 * advanced.
3310 */
3311 if (kvm_hlt_in_guest(vcpu->kvm) &&
3312 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3313 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3314}
3315
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003316static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003317{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003318 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003319 unsigned nr = vcpu->arch.exception.nr;
3320 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003321 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003322 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003323
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003324 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003325 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003326 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3327 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003328
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003329 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003330 int inc_eip = 0;
3331 if (kvm_exception_is_soft(nr))
3332 inc_eip = vcpu->arch.event_exit_inst_len;
3333 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003334 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003335 return;
3336 }
3337
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003338 WARN_ON_ONCE(vmx->emulation_required);
3339
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003340 if (kvm_exception_is_soft(nr)) {
3341 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3342 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003343 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3344 } else
3345 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3346
3347 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003348
3349 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003350}
3351
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003352static bool vmx_rdtscp_supported(void)
3353{
3354 return cpu_has_vmx_rdtscp();
3355}
3356
Mao, Junjiead756a12012-07-02 01:18:48 +00003357static bool vmx_invpcid_supported(void)
3358{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003359 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003360}
3361
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362/*
Eddie Donga75beee2007-05-17 18:55:15 +03003363 * Swap MSR entry in host/guest MSR entry array.
3364 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003365static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003366{
Avi Kivity26bb0982009-09-07 11:14:12 +03003367 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003368
3369 tmp = vmx->guest_msrs[to];
3370 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3371 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003372}
3373
3374/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003375 * Set up the vmcs to automatically save and restore system
3376 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3377 * mode, as fiddling with msrs is very expensive.
3378 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003379static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003380{
Avi Kivity26bb0982009-09-07 11:14:12 +03003381 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003382
Eddie Donga75beee2007-05-17 18:55:15 +03003383 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003384#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003385 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003386 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003387 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003388 move_msr_up(vmx, index, save_nmsrs++);
3389 index = __find_msr_index(vmx, MSR_LSTAR);
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_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003395 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003396 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003397 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003398 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003399 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003400 * if efer.sce is enabled.
3401 */
Brian Gerst8c065852010-07-17 09:03:26 -04003402 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003403 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003404 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003405 }
Eddie Donga75beee2007-05-17 18:55:15 +03003406#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003407 index = __find_msr_index(vmx, MSR_EFER);
3408 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003409 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003410
Avi Kivity26bb0982009-09-07 11:14:12 +03003411 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003412
Yang Zhang8d146952013-01-25 10:18:50 +08003413 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003414 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003415}
3416
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003417static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003418{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003419 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003421 if (is_guest_mode(vcpu) &&
3422 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3423 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3424
3425 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426}
3427
3428/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003429 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003431static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003433 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003434 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003435 * We're here if L1 chose not to trap WRMSR to TSC. According
3436 * to the spec, this should set L1's TSC; The offset that L1
3437 * set for L2 remains unchanged, and still needs to be added
3438 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003439 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003440 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003441 /* recalculate vmcs02.TSC_OFFSET: */
3442 vmcs12 = get_vmcs12(vcpu);
3443 vmcs_write64(TSC_OFFSET, offset +
3444 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3445 vmcs12->tsc_offset : 0));
3446 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003447 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3448 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003449 vmcs_write64(TSC_OFFSET, offset);
3450 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
Nadav Har'El801d3422011-05-25 23:02:23 +03003453/*
3454 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3455 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3456 * all guests if the "nested" module option is off, and can also be disabled
3457 * for a single guest by disabling its VMX cpuid bit.
3458 */
3459static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3460{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003461 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003462}
3463
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003465 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3466 * returned for the various VMX controls MSRs when nested VMX is enabled.
3467 * The same values should also be used to verify that vmcs12 control fields are
3468 * valid during nested entry from L1 to L2.
3469 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3470 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3471 * bit in the high half is on if the corresponding bit in the control field
3472 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003473 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003474static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003475{
Paolo Bonzini13893092018-02-26 13:40:09 +01003476 if (!nested) {
3477 memset(msrs, 0, sizeof(*msrs));
3478 return;
3479 }
3480
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003481 /*
3482 * Note that as a general rule, the high half of the MSRs (bits in
3483 * the control fields which may be 1) should be initialized by the
3484 * intersection of the underlying hardware's MSR (i.e., features which
3485 * can be supported) and the list of features we want to expose -
3486 * because they are known to be properly supported in our code.
3487 * Also, usually, the low half of the MSRs (bits which must be 1) can
3488 * be set to 0, meaning that L1 may turn off any of these bits. The
3489 * reason is that if one of these bits is necessary, it will appear
3490 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3491 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003492 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003493 * These rules have exceptions below.
3494 */
3495
3496 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003497 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003498 msrs->pinbased_ctls_low,
3499 msrs->pinbased_ctls_high);
3500 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003501 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003503 PIN_BASED_EXT_INTR_MASK |
3504 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003505 PIN_BASED_VIRTUAL_NMIS |
3506 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003507 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003508 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003509 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003510
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003511 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003512 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003513 msrs->exit_ctls_low,
3514 msrs->exit_ctls_high);
3515 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003516 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003517
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003518 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003519#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003520 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003521#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003522 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003523 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003524 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003525 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003526 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3527
Jan Kiszka2996fca2014-06-16 13:59:43 +02003528 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003529 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003530
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003531 /* entry controls */
3532 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003533 msrs->entry_ctls_low,
3534 msrs->entry_ctls_high);
3535 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003536 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003538#ifdef CONFIG_X86_64
3539 VM_ENTRY_IA32E_MODE |
3540#endif
3541 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003542 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003543 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003544
Jan Kiszka2996fca2014-06-16 13:59:43 +02003545 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003546 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003547
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003548 /* cpu-based controls */
3549 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003550 msrs->procbased_ctls_low,
3551 msrs->procbased_ctls_high);
3552 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003553 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003554 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003555 CPU_BASED_VIRTUAL_INTR_PENDING |
3556 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003557 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3558 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3559 CPU_BASED_CR3_STORE_EXITING |
3560#ifdef CONFIG_X86_64
3561 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3562#endif
3563 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003564 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3565 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3566 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3567 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003568 /*
3569 * We can allow some features even when not supported by the
3570 * hardware. For example, L1 can specify an MSR bitmap - and we
3571 * can use it to avoid exits to L1 - even when L0 runs L2
3572 * without MSR bitmaps.
3573 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003575 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003576 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003577
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003578 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003579 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003580 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3581
Paolo Bonzini80154d72017-08-24 13:55:35 +02003582 /*
3583 * secondary cpu-based controls. Do not include those that
3584 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3585 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003586 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003587 msrs->secondary_ctls_low,
3588 msrs->secondary_ctls_high);
3589 msrs->secondary_ctls_low = 0;
3590 msrs->secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01003591 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini1b073042016-10-25 16:06:30 +02003592 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003593 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003594 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003595 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003596 SECONDARY_EXEC_WBINVD_EXITING;
Liran Alon32c7acf2018-06-23 02:35:11 +03003597 /*
3598 * We can emulate "VMCS shadowing," even if the hardware
3599 * doesn't support it.
3600 */
3601 msrs->secondary_ctls_high |=
3602 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003603
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003604 if (enable_ept) {
3605 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003606 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003607 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003608 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003609 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003610 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003611 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003612 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003613 msrs->ept_caps &= vmx_capability.ept;
3614 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003615 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3616 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003617 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003618 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003619 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003620 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003621 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003622 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003623
Bandan Das27c42a12017-08-03 15:54:42 -04003624 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003625 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003626 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003627 /*
3628 * Advertise EPTP switching unconditionally
3629 * since we emulate it
3630 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003631 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003632 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003633 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003634 }
3635
Paolo Bonzinief697a72016-03-18 16:58:38 +01003636 /*
3637 * Old versions of KVM use the single-context version without
3638 * checking for support, so declare that it is supported even
3639 * though it is treated as global context. The alternative is
3640 * not failing the single-context invvpid, and it is worse.
3641 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003642 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003643 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003644 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003646 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003647 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003648
Radim Krčmář0790ec12015-03-17 14:02:32 +01003649 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003650 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003651 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3652
Jan Kiszkac18911a2013-03-13 16:06:41 +01003653 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003654 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003655 msrs->misc_low,
3656 msrs->misc_high);
3657 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3658 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003659 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003660 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003661 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003662 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003663
3664 /*
3665 * This MSR reports some information about VMX support. We
3666 * should return information about the VMX we emulate for the
3667 * guest, and the VMCS structure we give it - not about the
3668 * VMX support of the underlying hardware.
3669 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003670 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003671 VMCS12_REVISION |
3672 VMX_BASIC_TRUE_CTLS |
3673 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3674 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3675
3676 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003677 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003678
3679 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003680 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003681 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3682 * We picked the standard core2 setting.
3683 */
3684#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3685#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003686 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3687 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003688
3689 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003690 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3691 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003692
3693 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003694 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003695}
3696
David Matlack38991522016-11-29 18:14:08 -08003697/*
3698 * if fixed0[i] == 1: val[i] must be 1
3699 * if fixed1[i] == 0: val[i] must be 0
3700 */
3701static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3702{
3703 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003704}
3705
3706static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3707{
David Matlack38991522016-11-29 18:14:08 -08003708 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709}
3710
3711static inline u64 vmx_control_msr(u32 low, u32 high)
3712{
3713 return low | ((u64)high << 32);
3714}
3715
David Matlack62cc6b9d2016-11-29 18:14:07 -08003716static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3717{
3718 superset &= mask;
3719 subset &= mask;
3720
3721 return (superset | subset) == superset;
3722}
3723
3724static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3725{
3726 const u64 feature_and_reserved =
3727 /* feature (except bit 48; see below) */
3728 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3729 /* reserved */
3730 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003731 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003732
3733 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3734 return -EINVAL;
3735
3736 /*
3737 * KVM does not emulate a version of VMX that constrains physical
3738 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3739 */
3740 if (data & BIT_ULL(48))
3741 return -EINVAL;
3742
3743 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3744 vmx_basic_vmcs_revision_id(data))
3745 return -EINVAL;
3746
3747 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3748 return -EINVAL;
3749
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003750 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003751 return 0;
3752}
3753
3754static int
3755vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3756{
3757 u64 supported;
3758 u32 *lowp, *highp;
3759
3760 switch (msr_index) {
3761 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003762 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3763 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003764 break;
3765 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003766 lowp = &vmx->nested.msrs.procbased_ctls_low;
3767 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003768 break;
3769 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003770 lowp = &vmx->nested.msrs.exit_ctls_low;
3771 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003772 break;
3773 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003774 lowp = &vmx->nested.msrs.entry_ctls_low;
3775 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003776 break;
3777 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003778 lowp = &vmx->nested.msrs.secondary_ctls_low;
3779 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003780 break;
3781 default:
3782 BUG();
3783 }
3784
3785 supported = vmx_control_msr(*lowp, *highp);
3786
3787 /* Check must-be-1 bits are still 1. */
3788 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3789 return -EINVAL;
3790
3791 /* Check must-be-0 bits are still 0. */
3792 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3793 return -EINVAL;
3794
3795 *lowp = data;
3796 *highp = data >> 32;
3797 return 0;
3798}
3799
3800static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3801{
3802 const u64 feature_and_reserved_bits =
3803 /* feature */
3804 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3805 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3806 /* reserved */
3807 GENMASK_ULL(13, 9) | BIT_ULL(31);
3808 u64 vmx_misc;
3809
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003810 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3811 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003812
3813 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3814 return -EINVAL;
3815
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003816 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003817 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3818 vmx_misc_preemption_timer_rate(data) !=
3819 vmx_misc_preemption_timer_rate(vmx_misc))
3820 return -EINVAL;
3821
3822 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3823 return -EINVAL;
3824
3825 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3826 return -EINVAL;
3827
3828 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3829 return -EINVAL;
3830
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003831 vmx->nested.msrs.misc_low = data;
3832 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003833
3834 /*
3835 * If L1 has read-only VM-exit information fields, use the
3836 * less permissive vmx_vmwrite_bitmap to specify write
3837 * permissions for the shadow VMCS.
3838 */
3839 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3840 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3841
David Matlack62cc6b9d2016-11-29 18:14:07 -08003842 return 0;
3843}
3844
3845static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3846{
3847 u64 vmx_ept_vpid_cap;
3848
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003849 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3850 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003851
3852 /* Every bit is either reserved or a feature bit. */
3853 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3854 return -EINVAL;
3855
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003856 vmx->nested.msrs.ept_caps = data;
3857 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003858 return 0;
3859}
3860
3861static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3862{
3863 u64 *msr;
3864
3865 switch (msr_index) {
3866 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003867 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003868 break;
3869 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003870 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003871 break;
3872 default:
3873 BUG();
3874 }
3875
3876 /*
3877 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3878 * must be 1 in the restored value.
3879 */
3880 if (!is_bitwise_subset(data, *msr, -1ULL))
3881 return -EINVAL;
3882
3883 *msr = data;
3884 return 0;
3885}
3886
3887/*
3888 * Called when userspace is restoring VMX MSRs.
3889 *
3890 * Returns 0 on success, non-0 otherwise.
3891 */
3892static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3893{
3894 struct vcpu_vmx *vmx = to_vmx(vcpu);
3895
Jim Mattsona943ac52018-05-29 09:11:32 -07003896 /*
3897 * Don't allow changes to the VMX capability MSRs while the vCPU
3898 * is in VMX operation.
3899 */
3900 if (vmx->nested.vmxon)
3901 return -EBUSY;
3902
David Matlack62cc6b9d2016-11-29 18:14:07 -08003903 switch (msr_index) {
3904 case MSR_IA32_VMX_BASIC:
3905 return vmx_restore_vmx_basic(vmx, data);
3906 case MSR_IA32_VMX_PINBASED_CTLS:
3907 case MSR_IA32_VMX_PROCBASED_CTLS:
3908 case MSR_IA32_VMX_EXIT_CTLS:
3909 case MSR_IA32_VMX_ENTRY_CTLS:
3910 /*
3911 * The "non-true" VMX capability MSRs are generated from the
3912 * "true" MSRs, so we do not support restoring them directly.
3913 *
3914 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3915 * should restore the "true" MSRs with the must-be-1 bits
3916 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3917 * DEFAULT SETTINGS".
3918 */
3919 return -EINVAL;
3920 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3921 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3922 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3923 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3924 case MSR_IA32_VMX_PROCBASED_CTLS2:
3925 return vmx_restore_control_msr(vmx, msr_index, data);
3926 case MSR_IA32_VMX_MISC:
3927 return vmx_restore_vmx_misc(vmx, data);
3928 case MSR_IA32_VMX_CR0_FIXED0:
3929 case MSR_IA32_VMX_CR4_FIXED0:
3930 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3931 case MSR_IA32_VMX_CR0_FIXED1:
3932 case MSR_IA32_VMX_CR4_FIXED1:
3933 /*
3934 * These MSRs are generated based on the vCPU's CPUID, so we
3935 * do not support restoring them directly.
3936 */
3937 return -EINVAL;
3938 case MSR_IA32_VMX_EPT_VPID_CAP:
3939 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3940 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003941 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003942 return 0;
3943 default:
3944 /*
3945 * The rest of the VMX capability MSRs do not support restore.
3946 */
3947 return -EINVAL;
3948 }
3949}
3950
Jan Kiszkacae50132014-01-04 18:47:22 +01003951/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003952static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003953{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003954 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003955 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003956 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003957 break;
3958 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3959 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003960 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003961 msrs->pinbased_ctls_low,
3962 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003963 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3964 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003965 break;
3966 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3967 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003968 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003969 msrs->procbased_ctls_low,
3970 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003971 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3972 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003973 break;
3974 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3975 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003976 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003977 msrs->exit_ctls_low,
3978 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003979 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3980 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003981 break;
3982 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3983 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003984 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003985 msrs->entry_ctls_low,
3986 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003987 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3988 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003989 break;
3990 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003991 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003992 msrs->misc_low,
3993 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003995 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003996 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003999 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 break;
4001 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004002 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 break;
4004 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004005 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004006 break;
4007 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004008 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004009 break;
4010 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004011 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004012 msrs->secondary_ctls_low,
4013 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004016 *pdata = msrs->ept_caps |
4017 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004018 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004019 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004020 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004021 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004023 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004024 }
4025
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004026 return 0;
4027}
4028
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004029static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4030 uint64_t val)
4031{
4032 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4033
4034 return !(val & ~valid_bits);
4035}
4036
Tom Lendacky801e4592018-02-21 13:39:51 -06004037static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4038{
Paolo Bonzini13893092018-02-26 13:40:09 +01004039 switch (msr->index) {
4040 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4041 if (!nested)
4042 return 1;
4043 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4044 default:
4045 return 1;
4046 }
4047
4048 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004049}
4050
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004051/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052 * Reads an msr value (of 'msr_index') into 'pdata'.
4053 * Returns 0 on success, non-0 otherwise.
4054 * Assumes vcpu_load() was already called.
4055 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004056static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004058 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004059 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004061 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004062#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 break;
4066 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004067 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004069 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004070 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004071 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004072#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004074 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004075 case MSR_IA32_SPEC_CTRL:
4076 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004077 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4078 return 1;
4079
4080 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4081 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004082 case MSR_IA32_ARCH_CAPABILITIES:
4083 if (!msr_info->host_initiated &&
4084 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4085 return 1;
4086 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4087 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004089 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090 break;
4091 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004092 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093 break;
4094 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004095 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004097 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004098 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004099 (!msr_info->host_initiated &&
4100 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004101 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004102 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004103 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004104 case MSR_IA32_MCG_EXT_CTL:
4105 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004106 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004107 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004108 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004109 msr_info->data = vcpu->arch.mcg_ext_ctl;
4110 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004111 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004112 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004113 break;
4114 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4115 if (!nested_vmx_allowed(vcpu))
4116 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004117 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4118 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004119 case MSR_IA32_XSS:
4120 if (!vmx_xsaves_supported())
4121 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004122 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004123 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004124 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004125 if (!msr_info->host_initiated &&
4126 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004127 return 1;
4128 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004129 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004130 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004131 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004132 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004133 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004135 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136 }
4137
Avi Kivity6aa8b732006-12-10 02:21:36 -08004138 return 0;
4139}
4140
Jan Kiszkacae50132014-01-04 18:47:22 +01004141static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4142
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143/*
4144 * Writes msr value into into the appropriate "register".
4145 * Returns 0 on success, non-0 otherwise.
4146 * Assumes vcpu_load() was already called.
4147 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004148static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004150 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004151 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004152 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004153 u32 msr_index = msr_info->index;
4154 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004155
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004157 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004158 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004159 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004160#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004162 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163 vmcs_writel(GUEST_FS_BASE, data);
4164 break;
4165 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004166 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004167 vmcs_writel(GUEST_GS_BASE, data);
4168 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004169 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004170 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004171 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172#endif
4173 case MSR_IA32_SYSENTER_CS:
4174 vmcs_write32(GUEST_SYSENTER_CS, data);
4175 break;
4176 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004177 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 break;
4179 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004180 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004182 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004183 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004184 (!msr_info->host_initiated &&
4185 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004186 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004187 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004188 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004190 vmcs_write64(GUEST_BNDCFGS, data);
4191 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004192 case MSR_IA32_SPEC_CTRL:
4193 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004194 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4195 return 1;
4196
4197 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004198 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004199 return 1;
4200
4201 vmx->spec_ctrl = data;
4202
4203 if (!data)
4204 break;
4205
4206 /*
4207 * For non-nested:
4208 * When it's written (to non-zero) for the first time, pass
4209 * it through.
4210 *
4211 * For nested:
4212 * The handling of the MSR bitmap for L2 guests is done in
4213 * nested_vmx_merge_msr_bitmap. We should not touch the
4214 * vmcs02.msr_bitmap here since it gets completely overwritten
4215 * in the merging. We update the vmcs01 here for L1 as well
4216 * since it will end up touching the MSR anyway now.
4217 */
4218 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4219 MSR_IA32_SPEC_CTRL,
4220 MSR_TYPE_RW);
4221 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004222 case MSR_IA32_PRED_CMD:
4223 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004224 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4225 return 1;
4226
4227 if (data & ~PRED_CMD_IBPB)
4228 return 1;
4229
4230 if (!data)
4231 break;
4232
4233 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4234
4235 /*
4236 * For non-nested:
4237 * When it's written (to non-zero) for the first time, pass
4238 * it through.
4239 *
4240 * For nested:
4241 * The handling of the MSR bitmap for L2 guests is done in
4242 * nested_vmx_merge_msr_bitmap. We should not touch the
4243 * vmcs02.msr_bitmap here since it gets completely overwritten
4244 * in the merging.
4245 */
4246 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4247 MSR_TYPE_W);
4248 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004249 case MSR_IA32_ARCH_CAPABILITIES:
4250 if (!msr_info->host_initiated)
4251 return 1;
4252 vmx->arch_capabilities = data;
4253 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004254 case MSR_IA32_CR_PAT:
4255 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004256 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4257 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004258 vmcs_write64(GUEST_IA32_PAT, data);
4259 vcpu->arch.pat = data;
4260 break;
4261 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004262 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004263 break;
Will Auldba904632012-11-29 12:42:50 -08004264 case MSR_IA32_TSC_ADJUST:
4265 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004266 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004267 case MSR_IA32_MCG_EXT_CTL:
4268 if ((!msr_info->host_initiated &&
4269 !(to_vmx(vcpu)->msr_ia32_feature_control &
4270 FEATURE_CONTROL_LMCE)) ||
4271 (data & ~MCG_EXT_CTL_LMCE_EN))
4272 return 1;
4273 vcpu->arch.mcg_ext_ctl = data;
4274 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004275 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004276 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004277 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004278 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4279 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004280 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004281 if (msr_info->host_initiated && data == 0)
4282 vmx_leave_nested(vcpu);
4283 break;
4284 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004285 if (!msr_info->host_initiated)
4286 return 1; /* they are read-only */
4287 if (!nested_vmx_allowed(vcpu))
4288 return 1;
4289 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004290 case MSR_IA32_XSS:
4291 if (!vmx_xsaves_supported())
4292 return 1;
4293 /*
4294 * The only supported bit as of Skylake is bit 8, but
4295 * it is not supported on KVM.
4296 */
4297 if (data != 0)
4298 return 1;
4299 vcpu->arch.ia32_xss = data;
4300 if (vcpu->arch.ia32_xss != host_xss)
4301 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004302 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004303 else
4304 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4305 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004306 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004307 if (!msr_info->host_initiated &&
4308 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004309 return 1;
4310 /* Check reserved bit, higher 32 bits should be zero */
4311 if ((data >> 32) != 0)
4312 return 1;
4313 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004314 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004315 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004316 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004317 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004318 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004319 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4320 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004321 ret = kvm_set_shared_msr(msr->index, msr->data,
4322 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004323 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004324 if (ret)
4325 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004326 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004327 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004328 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004329 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330 }
4331
Eddie Dong2cc51562007-05-21 07:28:09 +03004332 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333}
4334
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004335static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004337 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4338 switch (reg) {
4339 case VCPU_REGS_RSP:
4340 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4341 break;
4342 case VCPU_REGS_RIP:
4343 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4344 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004345 case VCPU_EXREG_PDPTR:
4346 if (enable_ept)
4347 ept_save_pdptrs(vcpu);
4348 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004349 default:
4350 break;
4351 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004352}
4353
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354static __init int cpu_has_kvm_support(void)
4355{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004356 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357}
4358
4359static __init int vmx_disabled_by_bios(void)
4360{
4361 u64 msr;
4362
4363 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004364 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004365 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004366 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4367 && tboot_enabled())
4368 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004369 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004370 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004371 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004372 && !tboot_enabled()) {
4373 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004374 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004375 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004376 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004377 /* launched w/o TXT and VMX disabled */
4378 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4379 && !tboot_enabled())
4380 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004381 }
4382
4383 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384}
4385
Dongxiao Xu7725b892010-05-11 18:29:38 +08004386static void kvm_cpu_vmxon(u64 addr)
4387{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004388 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004389 intel_pt_handle_vmx(1);
4390
Dongxiao Xu7725b892010-05-11 18:29:38 +08004391 asm volatile (ASM_VMX_VMXON_RAX
4392 : : "a"(&addr), "m"(addr)
4393 : "memory", "cc");
4394}
4395
Radim Krčmář13a34e02014-08-28 15:13:03 +02004396static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397{
4398 int cpu = raw_smp_processor_id();
4399 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004400 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004401
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004402 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004403 return -EBUSY;
4404
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004405 /*
4406 * This can happen if we hot-added a CPU but failed to allocate
4407 * VP assist page for it.
4408 */
4409 if (static_branch_unlikely(&enable_evmcs) &&
4410 !hv_get_vp_assist_page(cpu))
4411 return -EFAULT;
4412
Nadav Har'Eld462b812011-05-24 15:26:10 +03004413 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004414 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4415 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004416
4417 /*
4418 * Now we can enable the vmclear operation in kdump
4419 * since the loaded_vmcss_on_cpu list on this cpu
4420 * has been initialized.
4421 *
4422 * Though the cpu is not in VMX operation now, there
4423 * is no problem to enable the vmclear operation
4424 * for the loaded_vmcss_on_cpu list is empty!
4425 */
4426 crash_enable_local_vmclear(cpu);
4427
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004429
4430 test_bits = FEATURE_CONTROL_LOCKED;
4431 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4432 if (tboot_enabled())
4433 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4434
4435 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004437 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4438 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004439 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004440 if (enable_ept)
4441 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004442
4443 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444}
4445
Nadav Har'Eld462b812011-05-24 15:26:10 +03004446static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004447{
4448 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004449 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004450
Nadav Har'Eld462b812011-05-24 15:26:10 +03004451 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4452 loaded_vmcss_on_cpu_link)
4453 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004454}
4455
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004456
4457/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4458 * tricks.
4459 */
4460static void kvm_cpu_vmxoff(void)
4461{
4462 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004463
4464 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004465 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004466}
4467
Radim Krčmář13a34e02014-08-28 15:13:03 +02004468static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004470 vmclear_local_loaded_vmcss();
4471 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004472}
4473
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004474static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004475 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004476{
4477 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004478 u32 ctl = ctl_min | ctl_opt;
4479
4480 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4481
4482 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4483 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4484
4485 /* Ensure minimum (required) set of control bits are supported. */
4486 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004487 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004488
4489 *result = ctl;
4490 return 0;
4491}
4492
Avi Kivity110312c2010-12-21 12:54:20 +02004493static __init bool allow_1_setting(u32 msr, u32 ctl)
4494{
4495 u32 vmx_msr_low, vmx_msr_high;
4496
4497 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4498 return vmx_msr_high & ctl;
4499}
4500
Yang, Sheng002c7f72007-07-31 14:23:01 +03004501static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004502{
4503 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004504 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004505 u32 _pin_based_exec_control = 0;
4506 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004507 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004508 u32 _vmexit_control = 0;
4509 u32 _vmentry_control = 0;
4510
Paolo Bonzini13893092018-02-26 13:40:09 +01004511 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304512 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004513#ifdef CONFIG_X86_64
4514 CPU_BASED_CR8_LOAD_EXITING |
4515 CPU_BASED_CR8_STORE_EXITING |
4516#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004517 CPU_BASED_CR3_LOAD_EXITING |
4518 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004519 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004520 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004521 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004522 CPU_BASED_MWAIT_EXITING |
4523 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004524 CPU_BASED_INVLPG_EXITING |
4525 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004526
Sheng Yangf78e0e22007-10-29 09:40:42 +08004527 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004528 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004529 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004530 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4531 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004532 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004533#ifdef CONFIG_X86_64
4534 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4535 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4536 ~CPU_BASED_CR8_STORE_EXITING;
4537#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004538 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004539 min2 = 0;
4540 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004541 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004542 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004543 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004544 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004545 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004546 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004547 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004548 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004549 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004550 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004551 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004552 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004553 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004554 SECONDARY_EXEC_RDSEED_EXITING |
4555 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004556 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004557 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004558 SECONDARY_EXEC_ENABLE_VMFUNC |
4559 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004560 if (adjust_vmx_controls(min2, opt2,
4561 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004562 &_cpu_based_2nd_exec_control) < 0)
4563 return -EIO;
4564 }
4565#ifndef CONFIG_X86_64
4566 if (!(_cpu_based_2nd_exec_control &
4567 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4568 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4569#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004570
4571 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4572 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004573 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004574 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4575 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004576
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004577 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4578 &vmx_capability.ept, &vmx_capability.vpid);
4579
Sheng Yangd56f5462008-04-25 10:13:16 +08004580 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004581 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4582 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004583 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4584 CPU_BASED_CR3_STORE_EXITING |
4585 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004586 } else if (vmx_capability.ept) {
4587 vmx_capability.ept = 0;
4588 pr_warn_once("EPT CAP should not exist if not support "
4589 "1-setting enable EPT VM-execution control\n");
4590 }
4591 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4592 vmx_capability.vpid) {
4593 vmx_capability.vpid = 0;
4594 pr_warn_once("VPID CAP should not exist if not support "
4595 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004596 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004597
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004598 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004599#ifdef CONFIG_X86_64
4600 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4601#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004602 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004603 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004604 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4605 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004606 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004607
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004608 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4609 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4610 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004611 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4612 &_pin_based_exec_control) < 0)
4613 return -EIO;
4614
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004615 if (cpu_has_broken_vmx_preemption_timer())
4616 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004617 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004618 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004619 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4620
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004621 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004622 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004623 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4624 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004625 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004626
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004627 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004628
4629 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4630 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004631 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004632
4633#ifdef CONFIG_X86_64
4634 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4635 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004636 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637#endif
4638
4639 /* Require Write-Back (WB) memory type for VMCS accesses. */
4640 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004641 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004642
Yang, Sheng002c7f72007-07-31 14:23:01 +03004643 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004644 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004645 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004646
Liran Alon2307af12018-06-29 22:59:04 +03004647 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004648
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4650 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004651 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004652 vmcs_conf->vmexit_ctrl = _vmexit_control;
4653 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004654
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004655 if (static_branch_unlikely(&enable_evmcs))
4656 evmcs_sanitize_exec_ctrls(vmcs_conf);
4657
Avi Kivity110312c2010-12-21 12:54:20 +02004658 cpu_has_load_ia32_efer =
4659 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4660 VM_ENTRY_LOAD_IA32_EFER)
4661 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4662 VM_EXIT_LOAD_IA32_EFER);
4663
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004664 cpu_has_load_perf_global_ctrl =
4665 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4666 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4667 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4668 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4669
4670 /*
4671 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004672 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004673 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4674 *
4675 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4676 *
4677 * AAK155 (model 26)
4678 * AAP115 (model 30)
4679 * AAT100 (model 37)
4680 * BC86,AAY89,BD102 (model 44)
4681 * BA97 (model 46)
4682 *
4683 */
4684 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4685 switch (boot_cpu_data.x86_model) {
4686 case 26:
4687 case 30:
4688 case 37:
4689 case 44:
4690 case 46:
4691 cpu_has_load_perf_global_ctrl = false;
4692 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4693 "does not work properly. Using workaround\n");
4694 break;
4695 default:
4696 break;
4697 }
4698 }
4699
Borislav Petkov782511b2016-04-04 22:25:03 +02004700 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004701 rdmsrl(MSR_IA32_XSS, host_xss);
4702
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004703 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004704}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004705
Liran Alon491a6032018-06-23 02:35:12 +03004706static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707{
4708 int node = cpu_to_node(cpu);
4709 struct page *pages;
4710 struct vmcs *vmcs;
4711
Vlastimil Babka96db8002015-09-08 15:03:50 -07004712 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713 if (!pages)
4714 return NULL;
4715 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004716 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004717
4718 /* KVM supports Enlightened VMCS v1 only */
4719 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004720 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004721 else
Liran Alon392b2f22018-06-23 02:35:01 +03004722 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004723
Liran Alon491a6032018-06-23 02:35:12 +03004724 if (shadow)
4725 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 return vmcs;
4727}
4728
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729static void free_vmcs(struct vmcs *vmcs)
4730{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004731 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732}
4733
Nadav Har'Eld462b812011-05-24 15:26:10 +03004734/*
4735 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4736 */
4737static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4738{
4739 if (!loaded_vmcs->vmcs)
4740 return;
4741 loaded_vmcs_clear(loaded_vmcs);
4742 free_vmcs(loaded_vmcs->vmcs);
4743 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004744 if (loaded_vmcs->msr_bitmap)
4745 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004746 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004747}
4748
Liran Alon491a6032018-06-23 02:35:12 +03004749static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004750{
Liran Alon491a6032018-06-23 02:35:12 +03004751 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004752}
4753
4754static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4755{
Liran Alon491a6032018-06-23 02:35:12 +03004756 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004757 if (!loaded_vmcs->vmcs)
4758 return -ENOMEM;
4759
4760 loaded_vmcs->shadow_vmcs = NULL;
4761 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004762
4763 if (cpu_has_vmx_msr_bitmap()) {
4764 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4765 if (!loaded_vmcs->msr_bitmap)
4766 goto out_vmcs;
4767 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004768
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004769 if (IS_ENABLED(CONFIG_HYPERV) &&
4770 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004771 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4772 struct hv_enlightened_vmcs *evmcs =
4773 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4774
4775 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4776 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004777 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004778
4779 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4780
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004781 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004782
4783out_vmcs:
4784 free_loaded_vmcs(loaded_vmcs);
4785 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004786}
4787
Sam Ravnborg39959582007-06-01 00:47:13 -07004788static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789{
4790 int cpu;
4791
Zachary Amsden3230bb42009-09-29 11:38:37 -10004792 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004794 per_cpu(vmxarea, cpu) = NULL;
4795 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796}
4797
Jim Mattsond37f4262017-12-22 12:12:16 -08004798enum vmcs_field_width {
4799 VMCS_FIELD_WIDTH_U16 = 0,
4800 VMCS_FIELD_WIDTH_U64 = 1,
4801 VMCS_FIELD_WIDTH_U32 = 2,
4802 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004803};
4804
Jim Mattsond37f4262017-12-22 12:12:16 -08004805static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004806{
4807 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004808 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004809 return (field >> 13) & 0x3 ;
4810}
4811
4812static inline int vmcs_field_readonly(unsigned long field)
4813{
4814 return (((field >> 10) & 0x3) == 1);
4815}
4816
Bandan Dasfe2b2012014-04-21 15:20:14 -04004817static void init_vmcs_shadow_fields(void)
4818{
4819 int i, j;
4820
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004821 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4822 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004823 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004824 (i + 1 == max_shadow_read_only_fields ||
4825 shadow_read_only_fields[i + 1] != field + 1))
4826 pr_err("Missing field from shadow_read_only_field %x\n",
4827 field + 1);
4828
4829 clear_bit(field, vmx_vmread_bitmap);
4830#ifdef CONFIG_X86_64
4831 if (field & 1)
4832 continue;
4833#endif
4834 if (j < i)
4835 shadow_read_only_fields[j] = field;
4836 j++;
4837 }
4838 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004839
4840 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004841 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004842 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004843 (i + 1 == max_shadow_read_write_fields ||
4844 shadow_read_write_fields[i + 1] != field + 1))
4845 pr_err("Missing field from shadow_read_write_field %x\n",
4846 field + 1);
4847
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004848 /*
4849 * PML and the preemption timer can be emulated, but the
4850 * processor cannot vmwrite to fields that don't exist
4851 * on bare metal.
4852 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004853 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004854 case GUEST_PML_INDEX:
4855 if (!cpu_has_vmx_pml())
4856 continue;
4857 break;
4858 case VMX_PREEMPTION_TIMER_VALUE:
4859 if (!cpu_has_vmx_preemption_timer())
4860 continue;
4861 break;
4862 case GUEST_INTR_STATUS:
4863 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004864 continue;
4865 break;
4866 default:
4867 break;
4868 }
4869
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004870 clear_bit(field, vmx_vmwrite_bitmap);
4871 clear_bit(field, vmx_vmread_bitmap);
4872#ifdef CONFIG_X86_64
4873 if (field & 1)
4874 continue;
4875#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004876 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004877 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004878 j++;
4879 }
4880 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004881}
4882
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883static __init int alloc_kvm_area(void)
4884{
4885 int cpu;
4886
Zachary Amsden3230bb42009-09-29 11:38:37 -10004887 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 struct vmcs *vmcs;
4889
Liran Alon491a6032018-06-23 02:35:12 +03004890 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004891 if (!vmcs) {
4892 free_kvm_area();
4893 return -ENOMEM;
4894 }
4895
Liran Alon2307af12018-06-29 22:59:04 +03004896 /*
4897 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4898 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4899 * revision_id reported by MSR_IA32_VMX_BASIC.
4900 *
4901 * However, even though not explictly documented by
4902 * TLFS, VMXArea passed as VMXON argument should
4903 * still be marked with revision_id reported by
4904 * physical CPU.
4905 */
4906 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004907 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004908
Avi Kivity6aa8b732006-12-10 02:21:36 -08004909 per_cpu(vmxarea, cpu) = vmcs;
4910 }
4911 return 0;
4912}
4913
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004914static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004915 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004916{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004917 if (!emulate_invalid_guest_state) {
4918 /*
4919 * CS and SS RPL should be equal during guest entry according
4920 * to VMX spec, but in reality it is not always so. Since vcpu
4921 * is in the middle of the transition from real mode to
4922 * protected mode it is safe to assume that RPL 0 is a good
4923 * default value.
4924 */
4925 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004926 save->selector &= ~SEGMENT_RPL_MASK;
4927 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004928 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004929 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004930 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004931}
4932
4933static void enter_pmode(struct kvm_vcpu *vcpu)
4934{
4935 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937
Gleb Natapovd99e4152012-12-20 16:57:45 +02004938 /*
4939 * Update real mode segment cache. It may be not up-to-date if sement
4940 * register was written while vcpu was in a guest mode.
4941 */
4942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4948
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004949 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004950
Avi Kivity2fb92db2011-04-27 19:42:18 +03004951 vmx_segment_cache_clear(vmx);
4952
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004953 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004954
4955 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004956 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4957 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958 vmcs_writel(GUEST_RFLAGS, flags);
4959
Rusty Russell66aee912007-07-17 23:34:16 +10004960 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4961 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004962
4963 update_exception_bitmap(vcpu);
4964
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004965 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4966 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4967 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4968 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4969 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4970 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971}
4972
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004973static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974{
Mathias Krause772e0312012-08-30 01:30:19 +02004975 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004976 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004977
Gleb Natapovd99e4152012-12-20 16:57:45 +02004978 var.dpl = 0x3;
4979 if (seg == VCPU_SREG_CS)
4980 var.type = 0x3;
4981
4982 if (!emulate_invalid_guest_state) {
4983 var.selector = var.base >> 4;
4984 var.base = var.base & 0xffff0;
4985 var.limit = 0xffff;
4986 var.g = 0;
4987 var.db = 0;
4988 var.present = 1;
4989 var.s = 1;
4990 var.l = 0;
4991 var.unusable = 0;
4992 var.type = 0x3;
4993 var.avl = 0;
4994 if (save->base & 0xf)
4995 printk_once(KERN_WARNING "kvm: segment base is not "
4996 "paragraph aligned when entering "
4997 "protected mode (seg=%d)", seg);
4998 }
4999
5000 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005001 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005002 vmcs_write32(sf->limit, var.limit);
5003 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005004}
5005
5006static void enter_rmode(struct kvm_vcpu *vcpu)
5007{
5008 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005009 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005010 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005019
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005020 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005021
Gleb Natapov776e58e2011-03-13 12:34:27 +02005022 /*
5023 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005024 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005025 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005026 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005027 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5028 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005029
Avi Kivity2fb92db2011-04-27 19:42:18 +03005030 vmx_segment_cache_clear(vmx);
5031
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005032 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5035
5036 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005037 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005039 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040
5041 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005042 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005043 update_exception_bitmap(vcpu);
5044
Gleb Natapovd99e4152012-12-20 16:57:45 +02005045 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5046 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5047 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5048 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5049 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5050 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005051
Eddie Dong8668a3c2007-10-10 14:26:45 +08005052 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053}
5054
Amit Shah401d10d2009-02-20 22:53:37 +05305055static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5056{
5057 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005058 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5059
5060 if (!msr)
5061 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305062
Avi Kivityf6801df2010-01-21 15:31:50 +02005063 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305064 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005065 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305066 msr->data = efer;
5067 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005068 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305069
5070 msr->data = efer & ~EFER_LME;
5071 }
5072 setup_msrs(vmx);
5073}
5074
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005075#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005076
5077static void enter_lmode(struct kvm_vcpu *vcpu)
5078{
5079 u32 guest_tr_ar;
5080
Avi Kivity2fb92db2011-04-27 19:42:18 +03005081 vmx_segment_cache_clear(to_vmx(vcpu));
5082
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005084 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005085 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5086 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005087 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005088 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5089 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005090 }
Avi Kivityda38f432010-07-06 11:30:49 +03005091 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092}
5093
5094static void exit_lmode(struct kvm_vcpu *vcpu)
5095{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005096 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005097 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005098}
5099
5100#endif
5101
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005102static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5103 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005104{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005105 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005106 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5107 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005108 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005109 } else {
5110 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005111 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005112}
5113
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005114static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005115{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005116 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005117}
5118
Junaid Shahidfaff8752018-06-29 13:10:05 -07005119static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5120{
5121 int vpid = to_vmx(vcpu)->vpid;
5122
5123 if (!vpid_sync_vcpu_addr(vpid, addr))
5124 vpid_sync_context(vpid);
5125
5126 /*
5127 * If VPIDs are not supported or enabled, then the above is a no-op.
5128 * But we don't really need a TLB flush in that case anyway, because
5129 * each VM entry/exit includes an implicit flush when VPID is 0.
5130 */
5131}
5132
Avi Kivitye8467fd2009-12-29 18:43:06 +02005133static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5134{
5135 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5136
5137 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5138 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5139}
5140
Avi Kivityaff48ba2010-12-05 18:56:11 +02005141static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5142{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005143 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005144 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5145 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5146}
5147
Anthony Liguori25c4c272007-04-27 09:29:21 +03005148static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005149{
Avi Kivityfc78f512009-12-07 12:16:48 +02005150 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5151
5152 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5153 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005154}
5155
Sheng Yang14394422008-04-28 12:24:45 +08005156static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5157{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005158 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5159
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005160 if (!test_bit(VCPU_EXREG_PDPTR,
5161 (unsigned long *)&vcpu->arch.regs_dirty))
5162 return;
5163
Sheng Yang14394422008-04-28 12:24:45 +08005164 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005165 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5166 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5167 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5168 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005169 }
5170}
5171
Avi Kivity8f5d5492009-05-31 18:41:29 +03005172static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5173{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005174 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5175
Avi Kivity8f5d5492009-05-31 18:41:29 +03005176 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005177 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5178 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5179 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5180 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005181 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005182
5183 __set_bit(VCPU_EXREG_PDPTR,
5184 (unsigned long *)&vcpu->arch.regs_avail);
5185 __set_bit(VCPU_EXREG_PDPTR,
5186 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005187}
5188
David Matlack38991522016-11-29 18:14:08 -08005189static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5190{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005191 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5192 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005193 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5194
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005195 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005196 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5197 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5198 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5199
5200 return fixed_bits_valid(val, fixed0, fixed1);
5201}
5202
5203static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5204{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005205 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5206 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005207
5208 return fixed_bits_valid(val, fixed0, fixed1);
5209}
5210
5211static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5212{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005213 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5214 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005215
5216 return fixed_bits_valid(val, fixed0, fixed1);
5217}
5218
5219/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5220#define nested_guest_cr4_valid nested_cr4_valid
5221#define nested_host_cr4_valid nested_cr4_valid
5222
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005223static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005224
5225static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5226 unsigned long cr0,
5227 struct kvm_vcpu *vcpu)
5228{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005229 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5230 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005231 if (!(cr0 & X86_CR0_PG)) {
5232 /* From paging/starting to nonpaging */
5233 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005234 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005235 (CPU_BASED_CR3_LOAD_EXITING |
5236 CPU_BASED_CR3_STORE_EXITING));
5237 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005238 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005239 } else if (!is_paging(vcpu)) {
5240 /* From nonpaging to paging */
5241 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005242 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005243 ~(CPU_BASED_CR3_LOAD_EXITING |
5244 CPU_BASED_CR3_STORE_EXITING));
5245 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005246 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005247 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005248
5249 if (!(cr0 & X86_CR0_WP))
5250 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005251}
5252
Avi Kivity6aa8b732006-12-10 02:21:36 -08005253static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5254{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005255 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005256 unsigned long hw_cr0;
5257
Gleb Natapov50378782013-02-04 16:00:28 +02005258 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005259 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005260 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005261 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005262 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005263
Gleb Natapov218e7632013-01-21 15:36:45 +02005264 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5265 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005266
Gleb Natapov218e7632013-01-21 15:36:45 +02005267 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5268 enter_rmode(vcpu);
5269 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005271#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005272 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005273 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005274 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005275 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005276 exit_lmode(vcpu);
5277 }
5278#endif
5279
Sean Christophersonb4d18512018-03-05 12:04:40 -08005280 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005281 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5282
Avi Kivity6aa8b732006-12-10 02:21:36 -08005283 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005284 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005285 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005286
5287 /* depends on vcpu->arch.cr0 to be set to a new value */
5288 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005289}
5290
Yu Zhang855feb62017-08-24 20:27:55 +08005291static int get_ept_level(struct kvm_vcpu *vcpu)
5292{
5293 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5294 return 5;
5295 return 4;
5296}
5297
Peter Feiner995f00a2017-06-30 17:26:32 -07005298static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005299{
Yu Zhang855feb62017-08-24 20:27:55 +08005300 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005301
Yu Zhang855feb62017-08-24 20:27:55 +08005302 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005303
Peter Feiner995f00a2017-06-30 17:26:32 -07005304 if (enable_ept_ad_bits &&
5305 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005306 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005307 eptp |= (root_hpa & PAGE_MASK);
5308
5309 return eptp;
5310}
5311
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5313{
Tianyu Lan877ad952018-07-19 08:40:23 +00005314 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005315 unsigned long guest_cr3;
5316 u64 eptp;
5317
5318 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005319 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005320 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005321 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005322
5323 if (kvm_x86_ops->tlb_remote_flush) {
5324 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5325 to_vmx(vcpu)->ept_pointer = eptp;
5326 to_kvm_vmx(kvm)->ept_pointers_match
5327 = EPT_POINTERS_CHECK;
5328 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5329 }
5330
Sean Christophersone90008d2018-03-05 12:04:37 -08005331 if (enable_unrestricted_guest || is_paging(vcpu) ||
5332 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005333 guest_cr3 = kvm_read_cr3(vcpu);
5334 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005335 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005336 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005337 }
5338
Sheng Yang14394422008-04-28 12:24:45 +08005339 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340}
5341
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005342static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005343{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005344 /*
5345 * Pass through host's Machine Check Enable value to hw_cr4, which
5346 * is in force while we are in guest mode. Do not let guests control
5347 * this bit, even if host CR4.MCE == 0.
5348 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005349 unsigned long hw_cr4;
5350
5351 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5352 if (enable_unrestricted_guest)
5353 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5354 else if (to_vmx(vcpu)->rmode.vm86_active)
5355 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5356 else
5357 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005358
Sean Christopherson64f7a112018-04-30 10:01:06 -07005359 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5360 if (cr4 & X86_CR4_UMIP) {
5361 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005362 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005363 hw_cr4 &= ~X86_CR4_UMIP;
5364 } else if (!is_guest_mode(vcpu) ||
5365 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5366 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5367 SECONDARY_EXEC_DESC);
5368 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005369
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005370 if (cr4 & X86_CR4_VMXE) {
5371 /*
5372 * To use VMXON (and later other VMX instructions), a guest
5373 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5374 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005375 * is here. We operate under the default treatment of SMM,
5376 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005377 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005378 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005379 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005380 }
David Matlack38991522016-11-29 18:14:08 -08005381
5382 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005383 return 1;
5384
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005385 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005386
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005387 if (!enable_unrestricted_guest) {
5388 if (enable_ept) {
5389 if (!is_paging(vcpu)) {
5390 hw_cr4 &= ~X86_CR4_PAE;
5391 hw_cr4 |= X86_CR4_PSE;
5392 } else if (!(cr4 & X86_CR4_PAE)) {
5393 hw_cr4 &= ~X86_CR4_PAE;
5394 }
5395 }
5396
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005397 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005398 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5399 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5400 * to be manually disabled when guest switches to non-paging
5401 * mode.
5402 *
5403 * If !enable_unrestricted_guest, the CPU is always running
5404 * with CR0.PG=1 and CR4 needs to be modified.
5405 * If enable_unrestricted_guest, the CPU automatically
5406 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005407 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005408 if (!is_paging(vcpu))
5409 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5410 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005411
Sheng Yang14394422008-04-28 12:24:45 +08005412 vmcs_writel(CR4_READ_SHADOW, cr4);
5413 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005414 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005415}
5416
Avi Kivity6aa8b732006-12-10 02:21:36 -08005417static void vmx_get_segment(struct kvm_vcpu *vcpu,
5418 struct kvm_segment *var, int seg)
5419{
Avi Kivitya9179492011-01-03 14:28:52 +02005420 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005421 u32 ar;
5422
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005423 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005424 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005425 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005426 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005427 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005428 var->base = vmx_read_guest_seg_base(vmx, seg);
5429 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5430 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005431 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005432 var->base = vmx_read_guest_seg_base(vmx, seg);
5433 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5434 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5435 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005436 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005437 var->type = ar & 15;
5438 var->s = (ar >> 4) & 1;
5439 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005440 /*
5441 * Some userspaces do not preserve unusable property. Since usable
5442 * segment has to be present according to VMX spec we can use present
5443 * property to amend userspace bug by making unusable segment always
5444 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5445 * segment as unusable.
5446 */
5447 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005448 var->avl = (ar >> 12) & 1;
5449 var->l = (ar >> 13) & 1;
5450 var->db = (ar >> 14) & 1;
5451 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005452}
5453
Avi Kivitya9179492011-01-03 14:28:52 +02005454static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5455{
Avi Kivitya9179492011-01-03 14:28:52 +02005456 struct kvm_segment s;
5457
5458 if (to_vmx(vcpu)->rmode.vm86_active) {
5459 vmx_get_segment(vcpu, &s, seg);
5460 return s.base;
5461 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005462 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005463}
5464
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005465static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005466{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005467 struct vcpu_vmx *vmx = to_vmx(vcpu);
5468
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005469 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005470 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005471 else {
5472 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005473 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005474 }
Avi Kivity69c73022011-03-07 15:26:44 +02005475}
5476
Avi Kivity653e3102007-05-07 10:55:37 +03005477static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005478{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005479 u32 ar;
5480
Avi Kivityf0495f92012-06-07 17:06:10 +03005481 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005482 ar = 1 << 16;
5483 else {
5484 ar = var->type & 15;
5485 ar |= (var->s & 1) << 4;
5486 ar |= (var->dpl & 3) << 5;
5487 ar |= (var->present & 1) << 7;
5488 ar |= (var->avl & 1) << 12;
5489 ar |= (var->l & 1) << 13;
5490 ar |= (var->db & 1) << 14;
5491 ar |= (var->g & 1) << 15;
5492 }
Avi Kivity653e3102007-05-07 10:55:37 +03005493
5494 return ar;
5495}
5496
5497static void vmx_set_segment(struct kvm_vcpu *vcpu,
5498 struct kvm_segment *var, int seg)
5499{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005500 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005501 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005502
Avi Kivity2fb92db2011-04-27 19:42:18 +03005503 vmx_segment_cache_clear(vmx);
5504
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005505 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5506 vmx->rmode.segs[seg] = *var;
5507 if (seg == VCPU_SREG_TR)
5508 vmcs_write16(sf->selector, var->selector);
5509 else if (var->s)
5510 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005511 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005512 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005513
Avi Kivity653e3102007-05-07 10:55:37 +03005514 vmcs_writel(sf->base, var->base);
5515 vmcs_write32(sf->limit, var->limit);
5516 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005517
5518 /*
5519 * Fix the "Accessed" bit in AR field of segment registers for older
5520 * qemu binaries.
5521 * IA32 arch specifies that at the time of processor reset the
5522 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005523 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005524 * state vmexit when "unrestricted guest" mode is turned on.
5525 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5526 * tree. Newer qemu binaries with that qemu fix would not need this
5527 * kvm hack.
5528 */
5529 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005530 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005531
Gleb Natapovf924d662012-12-12 19:10:55 +02005532 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005533
5534out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005535 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005536}
5537
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5539{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005540 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541
5542 *db = (ar >> 14) & 1;
5543 *l = (ar >> 13) & 1;
5544}
5545
Gleb Natapov89a27f42010-02-16 10:51:48 +02005546static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005547{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005548 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5549 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550}
5551
Gleb Natapov89a27f42010-02-16 10:51:48 +02005552static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005554 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5555 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556}
5557
Gleb Natapov89a27f42010-02-16 10:51:48 +02005558static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005560 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5561 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562}
5563
Gleb Natapov89a27f42010-02-16 10:51:48 +02005564static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005566 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5567 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005568}
5569
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005570static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5571{
5572 struct kvm_segment var;
5573 u32 ar;
5574
5575 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005576 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005577 if (seg == VCPU_SREG_CS)
5578 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005579 ar = vmx_segment_access_rights(&var);
5580
5581 if (var.base != (var.selector << 4))
5582 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005583 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005584 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005585 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005586 return false;
5587
5588 return true;
5589}
5590
5591static bool code_segment_valid(struct kvm_vcpu *vcpu)
5592{
5593 struct kvm_segment cs;
5594 unsigned int cs_rpl;
5595
5596 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005597 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005598
Avi Kivity1872a3f2009-01-04 23:26:52 +02005599 if (cs.unusable)
5600 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005601 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005602 return false;
5603 if (!cs.s)
5604 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005605 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005606 if (cs.dpl > cs_rpl)
5607 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005608 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005609 if (cs.dpl != cs_rpl)
5610 return false;
5611 }
5612 if (!cs.present)
5613 return false;
5614
5615 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5616 return true;
5617}
5618
5619static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5620{
5621 struct kvm_segment ss;
5622 unsigned int ss_rpl;
5623
5624 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005625 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005626
Avi Kivity1872a3f2009-01-04 23:26:52 +02005627 if (ss.unusable)
5628 return true;
5629 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005630 return false;
5631 if (!ss.s)
5632 return false;
5633 if (ss.dpl != ss_rpl) /* DPL != RPL */
5634 return false;
5635 if (!ss.present)
5636 return false;
5637
5638 return true;
5639}
5640
5641static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5642{
5643 struct kvm_segment var;
5644 unsigned int rpl;
5645
5646 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005647 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005648
Avi Kivity1872a3f2009-01-04 23:26:52 +02005649 if (var.unusable)
5650 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005651 if (!var.s)
5652 return false;
5653 if (!var.present)
5654 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005655 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005656 if (var.dpl < rpl) /* DPL < RPL */
5657 return false;
5658 }
5659
5660 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5661 * rights flags
5662 */
5663 return true;
5664}
5665
5666static bool tr_valid(struct kvm_vcpu *vcpu)
5667{
5668 struct kvm_segment tr;
5669
5670 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5671
Avi Kivity1872a3f2009-01-04 23:26:52 +02005672 if (tr.unusable)
5673 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005674 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005675 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005676 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005677 return false;
5678 if (!tr.present)
5679 return false;
5680
5681 return true;
5682}
5683
5684static bool ldtr_valid(struct kvm_vcpu *vcpu)
5685{
5686 struct kvm_segment ldtr;
5687
5688 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5689
Avi Kivity1872a3f2009-01-04 23:26:52 +02005690 if (ldtr.unusable)
5691 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005692 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005693 return false;
5694 if (ldtr.type != 2)
5695 return false;
5696 if (!ldtr.present)
5697 return false;
5698
5699 return true;
5700}
5701
5702static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5703{
5704 struct kvm_segment cs, ss;
5705
5706 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5707 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5708
Nadav Amitb32a9912015-03-29 16:33:04 +03005709 return ((cs.selector & SEGMENT_RPL_MASK) ==
5710 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005711}
5712
5713/*
5714 * Check if guest state is valid. Returns true if valid, false if
5715 * not.
5716 * We assume that registers are always usable
5717 */
5718static bool guest_state_valid(struct kvm_vcpu *vcpu)
5719{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005720 if (enable_unrestricted_guest)
5721 return true;
5722
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005723 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005724 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005725 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5726 return false;
5727 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5728 return false;
5729 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5730 return false;
5731 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5732 return false;
5733 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5734 return false;
5735 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5736 return false;
5737 } else {
5738 /* protected mode guest state checks */
5739 if (!cs_ss_rpl_check(vcpu))
5740 return false;
5741 if (!code_segment_valid(vcpu))
5742 return false;
5743 if (!stack_segment_valid(vcpu))
5744 return false;
5745 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5746 return false;
5747 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5748 return false;
5749 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5750 return false;
5751 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5752 return false;
5753 if (!tr_valid(vcpu))
5754 return false;
5755 if (!ldtr_valid(vcpu))
5756 return false;
5757 }
5758 /* TODO:
5759 * - Add checks on RIP
5760 * - Add checks on RFLAGS
5761 */
5762
5763 return true;
5764}
5765
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005766static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5767{
5768 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5769}
5770
Mike Dayd77c26f2007-10-08 09:02:08 -04005771static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005772{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005773 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005774 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005775 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005776
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005777 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005778 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005779 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5780 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005781 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005782 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005783 r = kvm_write_guest_page(kvm, fn++, &data,
5784 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005785 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005786 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005787 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5788 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005789 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005790 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5791 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005792 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005793 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005794 r = kvm_write_guest_page(kvm, fn, &data,
5795 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5796 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005797out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005798 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005799 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005800}
5801
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005802static int init_rmode_identity_map(struct kvm *kvm)
5803{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005804 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005805 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005806 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005807 u32 tmp;
5808
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005809 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005810 mutex_lock(&kvm->slots_lock);
5811
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005812 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005813 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005814
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005815 if (!kvm_vmx->ept_identity_map_addr)
5816 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5817 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005818
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005819 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005820 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005821 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005822 goto out2;
5823
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005824 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005825 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5826 if (r < 0)
5827 goto out;
5828 /* Set up identity-mapping pagetable for EPT in real mode */
5829 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5830 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5831 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5832 r = kvm_write_guest_page(kvm, identity_map_pfn,
5833 &tmp, i * sizeof(tmp), sizeof(tmp));
5834 if (r < 0)
5835 goto out;
5836 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005837 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005838
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005839out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005840 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005841
5842out2:
5843 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005844 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005845}
5846
Avi Kivity6aa8b732006-12-10 02:21:36 -08005847static void seg_setup(int seg)
5848{
Mathias Krause772e0312012-08-30 01:30:19 +02005849 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005850 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005851
5852 vmcs_write16(sf->selector, 0);
5853 vmcs_writel(sf->base, 0);
5854 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005855 ar = 0x93;
5856 if (seg == VCPU_SREG_CS)
5857 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005858
5859 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005860}
5861
Sheng Yangf78e0e22007-10-29 09:40:42 +08005862static int alloc_apic_access_page(struct kvm *kvm)
5863{
Xiao Guangrong44841412012-09-07 14:14:20 +08005864 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005865 int r = 0;
5866
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005867 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005868 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005869 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005870 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5871 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005872 if (r)
5873 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005874
Tang Chen73a6d942014-09-11 13:38:00 +08005875 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005876 if (is_error_page(page)) {
5877 r = -EFAULT;
5878 goto out;
5879 }
5880
Tang Chenc24ae0d2014-09-24 15:57:58 +08005881 /*
5882 * Do not pin the page in memory, so that memory hot-unplug
5883 * is able to migrate it.
5884 */
5885 put_page(page);
5886 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005887out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005888 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005889 return r;
5890}
5891
Wanpeng Li991e7a02015-09-16 17:30:05 +08005892static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005893{
5894 int vpid;
5895
Avi Kivity919818a2009-03-23 18:01:29 +02005896 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005897 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005898 spin_lock(&vmx_vpid_lock);
5899 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005900 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005901 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005902 else
5903 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005904 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005905 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005906}
5907
Wanpeng Li991e7a02015-09-16 17:30:05 +08005908static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005909{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005910 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005911 return;
5912 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005913 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005914 spin_unlock(&vmx_vpid_lock);
5915}
5916
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005917static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5918 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005919{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005920 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005921
5922 if (!cpu_has_vmx_msr_bitmap())
5923 return;
5924
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005925 if (static_branch_unlikely(&enable_evmcs))
5926 evmcs_touch_msr_bitmap();
5927
Sheng Yang25c5f222008-03-28 13:18:56 +08005928 /*
5929 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5930 * have the write-low and read-high bitmap offsets the wrong way round.
5931 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5932 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005933 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005934 if (type & MSR_TYPE_R)
5935 /* read-low */
5936 __clear_bit(msr, msr_bitmap + 0x000 / f);
5937
5938 if (type & MSR_TYPE_W)
5939 /* write-low */
5940 __clear_bit(msr, msr_bitmap + 0x800 / f);
5941
Sheng Yang25c5f222008-03-28 13:18:56 +08005942 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5943 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005944 if (type & MSR_TYPE_R)
5945 /* read-high */
5946 __clear_bit(msr, msr_bitmap + 0x400 / f);
5947
5948 if (type & MSR_TYPE_W)
5949 /* write-high */
5950 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5951
5952 }
5953}
5954
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005955static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5956 u32 msr, int type)
5957{
5958 int f = sizeof(unsigned long);
5959
5960 if (!cpu_has_vmx_msr_bitmap())
5961 return;
5962
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005963 if (static_branch_unlikely(&enable_evmcs))
5964 evmcs_touch_msr_bitmap();
5965
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005966 /*
5967 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5968 * have the write-low and read-high bitmap offsets the wrong way round.
5969 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5970 */
5971 if (msr <= 0x1fff) {
5972 if (type & MSR_TYPE_R)
5973 /* read-low */
5974 __set_bit(msr, msr_bitmap + 0x000 / f);
5975
5976 if (type & MSR_TYPE_W)
5977 /* write-low */
5978 __set_bit(msr, msr_bitmap + 0x800 / f);
5979
5980 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5981 msr &= 0x1fff;
5982 if (type & MSR_TYPE_R)
5983 /* read-high */
5984 __set_bit(msr, msr_bitmap + 0x400 / f);
5985
5986 if (type & MSR_TYPE_W)
5987 /* write-high */
5988 __set_bit(msr, msr_bitmap + 0xc00 / f);
5989
5990 }
5991}
5992
5993static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5994 u32 msr, int type, bool value)
5995{
5996 if (value)
5997 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5998 else
5999 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6000}
6001
Wincy Vanf2b93282015-02-03 23:56:03 +08006002/*
6003 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6004 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6005 */
6006static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6007 unsigned long *msr_bitmap_nested,
6008 u32 msr, int type)
6009{
6010 int f = sizeof(unsigned long);
6011
Wincy Vanf2b93282015-02-03 23:56:03 +08006012 /*
6013 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6014 * have the write-low and read-high bitmap offsets the wrong way round.
6015 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6016 */
6017 if (msr <= 0x1fff) {
6018 if (type & MSR_TYPE_R &&
6019 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6020 /* read-low */
6021 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6022
6023 if (type & MSR_TYPE_W &&
6024 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6025 /* write-low */
6026 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6027
6028 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6029 msr &= 0x1fff;
6030 if (type & MSR_TYPE_R &&
6031 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6032 /* read-high */
6033 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6034
6035 if (type & MSR_TYPE_W &&
6036 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6037 /* write-high */
6038 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6039
6040 }
6041}
6042
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006043static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006044{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006045 u8 mode = 0;
6046
6047 if (cpu_has_secondary_exec_ctrls() &&
6048 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6049 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6050 mode |= MSR_BITMAP_MODE_X2APIC;
6051 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6052 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6053 }
6054
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006055 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006056}
6057
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006058#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6059
6060static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6061 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006062{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006063 int msr;
6064
6065 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6066 unsigned word = msr / BITS_PER_LONG;
6067 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6068 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006069 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006070
6071 if (mode & MSR_BITMAP_MODE_X2APIC) {
6072 /*
6073 * TPR reads and writes can be virtualized even if virtual interrupt
6074 * delivery is not in use.
6075 */
6076 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6077 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6078 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6079 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6080 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6081 }
6082 }
6083}
6084
6085static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6086{
6087 struct vcpu_vmx *vmx = to_vmx(vcpu);
6088 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6089 u8 mode = vmx_msr_bitmap_mode(vcpu);
6090 u8 changed = mode ^ vmx->msr_bitmap_mode;
6091
6092 if (!changed)
6093 return;
6094
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006095 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6096 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6097
6098 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006099}
6100
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006101static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006102{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006103 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006104}
6105
David Matlackc9f04402017-08-01 14:00:40 -07006106static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6107{
6108 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6109 gfn_t gfn;
6110
6111 /*
6112 * Don't need to mark the APIC access page dirty; it is never
6113 * written to by the CPU during APIC virtualization.
6114 */
6115
6116 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6117 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6118 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6119 }
6120
6121 if (nested_cpu_has_posted_intr(vmcs12)) {
6122 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6123 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6124 }
6125}
6126
6127
David Hildenbrand6342c502017-01-25 11:58:58 +01006128static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006129{
6130 struct vcpu_vmx *vmx = to_vmx(vcpu);
6131 int max_irr;
6132 void *vapic_page;
6133 u16 status;
6134
David Matlackc9f04402017-08-01 14:00:40 -07006135 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6136 return;
Wincy Van705699a2015-02-03 23:58:17 +08006137
David Matlackc9f04402017-08-01 14:00:40 -07006138 vmx->nested.pi_pending = false;
6139 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6140 return;
Wincy Van705699a2015-02-03 23:58:17 +08006141
David Matlackc9f04402017-08-01 14:00:40 -07006142 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6143 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006144 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006145 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6146 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006147 kunmap(vmx->nested.virtual_apic_page);
6148
6149 status = vmcs_read16(GUEST_INTR_STATUS);
6150 if ((u8)max_irr > ((u8)status & 0xff)) {
6151 status &= ~0xff;
6152 status |= (u8)max_irr;
6153 vmcs_write16(GUEST_INTR_STATUS, status);
6154 }
6155 }
David Matlackc9f04402017-08-01 14:00:40 -07006156
6157 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006158}
6159
Liran Alone6c67d82018-09-04 10:56:52 +03006160static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6161{
6162 struct vcpu_vmx *vmx = to_vmx(vcpu);
6163 void *vapic_page;
6164 u32 vppr;
6165 int rvi;
6166
6167 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6168 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6169 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6170 return false;
6171
6172 rvi = vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6173
6174 vapic_page = kmap(vmx->nested.virtual_apic_page);
6175 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6176 kunmap(vmx->nested.virtual_apic_page);
6177
6178 return ((rvi & 0xf0) > (vppr & 0xf0));
6179}
6180
Wincy Van06a55242017-04-28 13:13:59 +08006181static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6182 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006183{
6184#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006185 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6186
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006187 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006188 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006189 * The vector of interrupt to be delivered to vcpu had
6190 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006191 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006192 * Following cases will be reached in this block, and
6193 * we always send a notification event in all cases as
6194 * explained below.
6195 *
6196 * Case 1: vcpu keeps in non-root mode. Sending a
6197 * notification event posts the interrupt to vcpu.
6198 *
6199 * Case 2: vcpu exits to root mode and is still
6200 * runnable. PIR will be synced to vIRR before the
6201 * next vcpu entry. Sending a notification event in
6202 * this case has no effect, as vcpu is not in root
6203 * mode.
6204 *
6205 * Case 3: vcpu exits to root mode and is blocked.
6206 * vcpu_block() has already synced PIR to vIRR and
6207 * never blocks vcpu if vIRR is not cleared. Therefore,
6208 * a blocked vcpu here does not wait for any requested
6209 * interrupts in PIR, and sending a notification event
6210 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006211 */
Feng Wu28b835d2015-09-18 22:29:54 +08006212
Wincy Van06a55242017-04-28 13:13:59 +08006213 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006214 return true;
6215 }
6216#endif
6217 return false;
6218}
6219
Wincy Van705699a2015-02-03 23:58:17 +08006220static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6221 int vector)
6222{
6223 struct vcpu_vmx *vmx = to_vmx(vcpu);
6224
6225 if (is_guest_mode(vcpu) &&
6226 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006227 /*
6228 * If a posted intr is not recognized by hardware,
6229 * we will accomplish it in the next vmentry.
6230 */
6231 vmx->nested.pi_pending = true;
6232 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006233 /* the PIR and ON have been set by L1. */
6234 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6235 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006236 return 0;
6237 }
6238 return -1;
6239}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006240/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006241 * Send interrupt to vcpu via posted interrupt way.
6242 * 1. If target vcpu is running(non-root mode), send posted interrupt
6243 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6244 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6245 * interrupt from PIR in next vmentry.
6246 */
6247static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6248{
6249 struct vcpu_vmx *vmx = to_vmx(vcpu);
6250 int r;
6251
Wincy Van705699a2015-02-03 23:58:17 +08006252 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6253 if (!r)
6254 return;
6255
Yang Zhanga20ed542013-04-11 19:25:15 +08006256 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6257 return;
6258
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006259 /* If a previous notification has sent the IPI, nothing to do. */
6260 if (pi_test_and_set_on(&vmx->pi_desc))
6261 return;
6262
Wincy Van06a55242017-04-28 13:13:59 +08006263 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006264 kvm_vcpu_kick(vcpu);
6265}
6266
Avi Kivity6aa8b732006-12-10 02:21:36 -08006267/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006268 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6269 * will not change in the lifetime of the guest.
6270 * Note that host-state that does change is set elsewhere. E.g., host-state
6271 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6272 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006273static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006274{
6275 u32 low32, high32;
6276 unsigned long tmpl;
6277 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006278 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006279
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006280 cr0 = read_cr0();
6281 WARN_ON(cr0 & X86_CR0_TS);
6282 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006283
6284 /*
6285 * Save the most likely value for this task's CR3 in the VMCS.
6286 * We can't use __get_current_cr3_fast() because we're not atomic.
6287 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006288 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006289 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006290 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006291
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006292 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006293 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006294 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006295 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006296
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006297 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006298#ifdef CONFIG_X86_64
6299 /*
6300 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006301 * vmx_prepare_switch_to_host(), in case userspace uses
6302 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006303 */
6304 vmcs_write16(HOST_DS_SELECTOR, 0);
6305 vmcs_write16(HOST_ES_SELECTOR, 0);
6306#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006307 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6308 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006309#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006310 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6311 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6312
Juergen Gross87930012017-09-04 12:25:27 +02006313 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006314 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006315 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006316
Avi Kivity83287ea422012-09-16 15:10:57 +03006317 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006318
6319 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6320 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6321 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6322 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6323
6324 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6325 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6326 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6327 }
6328}
6329
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006330static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6331{
6332 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6333 if (enable_ept)
6334 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006335 if (is_guest_mode(&vmx->vcpu))
6336 vmx->vcpu.arch.cr4_guest_owned_bits &=
6337 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006338 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6339}
6340
Yang Zhang01e439b2013-04-11 19:25:12 +08006341static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6342{
6343 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6344
Andrey Smetanind62caab2015-11-10 15:36:33 +03006345 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006346 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006347
6348 if (!enable_vnmi)
6349 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6350
Yunhong Jiang64672c92016-06-13 14:19:59 -07006351 /* Enable the preemption timer dynamically */
6352 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006353 return pin_based_exec_ctrl;
6354}
6355
Andrey Smetanind62caab2015-11-10 15:36:33 +03006356static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6357{
6358 struct vcpu_vmx *vmx = to_vmx(vcpu);
6359
6360 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006361 if (cpu_has_secondary_exec_ctrls()) {
6362 if (kvm_vcpu_apicv_active(vcpu))
6363 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6364 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6365 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6366 else
6367 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6368 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6369 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6370 }
6371
6372 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006373 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006374}
6375
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006376static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6377{
6378 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006379
6380 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6381 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6382
Paolo Bonzini35754c92015-07-29 12:05:37 +02006383 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006384 exec_control &= ~CPU_BASED_TPR_SHADOW;
6385#ifdef CONFIG_X86_64
6386 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6387 CPU_BASED_CR8_LOAD_EXITING;
6388#endif
6389 }
6390 if (!enable_ept)
6391 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6392 CPU_BASED_CR3_LOAD_EXITING |
6393 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006394 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6395 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6396 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006397 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6398 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006399 return exec_control;
6400}
6401
Jim Mattson45ec3682017-08-23 16:32:04 -07006402static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006403{
Jim Mattson45ec3682017-08-23 16:32:04 -07006404 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006405 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006406}
6407
Jim Mattson75f4fc82017-08-23 16:32:03 -07006408static bool vmx_rdseed_supported(void)
6409{
6410 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006411 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006412}
6413
Paolo Bonzini80154d72017-08-24 13:55:35 +02006414static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006415{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006416 struct kvm_vcpu *vcpu = &vmx->vcpu;
6417
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006418 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006419
Paolo Bonzini80154d72017-08-24 13:55:35 +02006420 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006421 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6422 if (vmx->vpid == 0)
6423 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6424 if (!enable_ept) {
6425 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6426 enable_unrestricted_guest = 0;
6427 }
6428 if (!enable_unrestricted_guest)
6429 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006430 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006432 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006433 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6434 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006435 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006436
6437 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6438 * in vmx_set_cr4. */
6439 exec_control &= ~SECONDARY_EXEC_DESC;
6440
Abel Gordonabc4fc52013-04-18 14:35:25 +03006441 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6442 (handle_vmptrld).
6443 We can NOT enable shadow_vmcs here because we don't have yet
6444 a current VMCS12
6445 */
6446 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006447
6448 if (!enable_pml)
6449 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006450
Paolo Bonzini3db13482017-08-24 14:48:03 +02006451 if (vmx_xsaves_supported()) {
6452 /* Exposing XSAVES only when XSAVE is exposed */
6453 bool xsaves_enabled =
6454 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6455 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6456
6457 if (!xsaves_enabled)
6458 exec_control &= ~SECONDARY_EXEC_XSAVES;
6459
6460 if (nested) {
6461 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006462 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006463 SECONDARY_EXEC_XSAVES;
6464 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006465 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006466 ~SECONDARY_EXEC_XSAVES;
6467 }
6468 }
6469
Paolo Bonzini80154d72017-08-24 13:55:35 +02006470 if (vmx_rdtscp_supported()) {
6471 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6472 if (!rdtscp_enabled)
6473 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6474
6475 if (nested) {
6476 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006477 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006478 SECONDARY_EXEC_RDTSCP;
6479 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006480 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006481 ~SECONDARY_EXEC_RDTSCP;
6482 }
6483 }
6484
6485 if (vmx_invpcid_supported()) {
6486 /* Exposing INVPCID only when PCID is exposed */
6487 bool invpcid_enabled =
6488 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6489 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6490
6491 if (!invpcid_enabled) {
6492 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6493 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6494 }
6495
6496 if (nested) {
6497 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006498 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006499 SECONDARY_EXEC_ENABLE_INVPCID;
6500 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006501 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006502 ~SECONDARY_EXEC_ENABLE_INVPCID;
6503 }
6504 }
6505
Jim Mattson45ec3682017-08-23 16:32:04 -07006506 if (vmx_rdrand_supported()) {
6507 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6508 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006509 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006510
6511 if (nested) {
6512 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006513 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006514 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006515 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006516 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006517 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006518 }
6519 }
6520
Jim Mattson75f4fc82017-08-23 16:32:03 -07006521 if (vmx_rdseed_supported()) {
6522 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6523 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006524 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006525
6526 if (nested) {
6527 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006528 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006529 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006530 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006531 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006532 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006533 }
6534 }
6535
Paolo Bonzini80154d72017-08-24 13:55:35 +02006536 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006537}
6538
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006539static void ept_set_mmio_spte_mask(void)
6540{
6541 /*
6542 * EPT Misconfigurations can be generated if the value of bits 2:0
6543 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006544 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006545 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6546 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006547}
6548
Wanpeng Lif53cd632014-12-02 19:14:58 +08006549#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006550/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006551 * Sets up the vmcs for emulated real mode.
6552 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006553static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006554{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006555 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006556
Abel Gordon4607c2d2013-04-18 14:35:55 +03006557 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006558 /*
6559 * At vCPU creation, "VMWRITE to any supported field
6560 * in the VMCS" is supported, so use the more
6561 * permissive vmx_vmread_bitmap to specify both read
6562 * and write permissions for the shadow VMCS.
6563 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006564 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006565 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006566 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006567 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006568 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006569
Avi Kivity6aa8b732006-12-10 02:21:36 -08006570 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6571
Avi Kivity6aa8b732006-12-10 02:21:36 -08006572 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006573 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006574 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006575
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006576 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006577
Dan Williamsdfa169b2016-06-02 11:17:24 -07006578 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006579 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006580 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006581 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006582 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006583
Andrey Smetanind62caab2015-11-10 15:36:33 +03006584 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006585 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6586 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6587 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6588 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6589
6590 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006591
Li RongQing0bcf2612015-12-03 13:29:34 +08006592 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006593 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006594 }
6595
Wanpeng Lib31c1142018-03-12 04:53:04 -07006596 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006597 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006598 vmx->ple_window = ple_window;
6599 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006600 }
6601
Xiao Guangrongc3707952011-07-12 03:28:04 +08006602 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6603 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006604 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6605
Avi Kivity9581d442010-10-19 16:46:55 +02006606 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6607 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006608 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006609 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6610 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006611
Bandan Das2a499e42017-08-03 15:54:41 -04006612 if (cpu_has_vmx_vmfunc())
6613 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6614
Eddie Dong2cc51562007-05-21 07:28:09 +03006615 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6616 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006617 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006618 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006619 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006620
Radim Krčmář74545702015-04-27 15:11:25 +02006621 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6622 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006623
Paolo Bonzini03916db2014-07-24 14:21:57 +02006624 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006625 u32 index = vmx_msr_index[i];
6626 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006627 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006628
6629 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6630 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006631 if (wrmsr_safe(index, data_low, data_high) < 0)
6632 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006633 vmx->guest_msrs[j].index = i;
6634 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006635 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006636 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006637 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006638
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006639 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006640
6641 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642
6643 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006644 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006645
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006646 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6647 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6648
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006649 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006650
Wanpeng Lif53cd632014-12-02 19:14:58 +08006651 if (vmx_xsaves_supported())
6652 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6653
Peter Feiner4e595162016-07-07 14:49:58 -07006654 if (enable_pml) {
6655 ASSERT(vmx->pml_pg);
6656 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6657 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6658 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006659
6660 if (cpu_has_vmx_encls_vmexit())
6661 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006662}
6663
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006664static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006665{
6666 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006667 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006668 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006669
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006670 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006671 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006672
Wanpeng Li518e7b92018-02-28 14:03:31 +08006673 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006674 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006675 kvm_set_cr8(vcpu, 0);
6676
6677 if (!init_event) {
6678 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6679 MSR_IA32_APICBASE_ENABLE;
6680 if (kvm_vcpu_is_reset_bsp(vcpu))
6681 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6682 apic_base_msr.host_initiated = true;
6683 kvm_set_apic_base(vcpu, &apic_base_msr);
6684 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006685
Avi Kivity2fb92db2011-04-27 19:42:18 +03006686 vmx_segment_cache_clear(vmx);
6687
Avi Kivity5706be02008-08-20 15:07:31 +03006688 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006689 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006690 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006691
6692 seg_setup(VCPU_SREG_DS);
6693 seg_setup(VCPU_SREG_ES);
6694 seg_setup(VCPU_SREG_FS);
6695 seg_setup(VCPU_SREG_GS);
6696 seg_setup(VCPU_SREG_SS);
6697
6698 vmcs_write16(GUEST_TR_SELECTOR, 0);
6699 vmcs_writel(GUEST_TR_BASE, 0);
6700 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6701 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6702
6703 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6704 vmcs_writel(GUEST_LDTR_BASE, 0);
6705 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6706 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6707
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006708 if (!init_event) {
6709 vmcs_write32(GUEST_SYSENTER_CS, 0);
6710 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6711 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6712 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6713 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006714
Wanpeng Lic37c2872017-11-20 14:52:21 -08006715 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006716 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006717
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006718 vmcs_writel(GUEST_GDTR_BASE, 0);
6719 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6720
6721 vmcs_writel(GUEST_IDTR_BASE, 0);
6722 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6723
Anthony Liguori443381a2010-12-06 10:53:38 -06006724 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006725 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006726 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006727 if (kvm_mpx_supported())
6728 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006730 setup_msrs(vmx);
6731
Avi Kivity6aa8b732006-12-10 02:21:36 -08006732 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6733
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006734 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006735 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006736 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006737 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006738 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006739 vmcs_write32(TPR_THRESHOLD, 0);
6740 }
6741
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006742 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006743
Sheng Yang2384d2b2008-01-17 15:14:33 +08006744 if (vmx->vpid != 0)
6745 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6746
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006747 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006748 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006749 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006750 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006751 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006752
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006753 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006754
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006755 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006756 if (init_event)
6757 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006758}
6759
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006760/*
6761 * In nested virtualization, check if L1 asked to exit on external interrupts.
6762 * For most existing hypervisors, this will always return true.
6763 */
6764static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6765{
6766 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6767 PIN_BASED_EXT_INTR_MASK;
6768}
6769
Bandan Das77b0f5d2014-04-19 18:17:45 -04006770/*
6771 * In nested virtualization, check if L1 has set
6772 * VM_EXIT_ACK_INTR_ON_EXIT
6773 */
6774static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6775{
6776 return get_vmcs12(vcpu)->vm_exit_controls &
6777 VM_EXIT_ACK_INTR_ON_EXIT;
6778}
6779
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006780static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6781{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006782 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006783}
6784
Jan Kiszkac9a79532014-03-07 20:03:15 +01006785static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006786{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006787 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6788 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006789}
6790
Jan Kiszkac9a79532014-03-07 20:03:15 +01006791static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006792{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006793 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006794 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006795 enable_irq_window(vcpu);
6796 return;
6797 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006798
Paolo Bonzini47c01522016-12-19 11:44:07 +01006799 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6800 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006801}
6802
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006803static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006804{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006805 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006806 uint32_t intr;
6807 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006808
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006809 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006810
Avi Kivityfa89a812008-09-01 15:57:51 +03006811 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006812 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006813 int inc_eip = 0;
6814 if (vcpu->arch.interrupt.soft)
6815 inc_eip = vcpu->arch.event_exit_inst_len;
6816 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006817 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006818 return;
6819 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006820 intr = irq | INTR_INFO_VALID_MASK;
6821 if (vcpu->arch.interrupt.soft) {
6822 intr |= INTR_TYPE_SOFT_INTR;
6823 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6824 vmx->vcpu.arch.event_exit_inst_len);
6825 } else
6826 intr |= INTR_TYPE_EXT_INTR;
6827 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006828
6829 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006830}
6831
Sheng Yangf08864b2008-05-15 18:23:25 +08006832static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6833{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006834 struct vcpu_vmx *vmx = to_vmx(vcpu);
6835
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006836 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006837 /*
6838 * Tracking the NMI-blocked state in software is built upon
6839 * finding the next open IRQ window. This, in turn, depends on
6840 * well-behaving guests: They have to keep IRQs disabled at
6841 * least as long as the NMI handler runs. Otherwise we may
6842 * cause NMI nesting, maybe breaking the guest. But as this is
6843 * highly unlikely, we can live with the residual risk.
6844 */
6845 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6846 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6847 }
6848
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006849 ++vcpu->stat.nmi_injections;
6850 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006851
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006852 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006853 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006854 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006855 return;
6856 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006857
Sheng Yangf08864b2008-05-15 18:23:25 +08006858 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6859 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006860
6861 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006862}
6863
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006864static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6865{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006866 struct vcpu_vmx *vmx = to_vmx(vcpu);
6867 bool masked;
6868
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006869 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006870 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006871 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006872 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006873 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6874 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6875 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006876}
6877
6878static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6879{
6880 struct vcpu_vmx *vmx = to_vmx(vcpu);
6881
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006882 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006883 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6884 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6885 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6886 }
6887 } else {
6888 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6889 if (masked)
6890 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6891 GUEST_INTR_STATE_NMI);
6892 else
6893 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6894 GUEST_INTR_STATE_NMI);
6895 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006896}
6897
Jan Kiszka2505dc92013-04-14 12:12:47 +02006898static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6899{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006900 if (to_vmx(vcpu)->nested.nested_run_pending)
6901 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006902
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006903 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006904 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6905 return 0;
6906
Jan Kiszka2505dc92013-04-14 12:12:47 +02006907 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6908 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6909 | GUEST_INTR_STATE_NMI));
6910}
6911
Gleb Natapov78646122009-03-23 12:12:11 +02006912static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6913{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006914 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6915 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006916 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6917 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006918}
6919
Izik Eiduscbc94022007-10-25 00:29:55 +02006920static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6921{
6922 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006923
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006924 if (enable_unrestricted_guest)
6925 return 0;
6926
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006927 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6928 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006929 if (ret)
6930 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006931 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006932 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006933}
6934
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006935static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6936{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006937 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006938 return 0;
6939}
6940
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006941static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006942{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006943 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006944 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006945 /*
6946 * Update instruction length as we may reinject the exception
6947 * from user space while in guest debugging mode.
6948 */
6949 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6950 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006951 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006952 return false;
6953 /* fall through */
6954 case DB_VECTOR:
6955 if (vcpu->guest_debug &
6956 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6957 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006958 /* fall through */
6959 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006960 case OF_VECTOR:
6961 case BR_VECTOR:
6962 case UD_VECTOR:
6963 case DF_VECTOR:
6964 case SS_VECTOR:
6965 case GP_VECTOR:
6966 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006967 return true;
6968 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006969 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006970 return false;
6971}
6972
6973static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6974 int vec, u32 err_code)
6975{
6976 /*
6977 * Instruction with address size override prefix opcode 0x67
6978 * Cause the #SS fault with 0 error code in VM86 mode.
6979 */
6980 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006981 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006982 if (vcpu->arch.halt_request) {
6983 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006984 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006985 }
6986 return 1;
6987 }
6988 return 0;
6989 }
6990
6991 /*
6992 * Forward all other exceptions that are valid in real mode.
6993 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6994 * the required debugging infrastructure rework.
6995 */
6996 kvm_queue_exception(vcpu, vec);
6997 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006998}
6999
Andi Kleena0861c02009-06-08 17:37:09 +08007000/*
7001 * Trigger machine check on the host. We assume all the MSRs are already set up
7002 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7003 * We pass a fake environment to the machine check handler because we want
7004 * the guest to be always treated like user space, no matter what context
7005 * it used internally.
7006 */
7007static void kvm_machine_check(void)
7008{
7009#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7010 struct pt_regs regs = {
7011 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7012 .flags = X86_EFLAGS_IF,
7013 };
7014
7015 do_machine_check(&regs, 0);
7016#endif
7017}
7018
Avi Kivity851ba692009-08-24 11:10:17 +03007019static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007020{
7021 /* already handled by vcpu_run */
7022 return 1;
7023}
7024
Avi Kivity851ba692009-08-24 11:10:17 +03007025static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007026{
Avi Kivity1155f762007-11-22 11:30:47 +02007027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007028 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007029 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007030 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007031 u32 vect_info;
7032 enum emulation_result er;
7033
Avi Kivity1155f762007-11-22 11:30:47 +02007034 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007035 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036
Andi Kleena0861c02009-06-08 17:37:09 +08007037 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007038 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007039
Jim Mattsonef85b672016-12-12 11:01:37 -08007040 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007041 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007042
Wanpeng Li082d06e2018-04-03 16:28:48 -07007043 if (is_invalid_opcode(intr_info))
7044 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007045
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007047 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007049
Liran Alon9e869482018-03-12 13:12:51 +02007050 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7051 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007052 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007053 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7054 if (er == EMULATE_USER_EXIT)
7055 return 0;
7056 else if (er != EMULATE_DONE)
7057 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7058 return 1;
7059 }
7060
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007061 /*
7062 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7063 * MMIO, it is better to report an internal error.
7064 * See the comments in vmx_handle_exit.
7065 */
7066 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7067 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7068 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7069 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007070 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007071 vcpu->run->internal.data[0] = vect_info;
7072 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007073 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007074 return 0;
7075 }
7076
Avi Kivity6aa8b732006-12-10 02:21:36 -08007077 if (is_page_fault(intr_info)) {
7078 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007079 /* EPT won't cause page fault directly */
7080 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007081 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082 }
7083
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007084 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007085
7086 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7087 return handle_rmode_exception(vcpu, ex_no, error_code);
7088
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007089 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007090 case AC_VECTOR:
7091 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7092 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007093 case DB_VECTOR:
7094 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7095 if (!(vcpu->guest_debug &
7096 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007097 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007098 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007099 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007100 skip_emulated_instruction(vcpu);
7101
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007102 kvm_queue_exception(vcpu, DB_VECTOR);
7103 return 1;
7104 }
7105 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7106 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7107 /* fall through */
7108 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007109 /*
7110 * Update instruction length as we may reinject #BP from
7111 * user space while in guest debugging mode. Reading it for
7112 * #DB as well causes no harm, it is not used in that case.
7113 */
7114 vmx->vcpu.arch.event_exit_inst_len =
7115 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007116 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007117 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007118 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7119 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007120 break;
7121 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007122 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7123 kvm_run->ex.exception = ex_no;
7124 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007125 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007127 return 0;
7128}
7129
Avi Kivity851ba692009-08-24 11:10:17 +03007130static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007132 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133 return 1;
7134}
7135
Avi Kivity851ba692009-08-24 11:10:17 +03007136static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007137{
Avi Kivity851ba692009-08-24 11:10:17 +03007138 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007139 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007140 return 0;
7141}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142
Avi Kivity851ba692009-08-24 11:10:17 +03007143static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144{
He, Qingbfdaab02007-09-12 14:18:28 +08007145 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007146 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007147 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148
He, Qingbfdaab02007-09-12 14:18:28 +08007149 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007150 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007151
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007152 ++vcpu->stat.io_exits;
7153
Sean Christopherson432baf62018-03-08 08:57:26 -08007154 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007155 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007156
7157 port = exit_qualification >> 16;
7158 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007159 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007160
Sean Christophersondca7f122018-03-08 08:57:27 -08007161 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007162}
7163
Ingo Molnar102d8322007-02-19 14:37:47 +02007164static void
7165vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7166{
7167 /*
7168 * Patch in the VMCALL instruction:
7169 */
7170 hypercall[0] = 0x0f;
7171 hypercall[1] = 0x01;
7172 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007173}
7174
Guo Chao0fa06072012-06-28 15:16:19 +08007175/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007176static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7177{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007178 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007179 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7180 unsigned long orig_val = val;
7181
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007182 /*
7183 * We get here when L2 changed cr0 in a way that did not change
7184 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007185 * but did change L0 shadowed bits. So we first calculate the
7186 * effective cr0 value that L1 would like to write into the
7187 * hardware. It consists of the L2-owned bits from the new
7188 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007189 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007190 val = (val & ~vmcs12->cr0_guest_host_mask) |
7191 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7192
David Matlack38991522016-11-29 18:14:08 -08007193 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007194 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195
7196 if (kvm_set_cr0(vcpu, val))
7197 return 1;
7198 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007199 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007200 } else {
7201 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007202 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007203 return 1;
David Matlack38991522016-11-29 18:14:08 -08007204
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007205 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007206 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007207}
7208
7209static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7210{
7211 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007212 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7213 unsigned long orig_val = val;
7214
7215 /* analogously to handle_set_cr0 */
7216 val = (val & ~vmcs12->cr4_guest_host_mask) |
7217 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7218 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007219 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007220 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 return 0;
7222 } else
7223 return kvm_set_cr4(vcpu, val);
7224}
7225
Paolo Bonzini0367f202016-07-12 10:44:55 +02007226static int handle_desc(struct kvm_vcpu *vcpu)
7227{
7228 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007229 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007230}
7231
Avi Kivity851ba692009-08-24 11:10:17 +03007232static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007233{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007234 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007235 int cr;
7236 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007237 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007238 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007239
He, Qingbfdaab02007-09-12 14:18:28 +08007240 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241 cr = exit_qualification & 15;
7242 reg = (exit_qualification >> 8) & 15;
7243 switch ((exit_qualification >> 4) & 3) {
7244 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007245 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007246 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007247 switch (cr) {
7248 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007249 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007250 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007251 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007252 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007253 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007254 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007255 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007256 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007257 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007258 case 8: {
7259 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007260 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007261 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007262 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007263 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007264 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007265 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007266 return ret;
7267 /*
7268 * TODO: we might be squashing a
7269 * KVM_GUESTDBG_SINGLESTEP-triggered
7270 * KVM_EXIT_DEBUG here.
7271 */
Avi Kivity851ba692009-08-24 11:10:17 +03007272 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007273 return 0;
7274 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007275 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007276 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007277 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007278 WARN_ONCE(1, "Guest should always own CR0.TS");
7279 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007280 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007281 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007282 case 1: /*mov from cr*/
7283 switch (cr) {
7284 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007285 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007286 val = kvm_read_cr3(vcpu);
7287 kvm_register_write(vcpu, reg, val);
7288 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007289 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007290 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007291 val = kvm_get_cr8(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 }
7296 break;
7297 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007298 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007299 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007300 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007301
Kyle Huey6affcbe2016-11-29 12:40:40 -08007302 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303 default:
7304 break;
7305 }
Avi Kivity851ba692009-08-24 11:10:17 +03007306 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007307 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308 (int)(exit_qualification >> 4) & 3, cr);
7309 return 0;
7310}
7311
Avi Kivity851ba692009-08-24 11:10:17 +03007312static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007313{
He, Qingbfdaab02007-09-12 14:18:28 +08007314 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007315 int dr, dr7, reg;
7316
7317 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7318 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7319
7320 /* First, if DR does not exist, trigger UD */
7321 if (!kvm_require_dr(vcpu, dr))
7322 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007323
Jan Kiszkaf2483412010-01-20 18:20:20 +01007324 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007325 if (!kvm_require_cpl(vcpu, 0))
7326 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007327 dr7 = vmcs_readl(GUEST_DR7);
7328 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007329 /*
7330 * As the vm-exit takes precedence over the debug trap, we
7331 * need to emulate the latter, either for the host or the
7332 * guest debugging itself.
7333 */
7334 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007335 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007336 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007337 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007338 vcpu->run->debug.arch.exception = DB_VECTOR;
7339 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007340 return 0;
7341 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007342 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007343 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007344 kvm_queue_exception(vcpu, DB_VECTOR);
7345 return 1;
7346 }
7347 }
7348
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007349 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007350 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7351 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007352
7353 /*
7354 * No more DR vmexits; force a reload of the debug registers
7355 * and reenter on this instruction. The next vmexit will
7356 * retrieve the full state of the debug registers.
7357 */
7358 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7359 return 1;
7360 }
7361
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007362 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7363 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007364 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007365
7366 if (kvm_get_dr(vcpu, dr, &val))
7367 return 1;
7368 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007369 } else
Nadav Amit57773922014-06-18 17:19:23 +03007370 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007371 return 1;
7372
Kyle Huey6affcbe2016-11-29 12:40:40 -08007373 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007374}
7375
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007376static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7377{
7378 return vcpu->arch.dr6;
7379}
7380
7381static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7382{
7383}
7384
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007385static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7386{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007387 get_debugreg(vcpu->arch.db[0], 0);
7388 get_debugreg(vcpu->arch.db[1], 1);
7389 get_debugreg(vcpu->arch.db[2], 2);
7390 get_debugreg(vcpu->arch.db[3], 3);
7391 get_debugreg(vcpu->arch.dr6, 6);
7392 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7393
7394 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007395 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007396}
7397
Gleb Natapov020df072010-04-13 10:05:23 +03007398static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7399{
7400 vmcs_writel(GUEST_DR7, val);
7401}
7402
Avi Kivity851ba692009-08-24 11:10:17 +03007403static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007404{
Kyle Huey6a908b62016-11-29 12:40:37 -08007405 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406}
7407
Avi Kivity851ba692009-08-24 11:10:17 +03007408static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007409{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007410 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007411 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007412
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007413 msr_info.index = ecx;
7414 msr_info.host_initiated = false;
7415 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007416 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007417 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007418 return 1;
7419 }
7420
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007421 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007422
Avi Kivity6aa8b732006-12-10 02:21:36 -08007423 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007424 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7425 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007426 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007427}
7428
Avi Kivity851ba692009-08-24 11:10:17 +03007429static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007430{
Will Auld8fe8ab42012-11-29 12:42:12 -08007431 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007432 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7433 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7434 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007435
Will Auld8fe8ab42012-11-29 12:42:12 -08007436 msr.data = data;
7437 msr.index = ecx;
7438 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007439 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007440 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007441 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007442 return 1;
7443 }
7444
Avi Kivity59200272010-01-25 19:47:02 +02007445 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007446 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447}
7448
Avi Kivity851ba692009-08-24 11:10:17 +03007449static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007450{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007451 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007452 return 1;
7453}
7454
Avi Kivity851ba692009-08-24 11:10:17 +03007455static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007457 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7458 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007459
Avi Kivity3842d132010-07-27 12:30:24 +03007460 kvm_make_request(KVM_REQ_EVENT, vcpu);
7461
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007462 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007463 return 1;
7464}
7465
Avi Kivity851ba692009-08-24 11:10:17 +03007466static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467{
Avi Kivityd3bef152007-06-05 15:53:05 +03007468 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469}
7470
Avi Kivity851ba692009-08-24 11:10:17 +03007471static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007472{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007473 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007474}
7475
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007476static int handle_invd(struct kvm_vcpu *vcpu)
7477{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007478 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007482{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007483 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007484
7485 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007486 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007487}
7488
Avi Kivityfee84b02011-11-10 14:57:25 +02007489static int handle_rdpmc(struct kvm_vcpu *vcpu)
7490{
7491 int err;
7492
7493 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007494 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007495}
7496
Avi Kivity851ba692009-08-24 11:10:17 +03007497static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007498{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007499 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007500}
7501
Dexuan Cui2acf9232010-06-10 11:27:12 +08007502static int handle_xsetbv(struct kvm_vcpu *vcpu)
7503{
7504 u64 new_bv = kvm_read_edx_eax(vcpu);
7505 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7506
7507 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007508 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007509 return 1;
7510}
7511
Wanpeng Lif53cd632014-12-02 19:14:58 +08007512static int handle_xsaves(struct kvm_vcpu *vcpu)
7513{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007514 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007515 WARN(1, "this should never happen\n");
7516 return 1;
7517}
7518
7519static int handle_xrstors(struct kvm_vcpu *vcpu)
7520{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007521 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007522 WARN(1, "this should never happen\n");
7523 return 1;
7524}
7525
Avi Kivity851ba692009-08-24 11:10:17 +03007526static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007527{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007528 if (likely(fasteoi)) {
7529 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7530 int access_type, offset;
7531
7532 access_type = exit_qualification & APIC_ACCESS_TYPE;
7533 offset = exit_qualification & APIC_ACCESS_OFFSET;
7534 /*
7535 * Sane guest uses MOV to write EOI, with written value
7536 * not cared. So make a short-circuit here by avoiding
7537 * heavy instruction emulation.
7538 */
7539 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7540 (offset == APIC_EOI)) {
7541 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007542 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007543 }
7544 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007545 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007546}
7547
Yang Zhangc7c9c562013-01-25 10:18:51 +08007548static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7549{
7550 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7551 int vector = exit_qualification & 0xff;
7552
7553 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7554 kvm_apic_set_eoi_accelerated(vcpu, vector);
7555 return 1;
7556}
7557
Yang Zhang83d4c282013-01-25 10:18:49 +08007558static int handle_apic_write(struct kvm_vcpu *vcpu)
7559{
7560 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7561 u32 offset = exit_qualification & 0xfff;
7562
7563 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7564 kvm_apic_write_nodecode(vcpu, offset);
7565 return 1;
7566}
7567
Avi Kivity851ba692009-08-24 11:10:17 +03007568static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007569{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007570 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007571 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007572 bool has_error_code = false;
7573 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007574 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007575 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007576
7577 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007578 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007579 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007580
7581 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7582
7583 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007584 if (reason == TASK_SWITCH_GATE && idt_v) {
7585 switch (type) {
7586 case INTR_TYPE_NMI_INTR:
7587 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007588 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589 break;
7590 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007591 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007592 kvm_clear_interrupt_queue(vcpu);
7593 break;
7594 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007595 if (vmx->idt_vectoring_info &
7596 VECTORING_INFO_DELIVER_CODE_MASK) {
7597 has_error_code = true;
7598 error_code =
7599 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7600 }
7601 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007602 case INTR_TYPE_SOFT_EXCEPTION:
7603 kvm_clear_exception_queue(vcpu);
7604 break;
7605 default:
7606 break;
7607 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007608 }
Izik Eidus37817f22008-03-24 23:14:53 +02007609 tss_selector = exit_qualification;
7610
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007611 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7612 type != INTR_TYPE_EXT_INTR &&
7613 type != INTR_TYPE_NMI_INTR))
7614 skip_emulated_instruction(vcpu);
7615
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007616 if (kvm_task_switch(vcpu, tss_selector,
7617 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7618 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007619 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7620 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7621 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007622 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007623 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007624
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007625 /*
7626 * TODO: What about debug traps on tss switch?
7627 * Are we supposed to inject them and update dr6?
7628 */
7629
7630 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007631}
7632
Avi Kivity851ba692009-08-24 11:10:17 +03007633static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007634{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007635 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007636 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007637 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007638
Sheng Yangf9c617f2009-03-25 10:08:52 +08007639 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007640
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007641 /*
7642 * EPT violation happened while executing iret from NMI,
7643 * "blocked by NMI" bit has to be set before next VM entry.
7644 * There are errata that may cause this bit to not be set:
7645 * AAK134, BY25.
7646 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007647 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007648 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007649 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007650 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7651
Sheng Yang14394422008-04-28 12:24:45 +08007652 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007653 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007654
Junaid Shahid27959a42016-12-06 16:46:10 -08007655 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007656 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007657 ? PFERR_USER_MASK : 0;
7658 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007659 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007660 ? PFERR_WRITE_MASK : 0;
7661 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007662 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007663 ? PFERR_FETCH_MASK : 0;
7664 /* ept page table entry is present? */
7665 error_code |= (exit_qualification &
7666 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7667 EPT_VIOLATION_EXECUTABLE))
7668 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007669
Paolo Bonzinieebed242016-11-28 14:39:58 +01007670 error_code |= (exit_qualification & 0x100) != 0 ?
7671 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007672
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007673 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007674 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007675}
7676
Avi Kivity851ba692009-08-24 11:10:17 +03007677static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007678{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007679 gpa_t gpa;
7680
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007681 /*
7682 * A nested guest cannot optimize MMIO vmexits, because we have an
7683 * nGPA here instead of the required GPA.
7684 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007685 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007686 if (!is_guest_mode(vcpu) &&
7687 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007688 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007689 /*
7690 * Doing kvm_skip_emulated_instruction() depends on undefined
7691 * behavior: Intel's manual doesn't mandate
7692 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7693 * occurs and while on real hardware it was observed to be set,
7694 * other hypervisors (namely Hyper-V) don't set it, we end up
7695 * advancing IP with some random value. Disable fast mmio when
7696 * running nested and keep it for real hardware in hope that
7697 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7698 */
7699 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7700 return kvm_skip_emulated_instruction(vcpu);
7701 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007702 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007703 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007704 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007705
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007706 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007707}
7708
Avi Kivity851ba692009-08-24 11:10:17 +03007709static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007710{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007711 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007712 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7713 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007714 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007715 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007716
7717 return 1;
7718}
7719
Mohammed Gamal80ced182009-09-01 12:48:18 +02007720static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007721{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007722 struct vcpu_vmx *vmx = to_vmx(vcpu);
7723 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007724 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007725 u32 cpu_exec_ctrl;
7726 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007727 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007728
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007729 /*
7730 * We should never reach the point where we are emulating L2
7731 * due to invalid guest state as that means we incorrectly
7732 * allowed a nested VMEntry with an invalid vmcs12.
7733 */
7734 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7735
Avi Kivity49e9d552010-09-19 14:34:08 +02007736 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7737 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007738
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007739 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007740 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007741 return handle_interrupt_window(&vmx->vcpu);
7742
Radim Krčmář72875d82017-04-26 22:32:19 +02007743 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007744 return 1;
7745
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007746 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007747
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007748 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007749 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007750 ret = 0;
7751 goto out;
7752 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007753
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007754 if (err != EMULATE_DONE)
7755 goto emulation_error;
7756
7757 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7758 vcpu->arch.exception.pending)
7759 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007760
Gleb Natapov8d76c492013-05-08 18:38:44 +03007761 if (vcpu->arch.halt_request) {
7762 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007763 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007764 goto out;
7765 }
7766
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007767 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007768 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007769 if (need_resched())
7770 schedule();
7771 }
7772
Mohammed Gamal80ced182009-09-01 12:48:18 +02007773out:
7774 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007775
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007776emulation_error:
7777 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7778 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7779 vcpu->run->internal.ndata = 0;
7780 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007781}
7782
7783static void grow_ple_window(struct kvm_vcpu *vcpu)
7784{
7785 struct vcpu_vmx *vmx = to_vmx(vcpu);
7786 int old = vmx->ple_window;
7787
Babu Mogerc8e88712018-03-16 16:37:24 -04007788 vmx->ple_window = __grow_ple_window(old, ple_window,
7789 ple_window_grow,
7790 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007791
7792 if (vmx->ple_window != old)
7793 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007794
7795 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007796}
7797
7798static void shrink_ple_window(struct kvm_vcpu *vcpu)
7799{
7800 struct vcpu_vmx *vmx = to_vmx(vcpu);
7801 int old = vmx->ple_window;
7802
Babu Mogerc8e88712018-03-16 16:37:24 -04007803 vmx->ple_window = __shrink_ple_window(old, ple_window,
7804 ple_window_shrink,
7805 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007806
7807 if (vmx->ple_window != old)
7808 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007809
7810 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007811}
7812
7813/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007814 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7815 */
7816static void wakeup_handler(void)
7817{
7818 struct kvm_vcpu *vcpu;
7819 int cpu = smp_processor_id();
7820
7821 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7822 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7823 blocked_vcpu_list) {
7824 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7825
7826 if (pi_test_on(pi_desc) == 1)
7827 kvm_vcpu_kick(vcpu);
7828 }
7829 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7830}
7831
Peng Haoe01bca22018-04-07 05:47:32 +08007832static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007833{
7834 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7835 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7836 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7837 0ull, VMX_EPT_EXECUTABLE_MASK,
7838 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007839 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007840
7841 ept_set_mmio_spte_mask();
7842 kvm_enable_tdp();
7843}
7844
Tiejun Chenf2c76482014-10-28 10:14:47 +08007845static __init int hardware_setup(void)
7846{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007847 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007848 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007849
7850 rdmsrl_safe(MSR_EFER, &host_efer);
7851
7852 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7853 kvm_define_shared_msr(i, vmx_msr_index[i]);
7854
Radim Krčmář23611332016-09-29 22:41:33 +02007855 for (i = 0; i < VMX_BITMAP_NR; i++) {
7856 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7857 if (!vmx_bitmap[i])
7858 goto out;
7859 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007860
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007861 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7862 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7863
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007864 if (setup_vmcs_config(&vmcs_config) < 0) {
7865 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007866 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007867 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007868
7869 if (boot_cpu_has(X86_FEATURE_NX))
7870 kvm_enable_efer_bits(EFER_NX);
7871
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007872 if (boot_cpu_has(X86_FEATURE_MPX)) {
7873 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7874 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7875 }
7876
Wanpeng Li08d839c2017-03-23 05:30:08 -07007877 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7878 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007879 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007880
Tiejun Chenf2c76482014-10-28 10:14:47 +08007881 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007882 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007883 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007884 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007885 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007886
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007887 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007888 enable_ept_ad_bits = 0;
7889
Wanpeng Li8ad81822017-10-09 15:51:53 -07007890 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891 enable_unrestricted_guest = 0;
7892
Paolo Bonziniad15a292015-01-30 16:18:49 +01007893 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007894 flexpriority_enabled = 0;
7895
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007896 if (!cpu_has_virtual_nmis())
7897 enable_vnmi = 0;
7898
Paolo Bonziniad15a292015-01-30 16:18:49 +01007899 /*
7900 * set_apic_access_page_addr() is used to reload apic access
7901 * page upon invalidation. No need to do anything if not
7902 * using the APIC_ACCESS_ADDR VMCS field.
7903 */
7904 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007905 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007906
7907 if (!cpu_has_vmx_tpr_shadow())
7908 kvm_x86_ops->update_cr8_intercept = NULL;
7909
7910 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7911 kvm_disable_largepages();
7912
Tianyu Lan877ad952018-07-19 08:40:23 +00007913#if IS_ENABLED(CONFIG_HYPERV)
7914 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7915 && enable_ept)
7916 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7917#endif
7918
Wanpeng Li0f107682017-09-28 18:06:24 -07007919 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007920 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007921 ple_window = 0;
7922 ple_window_grow = 0;
7923 ple_window_max = 0;
7924 ple_window_shrink = 0;
7925 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007926
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007927 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007928 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007929 kvm_x86_ops->sync_pir_to_irr = NULL;
7930 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007931
Haozhong Zhang64903d62015-10-20 15:39:09 +08007932 if (cpu_has_vmx_tsc_scaling()) {
7933 kvm_has_tsc_control = true;
7934 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7935 kvm_tsc_scaling_ratio_frac_bits = 48;
7936 }
7937
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007938 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7939
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007940 if (enable_ept)
7941 vmx_enable_tdp();
7942 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007943 kvm_disable_tdp();
7944
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007945 if (!nested) {
7946 kvm_x86_ops->get_nested_state = NULL;
7947 kvm_x86_ops->set_nested_state = NULL;
7948 }
7949
Kai Huang843e4332015-01-28 10:54:28 +08007950 /*
7951 * Only enable PML when hardware supports PML feature, and both EPT
7952 * and EPT A/D bit features are enabled -- PML depends on them to work.
7953 */
7954 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7955 enable_pml = 0;
7956
7957 if (!enable_pml) {
7958 kvm_x86_ops->slot_enable_log_dirty = NULL;
7959 kvm_x86_ops->slot_disable_log_dirty = NULL;
7960 kvm_x86_ops->flush_log_dirty = NULL;
7961 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7962 }
7963
Sean Christophersond264ee02018-08-27 15:21:12 -07007964 if (!cpu_has_vmx_preemption_timer())
7965 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7966
Yunhong Jiang64672c92016-06-13 14:19:59 -07007967 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7968 u64 vmx_msr;
7969
7970 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7971 cpu_preemption_timer_multi =
7972 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7973 } else {
7974 kvm_x86_ops->set_hv_timer = NULL;
7975 kvm_x86_ops->cancel_hv_timer = NULL;
7976 }
7977
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007978 if (!cpu_has_vmx_shadow_vmcs())
7979 enable_shadow_vmcs = 0;
7980 if (enable_shadow_vmcs)
7981 init_vmcs_shadow_fields();
7982
Feng Wubf9f6ac2015-09-18 22:29:55 +08007983 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007984 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007985
Ashok Rajc45dcc72016-06-22 14:59:56 +08007986 kvm_mce_cap_supported |= MCG_LMCE_P;
7987
Tiejun Chenf2c76482014-10-28 10:14:47 +08007988 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007989
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007990out:
Radim Krčmář23611332016-09-29 22:41:33 +02007991 for (i = 0; i < VMX_BITMAP_NR; i++)
7992 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007993
7994 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007995}
7996
7997static __exit void hardware_unsetup(void)
7998{
Radim Krčmář23611332016-09-29 22:41:33 +02007999 int i;
8000
8001 for (i = 0; i < VMX_BITMAP_NR; i++)
8002 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008003
Tiejun Chenf2c76482014-10-28 10:14:47 +08008004 free_kvm_area();
8005}
8006
Avi Kivity6aa8b732006-12-10 02:21:36 -08008007/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008008 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8009 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8010 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008011static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008012{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008013 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008014 grow_ple_window(vcpu);
8015
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008016 /*
8017 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8018 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8019 * never set PAUSE_EXITING and just set PLE if supported,
8020 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8021 */
8022 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008023 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008024}
8025
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008026static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008027{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008028 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008029}
8030
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008031static int handle_mwait(struct kvm_vcpu *vcpu)
8032{
8033 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8034 return handle_nop(vcpu);
8035}
8036
Jim Mattson45ec3682017-08-23 16:32:04 -07008037static int handle_invalid_op(struct kvm_vcpu *vcpu)
8038{
8039 kvm_queue_exception(vcpu, UD_VECTOR);
8040 return 1;
8041}
8042
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008043static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8044{
8045 return 1;
8046}
8047
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008048static int handle_monitor(struct kvm_vcpu *vcpu)
8049{
8050 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8051 return handle_nop(vcpu);
8052}
8053
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008054/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008055 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8056 * set the success or error code of an emulated VMX instruction, as specified
8057 * by Vol 2B, VMX Instruction Reference, "Conventions".
8058 */
8059static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8060{
8061 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8062 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8063 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8064}
8065
8066static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8067{
8068 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8069 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8070 X86_EFLAGS_SF | X86_EFLAGS_OF))
8071 | X86_EFLAGS_CF);
8072}
8073
Abel Gordon145c28d2013-04-18 14:36:55 +03008074static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008075 u32 vm_instruction_error)
8076{
8077 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8078 /*
8079 * failValid writes the error number to the current VMCS, which
8080 * can't be done there isn't a current VMCS.
8081 */
8082 nested_vmx_failInvalid(vcpu);
8083 return;
8084 }
8085 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8086 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8087 X86_EFLAGS_SF | X86_EFLAGS_OF))
8088 | X86_EFLAGS_ZF);
8089 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8090 /*
8091 * We don't need to force a shadow sync because
8092 * VM_INSTRUCTION_ERROR is not shadowed
8093 */
8094}
Abel Gordon145c28d2013-04-18 14:36:55 +03008095
Wincy Vanff651cb2014-12-11 08:52:58 +03008096static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8097{
8098 /* TODO: not to reset guest simply here. */
8099 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008100 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008101}
8102
Jan Kiszkaf4124502014-03-07 20:03:13 +01008103static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8104{
8105 struct vcpu_vmx *vmx =
8106 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8107
8108 vmx->nested.preemption_timer_expired = true;
8109 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8110 kvm_vcpu_kick(&vmx->vcpu);
8111
8112 return HRTIMER_NORESTART;
8113}
8114
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008115/*
Bandan Das19677e32014-05-06 02:19:15 -04008116 * Decode the memory-address operand of a vmx instruction, as recorded on an
8117 * exit caused by such an instruction (run by a guest hypervisor).
8118 * On success, returns 0. When the operand is invalid, returns 1 and throws
8119 * #UD or #GP.
8120 */
8121static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8122 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008123 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008124{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008125 gva_t off;
8126 bool exn;
8127 struct kvm_segment s;
8128
Bandan Das19677e32014-05-06 02:19:15 -04008129 /*
8130 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8131 * Execution", on an exit, vmx_instruction_info holds most of the
8132 * addressing components of the operand. Only the displacement part
8133 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8134 * For how an actual address is calculated from all these components,
8135 * refer to Vol. 1, "Operand Addressing".
8136 */
8137 int scaling = vmx_instruction_info & 3;
8138 int addr_size = (vmx_instruction_info >> 7) & 7;
8139 bool is_reg = vmx_instruction_info & (1u << 10);
8140 int seg_reg = (vmx_instruction_info >> 15) & 7;
8141 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8142 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8143 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8144 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8145
8146 if (is_reg) {
8147 kvm_queue_exception(vcpu, UD_VECTOR);
8148 return 1;
8149 }
8150
8151 /* Addr = segment_base + offset */
8152 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008153 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008154 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008156 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off += kvm_register_read(vcpu, index_reg)<<scaling;
8158 vmx_get_segment(vcpu, &s, seg_reg);
8159 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008160
8161 if (addr_size == 1) /* 32 bit */
8162 *ret &= 0xffffffff;
8163
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008164 /* Checks for #GP/#SS exceptions. */
8165 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008166 if (is_long_mode(vcpu)) {
8167 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8168 * non-canonical form. This is the only check on the memory
8169 * destination for long mode!
8170 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008171 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008172 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008173 /* Protected mode: apply checks for segment validity in the
8174 * following order:
8175 * - segment type check (#GP(0) may be thrown)
8176 * - usability check (#GP(0)/#SS(0))
8177 * - limit check (#GP(0)/#SS(0))
8178 */
8179 if (wr)
8180 /* #GP(0) if the destination operand is located in a
8181 * read-only data segment or any code segment.
8182 */
8183 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8184 else
8185 /* #GP(0) if the source operand is located in an
8186 * execute-only code segment
8187 */
8188 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008189 if (exn) {
8190 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8191 return 1;
8192 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008193 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8194 */
8195 exn = (s.unusable != 0);
8196 /* Protected mode: #GP(0)/#SS(0) if the memory
8197 * operand is outside the segment limit.
8198 */
8199 exn = exn || (off + sizeof(u64) > s.limit);
8200 }
8201 if (exn) {
8202 kvm_queue_exception_e(vcpu,
8203 seg_reg == VCPU_SREG_SS ?
8204 SS_VECTOR : GP_VECTOR,
8205 0);
8206 return 1;
8207 }
8208
Bandan Das19677e32014-05-06 02:19:15 -04008209 return 0;
8210}
8211
Radim Krčmářcbf71272017-05-19 15:48:51 +02008212static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008213{
8214 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008215 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008216
8217 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008218 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008219 return 1;
8220
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008221 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008222 kvm_inject_page_fault(vcpu, &e);
8223 return 1;
8224 }
8225
Bandan Das3573e222014-05-06 02:19:16 -04008226 return 0;
8227}
8228
Liran Alonabfc52c2018-06-23 02:35:13 +03008229/*
8230 * Allocate a shadow VMCS and associate it with the currently loaded
8231 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8232 * VMCS is also VMCLEARed, so that it is ready for use.
8233 */
8234static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8235{
8236 struct vcpu_vmx *vmx = to_vmx(vcpu);
8237 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8238
8239 /*
8240 * We should allocate a shadow vmcs for vmcs01 only when L1
8241 * executes VMXON and free it when L1 executes VMXOFF.
8242 * As it is invalid to execute VMXON twice, we shouldn't reach
8243 * here when vmcs01 already have an allocated shadow vmcs.
8244 */
8245 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8246
8247 if (!loaded_vmcs->shadow_vmcs) {
8248 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8249 if (loaded_vmcs->shadow_vmcs)
8250 vmcs_clear(loaded_vmcs->shadow_vmcs);
8251 }
8252 return loaded_vmcs->shadow_vmcs;
8253}
8254
Jim Mattsone29acc52016-11-30 12:03:43 -08008255static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8256{
8257 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008258 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008259
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008260 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8261 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008262 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008263
8264 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8265 if (!vmx->nested.cached_vmcs12)
8266 goto out_cached_vmcs12;
8267
Liran Alon61ada742018-06-23 02:35:08 +03008268 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8269 if (!vmx->nested.cached_shadow_vmcs12)
8270 goto out_cached_shadow_vmcs12;
8271
Liran Alonabfc52c2018-06-23 02:35:13 +03008272 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8273 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008274
Jim Mattsone29acc52016-11-30 12:03:43 -08008275 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8276 HRTIMER_MODE_REL_PINNED);
8277 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8278
Roman Kagan63aff652018-07-19 21:59:07 +03008279 vmx->nested.vpid02 = allocate_vpid();
8280
Jim Mattsone29acc52016-11-30 12:03:43 -08008281 vmx->nested.vmxon = true;
8282 return 0;
8283
8284out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008285 kfree(vmx->nested.cached_shadow_vmcs12);
8286
8287out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008288 kfree(vmx->nested.cached_vmcs12);
8289
8290out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008291 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008292
Jim Mattsonde3a0022017-11-27 17:22:25 -06008293out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008294 return -ENOMEM;
8295}
8296
Bandan Das3573e222014-05-06 02:19:16 -04008297/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008298 * Emulate the VMXON instruction.
8299 * Currently, we just remember that VMX is active, and do not save or even
8300 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8301 * do not currently need to store anything in that guest-allocated memory
8302 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8303 * argument is different from the VMXON pointer (which the spec says they do).
8304 */
8305static int handle_vmon(struct kvm_vcpu *vcpu)
8306{
Jim Mattsone29acc52016-11-30 12:03:43 -08008307 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008308 gpa_t vmptr;
8309 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008311 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8312 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008313
Jim Mattson70f3aac2017-04-26 08:53:46 -07008314 /*
8315 * The Intel VMX Instruction Reference lists a bunch of bits that are
8316 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8317 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8318 * Otherwise, we should fail with #UD. But most faulting conditions
8319 * have already been checked by hardware, prior to the VM-exit for
8320 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8321 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008322 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008323 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008324 kvm_queue_exception(vcpu, UD_VECTOR);
8325 return 1;
8326 }
8327
Felix Wilhelm727ba742018-06-11 09:43:44 +02008328 /* CPL=0 must be checked manually. */
8329 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008330 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008331 return 1;
8332 }
8333
Abel Gordon145c28d2013-04-18 14:36:55 +03008334 if (vmx->nested.vmxon) {
8335 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008336 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008337 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008338
Haozhong Zhang3b840802016-06-22 14:59:54 +08008339 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008340 != VMXON_NEEDED_FEATURES) {
8341 kvm_inject_gp(vcpu, 0);
8342 return 1;
8343 }
8344
Radim Krčmářcbf71272017-05-19 15:48:51 +02008345 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008346 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008347
8348 /*
8349 * SDM 3: 24.11.5
8350 * The first 4 bytes of VMXON region contain the supported
8351 * VMCS revision identifier
8352 *
8353 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8354 * which replaces physical address width with 32
8355 */
8356 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8357 nested_vmx_failInvalid(vcpu);
8358 return kvm_skip_emulated_instruction(vcpu);
8359 }
8360
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008361 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8362 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008363 nested_vmx_failInvalid(vcpu);
8364 return kvm_skip_emulated_instruction(vcpu);
8365 }
8366 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8367 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008368 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008369 nested_vmx_failInvalid(vcpu);
8370 return kvm_skip_emulated_instruction(vcpu);
8371 }
8372 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008373 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008374
8375 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008376 ret = enter_vmx_operation(vcpu);
8377 if (ret)
8378 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008379
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008380 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008381 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008382}
8383
8384/*
8385 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8386 * for running VMX instructions (except VMXON, whose prerequisites are
8387 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008388 * Note that many of these exceptions have priority over VM exits, so they
8389 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008390 */
8391static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8392{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008393 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008394 kvm_queue_exception(vcpu, UD_VECTOR);
8395 return 0;
8396 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008397
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008398 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008399 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008400 return 0;
8401 }
8402
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008403 return 1;
8404}
8405
David Matlack8ca44e82017-08-01 14:00:39 -07008406static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8407{
8408 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8409 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8410}
8411
Abel Gordone7953d72013-04-18 14:37:55 +03008412static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8413{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008414 if (vmx->nested.current_vmptr == -1ull)
8415 return;
8416
Abel Gordon012f83c2013-04-18 14:39:25 +03008417 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008418 /* copy to memory all shadowed fields in case
8419 they were modified */
8420 copy_shadow_to_vmcs12(vmx);
8421 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008422 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008423 }
Wincy Van705699a2015-02-03 23:58:17 +08008424 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008425
8426 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008427 kvm_vcpu_write_guest_page(&vmx->vcpu,
8428 vmx->nested.current_vmptr >> PAGE_SHIFT,
8429 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008430
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008431 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008432}
8433
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008434/*
8435 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8436 * just stops using VMX.
8437 */
8438static void free_nested(struct vcpu_vmx *vmx)
8439{
Wanpeng Lib7455822017-11-22 14:04:00 -08008440 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008441 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008442
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008443 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008444 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008445 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008446 vmx->nested.posted_intr_nv = -1;
8447 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008448 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008449 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008450 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8451 free_vmcs(vmx->vmcs01.shadow_vmcs);
8452 vmx->vmcs01.shadow_vmcs = NULL;
8453 }
David Matlack4f2777b2016-07-13 17:16:37 -07008454 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008455 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008456 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008457 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008458 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008459 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008460 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008461 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008462 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008463 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008464 }
Wincy Van705699a2015-02-03 23:58:17 +08008465 if (vmx->nested.pi_desc_page) {
8466 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008467 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008468 vmx->nested.pi_desc_page = NULL;
8469 vmx->nested.pi_desc = NULL;
8470 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008471
Jim Mattsonde3a0022017-11-27 17:22:25 -06008472 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008473}
8474
8475/* Emulate the VMXOFF instruction */
8476static int handle_vmoff(struct kvm_vcpu *vcpu)
8477{
8478 if (!nested_vmx_check_permission(vcpu))
8479 return 1;
8480 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008481 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008482 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008483}
8484
Nadav Har'El27d6c862011-05-25 23:06:59 +03008485/* Emulate the VMCLEAR instruction */
8486static int handle_vmclear(struct kvm_vcpu *vcpu)
8487{
8488 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008489 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008490 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008491
8492 if (!nested_vmx_check_permission(vcpu))
8493 return 1;
8494
Radim Krčmářcbf71272017-05-19 15:48:51 +02008495 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008496 return 1;
8497
Radim Krčmářcbf71272017-05-19 15:48:51 +02008498 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8499 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8500 return kvm_skip_emulated_instruction(vcpu);
8501 }
8502
8503 if (vmptr == vmx->nested.vmxon_ptr) {
8504 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8505 return kvm_skip_emulated_instruction(vcpu);
8506 }
8507
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008508 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008509 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008510
Jim Mattson587d7e722017-03-02 12:41:48 -08008511 kvm_vcpu_write_guest(vcpu,
8512 vmptr + offsetof(struct vmcs12, launch_state),
8513 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008514
Nadav Har'El27d6c862011-05-25 23:06:59 +03008515 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008516 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008517}
8518
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008519static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8520
8521/* Emulate the VMLAUNCH instruction */
8522static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8523{
8524 return nested_vmx_run(vcpu, true);
8525}
8526
8527/* Emulate the VMRESUME instruction */
8528static int handle_vmresume(struct kvm_vcpu *vcpu)
8529{
8530
8531 return nested_vmx_run(vcpu, false);
8532}
8533
Nadav Har'El49f705c2011-05-25 23:08:30 +03008534/*
8535 * Read a vmcs12 field. Since these can have varying lengths and we return
8536 * one type, we chose the biggest type (u64) and zero-extend the return value
8537 * to that size. Note that the caller, handle_vmread, might need to use only
8538 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8539 * 64-bit fields are to be returned).
8540 */
Liran Alone2536742018-06-23 02:35:02 +03008541static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008542 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008543{
8544 short offset = vmcs_field_to_offset(field);
8545 char *p;
8546
8547 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008548 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008549
Liran Alone2536742018-06-23 02:35:02 +03008550 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551
Jim Mattsond37f4262017-12-22 12:12:16 -08008552 switch (vmcs_field_width(field)) {
8553 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008554 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008555 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008556 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008557 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008558 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008559 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008560 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008561 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008562 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008563 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008564 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008566 WARN_ON(1);
8567 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008568 }
8569}
8570
Abel Gordon20b97fe2013-04-18 14:36:25 +03008571
Liran Alone2536742018-06-23 02:35:02 +03008572static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008573 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008574 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008575 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008576 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578
Jim Mattsond37f4262017-12-22 12:12:16 -08008579 switch (vmcs_field_width(field)) {
8580 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008581 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008582 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008583 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008585 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008586 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008587 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008588 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008589 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008590 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008591 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008592 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 WARN_ON(1);
8594 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008595 }
8596
8597}
8598
Jim Mattsonf4160e42018-05-29 09:11:33 -07008599/*
8600 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8601 * they have been modified by the L1 guest. Note that the "read-only"
8602 * VM-exit information fields are actually writable if the vCPU is
8603 * configured to support "VMWRITE to any supported field in the VMCS."
8604 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008605static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8606{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008607 const u16 *fields[] = {
8608 shadow_read_write_fields,
8609 shadow_read_only_fields
8610 };
8611 const int max_fields[] = {
8612 max_shadow_read_write_fields,
8613 max_shadow_read_only_fields
8614 };
8615 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008616 unsigned long field;
8617 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008618 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008619
Jan Kiszka282da872014-10-08 18:05:39 +02008620 preempt_disable();
8621
Abel Gordon16f5b902013-04-18 14:38:25 +03008622 vmcs_load(shadow_vmcs);
8623
Jim Mattsonf4160e42018-05-29 09:11:33 -07008624 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8625 for (i = 0; i < max_fields[q]; i++) {
8626 field = fields[q][i];
8627 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008628 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008629 }
8630 /*
8631 * Skip the VM-exit information fields if they are read-only.
8632 */
8633 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8634 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008635 }
8636
8637 vmcs_clear(shadow_vmcs);
8638 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008639
8640 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008641}
8642
Abel Gordonc3114422013-04-18 14:38:55 +03008643static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8644{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008645 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008646 shadow_read_write_fields,
8647 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008648 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008649 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008650 max_shadow_read_write_fields,
8651 max_shadow_read_only_fields
8652 };
8653 int i, q;
8654 unsigned long field;
8655 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008656 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008657
8658 vmcs_load(shadow_vmcs);
8659
Mathias Krausec2bae892013-06-26 20:36:21 +02008660 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008661 for (i = 0; i < max_fields[q]; i++) {
8662 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008663 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008664 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008665 }
8666 }
8667
8668 vmcs_clear(shadow_vmcs);
8669 vmcs_load(vmx->loaded_vmcs->vmcs);
8670}
8671
Nadav Har'El49f705c2011-05-25 23:08:30 +03008672/*
8673 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8674 * used before) all generate the same failure when it is missing.
8675 */
8676static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8677{
8678 struct vcpu_vmx *vmx = to_vmx(vcpu);
8679 if (vmx->nested.current_vmptr == -1ull) {
8680 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008681 return 0;
8682 }
8683 return 1;
8684}
8685
8686static int handle_vmread(struct kvm_vcpu *vcpu)
8687{
8688 unsigned long field;
8689 u64 field_value;
8690 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8691 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8692 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008693 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008694
Kyle Hueyeb277562016-11-29 12:40:39 -08008695 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008696 return 1;
8697
Kyle Huey6affcbe2016-11-29 12:40:40 -08008698 if (!nested_vmx_check_vmcs12(vcpu))
8699 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008700
Liran Alon6d894f42018-06-23 02:35:09 +03008701 if (!is_guest_mode(vcpu))
8702 vmcs12 = get_vmcs12(vcpu);
8703 else {
8704 /*
8705 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8706 * to shadowed-field sets the ALU flags for VMfailInvalid.
8707 */
8708 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8709 nested_vmx_failInvalid(vcpu);
8710 return kvm_skip_emulated_instruction(vcpu);
8711 }
8712 vmcs12 = get_shadow_vmcs12(vcpu);
8713 }
8714
Nadav Har'El49f705c2011-05-25 23:08:30 +03008715 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008716 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008717 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008718 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008720 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008721 }
8722 /*
8723 * Now copy part of this value to register or memory, as requested.
8724 * Note that the number of bits actually copied is 32 or 64 depending
8725 * on the guest's mode (32 or 64 bit), not on the given field's length.
8726 */
8727 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008728 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008729 field_value);
8730 } else {
8731 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008732 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008733 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008734 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008735 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8736 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737 }
8738
8739 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008740 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008741}
8742
8743
8744static int handle_vmwrite(struct kvm_vcpu *vcpu)
8745{
8746 unsigned long field;
8747 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008748 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008749 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8750 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008751
Nadav Har'El49f705c2011-05-25 23:08:30 +03008752 /* The value to write might be 32 or 64 bits, depending on L1's long
8753 * mode, and eventually we need to write that into a field of several
8754 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008755 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 * bits into the vmcs12 field.
8757 */
8758 u64 field_value = 0;
8759 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008760 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008761
Kyle Hueyeb277562016-11-29 12:40:39 -08008762 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763 return 1;
8764
Kyle Huey6affcbe2016-11-29 12:40:40 -08008765 if (!nested_vmx_check_vmcs12(vcpu))
8766 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008767
Nadav Har'El49f705c2011-05-25 23:08:30 +03008768 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008769 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 (((vmx_instruction_info) >> 3) & 0xf));
8771 else {
8772 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008773 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008774 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008775 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8776 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008777 kvm_inject_page_fault(vcpu, &e);
8778 return 1;
8779 }
8780 }
8781
8782
Nadav Amit27e6fb52014-06-18 17:19:26 +03008783 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008784 /*
8785 * If the vCPU supports "VMWRITE to any supported field in the
8786 * VMCS," then the "read-only" fields are actually read/write.
8787 */
8788 if (vmcs_field_readonly(field) &&
8789 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008790 nested_vmx_failValid(vcpu,
8791 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008792 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008793 }
8794
Liran Alon6d894f42018-06-23 02:35:09 +03008795 if (!is_guest_mode(vcpu))
8796 vmcs12 = get_vmcs12(vcpu);
8797 else {
8798 /*
8799 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8800 * to shadowed-field sets the ALU flags for VMfailInvalid.
8801 */
8802 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8803 nested_vmx_failInvalid(vcpu);
8804 return kvm_skip_emulated_instruction(vcpu);
8805 }
8806 vmcs12 = get_shadow_vmcs12(vcpu);
8807
8808 }
8809
8810 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008811 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008812 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008813 }
8814
Liran Alon6d894f42018-06-23 02:35:09 +03008815 /*
8816 * Do not track vmcs12 dirty-state if in guest-mode
8817 * as we actually dirty shadow vmcs12 instead of vmcs12.
8818 */
8819 if (!is_guest_mode(vcpu)) {
8820 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008821#define SHADOW_FIELD_RW(x) case x:
8822#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008823 /*
8824 * The fields that can be updated by L1 without a vmexit are
8825 * always updated in the vmcs02, the others go down the slow
8826 * path of prepare_vmcs02.
8827 */
8828 break;
8829 default:
8830 vmx->nested.dirty_vmcs12 = true;
8831 break;
8832 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008833 }
8834
Nadav Har'El49f705c2011-05-25 23:08:30 +03008835 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008836 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008837}
8838
Jim Mattsona8bc2842016-11-30 12:03:44 -08008839static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8840{
8841 vmx->nested.current_vmptr = vmptr;
8842 if (enable_shadow_vmcs) {
8843 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8844 SECONDARY_EXEC_SHADOW_VMCS);
8845 vmcs_write64(VMCS_LINK_POINTER,
8846 __pa(vmx->vmcs01.shadow_vmcs));
8847 vmx->nested.sync_shadow_vmcs = true;
8848 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008849 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008850}
8851
Nadav Har'El63846662011-05-25 23:07:29 +03008852/* Emulate the VMPTRLD instruction */
8853static int handle_vmptrld(struct kvm_vcpu *vcpu)
8854{
8855 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008856 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008857
8858 if (!nested_vmx_check_permission(vcpu))
8859 return 1;
8860
Radim Krčmářcbf71272017-05-19 15:48:51 +02008861 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008862 return 1;
8863
Radim Krčmářcbf71272017-05-19 15:48:51 +02008864 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8865 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8866 return kvm_skip_emulated_instruction(vcpu);
8867 }
8868
8869 if (vmptr == vmx->nested.vmxon_ptr) {
8870 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8871 return kvm_skip_emulated_instruction(vcpu);
8872 }
8873
Nadav Har'El63846662011-05-25 23:07:29 +03008874 if (vmx->nested.current_vmptr != vmptr) {
8875 struct vmcs12 *new_vmcs12;
8876 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008877 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8878 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008879 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008880 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008881 }
8882 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008883 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008884 (new_vmcs12->hdr.shadow_vmcs &&
8885 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008886 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008887 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008888 nested_vmx_failValid(vcpu,
8889 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008890 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008891 }
Nadav Har'El63846662011-05-25 23:07:29 +03008892
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008893 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008894 /*
8895 * Load VMCS12 from guest memory since it is not already
8896 * cached.
8897 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008898 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8899 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008900 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008901
Jim Mattsona8bc2842016-11-30 12:03:44 -08008902 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008903 }
8904
8905 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008906 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008907}
8908
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008909/* Emulate the VMPTRST instruction */
8910static int handle_vmptrst(struct kvm_vcpu *vcpu)
8911{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008912 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8913 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8914 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008915 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008916 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917
8918 if (!nested_vmx_check_permission(vcpu))
8919 return 1;
8920
Sean Christopherson0a06d422018-07-19 10:31:00 -07008921 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008922 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008923 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008924 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8925 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008926 kvm_inject_page_fault(vcpu, &e);
8927 return 1;
8928 }
8929 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008930 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008931}
8932
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008933/* Emulate the INVEPT instruction */
8934static int handle_invept(struct kvm_vcpu *vcpu)
8935{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008937 u32 vmx_instruction_info, types;
8938 unsigned long type;
8939 gva_t gva;
8940 struct x86_exception e;
8941 struct {
8942 u64 eptp, gpa;
8943 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008944
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008945 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008946 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008947 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008948 kvm_queue_exception(vcpu, UD_VECTOR);
8949 return 1;
8950 }
8951
8952 if (!nested_vmx_check_permission(vcpu))
8953 return 1;
8954
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008955 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008956 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008958 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959
Jim Mattson85c856b2016-10-26 08:38:38 -07008960 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961 nested_vmx_failValid(vcpu,
8962 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008963 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008964 }
8965
8966 /* According to the Intel VMX instruction reference, the memory
8967 * operand is read even if it isn't needed (e.g., for type==global)
8968 */
8969 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008970 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008971 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008972 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008973 kvm_inject_page_fault(vcpu, &e);
8974 return 1;
8975 }
8976
8977 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008978 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008979 /*
8980 * TODO: track mappings and invalidate
8981 * single context requests appropriately
8982 */
8983 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008984 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008985 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 nested_vmx_succeed(vcpu);
8987 break;
8988 default:
8989 BUG_ON(1);
8990 break;
8991 }
8992
Kyle Huey6affcbe2016-11-29 12:40:40 -08008993 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008994}
8995
Petr Matouseka642fc32014-09-23 20:22:30 +02008996static int handle_invvpid(struct kvm_vcpu *vcpu)
8997{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07008998 struct vcpu_vmx *vmx = to_vmx(vcpu);
8999 u32 vmx_instruction_info;
9000 unsigned long type, types;
9001 gva_t gva;
9002 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009003 struct {
9004 u64 vpid;
9005 u64 gla;
9006 } operand;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009007
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009008 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009009 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009010 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009011 kvm_queue_exception(vcpu, UD_VECTOR);
9012 return 1;
9013 }
9014
9015 if (!nested_vmx_check_permission(vcpu))
9016 return 1;
9017
9018 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9019 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9020
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009021 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009022 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009023
Jim Mattson85c856b2016-10-26 08:38:38 -07009024 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009025 nested_vmx_failValid(vcpu,
9026 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009027 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009028 }
9029
9030 /* according to the intel vmx instruction reference, the memory
9031 * operand is read even if it isn't needed (e.g., for type==global)
9032 */
9033 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9034 vmx_instruction_info, false, &gva))
9035 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009036 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009037 kvm_inject_page_fault(vcpu, &e);
9038 return 1;
9039 }
Jim Mattson40352602017-06-28 09:37:37 -07009040 if (operand.vpid >> 16) {
9041 nested_vmx_failValid(vcpu,
9042 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9043 return kvm_skip_emulated_instruction(vcpu);
9044 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009045
9046 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009047 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009048 if (!operand.vpid ||
9049 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009050 nested_vmx_failValid(vcpu,
9051 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9052 return kvm_skip_emulated_instruction(vcpu);
9053 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009054 if (cpu_has_vmx_invvpid_individual_addr() &&
9055 vmx->nested.vpid02) {
9056 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
9057 vmx->nested.vpid02, operand.gla);
9058 } else
9059 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
9060 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009061 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009062 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009063 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009064 nested_vmx_failValid(vcpu,
9065 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009066 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009067 }
Liran Aloncd9a4912018-05-22 17:16:15 +03009068 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009069 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009070 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Aloncd9a4912018-05-22 17:16:15 +03009071 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009072 break;
9073 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009074 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009075 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009076 }
9077
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009078 nested_vmx_succeed(vcpu);
9079
Kyle Huey6affcbe2016-11-29 12:40:40 -08009080 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009081}
9082
Junaid Shahideb4b2482018-06-27 14:59:14 -07009083static int handle_invpcid(struct kvm_vcpu *vcpu)
9084{
9085 u32 vmx_instruction_info;
9086 unsigned long type;
9087 bool pcid_enabled;
9088 gva_t gva;
9089 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009090 unsigned i;
9091 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009092 struct {
9093 u64 pcid;
9094 u64 gla;
9095 } operand;
9096
9097 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9098 kvm_queue_exception(vcpu, UD_VECTOR);
9099 return 1;
9100 }
9101
9102 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9103 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9104
9105 if (type > 3) {
9106 kvm_inject_gp(vcpu, 0);
9107 return 1;
9108 }
9109
9110 /* According to the Intel instruction reference, the memory operand
9111 * is read even if it isn't needed (e.g., for type==all)
9112 */
9113 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9114 vmx_instruction_info, false, &gva))
9115 return 1;
9116
9117 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9118 kvm_inject_page_fault(vcpu, &e);
9119 return 1;
9120 }
9121
9122 if (operand.pcid >> 12 != 0) {
9123 kvm_inject_gp(vcpu, 0);
9124 return 1;
9125 }
9126
9127 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9128
9129 switch (type) {
9130 case INVPCID_TYPE_INDIV_ADDR:
9131 if ((!pcid_enabled && (operand.pcid != 0)) ||
9132 is_noncanonical_address(operand.gla, vcpu)) {
9133 kvm_inject_gp(vcpu, 0);
9134 return 1;
9135 }
9136 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9137 return kvm_skip_emulated_instruction(vcpu);
9138
9139 case INVPCID_TYPE_SINGLE_CTXT:
9140 if (!pcid_enabled && (operand.pcid != 0)) {
9141 kvm_inject_gp(vcpu, 0);
9142 return 1;
9143 }
9144
9145 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9146 kvm_mmu_sync_roots(vcpu);
9147 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9148 }
9149
Junaid Shahidb94742c2018-06-27 14:59:20 -07009150 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9151 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9152 == operand.pcid)
9153 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009154
Junaid Shahidb94742c2018-06-27 14:59:20 -07009155 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009156 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009157 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009158 * given PCID, then nothing needs to be done here because a
9159 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009160 */
9161
9162 return kvm_skip_emulated_instruction(vcpu);
9163
9164 case INVPCID_TYPE_ALL_NON_GLOBAL:
9165 /*
9166 * Currently, KVM doesn't mark global entries in the shadow
9167 * page tables, so a non-global flush just degenerates to a
9168 * global flush. If needed, we could optimize this later by
9169 * keeping track of global entries in shadow page tables.
9170 */
9171
9172 /* fall-through */
9173 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9174 kvm_mmu_unload(vcpu);
9175 return kvm_skip_emulated_instruction(vcpu);
9176
9177 default:
9178 BUG(); /* We have already checked above that type <= 3 */
9179 }
9180}
9181
Kai Huang843e4332015-01-28 10:54:28 +08009182static int handle_pml_full(struct kvm_vcpu *vcpu)
9183{
9184 unsigned long exit_qualification;
9185
9186 trace_kvm_pml_full(vcpu->vcpu_id);
9187
9188 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9189
9190 /*
9191 * PML buffer FULL happened while executing iret from NMI,
9192 * "blocked by NMI" bit has to be set before next VM entry.
9193 */
9194 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009195 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009196 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9197 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9198 GUEST_INTR_STATE_NMI);
9199
9200 /*
9201 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9202 * here.., and there's no userspace involvement needed for PML.
9203 */
9204 return 1;
9205}
9206
Yunhong Jiang64672c92016-06-13 14:19:59 -07009207static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9208{
Sean Christophersond264ee02018-08-27 15:21:12 -07009209 if (!to_vmx(vcpu)->req_immediate_exit)
9210 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009211 return 1;
9212}
9213
Bandan Das41ab9372017-08-03 15:54:43 -04009214static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9215{
9216 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009217 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9218
9219 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009220 switch (address & VMX_EPTP_MT_MASK) {
9221 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009222 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009223 return false;
9224 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009225 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009226 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009227 return false;
9228 break;
9229 default:
9230 return false;
9231 }
9232
David Hildenbrandbb97a012017-08-10 23:15:28 +02009233 /* only 4 levels page-walk length are valid */
9234 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009235 return false;
9236
9237 /* Reserved bits should not be set */
9238 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9239 return false;
9240
9241 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009242 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009243 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009244 return false;
9245 }
9246
9247 return true;
9248}
9249
9250static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9251 struct vmcs12 *vmcs12)
9252{
9253 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9254 u64 address;
9255 bool accessed_dirty;
9256 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9257
9258 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9259 !nested_cpu_has_ept(vmcs12))
9260 return 1;
9261
9262 if (index >= VMFUNC_EPTP_ENTRIES)
9263 return 1;
9264
9265
9266 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9267 &address, index * 8, 8))
9268 return 1;
9269
David Hildenbrandbb97a012017-08-10 23:15:28 +02009270 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009271
9272 /*
9273 * If the (L2) guest does a vmfunc to the currently
9274 * active ept pointer, we don't have to do anything else
9275 */
9276 if (vmcs12->ept_pointer != address) {
9277 if (!valid_ept_address(vcpu, address))
9278 return 1;
9279
9280 kvm_mmu_unload(vcpu);
9281 mmu->ept_ad = accessed_dirty;
9282 mmu->base_role.ad_disabled = !accessed_dirty;
9283 vmcs12->ept_pointer = address;
9284 /*
9285 * TODO: Check what's the correct approach in case
9286 * mmu reload fails. Currently, we just let the next
9287 * reload potentially fail
9288 */
9289 kvm_mmu_reload(vcpu);
9290 }
9291
9292 return 0;
9293}
9294
Bandan Das2a499e42017-08-03 15:54:41 -04009295static int handle_vmfunc(struct kvm_vcpu *vcpu)
9296{
Bandan Das27c42a12017-08-03 15:54:42 -04009297 struct vcpu_vmx *vmx = to_vmx(vcpu);
9298 struct vmcs12 *vmcs12;
9299 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9300
9301 /*
9302 * VMFUNC is only supported for nested guests, but we always enable the
9303 * secondary control for simplicity; for non-nested mode, fake that we
9304 * didn't by injecting #UD.
9305 */
9306 if (!is_guest_mode(vcpu)) {
9307 kvm_queue_exception(vcpu, UD_VECTOR);
9308 return 1;
9309 }
9310
9311 vmcs12 = get_vmcs12(vcpu);
9312 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9313 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009314
9315 switch (function) {
9316 case 0:
9317 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9318 goto fail;
9319 break;
9320 default:
9321 goto fail;
9322 }
9323 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009324
9325fail:
9326 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9327 vmcs_read32(VM_EXIT_INTR_INFO),
9328 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009329 return 1;
9330}
9331
Sean Christopherson0b665d32018-08-14 09:33:34 -07009332static int handle_encls(struct kvm_vcpu *vcpu)
9333{
9334 /*
9335 * SGX virtualization is not yet supported. There is no software
9336 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9337 * to prevent the guest from executing ENCLS.
9338 */
9339 kvm_queue_exception(vcpu, UD_VECTOR);
9340 return 1;
9341}
9342
Nadav Har'El0140cae2011-05-25 23:06:28 +03009343/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009344 * The exit handlers return 1 if the exit was handled fully and guest execution
9345 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9346 * to be done to userspace and return 0.
9347 */
Mathias Krause772e0312012-08-30 01:30:19 +02009348static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009349 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9350 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009351 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009352 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009353 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009354 [EXIT_REASON_CR_ACCESS] = handle_cr,
9355 [EXIT_REASON_DR_ACCESS] = handle_dr,
9356 [EXIT_REASON_CPUID] = handle_cpuid,
9357 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9358 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9359 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9360 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009361 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009362 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009363 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009364 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009365 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009366 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009367 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009368 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009369 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009370 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009371 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009372 [EXIT_REASON_VMOFF] = handle_vmoff,
9373 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009374 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9375 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009376 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009377 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009378 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009379 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009380 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009381 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009382 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9383 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009384 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9385 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009386 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009387 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009388 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009389 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009390 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009391 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009392 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009393 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009394 [EXIT_REASON_XSAVES] = handle_xsaves,
9395 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009396 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009397 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009398 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009399 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009400 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009401};
9402
9403static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009404 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009405
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009406static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9407 struct vmcs12 *vmcs12)
9408{
9409 unsigned long exit_qualification;
9410 gpa_t bitmap, last_bitmap;
9411 unsigned int port;
9412 int size;
9413 u8 b;
9414
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009415 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009416 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009417
9418 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9419
9420 port = exit_qualification >> 16;
9421 size = (exit_qualification & 7) + 1;
9422
9423 last_bitmap = (gpa_t)-1;
9424 b = -1;
9425
9426 while (size > 0) {
9427 if (port < 0x8000)
9428 bitmap = vmcs12->io_bitmap_a;
9429 else if (port < 0x10000)
9430 bitmap = vmcs12->io_bitmap_b;
9431 else
Joe Perches1d804d02015-03-30 16:46:09 -07009432 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009433 bitmap += (port & 0x7fff) / 8;
9434
9435 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009436 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009437 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009438 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009439 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009440
9441 port++;
9442 size--;
9443 last_bitmap = bitmap;
9444 }
9445
Joe Perches1d804d02015-03-30 16:46:09 -07009446 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009447}
9448
Nadav Har'El644d7112011-05-25 23:12:35 +03009449/*
9450 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9451 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9452 * disinterest in the current event (read or write a specific MSR) by using an
9453 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9454 */
9455static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9456 struct vmcs12 *vmcs12, u32 exit_reason)
9457{
9458 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9459 gpa_t bitmap;
9460
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009461 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009462 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009463
9464 /*
9465 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9466 * for the four combinations of read/write and low/high MSR numbers.
9467 * First we need to figure out which of the four to use:
9468 */
9469 bitmap = vmcs12->msr_bitmap;
9470 if (exit_reason == EXIT_REASON_MSR_WRITE)
9471 bitmap += 2048;
9472 if (msr_index >= 0xc0000000) {
9473 msr_index -= 0xc0000000;
9474 bitmap += 1024;
9475 }
9476
9477 /* Then read the msr_index'th bit from this bitmap: */
9478 if (msr_index < 1024*8) {
9479 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009480 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009481 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009482 return 1 & (b >> (msr_index & 7));
9483 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009484 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009485}
9486
9487/*
9488 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9489 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9490 * intercept (via guest_host_mask etc.) the current event.
9491 */
9492static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9493 struct vmcs12 *vmcs12)
9494{
9495 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9496 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009497 int reg;
9498 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009499
9500 switch ((exit_qualification >> 4) & 3) {
9501 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009502 reg = (exit_qualification >> 8) & 15;
9503 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009504 switch (cr) {
9505 case 0:
9506 if (vmcs12->cr0_guest_host_mask &
9507 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009508 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009509 break;
9510 case 3:
9511 if ((vmcs12->cr3_target_count >= 1 &&
9512 vmcs12->cr3_target_value0 == val) ||
9513 (vmcs12->cr3_target_count >= 2 &&
9514 vmcs12->cr3_target_value1 == val) ||
9515 (vmcs12->cr3_target_count >= 3 &&
9516 vmcs12->cr3_target_value2 == val) ||
9517 (vmcs12->cr3_target_count >= 4 &&
9518 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009519 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009520 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009521 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009522 break;
9523 case 4:
9524 if (vmcs12->cr4_guest_host_mask &
9525 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009526 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009527 break;
9528 case 8:
9529 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009530 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009531 break;
9532 }
9533 break;
9534 case 2: /* clts */
9535 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9536 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009537 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009538 break;
9539 case 1: /* mov from cr */
9540 switch (cr) {
9541 case 3:
9542 if (vmcs12->cpu_based_vm_exec_control &
9543 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 break;
9546 case 8:
9547 if (vmcs12->cpu_based_vm_exec_control &
9548 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009549 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009550 break;
9551 }
9552 break;
9553 case 3: /* lmsw */
9554 /*
9555 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9556 * cr0. Other attempted changes are ignored, with no exit.
9557 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009558 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009559 if (vmcs12->cr0_guest_host_mask & 0xe &
9560 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009561 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009562 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9563 !(vmcs12->cr0_read_shadow & 0x1) &&
9564 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009565 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009566 break;
9567 }
Joe Perches1d804d02015-03-30 16:46:09 -07009568 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009569}
9570
Liran Alona7cde482018-06-23 02:35:10 +03009571static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9572 struct vmcs12 *vmcs12, gpa_t bitmap)
9573{
9574 u32 vmx_instruction_info;
9575 unsigned long field;
9576 u8 b;
9577
9578 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9579 return true;
9580
9581 /* Decode instruction info and find the field to access */
9582 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9583 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9584
9585 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9586 if (field >> 15)
9587 return true;
9588
9589 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9590 return true;
9591
9592 return 1 & (b >> (field & 7));
9593}
9594
Nadav Har'El644d7112011-05-25 23:12:35 +03009595/*
9596 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9597 * should handle it ourselves in L0 (and then continue L2). Only call this
9598 * when in is_guest_mode (L2).
9599 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009600static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009601{
Nadav Har'El644d7112011-05-25 23:12:35 +03009602 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9603 struct vcpu_vmx *vmx = to_vmx(vcpu);
9604 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9605
Jim Mattson4f350c62017-09-14 16:31:44 -07009606 if (vmx->nested.nested_run_pending)
9607 return false;
9608
9609 if (unlikely(vmx->fail)) {
9610 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9611 vmcs_read32(VM_INSTRUCTION_ERROR));
9612 return true;
9613 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009614
David Matlackc9f04402017-08-01 14:00:40 -07009615 /*
9616 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009617 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9618 * Page). The CPU may write to these pages via their host
9619 * physical address while L2 is running, bypassing any
9620 * address-translation-based dirty tracking (e.g. EPT write
9621 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009622 *
9623 * Mark them dirty on every exit from L2 to prevent them from
9624 * getting out of sync with dirty tracking.
9625 */
9626 nested_mark_vmcs12_pages_dirty(vcpu);
9627
Jim Mattson4f350c62017-09-14 16:31:44 -07009628 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9629 vmcs_readl(EXIT_QUALIFICATION),
9630 vmx->idt_vectoring_info,
9631 intr_info,
9632 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9633 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009634
9635 switch (exit_reason) {
9636 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009637 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009638 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009639 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009640 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009641 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009642 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009643 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009644 else if (is_debug(intr_info) &&
9645 vcpu->guest_debug &
9646 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9647 return false;
9648 else if (is_breakpoint(intr_info) &&
9649 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9650 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009651 return vmcs12->exception_bitmap &
9652 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9653 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009654 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009655 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009656 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009657 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009658 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009659 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009660 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009661 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009662 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009663 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009664 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009665 case EXIT_REASON_HLT:
9666 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9667 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009668 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009669 case EXIT_REASON_INVLPG:
9670 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9671 case EXIT_REASON_RDPMC:
9672 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009673 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009674 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009675 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009676 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009677 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009678 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009679 case EXIT_REASON_VMREAD:
9680 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9681 vmcs12->vmread_bitmap);
9682 case EXIT_REASON_VMWRITE:
9683 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9684 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009685 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9686 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009687 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009688 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009689 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009690 /*
9691 * VMX instructions trap unconditionally. This allows L1 to
9692 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9693 */
Joe Perches1d804d02015-03-30 16:46:09 -07009694 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009695 case EXIT_REASON_CR_ACCESS:
9696 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9697 case EXIT_REASON_DR_ACCESS:
9698 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9699 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009700 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009701 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9702 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009703 case EXIT_REASON_MSR_READ:
9704 case EXIT_REASON_MSR_WRITE:
9705 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9706 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009707 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009708 case EXIT_REASON_MWAIT_INSTRUCTION:
9709 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009710 case EXIT_REASON_MONITOR_TRAP_FLAG:
9711 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009712 case EXIT_REASON_MONITOR_INSTRUCTION:
9713 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9714 case EXIT_REASON_PAUSE_INSTRUCTION:
9715 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9716 nested_cpu_has2(vmcs12,
9717 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9718 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009719 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009720 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009721 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009722 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009723 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009724 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009725 /*
9726 * The controls for "virtualize APIC accesses," "APIC-
9727 * register virtualization," and "virtual-interrupt
9728 * delivery" only come from vmcs12.
9729 */
Joe Perches1d804d02015-03-30 16:46:09 -07009730 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009731 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009732 /*
9733 * L0 always deals with the EPT violation. If nested EPT is
9734 * used, and the nested mmu code discovers that the address is
9735 * missing in the guest EPT table (EPT12), the EPT violation
9736 * will be injected with nested_ept_inject_page_fault()
9737 */
Joe Perches1d804d02015-03-30 16:46:09 -07009738 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009739 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009740 /*
9741 * L2 never uses directly L1's EPT, but rather L0's own EPT
9742 * table (shadow on EPT) or a merged EPT table that L0 built
9743 * (EPT on EPT). So any problems with the structure of the
9744 * table is L0's fault.
9745 */
Joe Perches1d804d02015-03-30 16:46:09 -07009746 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009747 case EXIT_REASON_INVPCID:
9748 return
9749 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9750 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009751 case EXIT_REASON_WBINVD:
9752 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9753 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009754 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009755 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9756 /*
9757 * This should never happen, since it is not possible to
9758 * set XSS to a non-zero value---neither in L1 nor in L2.
9759 * If if it were, XSS would have to be checked against
9760 * the XSS exit bitmap in vmcs12.
9761 */
9762 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009763 case EXIT_REASON_PREEMPTION_TIMER:
9764 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009765 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009766 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009767 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009768 case EXIT_REASON_VMFUNC:
9769 /* VM functions are emulated through L2->L0 vmexits. */
9770 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009771 case EXIT_REASON_ENCLS:
9772 /* SGX is never exposed to L1 */
9773 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009774 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009775 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009776 }
9777}
9778
Paolo Bonzini7313c692017-07-27 10:31:25 +02009779static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9780{
9781 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9782
9783 /*
9784 * At this point, the exit interruption info in exit_intr_info
9785 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9786 * we need to query the in-kernel LAPIC.
9787 */
9788 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9789 if ((exit_intr_info &
9790 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9791 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9792 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9793 vmcs12->vm_exit_intr_error_code =
9794 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9795 }
9796
9797 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9798 vmcs_readl(EXIT_QUALIFICATION));
9799 return 1;
9800}
9801
Avi Kivity586f9602010-11-18 13:09:54 +02009802static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9803{
9804 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9805 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9806}
9807
Kai Huanga3eaa862015-11-04 13:46:05 +08009808static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009809{
Kai Huanga3eaa862015-11-04 13:46:05 +08009810 if (vmx->pml_pg) {
9811 __free_page(vmx->pml_pg);
9812 vmx->pml_pg = NULL;
9813 }
Kai Huang843e4332015-01-28 10:54:28 +08009814}
9815
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009816static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009817{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009818 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009819 u64 *pml_buf;
9820 u16 pml_idx;
9821
9822 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9823
9824 /* Do nothing if PML buffer is empty */
9825 if (pml_idx == (PML_ENTITY_NUM - 1))
9826 return;
9827
9828 /* PML index always points to next available PML buffer entity */
9829 if (pml_idx >= PML_ENTITY_NUM)
9830 pml_idx = 0;
9831 else
9832 pml_idx++;
9833
9834 pml_buf = page_address(vmx->pml_pg);
9835 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9836 u64 gpa;
9837
9838 gpa = pml_buf[pml_idx];
9839 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009840 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009841 }
9842
9843 /* reset PML index */
9844 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9845}
9846
9847/*
9848 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9849 * Called before reporting dirty_bitmap to userspace.
9850 */
9851static void kvm_flush_pml_buffers(struct kvm *kvm)
9852{
9853 int i;
9854 struct kvm_vcpu *vcpu;
9855 /*
9856 * We only need to kick vcpu out of guest mode here, as PML buffer
9857 * is flushed at beginning of all VMEXITs, and it's obvious that only
9858 * vcpus running in guest are possible to have unflushed GPAs in PML
9859 * buffer.
9860 */
9861 kvm_for_each_vcpu(i, vcpu, kvm)
9862 kvm_vcpu_kick(vcpu);
9863}
9864
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009865static void vmx_dump_sel(char *name, uint32_t sel)
9866{
9867 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009868 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009869 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9870 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9871 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9872}
9873
9874static void vmx_dump_dtsel(char *name, uint32_t limit)
9875{
9876 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9877 name, vmcs_read32(limit),
9878 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9879}
9880
9881static void dump_vmcs(void)
9882{
9883 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9884 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9885 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9886 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9887 u32 secondary_exec_control = 0;
9888 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009889 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009890 int i, n;
9891
9892 if (cpu_has_secondary_exec_ctrls())
9893 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9894
9895 pr_err("*** Guest State ***\n");
9896 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9897 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9898 vmcs_readl(CR0_GUEST_HOST_MASK));
9899 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9900 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9901 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9902 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9903 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9904 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009905 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9906 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9907 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9908 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009909 }
9910 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9911 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9912 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9913 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9914 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9915 vmcs_readl(GUEST_SYSENTER_ESP),
9916 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9917 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9918 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9919 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9920 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9921 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9922 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9923 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9924 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9925 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9926 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9927 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9928 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009929 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9930 efer, vmcs_read64(GUEST_IA32_PAT));
9931 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9932 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009933 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009934 if (cpu_has_load_perf_global_ctrl &&
9935 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009936 pr_err("PerfGlobCtl = 0x%016llx\n",
9937 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009938 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009939 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009940 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9941 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9942 vmcs_read32(GUEST_ACTIVITY_STATE));
9943 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9944 pr_err("InterruptStatus = %04x\n",
9945 vmcs_read16(GUEST_INTR_STATUS));
9946
9947 pr_err("*** Host State ***\n");
9948 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9949 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9950 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9951 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9952 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9953 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9954 vmcs_read16(HOST_TR_SELECTOR));
9955 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9956 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9957 vmcs_readl(HOST_TR_BASE));
9958 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9959 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9960 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9961 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9962 vmcs_readl(HOST_CR4));
9963 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9964 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9965 vmcs_read32(HOST_IA32_SYSENTER_CS),
9966 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9967 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009968 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9969 vmcs_read64(HOST_IA32_EFER),
9970 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009971 if (cpu_has_load_perf_global_ctrl &&
9972 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009973 pr_err("PerfGlobCtl = 0x%016llx\n",
9974 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009975
9976 pr_err("*** Control State ***\n");
9977 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9978 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9979 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9980 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9981 vmcs_read32(EXCEPTION_BITMAP),
9982 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9983 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9984 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9985 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9986 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9987 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9988 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9989 vmcs_read32(VM_EXIT_INTR_INFO),
9990 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9991 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9992 pr_err(" reason=%08x qualification=%016lx\n",
9993 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
9994 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
9995 vmcs_read32(IDT_VECTORING_INFO_FIELD),
9996 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009997 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08009998 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009999 pr_err("TSC Multiplier = 0x%016llx\n",
10000 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010001 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10002 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10003 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10004 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10005 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010006 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010007 n = vmcs_read32(CR3_TARGET_COUNT);
10008 for (i = 0; i + 1 < n; i += 4)
10009 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10010 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10011 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10012 if (i < n)
10013 pr_err("CR3 target%u=%016lx\n",
10014 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10015 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10016 pr_err("PLE Gap=%08x Window=%08x\n",
10017 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10018 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10019 pr_err("Virtual processor ID = 0x%04x\n",
10020 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10021}
10022
Avi Kivity6aa8b732006-12-10 02:21:36 -080010023/*
10024 * The guest has exited. See if we can fix it or if we need userspace
10025 * assistance.
10026 */
Avi Kivity851ba692009-08-24 11:10:17 +030010027static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010028{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010029 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010030 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010031 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010032
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010033 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10034
Kai Huang843e4332015-01-28 10:54:28 +080010035 /*
10036 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10037 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10038 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10039 * mode as if vcpus is in root mode, the PML buffer must has been
10040 * flushed already.
10041 */
10042 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010043 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010044
Mohammed Gamal80ced182009-09-01 12:48:18 +020010045 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010046 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010047 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010048
Paolo Bonzini7313c692017-07-27 10:31:25 +020010049 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10050 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010051
Mohammed Gamal51207022010-05-31 22:40:54 +030010052 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010053 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010054 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10055 vcpu->run->fail_entry.hardware_entry_failure_reason
10056 = exit_reason;
10057 return 0;
10058 }
10059
Avi Kivity29bd8a72007-09-10 17:27:03 +030010060 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010061 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10062 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010063 = vmcs_read32(VM_INSTRUCTION_ERROR);
10064 return 0;
10065 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010066
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010067 /*
10068 * Note:
10069 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10070 * delivery event since it indicates guest is accessing MMIO.
10071 * The vm-exit can be triggered again after return to guest that
10072 * will cause infinite loop.
10073 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010074 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010075 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010076 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010077 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010078 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10079 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10080 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010081 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010082 vcpu->run->internal.data[0] = vectoring_info;
10083 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010084 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10085 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10086 vcpu->run->internal.ndata++;
10087 vcpu->run->internal.data[3] =
10088 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10089 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010090 return 0;
10091 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010092
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010093 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010094 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10095 if (vmx_interrupt_allowed(vcpu)) {
10096 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10097 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10098 vcpu->arch.nmi_pending) {
10099 /*
10100 * This CPU don't support us in finding the end of an
10101 * NMI-blocked window if the guest runs with IRQs
10102 * disabled. So we pull the trigger after 1 s of
10103 * futile waiting, but inform the user about this.
10104 */
10105 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10106 "state on VCPU %d after 1 s timeout\n",
10107 __func__, vcpu->vcpu_id);
10108 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10109 }
10110 }
10111
Avi Kivity6aa8b732006-12-10 02:21:36 -080010112 if (exit_reason < kvm_vmx_max_exit_handlers
10113 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010114 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010115 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010116 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10117 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010118 kvm_queue_exception(vcpu, UD_VECTOR);
10119 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010120 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010121}
10122
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010123/*
10124 * Software based L1D cache flush which is used when microcode providing
10125 * the cache control MSR is not loaded.
10126 *
10127 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10128 * flush it is required to read in 64 KiB because the replacement algorithm
10129 * is not exactly LRU. This could be sized at runtime via topology
10130 * information but as all relevant affected CPUs have 32KiB L1D cache size
10131 * there is no point in doing so.
10132 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010133static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010134{
10135 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010136
10137 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010138 * This code is only executed when the the flush mode is 'cond' or
10139 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010140 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010141 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010142 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010143
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010144 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010145 * Clear the per-vcpu flush bit, it gets set again
10146 * either from vcpu_run() or from one of the unsafe
10147 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010148 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010149 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010150 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010151
10152 /*
10153 * Clear the per-cpu flush bit, it gets set again from
10154 * the interrupt handlers.
10155 */
10156 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10157 kvm_clear_cpu_l1tf_flush_l1d();
10158
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010159 if (!flush_l1d)
10160 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010161 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010162
10163 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010164
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010165 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10166 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10167 return;
10168 }
10169
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010170 asm volatile(
10171 /* First ensure the pages are in the TLB */
10172 "xorl %%eax, %%eax\n"
10173 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010174 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010175 "addl $4096, %%eax\n\t"
10176 "cmpl %%eax, %[size]\n\t"
10177 "jne .Lpopulate_tlb\n\t"
10178 "xorl %%eax, %%eax\n\t"
10179 "cpuid\n\t"
10180 /* Now fill the cache */
10181 "xorl %%eax, %%eax\n"
10182 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010183 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010184 "addl $64, %%eax\n\t"
10185 "cmpl %%eax, %[size]\n\t"
10186 "jne .Lfill_cache\n\t"
10187 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010188 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010189 [size] "r" (size)
10190 : "eax", "ebx", "ecx", "edx");
10191}
10192
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010193static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010194{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010195 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10196
10197 if (is_guest_mode(vcpu) &&
10198 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10199 return;
10200
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010201 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010202 vmcs_write32(TPR_THRESHOLD, 0);
10203 return;
10204 }
10205
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010206 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010207}
10208
Jim Mattson8d860bb2018-05-09 16:56:05 -040010209static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010210{
10211 u32 sec_exec_control;
10212
Jim Mattson8d860bb2018-05-09 16:56:05 -040010213 if (!lapic_in_kernel(vcpu))
10214 return;
10215
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010216 /* Postpone execution until vmcs01 is the current VMCS. */
10217 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010218 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010219 return;
10220 }
10221
Paolo Bonzini35754c92015-07-29 12:05:37 +020010222 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +080010223 return;
10224
10225 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010226 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10227 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010228
Jim Mattson8d860bb2018-05-09 16:56:05 -040010229 switch (kvm_get_apic_mode(vcpu)) {
10230 case LAPIC_MODE_INVALID:
10231 WARN_ONCE(true, "Invalid local APIC state");
10232 case LAPIC_MODE_DISABLED:
10233 break;
10234 case LAPIC_MODE_XAPIC:
10235 if (flexpriority_enabled) {
10236 sec_exec_control |=
10237 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10238 vmx_flush_tlb(vcpu, true);
10239 }
10240 break;
10241 case LAPIC_MODE_X2APIC:
10242 if (cpu_has_vmx_virtualize_x2apic_mode())
10243 sec_exec_control |=
10244 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10245 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010246 }
10247 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10248
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010249 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010250}
10251
Tang Chen38b99172014-09-24 15:57:54 +080010252static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10253{
Jim Mattsonab5df312018-05-09 17:02:03 -040010254 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010255 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010256 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010257 }
Tang Chen38b99172014-09-24 15:57:54 +080010258}
10259
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010260static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010261{
10262 u16 status;
10263 u8 old;
10264
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010265 if (max_isr == -1)
10266 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010267
10268 status = vmcs_read16(GUEST_INTR_STATUS);
10269 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010270 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010271 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010272 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010273 vmcs_write16(GUEST_INTR_STATUS, status);
10274 }
10275}
10276
10277static void vmx_set_rvi(int vector)
10278{
10279 u16 status;
10280 u8 old;
10281
Wei Wang4114c272014-11-05 10:53:43 +080010282 if (vector == -1)
10283 vector = 0;
10284
Yang Zhangc7c9c562013-01-25 10:18:51 +080010285 status = vmcs_read16(GUEST_INTR_STATUS);
10286 old = (u8)status & 0xff;
10287 if ((u8)vector != old) {
10288 status &= ~0xff;
10289 status |= (u8)vector;
10290 vmcs_write16(GUEST_INTR_STATUS, status);
10291 }
10292}
10293
10294static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10295{
Liran Alon851c1a182017-12-24 18:12:56 +020010296 /*
10297 * When running L2, updating RVI is only relevant when
10298 * vmcs12 virtual-interrupt-delivery enabled.
10299 * However, it can be enabled only when L1 also
10300 * intercepts external-interrupts and in that case
10301 * we should not update vmcs02 RVI but instead intercept
10302 * interrupt. Therefore, do nothing when running L2.
10303 */
10304 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010305 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010306}
10307
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010308static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010309{
10310 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010311 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010312 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010313
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010314 WARN_ON(!vcpu->arch.apicv_active);
10315 if (pi_test_on(&vmx->pi_desc)) {
10316 pi_clear_on(&vmx->pi_desc);
10317 /*
10318 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10319 * But on x86 this is just a compiler barrier anyway.
10320 */
10321 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010322 max_irr_updated =
10323 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10324
10325 /*
10326 * If we are running L2 and L1 has a new pending interrupt
10327 * which can be injected, we should re-evaluate
10328 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010329 * If L1 intercepts external-interrupts, we should
10330 * exit from L2 to L1. Otherwise, interrupt should be
10331 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010332 */
Liran Alon851c1a182017-12-24 18:12:56 +020010333 if (is_guest_mode(vcpu) && max_irr_updated) {
10334 if (nested_exit_on_intr(vcpu))
10335 kvm_vcpu_exiting_guest_mode(vcpu);
10336 else
10337 kvm_make_request(KVM_REQ_EVENT, vcpu);
10338 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010339 } else {
10340 max_irr = kvm_lapic_find_highest_irr(vcpu);
10341 }
10342 vmx_hwapic_irr_update(vcpu, max_irr);
10343 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010344}
10345
Andrey Smetanin63086302015-11-10 15:36:32 +030010346static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010347{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010348 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010349 return;
10350
Yang Zhangc7c9c562013-01-25 10:18:51 +080010351 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10352 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10353 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10354 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10355}
10356
Paolo Bonzini967235d2016-12-19 14:03:45 +010010357static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10358{
10359 struct vcpu_vmx *vmx = to_vmx(vcpu);
10360
10361 pi_clear_on(&vmx->pi_desc);
10362 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10363}
10364
Avi Kivity51aa01d2010-07-20 14:31:20 +030010365static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010366{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010367 u32 exit_intr_info = 0;
10368 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010369
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010370 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10371 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010372 return;
10373
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010374 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10375 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10376 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010377
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010378 /* if exit due to PF check for async PF */
10379 if (is_page_fault(exit_intr_info))
10380 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10381
Andi Kleena0861c02009-06-08 17:37:09 +080010382 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010383 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10384 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010385 kvm_machine_check();
10386
Gleb Natapov20f65982009-05-11 13:35:55 +030010387 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010388 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010389 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010390 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010391 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010392 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010393}
Gleb Natapov20f65982009-05-11 13:35:55 +030010394
Yang Zhanga547c6d2013-04-11 19:25:10 +080010395static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10396{
10397 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10398
Yang Zhanga547c6d2013-04-11 19:25:10 +080010399 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10400 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10401 unsigned int vector;
10402 unsigned long entry;
10403 gate_desc *desc;
10404 struct vcpu_vmx *vmx = to_vmx(vcpu);
10405#ifdef CONFIG_X86_64
10406 unsigned long tmp;
10407#endif
10408
10409 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10410 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010411 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010412 asm volatile(
10413#ifdef CONFIG_X86_64
10414 "mov %%" _ASM_SP ", %[sp]\n\t"
10415 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10416 "push $%c[ss]\n\t"
10417 "push %[sp]\n\t"
10418#endif
10419 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010420 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010421 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010422 :
10423#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010424 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010425#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010426 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010427 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010428 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010429 [ss]"i"(__KERNEL_DS),
10430 [cs]"i"(__KERNEL_CS)
10431 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010432 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010433}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010434STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010435
Tom Lendackybc226f02018-05-10 22:06:39 +020010436static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010437{
Tom Lendackybc226f02018-05-10 22:06:39 +020010438 switch (index) {
10439 case MSR_IA32_SMBASE:
10440 /*
10441 * We cannot do SMM unless we can run the guest in big
10442 * real mode.
10443 */
10444 return enable_unrestricted_guest || emulate_invalid_guest_state;
10445 case MSR_AMD64_VIRT_SPEC_CTRL:
10446 /* This is AMD only. */
10447 return false;
10448 default:
10449 return true;
10450 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010451}
10452
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010453static bool vmx_mpx_supported(void)
10454{
10455 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10456 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10457}
10458
Wanpeng Li55412b22014-12-02 19:21:30 +080010459static bool vmx_xsaves_supported(void)
10460{
10461 return vmcs_config.cpu_based_2nd_exec_ctrl &
10462 SECONDARY_EXEC_XSAVES;
10463}
10464
Avi Kivity51aa01d2010-07-20 14:31:20 +030010465static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10466{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010467 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010468 bool unblock_nmi;
10469 u8 vector;
10470 bool idtv_info_valid;
10471
10472 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010473
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010474 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010475 if (vmx->loaded_vmcs->nmi_known_unmasked)
10476 return;
10477 /*
10478 * Can't use vmx->exit_intr_info since we're not sure what
10479 * the exit reason is.
10480 */
10481 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10482 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10483 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10484 /*
10485 * SDM 3: 27.7.1.2 (September 2008)
10486 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10487 * a guest IRET fault.
10488 * SDM 3: 23.2.2 (September 2008)
10489 * Bit 12 is undefined in any of the following cases:
10490 * If the VM exit sets the valid bit in the IDT-vectoring
10491 * information field.
10492 * If the VM exit is due to a double fault.
10493 */
10494 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10495 vector != DF_VECTOR && !idtv_info_valid)
10496 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10497 GUEST_INTR_STATE_NMI);
10498 else
10499 vmx->loaded_vmcs->nmi_known_unmasked =
10500 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10501 & GUEST_INTR_STATE_NMI);
10502 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10503 vmx->loaded_vmcs->vnmi_blocked_time +=
10504 ktime_to_ns(ktime_sub(ktime_get(),
10505 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010506}
10507
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010508static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010509 u32 idt_vectoring_info,
10510 int instr_len_field,
10511 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010512{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010513 u8 vector;
10514 int type;
10515 bool idtv_info_valid;
10516
10517 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010518
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010519 vcpu->arch.nmi_injected = false;
10520 kvm_clear_exception_queue(vcpu);
10521 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010522
10523 if (!idtv_info_valid)
10524 return;
10525
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010526 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010527
Avi Kivity668f6122008-07-02 09:28:55 +030010528 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10529 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010530
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010531 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010532 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010533 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010534 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010535 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010536 * Clear bit "block by NMI" before VM entry if a NMI
10537 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010538 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010539 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010540 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010541 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010542 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010543 /* fall through */
10544 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010545 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010546 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010547 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010548 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010549 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010550 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010551 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010552 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010553 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010555 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010556 break;
10557 default:
10558 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010559 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010560}
10561
Avi Kivity83422e12010-07-20 14:43:23 +030010562static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10563{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010564 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010565 VM_EXIT_INSTRUCTION_LEN,
10566 IDT_VECTORING_ERROR_CODE);
10567}
10568
Avi Kivityb463a6f2010-07-20 15:06:17 +030010569static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10570{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010571 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010572 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10573 VM_ENTRY_INSTRUCTION_LEN,
10574 VM_ENTRY_EXCEPTION_ERROR_CODE);
10575
10576 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10577}
10578
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010579static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10580{
10581 int i, nr_msrs;
10582 struct perf_guest_switch_msr *msrs;
10583
10584 msrs = perf_guest_get_msrs(&nr_msrs);
10585
10586 if (!msrs)
10587 return;
10588
10589 for (i = 0; i < nr_msrs; i++)
10590 if (msrs[i].host == msrs[i].guest)
10591 clear_atomic_switch_msr(vmx, msrs[i].msr);
10592 else
10593 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010594 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010595}
10596
Sean Christophersonf459a702018-08-27 15:21:11 -070010597static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10598{
10599 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10600 if (!vmx->loaded_vmcs->hv_timer_armed)
10601 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10602 PIN_BASED_VMX_PREEMPTION_TIMER);
10603 vmx->loaded_vmcs->hv_timer_armed = true;
10604}
10605
10606static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010607{
10608 struct vcpu_vmx *vmx = to_vmx(vcpu);
10609 u64 tscl;
10610 u32 delta_tsc;
10611
Sean Christophersond264ee02018-08-27 15:21:12 -070010612 if (vmx->req_immediate_exit) {
10613 vmx_arm_hv_timer(vmx, 0);
10614 return;
10615 }
10616
Sean Christophersonf459a702018-08-27 15:21:11 -070010617 if (vmx->hv_deadline_tsc != -1) {
10618 tscl = rdtsc();
10619 if (vmx->hv_deadline_tsc > tscl)
10620 /* set_hv_timer ensures the delta fits in 32-bits */
10621 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10622 cpu_preemption_timer_multi);
10623 else
10624 delta_tsc = 0;
10625
10626 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010627 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010628 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010629
Sean Christophersonf459a702018-08-27 15:21:11 -070010630 if (vmx->loaded_vmcs->hv_timer_armed)
10631 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10632 PIN_BASED_VMX_PREEMPTION_TIMER);
10633 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010634}
10635
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010636static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010637{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010639 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010640
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010641 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010642 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010643 vmx->loaded_vmcs->soft_vnmi_blocked))
10644 vmx->loaded_vmcs->entry_time = ktime_get();
10645
Avi Kivity104f2262010-11-18 13:12:52 +020010646 /* Don't enter VMX if guest state is invalid, let the exit handler
10647 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010648 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010649 return;
10650
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010651 if (vmx->ple_window_dirty) {
10652 vmx->ple_window_dirty = false;
10653 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10654 }
10655
Abel Gordon012f83c2013-04-18 14:39:25 +030010656 if (vmx->nested.sync_shadow_vmcs) {
10657 copy_vmcs12_to_shadow(vmx);
10658 vmx->nested.sync_shadow_vmcs = false;
10659 }
10660
Avi Kivity104f2262010-11-18 13:12:52 +020010661 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10662 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10663 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10664 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10665
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010666 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010667 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010668 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010669 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010670 }
10671
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010672 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010673 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010674 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010675 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010676 }
10677
Avi Kivity104f2262010-11-18 13:12:52 +020010678 /* When single-stepping over STI and MOV SS, we must clear the
10679 * corresponding interruptibility bits in the guest state. Otherwise
10680 * vmentry fails as it then expects bit 14 (BS) in pending debug
10681 * exceptions being set, but that's not correct for the guest debugging
10682 * case. */
10683 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10684 vmx_set_interrupt_shadow(vcpu, 0);
10685
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010686 if (static_cpu_has(X86_FEATURE_PKU) &&
10687 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10688 vcpu->arch.pkru != vmx->host_pkru)
10689 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010690
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010691 atomic_switch_perf_msrs(vmx);
10692
Sean Christophersonf459a702018-08-27 15:21:11 -070010693 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010694
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010695 /*
10696 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10697 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10698 * is no need to worry about the conditional branch over the wrmsr
10699 * being speculatively taken.
10700 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010701 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010702
Nadav Har'Eld462b812011-05-24 15:26:10 +030010703 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010704
10705 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10706 (unsigned long)&current_evmcs->host_rsp : 0;
10707
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010708 if (static_branch_unlikely(&vmx_l1d_should_flush))
10709 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010710
Avi Kivity104f2262010-11-18 13:12:52 +020010711 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010712 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010713 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10714 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10715 "push %%" _ASM_CX " \n\t"
10716 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010717 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010718 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010719 /* Avoid VMWRITE when Enlightened VMCS is in use */
10720 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10721 "jz 2f \n\t"
10722 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10723 "jmp 1f \n\t"
10724 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010725 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010726 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010727 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010728 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10729 "mov %%cr2, %%" _ASM_DX " \n\t"
10730 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010731 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010732 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010733 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010734 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010735 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010736 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010737 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10738 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10739 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10740 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10741 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10742 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010743#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010744 "mov %c[r8](%0), %%r8 \n\t"
10745 "mov %c[r9](%0), %%r9 \n\t"
10746 "mov %c[r10](%0), %%r10 \n\t"
10747 "mov %c[r11](%0), %%r11 \n\t"
10748 "mov %c[r12](%0), %%r12 \n\t"
10749 "mov %c[r13](%0), %%r13 \n\t"
10750 "mov %c[r14](%0), %%r14 \n\t"
10751 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010752#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010753 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010754
Avi Kivity6aa8b732006-12-10 02:21:36 -080010755 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010756 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010757 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010758 "jmp 2f \n\t"
10759 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10760 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010761 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010762 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010763 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010764 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010765 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10766 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10767 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10768 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10769 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10770 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10771 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010772#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010773 "mov %%r8, %c[r8](%0) \n\t"
10774 "mov %%r9, %c[r9](%0) \n\t"
10775 "mov %%r10, %c[r10](%0) \n\t"
10776 "mov %%r11, %c[r11](%0) \n\t"
10777 "mov %%r12, %c[r12](%0) \n\t"
10778 "mov %%r13, %c[r13](%0) \n\t"
10779 "mov %%r14, %c[r14](%0) \n\t"
10780 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010781 "xor %%r8d, %%r8d \n\t"
10782 "xor %%r9d, %%r9d \n\t"
10783 "xor %%r10d, %%r10d \n\t"
10784 "xor %%r11d, %%r11d \n\t"
10785 "xor %%r12d, %%r12d \n\t"
10786 "xor %%r13d, %%r13d \n\t"
10787 "xor %%r14d, %%r14d \n\t"
10788 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010789#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010790 "mov %%cr2, %%" _ASM_AX " \n\t"
10791 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010792
Jim Mattson0cb5b302018-01-03 14:31:38 -080010793 "xor %%eax, %%eax \n\t"
10794 "xor %%ebx, %%ebx \n\t"
10795 "xor %%esi, %%esi \n\t"
10796 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010797 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010798 ".pushsection .rodata \n\t"
10799 ".global vmx_return \n\t"
10800 "vmx_return: " _ASM_PTR " 2b \n\t"
10801 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010802 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010803 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010804 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010805 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010806 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10807 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10808 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10809 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10810 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10811 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10812 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010813#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010814 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10815 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10816 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10817 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10818 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10819 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10820 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10821 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010822#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010823 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10824 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010825 : "cc", "memory"
10826#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010827 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010828 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010829#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010830 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010831#endif
10832 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010833
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010834 /*
10835 * We do not use IBRS in the kernel. If this vCPU has used the
10836 * SPEC_CTRL MSR it may have left it on; save the value and
10837 * turn it off. This is much more efficient than blindly adding
10838 * it to the atomic save/restore list. Especially as the former
10839 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10840 *
10841 * For non-nested case:
10842 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10843 * save it.
10844 *
10845 * For nested case:
10846 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10847 * save it.
10848 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010849 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010850 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010851
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010852 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010853
David Woodhouse117cc7a2018-01-12 11:11:27 +000010854 /* Eliminate branch target predictions from guest mode */
10855 vmexit_fill_RSB();
10856
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010857 /* All fields are clean at this point */
10858 if (static_branch_unlikely(&enable_evmcs))
10859 current_evmcs->hv_clean_fields |=
10860 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10861
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010862 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010863 if (vmx->host_debugctlmsr)
10864 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010865
Avi Kivityaa67f602012-08-01 16:48:03 +030010866#ifndef CONFIG_X86_64
10867 /*
10868 * The sysexit path does not restore ds/es, so we must set them to
10869 * a reasonable value ourselves.
10870 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010871 * We can't defer this to vmx_prepare_switch_to_host() since that
10872 * function may be executed in interrupt context, which saves and
10873 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010874 */
10875 loadsegment(ds, __USER_DS);
10876 loadsegment(es, __USER_DS);
10877#endif
10878
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010879 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010880 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010881 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010882 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010883 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010884 vcpu->arch.regs_dirty = 0;
10885
Gleb Natapove0b890d2013-09-25 12:51:33 +030010886 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010887 * eager fpu is enabled if PKEY is supported and CR4 is switched
10888 * back on host, so it is safe to read guest PKRU from current
10889 * XSAVE.
10890 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010891 if (static_cpu_has(X86_FEATURE_PKU) &&
10892 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10893 vcpu->arch.pkru = __read_pkru();
10894 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010895 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010896 }
10897
Gleb Natapove0b890d2013-09-25 12:51:33 +030010898 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010899 vmx->idt_vectoring_info = 0;
10900
10901 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10902 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10903 return;
10904
10905 vmx->loaded_vmcs->launched = 1;
10906 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010907
Avi Kivity51aa01d2010-07-20 14:31:20 +030010908 vmx_complete_atomic_exit(vmx);
10909 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010910 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010911}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010912STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010913
Sean Christopherson434a1e92018-03-20 12:17:18 -070010914static struct kvm *vmx_vm_alloc(void)
10915{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010916 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010917 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010918}
10919
10920static void vmx_vm_free(struct kvm *kvm)
10921{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010922 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010923}
10924
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010925static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010926{
10927 struct vcpu_vmx *vmx = to_vmx(vcpu);
10928 int cpu;
10929
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010930 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010931 return;
10932
10933 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010934 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010935 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010936 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010937 put_cpu();
10938}
10939
Jim Mattson2f1fe812016-07-08 15:36:06 -070010940/*
10941 * Ensure that the current vmcs of the logical processor is the
10942 * vmcs01 of the vcpu before calling free_nested().
10943 */
10944static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10945{
10946 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010947
Christoffer Dallec7660c2017-12-04 21:35:23 +010010948 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010949 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010950 free_nested(vmx);
10951 vcpu_put(vcpu);
10952}
10953
Avi Kivity6aa8b732006-12-10 02:21:36 -080010954static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10955{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010956 struct vcpu_vmx *vmx = to_vmx(vcpu);
10957
Kai Huang843e4332015-01-28 10:54:28 +080010958 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010959 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010960 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010961 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010962 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010963 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010964 kfree(vmx->guest_msrs);
10965 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010966 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010967}
10968
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010969static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010970{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010971 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010972 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010973 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010974 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010975
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010976 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010977 return ERR_PTR(-ENOMEM);
10978
Wanpeng Li991e7a02015-09-16 17:30:05 +080010979 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010980
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010981 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10982 if (err)
10983 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080010984
Peter Feiner4e595162016-07-07 14:49:58 -070010985 err = -ENOMEM;
10986
10987 /*
10988 * If PML is turned on, failure on enabling PML just results in failure
10989 * of creating the vcpu, therefore we can simplify PML logic (by
10990 * avoiding dealing with cases, such as enabling PML partially on vcpus
10991 * for the guest, etc.
10992 */
10993 if (enable_pml) {
10994 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
10995 if (!vmx->pml_pg)
10996 goto uninit_vcpu;
10997 }
10998
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010999 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011000 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11001 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011002
Peter Feiner4e595162016-07-07 14:49:58 -070011003 if (!vmx->guest_msrs)
11004 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011005
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011006 err = alloc_loaded_vmcs(&vmx->vmcs01);
11007 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011008 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011009
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011010 msr_bitmap = vmx->vmcs01.msr_bitmap;
11011 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11012 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11013 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11014 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11015 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11016 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11017 vmx->msr_bitmap_mode = 0;
11018
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011019 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011020 cpu = get_cpu();
11021 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011022 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011023 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011024 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011025 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011026 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011027 err = alloc_apic_access_page(kvm);
11028 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011029 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011030 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011031
Sean Christophersone90008d2018-03-05 12:04:37 -080011032 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011033 err = init_rmode_identity_map(kvm);
11034 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011035 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011036 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011037
Roman Kagan63aff652018-07-19 21:59:07 +030011038 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011039 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11040 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011041
Wincy Van705699a2015-02-03 23:58:17 +080011042 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011043 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011044
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011045 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11046
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011047 /*
11048 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11049 * or POSTED_INTR_WAKEUP_VECTOR.
11050 */
11051 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11052 vmx->pi_desc.sn = 1;
11053
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011054 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011055
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011056free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011057 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011058free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011059 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011060free_pml:
11061 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011062uninit_vcpu:
11063 kvm_vcpu_uninit(&vmx->vcpu);
11064free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011065 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011066 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011067 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011068}
11069
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011070#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"
11071#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 -040011072
Wanpeng Lib31c1142018-03-12 04:53:04 -070011073static int vmx_vm_init(struct kvm *kvm)
11074{
Tianyu Lan877ad952018-07-19 08:40:23 +000011075 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11076
Wanpeng Lib31c1142018-03-12 04:53:04 -070011077 if (!ple_gap)
11078 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011079
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011080 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11081 switch (l1tf_mitigation) {
11082 case L1TF_MITIGATION_OFF:
11083 case L1TF_MITIGATION_FLUSH_NOWARN:
11084 /* 'I explicitly don't care' is set */
11085 break;
11086 case L1TF_MITIGATION_FLUSH:
11087 case L1TF_MITIGATION_FLUSH_NOSMT:
11088 case L1TF_MITIGATION_FULL:
11089 /*
11090 * Warn upon starting the first VM in a potentially
11091 * insecure environment.
11092 */
11093 if (cpu_smt_control == CPU_SMT_ENABLED)
11094 pr_warn_once(L1TF_MSG_SMT);
11095 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11096 pr_warn_once(L1TF_MSG_L1D);
11097 break;
11098 case L1TF_MITIGATION_FULL_FORCE:
11099 /* Flush is enforced */
11100 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011101 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011102 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011103 return 0;
11104}
11105
Yang, Sheng002c7f72007-07-31 14:23:01 +030011106static void __init vmx_check_processor_compat(void *rtn)
11107{
11108 struct vmcs_config vmcs_conf;
11109
11110 *(int *)rtn = 0;
11111 if (setup_vmcs_config(&vmcs_conf) < 0)
11112 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011113 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011114 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11115 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11116 smp_processor_id());
11117 *(int *)rtn = -EIO;
11118 }
11119}
11120
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011121static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011122{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011123 u8 cache;
11124 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011125
Sheng Yang522c68c2009-04-27 20:35:43 +080011126 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011127 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011128 * 2. EPT with VT-d:
11129 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011130 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011131 * b. VT-d with snooping control feature: snooping control feature of
11132 * VT-d engine can guarantee the cache correctness. Just set it
11133 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011134 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011135 * consistent with host MTRR
11136 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011137 if (is_mmio) {
11138 cache = MTRR_TYPE_UNCACHABLE;
11139 goto exit;
11140 }
11141
11142 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011143 ipat = VMX_EPT_IPAT_BIT;
11144 cache = MTRR_TYPE_WRBACK;
11145 goto exit;
11146 }
11147
11148 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11149 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011150 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011151 cache = MTRR_TYPE_WRBACK;
11152 else
11153 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011154 goto exit;
11155 }
11156
Xiao Guangrongff536042015-06-15 16:55:22 +080011157 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011158
11159exit:
11160 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011161}
11162
Sheng Yang17cc3932010-01-05 19:02:27 +080011163static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011164{
Sheng Yang878403b2010-01-05 19:02:29 +080011165 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11166 return PT_DIRECTORY_LEVEL;
11167 else
11168 /* For shadow and EPT supported 1GB page */
11169 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011170}
11171
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011172static void vmcs_set_secondary_exec_control(u32 new_ctl)
11173{
11174 /*
11175 * These bits in the secondary execution controls field
11176 * are dynamic, the others are mostly based on the hypervisor
11177 * architecture and the guest's CPUID. Do not touch the
11178 * dynamic bits.
11179 */
11180 u32 mask =
11181 SECONDARY_EXEC_SHADOW_VMCS |
11182 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011183 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11184 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011185
11186 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11187
11188 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11189 (new_ctl & ~mask) | (cur_ctl & mask));
11190}
11191
David Matlack8322ebb2016-11-29 18:14:09 -080011192/*
11193 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11194 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11195 */
11196static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11197{
11198 struct vcpu_vmx *vmx = to_vmx(vcpu);
11199 struct kvm_cpuid_entry2 *entry;
11200
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011201 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11202 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011203
11204#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11205 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011206 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011207} while (0)
11208
11209 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11210 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11211 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11212 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11213 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11214 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11215 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11216 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11217 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11218 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11219 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11220 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11221 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11222 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11223 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11224
11225 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11226 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11227 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11228 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11229 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011230 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011231
11232#undef cr4_fixed1_update
11233}
11234
Liran Alon5f76f6f2018-09-14 03:25:52 +030011235static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11236{
11237 struct vcpu_vmx *vmx = to_vmx(vcpu);
11238
11239 if (kvm_mpx_supported()) {
11240 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11241
11242 if (mpx_enabled) {
11243 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11244 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11245 } else {
11246 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11247 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11248 }
11249 }
11250}
11251
Sheng Yang0e851882009-12-18 16:48:46 +080011252static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11253{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011254 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011255
Paolo Bonzini80154d72017-08-24 13:55:35 +020011256 if (cpu_has_secondary_exec_ctrls()) {
11257 vmx_compute_secondary_exec_control(vmx);
11258 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011259 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011260
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011261 if (nested_vmx_allowed(vcpu))
11262 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11263 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11264 else
11265 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11266 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011267
Liran Alon5f76f6f2018-09-14 03:25:52 +030011268 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011269 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011270 nested_vmx_entry_exit_ctls_update(vcpu);
11271 }
Sheng Yang0e851882009-12-18 16:48:46 +080011272}
11273
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011274static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11275{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011276 if (func == 1 && nested)
11277 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011278}
11279
Yang Zhang25d92082013-08-06 12:00:32 +030011280static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11281 struct x86_exception *fault)
11282{
Jan Kiszka533558b2014-01-04 18:47:20 +010011283 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011284 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011285 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011286 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011287
Bandan Dasc5f983f2017-05-05 15:25:14 -040011288 if (vmx->nested.pml_full) {
11289 exit_reason = EXIT_REASON_PML_FULL;
11290 vmx->nested.pml_full = false;
11291 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11292 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011293 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011294 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011295 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011296
11297 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011298 vmcs12->guest_physical_address = fault->address;
11299}
11300
Peter Feiner995f00a2017-06-30 17:26:32 -070011301static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11302{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011303 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011304}
11305
Nadav Har'El155a97a2013-08-05 11:07:16 +030011306/* Callbacks for nested_ept_init_mmu_context: */
11307
11308static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11309{
11310 /* return the page table to be shadowed - in our case, EPT12 */
11311 return get_vmcs12(vcpu)->ept_pointer;
11312}
11313
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011314static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011315{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011316 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011317 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011318 return 1;
11319
Paolo Bonziniad896af2013-10-02 16:56:14 +020011320 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011321 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011322 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011323 nested_ept_ad_enabled(vcpu),
11324 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011325 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11326 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11327 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11328
11329 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011330 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011331}
11332
11333static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11334{
11335 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11336}
11337
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011338static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11339 u16 error_code)
11340{
11341 bool inequality, bit;
11342
11343 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11344 inequality =
11345 (error_code & vmcs12->page_fault_error_code_mask) !=
11346 vmcs12->page_fault_error_code_match;
11347 return inequality ^ bit;
11348}
11349
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011350static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11351 struct x86_exception *fault)
11352{
11353 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11354
11355 WARN_ON(!is_guest_mode(vcpu));
11356
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011357 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11358 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011359 vmcs12->vm_exit_intr_error_code = fault->error_code;
11360 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11361 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11362 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11363 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011364 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011365 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011366 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011367}
11368
Paolo Bonzinic9923842017-12-13 14:16:30 +010011369static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11370 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011371
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011372static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011373{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011374 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011375 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011376 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011377 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011378
11379 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011380 /*
11381 * Translate L1 physical address to host physical
11382 * address for vmcs02. Keep the page pinned, so this
11383 * physical address remains valid. We keep a reference
11384 * to it so we can release it later.
11385 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011386 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011387 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011388 vmx->nested.apic_access_page = NULL;
11389 }
11390 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011391 /*
11392 * If translation failed, no matter: This feature asks
11393 * to exit when accessing the given address, and if it
11394 * can never be accessed, this feature won't do
11395 * anything anyway.
11396 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011397 if (!is_error_page(page)) {
11398 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011399 hpa = page_to_phys(vmx->nested.apic_access_page);
11400 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11401 } else {
11402 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11403 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11404 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011405 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011406
11407 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011408 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011409 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011410 vmx->nested.virtual_apic_page = NULL;
11411 }
11412 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011413
11414 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011415 * If translation failed, VM entry will fail because
11416 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11417 * Failing the vm entry is _not_ what the processor
11418 * does but it's basically the only possibility we
11419 * have. We could still enter the guest if CR8 load
11420 * exits are enabled, CR8 store exits are enabled, and
11421 * virtualize APIC access is disabled; in this case
11422 * the processor would never use the TPR shadow and we
11423 * could simply clear the bit from the execution
11424 * control. But such a configuration is useless, so
11425 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011426 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011427 if (!is_error_page(page)) {
11428 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011429 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11430 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11431 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011432 }
11433
Wincy Van705699a2015-02-03 23:58:17 +080011434 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011435 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11436 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011437 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011438 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011439 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011440 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11441 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011442 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011443 vmx->nested.pi_desc_page = page;
11444 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011445 vmx->nested.pi_desc =
11446 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11447 (unsigned long)(vmcs12->posted_intr_desc_addr &
11448 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011449 vmcs_write64(POSTED_INTR_DESC_ADDR,
11450 page_to_phys(vmx->nested.pi_desc_page) +
11451 (unsigned long)(vmcs12->posted_intr_desc_addr &
11452 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011453 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011454 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011455 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11456 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011457 else
11458 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11459 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011460}
11461
Jan Kiszkaf4124502014-03-07 20:03:13 +010011462static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11463{
11464 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11465 struct vcpu_vmx *vmx = to_vmx(vcpu);
11466
Sean Christopherson4c008122018-08-27 15:21:10 -070011467 /*
11468 * A timer value of zero is architecturally guaranteed to cause
11469 * a VMExit prior to executing any instructions in the guest.
11470 */
11471 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011472 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11473 return;
11474 }
11475
Sean Christopherson4c008122018-08-27 15:21:10 -070011476 if (vcpu->arch.virtual_tsc_khz == 0)
11477 return;
11478
Jan Kiszkaf4124502014-03-07 20:03:13 +010011479 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11480 preemption_timeout *= 1000000;
11481 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11482 hrtimer_start(&vmx->nested.preemption_timer,
11483 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11484}
11485
Jim Mattson56a20512017-07-06 16:33:06 -070011486static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11487 struct vmcs12 *vmcs12)
11488{
11489 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11490 return 0;
11491
11492 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11493 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11494 return -EINVAL;
11495
11496 return 0;
11497}
11498
Wincy Van3af18d92015-02-03 23:49:31 +080011499static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11500 struct vmcs12 *vmcs12)
11501{
Wincy Van3af18d92015-02-03 23:49:31 +080011502 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11503 return 0;
11504
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011505 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011506 return -EINVAL;
11507
11508 return 0;
11509}
11510
Jim Mattson712b12d2017-08-24 13:24:47 -070011511static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11512 struct vmcs12 *vmcs12)
11513{
11514 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11515 return 0;
11516
11517 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11518 return -EINVAL;
11519
11520 return 0;
11521}
11522
Wincy Van3af18d92015-02-03 23:49:31 +080011523/*
11524 * Merge L0's and L1's MSR bitmap, return false to indicate that
11525 * we do not use the hardware.
11526 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011527static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11528 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011529{
Wincy Van82f0dd42015-02-03 23:57:18 +080011530 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011531 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011532 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011533 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011534 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011535 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011536 *
11537 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11538 * ensures that we do not accidentally generate an L02 MSR bitmap
11539 * from the L12 MSR bitmap that is too permissive.
11540 * 2. That L1 or L2s have actually used the MSR. This avoids
11541 * unnecessarily merging of the bitmap if the MSR is unused. This
11542 * works properly because we only update the L01 MSR bitmap lazily.
11543 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11544 * updated to reflect this when L1 (or its L2s) actually write to
11545 * the MSR.
11546 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011547 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11548 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011549
Paolo Bonzinic9923842017-12-13 14:16:30 +010011550 /* Nothing to do if the MSR bitmap is not in use. */
11551 if (!cpu_has_vmx_msr_bitmap() ||
11552 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11553 return false;
11554
Ashok Raj15d45072018-02-01 22:59:43 +010011555 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011556 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011557 return false;
11558
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011559 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11560 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011561 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011562
Radim Krčmářd048c092016-08-08 20:16:22 +020011563 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011564 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11565 /*
11566 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11567 * just lets the processor take the value from the virtual-APIC page;
11568 * take those 256 bits directly from the L1 bitmap.
11569 */
11570 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11571 unsigned word = msr / BITS_PER_LONG;
11572 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11573 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011574 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011575 } else {
11576 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11577 unsigned word = msr / BITS_PER_LONG;
11578 msr_bitmap_l0[word] = ~0;
11579 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11580 }
11581 }
11582
11583 nested_vmx_disable_intercept_for_msr(
11584 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011585 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011586 MSR_TYPE_W);
11587
11588 if (nested_cpu_has_vid(vmcs12)) {
11589 nested_vmx_disable_intercept_for_msr(
11590 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011591 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011592 MSR_TYPE_W);
11593 nested_vmx_disable_intercept_for_msr(
11594 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011595 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011596 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011597 }
Ashok Raj15d45072018-02-01 22:59:43 +010011598
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011599 if (spec_ctrl)
11600 nested_vmx_disable_intercept_for_msr(
11601 msr_bitmap_l1, msr_bitmap_l0,
11602 MSR_IA32_SPEC_CTRL,
11603 MSR_TYPE_R | MSR_TYPE_W);
11604
Ashok Raj15d45072018-02-01 22:59:43 +010011605 if (pred_cmd)
11606 nested_vmx_disable_intercept_for_msr(
11607 msr_bitmap_l1, msr_bitmap_l0,
11608 MSR_IA32_PRED_CMD,
11609 MSR_TYPE_W);
11610
Wincy Vanf2b93282015-02-03 23:56:03 +080011611 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011612 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011613
11614 return true;
11615}
11616
Liran Alon61ada742018-06-23 02:35:08 +030011617static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11618 struct vmcs12 *vmcs12)
11619{
11620 struct vmcs12 *shadow;
11621 struct page *page;
11622
11623 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11624 vmcs12->vmcs_link_pointer == -1ull)
11625 return;
11626
11627 shadow = get_shadow_vmcs12(vcpu);
11628 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11629
11630 memcpy(shadow, kmap(page), VMCS12_SIZE);
11631
11632 kunmap(page);
11633 kvm_release_page_clean(page);
11634}
11635
11636static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11637 struct vmcs12 *vmcs12)
11638{
11639 struct vcpu_vmx *vmx = to_vmx(vcpu);
11640
11641 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11642 vmcs12->vmcs_link_pointer == -1ull)
11643 return;
11644
11645 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11646 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11647}
11648
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011649static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11650 struct vmcs12 *vmcs12)
11651{
11652 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11653 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11654 return -EINVAL;
11655 else
11656 return 0;
11657}
11658
Wincy Vanf2b93282015-02-03 23:56:03 +080011659static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11660 struct vmcs12 *vmcs12)
11661{
Wincy Van82f0dd42015-02-03 23:57:18 +080011662 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011663 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011664 !nested_cpu_has_vid(vmcs12) &&
11665 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011666 return 0;
11667
11668 /*
11669 * If virtualize x2apic mode is enabled,
11670 * virtualize apic access must be disabled.
11671 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011672 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11673 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011674 return -EINVAL;
11675
Wincy Van608406e2015-02-03 23:57:51 +080011676 /*
11677 * If virtual interrupt delivery is enabled,
11678 * we must exit on external interrupts.
11679 */
11680 if (nested_cpu_has_vid(vmcs12) &&
11681 !nested_exit_on_intr(vcpu))
11682 return -EINVAL;
11683
Wincy Van705699a2015-02-03 23:58:17 +080011684 /*
11685 * bits 15:8 should be zero in posted_intr_nv,
11686 * the descriptor address has been already checked
11687 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011688 *
11689 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011690 */
11691 if (nested_cpu_has_posted_intr(vmcs12) &&
11692 (!nested_cpu_has_vid(vmcs12) ||
11693 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011694 (vmcs12->posted_intr_nv & 0xff00) ||
11695 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11696 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011697 return -EINVAL;
11698
Wincy Vanf2b93282015-02-03 23:56:03 +080011699 /* tpr shadow is needed by all apicv features. */
11700 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11701 return -EINVAL;
11702
11703 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011704}
11705
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011706static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11707 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011708 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011709{
Liran Alone2536742018-06-23 02:35:02 +030011710 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011711 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011712 u64 count, addr;
11713
Liran Alone2536742018-06-23 02:35:02 +030011714 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11715 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011716 WARN_ON(1);
11717 return -EINVAL;
11718 }
11719 if (count == 0)
11720 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011721 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011722 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11723 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011724 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011725 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11726 addr_field, maxphyaddr, count, addr);
11727 return -EINVAL;
11728 }
11729 return 0;
11730}
11731
11732static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11733 struct vmcs12 *vmcs12)
11734{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735 if (vmcs12->vm_exit_msr_load_count == 0 &&
11736 vmcs12->vm_exit_msr_store_count == 0 &&
11737 vmcs12->vm_entry_msr_load_count == 0)
11738 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011739 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011740 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011741 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011742 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011743 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011744 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011745 return -EINVAL;
11746 return 0;
11747}
11748
Bandan Dasc5f983f2017-05-05 15:25:14 -040011749static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11750 struct vmcs12 *vmcs12)
11751{
11752 u64 address = vmcs12->pml_address;
11753 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11754
11755 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11756 if (!nested_cpu_has_ept(vmcs12) ||
11757 !IS_ALIGNED(address, 4096) ||
11758 address >> maxphyaddr)
11759 return -EINVAL;
11760 }
11761
11762 return 0;
11763}
11764
Liran Alona8a7c022018-06-23 02:35:06 +030011765static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11766 struct vmcs12 *vmcs12)
11767{
11768 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11769 return 0;
11770
11771 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11772 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11773 return -EINVAL;
11774
11775 return 0;
11776}
11777
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011778static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11779 struct vmx_msr_entry *e)
11780{
11781 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011782 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011783 return -EINVAL;
11784 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11785 e->index == MSR_IA32_UCODE_REV)
11786 return -EINVAL;
11787 if (e->reserved != 0)
11788 return -EINVAL;
11789 return 0;
11790}
11791
11792static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11793 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011794{
11795 if (e->index == MSR_FS_BASE ||
11796 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011797 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11798 nested_vmx_msr_check_common(vcpu, e))
11799 return -EINVAL;
11800 return 0;
11801}
11802
11803static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11804 struct vmx_msr_entry *e)
11805{
11806 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11807 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011808 return -EINVAL;
11809 return 0;
11810}
11811
11812/*
11813 * Load guest's/host's msr at nested entry/exit.
11814 * return 0 for success, entry index for failure.
11815 */
11816static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11817{
11818 u32 i;
11819 struct vmx_msr_entry e;
11820 struct msr_data msr;
11821
11822 msr.host_initiated = false;
11823 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011824 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11825 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011826 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011827 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11828 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011829 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011830 }
11831 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011832 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011833 "%s check failed (%u, 0x%x, 0x%x)\n",
11834 __func__, i, e.index, e.reserved);
11835 goto fail;
11836 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011837 msr.index = e.index;
11838 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011839 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011840 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011841 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11842 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011843 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011844 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011845 }
11846 return 0;
11847fail:
11848 return i + 1;
11849}
11850
11851static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11852{
11853 u32 i;
11854 struct vmx_msr_entry e;
11855
11856 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011857 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011858 if (kvm_vcpu_read_guest(vcpu,
11859 gpa + i * sizeof(e),
11860 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011861 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011862 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11863 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011864 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011865 }
11866 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011867 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011868 "%s check failed (%u, 0x%x, 0x%x)\n",
11869 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011870 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011871 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011872 msr_info.host_initiated = false;
11873 msr_info.index = e.index;
11874 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011875 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011876 "%s cannot read MSR (%u, 0x%x)\n",
11877 __func__, i, e.index);
11878 return -EINVAL;
11879 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011880 if (kvm_vcpu_write_guest(vcpu,
11881 gpa + i * sizeof(e) +
11882 offsetof(struct vmx_msr_entry, value),
11883 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011884 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011885 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011886 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011887 return -EINVAL;
11888 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011889 }
11890 return 0;
11891}
11892
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011893static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11894{
11895 unsigned long invalid_mask;
11896
11897 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11898 return (val & invalid_mask) == 0;
11899}
11900
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011901/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011902 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11903 * emulating VM entry into a guest with EPT enabled.
11904 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11905 * is assigned to entry_failure_code on failure.
11906 */
11907static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011908 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011909{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011910 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011911 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011912 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11913 return 1;
11914 }
11915
11916 /*
11917 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11918 * must not be dereferenced.
11919 */
11920 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11921 !nested_ept) {
11922 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11923 *entry_failure_code = ENTRY_FAIL_PDPTE;
11924 return 1;
11925 }
11926 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011927 }
11928
Junaid Shahid50c28f22018-06-27 14:59:11 -070011929 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011930 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011931
11932 vcpu->arch.cr3 = cr3;
11933 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11934
11935 kvm_init_mmu(vcpu, false);
11936
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011937 return 0;
11938}
11939
Jim Mattson6514dc32018-04-26 16:09:12 -070011940static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011941{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011942 struct vcpu_vmx *vmx = to_vmx(vcpu);
11943
11944 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11945 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11946 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11947 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11948 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11949 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11950 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11951 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11952 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11953 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11954 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11955 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11956 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11957 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11958 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11959 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11960 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11961 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11962 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11963 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11964 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11965 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11966 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11967 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11968 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11969 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11970 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11971 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11972 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
11973 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
11974 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010011975
11976 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
11977 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
11978 vmcs12->guest_pending_dbg_exceptions);
11979 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
11980 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
11981
11982 if (nested_cpu_has_xsaves(vmcs12))
11983 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
11984 vmcs_write64(VMCS_LINK_POINTER, -1ull);
11985
11986 if (cpu_has_vmx_posted_intr())
11987 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
11988
11989 /*
11990 * Whether page-faults are trapped is determined by a combination of
11991 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
11992 * If enable_ept, L0 doesn't care about page faults and we should
11993 * set all of these to L1's desires. However, if !enable_ept, L0 does
11994 * care about (at least some) page faults, and because it is not easy
11995 * (if at all possible?) to merge L0 and L1's desires, we simply ask
11996 * to exit on each and every L2 page fault. This is done by setting
11997 * MASK=MATCH=0 and (see below) EB.PF=1.
11998 * Note that below we don't need special code to set EB.PF beyond the
11999 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12000 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12001 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12002 */
12003 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12004 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12005 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12006 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12007
12008 /* All VMFUNCs are currently emulated through L0 vmexits. */
12009 if (cpu_has_vmx_vmfunc())
12010 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12011
12012 if (cpu_has_vmx_apicv()) {
12013 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12014 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12015 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12016 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12017 }
12018
12019 /*
12020 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12021 * Some constant fields are set here by vmx_set_constant_host_state().
12022 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012023 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12024 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012025 */
12026 vmx_set_constant_host_state(vmx);
12027
12028 /*
12029 * Set the MSR load/store lists to match L0's settings.
12030 */
12031 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012032 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12033 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12034 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12035 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012036
12037 set_cr4_guest_host_mask(vmx);
12038
12039 if (vmx_mpx_supported())
12040 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12041
12042 if (enable_vpid) {
12043 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12044 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12045 else
12046 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12047 }
12048
12049 /*
12050 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12051 */
12052 if (enable_ept) {
12053 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12054 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12055 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12056 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12057 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012058
12059 if (cpu_has_vmx_msr_bitmap())
12060 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012061}
12062
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012063/*
12064 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12065 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012066 * 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 +030012067 * guest in a way that will both be appropriate to L1's requests, and our
12068 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12069 * function also has additional necessary side-effects, like setting various
12070 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012071 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12072 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012073 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012074static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012075 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012076{
12077 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012078 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012079
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012080 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012081 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012082 vmx->nested.dirty_vmcs12 = false;
12083 }
12084
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012085 /*
12086 * First, the fields that are shadowed. This must be kept in sync
12087 * with vmx_shadow_fields.h.
12088 */
12089
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012090 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012091 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012092 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012093 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12094 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012095
Jim Mattson6514dc32018-04-26 16:09:12 -070012096 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012097 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012098 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12099 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12100 } else {
12101 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12102 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12103 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012104 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012105 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12106 vmcs12->vm_entry_intr_info_field);
12107 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12108 vmcs12->vm_entry_exception_error_code);
12109 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12110 vmcs12->vm_entry_instruction_len);
12111 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12112 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012113 vmx->loaded_vmcs->nmi_known_unmasked =
12114 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012115 } else {
12116 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12117 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012118 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012119
Jan Kiszkaf4124502014-03-07 20:03:13 +010012120 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012121
Sean Christophersonf459a702018-08-27 15:21:11 -070012122 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012123 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012124 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12125 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012126
12127 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012128 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012129 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12130 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012131 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012132 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012133 }
Wincy Van705699a2015-02-03 23:58:17 +080012134
Jan Kiszkaf4124502014-03-07 20:03:13 +010012135 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012136
Jan Kiszkaf4124502014-03-07 20:03:13 +010012137 vmx->nested.preemption_timer_expired = false;
12138 if (nested_cpu_has_preemption_timer(vmcs12))
12139 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012140
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012141 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012142 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012143
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012144 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012145 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012146 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012147 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012148 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012149 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012150 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12151 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012152 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012153 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12154 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12155 ~SECONDARY_EXEC_ENABLE_PML;
12156 exec_control |= vmcs12_exec_ctrl;
12157 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012158
Liran Alon32c7acf2018-06-23 02:35:11 +030012159 /* VMCS shadowing for L2 is emulated for now */
12160 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12161
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012162 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012163 vmcs_write16(GUEST_INTR_STATUS,
12164 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012165
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012166 /*
12167 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12168 * nested_get_vmcs12_pages will either fix it up or
12169 * remove the VM execution control.
12170 */
12171 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12172 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12173
Sean Christopherson0b665d32018-08-14 09:33:34 -070012174 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12175 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12176
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012177 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12178 }
12179
Jim Mattson83bafef2016-10-04 10:48:38 -070012180 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012181 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12182 * entry, but only if the current (host) sp changed from the value
12183 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12184 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12185 * here we just force the write to happen on entry.
12186 */
12187 vmx->host_rsp = 0;
12188
12189 exec_control = vmx_exec_control(vmx); /* L0's desires */
12190 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12191 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12192 exec_control &= ~CPU_BASED_TPR_SHADOW;
12193 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012194
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012195 /*
12196 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12197 * nested_get_vmcs12_pages can't fix it up, the illegal value
12198 * will result in a VM entry failure.
12199 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012200 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012201 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012202 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012203 } else {
12204#ifdef CONFIG_X86_64
12205 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12206 CPU_BASED_CR8_STORE_EXITING;
12207#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012208 }
12209
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012210 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012211 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12212 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012213 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012214 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12215 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12216
12217 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12218
12219 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12220 * bitwise-or of what L1 wants to trap for L2, and what we want to
12221 * trap. Note that CR0.TS also needs updating - we do this later.
12222 */
12223 update_exception_bitmap(vcpu);
12224 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12225 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12226
Nadav Har'El8049d652013-08-05 11:07:06 +030012227 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12228 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12229 * bits are further modified by vmx_set_efer() below.
12230 */
Jan Kiszkaf4124502014-03-07 20:03:13 +010012231 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012232
12233 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12234 * emulated by vmx_set_efer(), below.
12235 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012236 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012237 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12238 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012239 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12240
Jim Mattson6514dc32018-04-26 16:09:12 -070012241 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012242 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012243 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012244 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012245 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012246 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012247 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012248
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012249 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12250
Peter Feinerc95ba922016-08-17 09:36:47 -070012251 if (kvm_has_tsc_control)
12252 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012253
12254 if (enable_vpid) {
12255 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012256 * There is no direct mapping between vpid02 and vpid12, the
12257 * vpid02 is per-vCPU for L0 and reused while the value of
12258 * vpid12 is changed w/ one invvpid during nested vmentry.
12259 * The vpid12 is allocated by L1 for L2, so it will not
12260 * influence global bitmap(for vpid01 and vpid02 allocation)
12261 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012262 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012263 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012264 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12265 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012266 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012267 }
12268 } else {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080012269 vmx_flush_tlb(vcpu, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012270 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012271 }
12272
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012273 if (enable_pml) {
12274 /*
12275 * Conceptually we want to copy the PML address and index from
12276 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12277 * since we always flush the log on each vmexit, this happens
12278 * to be equivalent to simply resetting the fields in vmcs02.
12279 */
12280 ASSERT(vmx->pml_pg);
12281 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12282 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12283 }
12284
Nadav Har'El155a97a2013-08-05 11:07:16 +030012285 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012286 if (nested_ept_init_mmu_context(vcpu)) {
12287 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12288 return 1;
12289 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012290 } else if (nested_cpu_has2(vmcs12,
12291 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012292 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012293 }
12294
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012295 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012296 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12297 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012298 * The CR0_READ_SHADOW is what L2 should have expected to read given
12299 * the specifications by L1; It's not enough to take
12300 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12301 * have more bits than L1 expected.
12302 */
12303 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12304 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12305
12306 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12307 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12308
Jim Mattson6514dc32018-04-26 16:09:12 -070012309 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012310 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012311 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12312 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12313 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12314 else
12315 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12316 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12317 vmx_set_efer(vcpu, vcpu->arch.efer);
12318
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012319 /*
12320 * Guest state is invalid and unrestricted guest is disabled,
12321 * which means L1 attempted VMEntry to L2 with invalid state.
12322 * Fail the VMEntry.
12323 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012324 if (vmx->emulation_required) {
12325 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012326 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012327 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012328
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012329 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012330 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012331 entry_failure_code))
12332 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012333
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012334 if (!enable_ept)
12335 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12336
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012337 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12338 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012339 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012340}
12341
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012342static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12343{
12344 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12345 nested_cpu_has_virtual_nmis(vmcs12))
12346 return -EINVAL;
12347
12348 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12349 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12350 return -EINVAL;
12351
12352 return 0;
12353}
12354
Jim Mattsonca0bde22016-11-30 12:03:46 -080012355static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12356{
12357 struct vcpu_vmx *vmx = to_vmx(vcpu);
12358
12359 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12360 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12361 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12362
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012363 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12364 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12365
Jim Mattson56a20512017-07-06 16:33:06 -070012366 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12367 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12368
Jim Mattsonca0bde22016-11-30 12:03:46 -080012369 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12370 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12371
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012372 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12373 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12374
Jim Mattson712b12d2017-08-24 13:24:47 -070012375 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12376 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12377
Jim Mattsonca0bde22016-11-30 12:03:46 -080012378 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12379 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12380
12381 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12382 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12383
Bandan Dasc5f983f2017-05-05 15:25:14 -040012384 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12385 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12386
Liran Alona8a7c022018-06-23 02:35:06 +030012387 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12388 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12389
Jim Mattsonca0bde22016-11-30 12:03:46 -080012390 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012391 vmx->nested.msrs.procbased_ctls_low,
12392 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012393 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12394 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012395 vmx->nested.msrs.secondary_ctls_low,
12396 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012397 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012398 vmx->nested.msrs.pinbased_ctls_low,
12399 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012400 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012401 vmx->nested.msrs.exit_ctls_low,
12402 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012403 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012404 vmx->nested.msrs.entry_ctls_low,
12405 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012406 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12407
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012408 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012409 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12410
Bandan Das41ab9372017-08-03 15:54:43 -040012411 if (nested_cpu_has_vmfunc(vmcs12)) {
12412 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012413 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012414 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12415
12416 if (nested_cpu_has_eptp_switching(vmcs12)) {
12417 if (!nested_cpu_has_ept(vmcs12) ||
12418 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12419 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12420 }
12421 }
Bandan Das27c42a12017-08-03 15:54:42 -040012422
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012423 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12424 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12425
Jim Mattsonca0bde22016-11-30 12:03:46 -080012426 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12427 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12428 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12429 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12430
Marc Orr04473782018-06-20 17:21:29 -070012431 /*
12432 * From the Intel SDM, volume 3:
12433 * Fields relevant to VM-entry event injection must be set properly.
12434 * These fields are the VM-entry interruption-information field, the
12435 * VM-entry exception error code, and the VM-entry instruction length.
12436 */
12437 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12438 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12439 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12440 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12441 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12442 bool should_have_error_code;
12443 bool urg = nested_cpu_has2(vmcs12,
12444 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12445 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12446
12447 /* VM-entry interruption-info field: interruption type */
12448 if (intr_type == INTR_TYPE_RESERVED ||
12449 (intr_type == INTR_TYPE_OTHER_EVENT &&
12450 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12451 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12452
12453 /* VM-entry interruption-info field: vector */
12454 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12455 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12456 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12457 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12458
12459 /* VM-entry interruption-info field: deliver error code */
12460 should_have_error_code =
12461 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12462 x86_exception_has_error_code(vector);
12463 if (has_error_code != should_have_error_code)
12464 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12465
12466 /* VM-entry exception error code */
12467 if (has_error_code &&
12468 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12469 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12470
12471 /* VM-entry interruption-info field: reserved bits */
12472 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12473 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12474
12475 /* VM-entry instruction length */
12476 switch (intr_type) {
12477 case INTR_TYPE_SOFT_EXCEPTION:
12478 case INTR_TYPE_SOFT_INTR:
12479 case INTR_TYPE_PRIV_SW_EXCEPTION:
12480 if ((vmcs12->vm_entry_instruction_len > 15) ||
12481 (vmcs12->vm_entry_instruction_len == 0 &&
12482 !nested_cpu_has_zero_length_injection(vcpu)))
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484 }
12485 }
12486
Jim Mattsonca0bde22016-11-30 12:03:46 -080012487 return 0;
12488}
12489
Liran Alonf145d902018-06-23 02:35:07 +030012490static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12491 struct vmcs12 *vmcs12)
12492{
12493 int r;
12494 struct page *page;
12495 struct vmcs12 *shadow;
12496
12497 if (vmcs12->vmcs_link_pointer == -1ull)
12498 return 0;
12499
12500 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12501 return -EINVAL;
12502
12503 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12504 if (is_error_page(page))
12505 return -EINVAL;
12506
12507 r = 0;
12508 shadow = kmap(page);
12509 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12510 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12511 r = -EINVAL;
12512 kunmap(page);
12513 kvm_release_page_clean(page);
12514 return r;
12515}
12516
Jim Mattsonca0bde22016-11-30 12:03:46 -080012517static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12518 u32 *exit_qual)
12519{
12520 bool ia32e;
12521
12522 *exit_qual = ENTRY_FAIL_DEFAULT;
12523
12524 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12525 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12526 return 1;
12527
Liran Alonf145d902018-06-23 02:35:07 +030012528 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012529 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12530 return 1;
12531 }
12532
12533 /*
12534 * If the load IA32_EFER VM-entry control is 1, the following checks
12535 * are performed on the field for the IA32_EFER MSR:
12536 * - Bits reserved in the IA32_EFER MSR must be 0.
12537 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12538 * the IA-32e mode guest VM-exit control. It must also be identical
12539 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12540 * CR0.PG) is 1.
12541 */
12542 if (to_vmx(vcpu)->nested.nested_run_pending &&
12543 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12544 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12545 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12546 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12547 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12548 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12549 return 1;
12550 }
12551
12552 /*
12553 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12554 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12555 * the values of the LMA and LME bits in the field must each be that of
12556 * the host address-space size VM-exit control.
12557 */
12558 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12559 ia32e = (vmcs12->vm_exit_controls &
12560 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12561 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12562 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12563 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12564 return 1;
12565 }
12566
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012567 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12568 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12569 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12570 return 1;
12571
Jim Mattsonca0bde22016-11-30 12:03:46 -080012572 return 0;
12573}
12574
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012575/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012576 * If exit_qual is NULL, this is being called from state restore (either RSM
12577 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012578 */
12579static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012580{
12581 struct vcpu_vmx *vmx = to_vmx(vcpu);
12582 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012583 bool from_vmentry = !!exit_qual;
12584 u32 dummy_exit_qual;
Liran Alonb5861e52018-09-03 15:20:22 +030012585 u32 vmcs01_cpu_exec_ctrl;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012586 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012587
Liran Alonb5861e52018-09-03 15:20:22 +030012588 vmcs01_cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
12589
Jim Mattson858e25c2016-11-30 12:03:47 -080012590 enter_guest_mode(vcpu);
12591
12592 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12593 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
12594
Jim Mattsonde3a0022017-11-27 17:22:25 -060012595 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012596 vmx_segment_cache_clear(vmx);
12597
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012598 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12599 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12600
12601 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012602 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012603 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012604
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012605 if (from_vmentry) {
12606 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012607
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012608 r = EXIT_REASON_MSR_LOAD_FAIL;
12609 *exit_qual = nested_vmx_load_msr(vcpu,
12610 vmcs12->vm_entry_msr_load_addr,
12611 vmcs12->vm_entry_msr_load_count);
12612 if (*exit_qual)
12613 goto fail;
12614 } else {
12615 /*
12616 * The MMU is not initialized to point at the right entities yet and
12617 * "get pages" would need to read data from the guest (i.e. we will
12618 * need to perform gpa to hpa translation). Request a call
12619 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12620 * have already been set at vmentry time and should not be reset.
12621 */
12622 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12623 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012624
Jim Mattson858e25c2016-11-30 12:03:47 -080012625 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012626 * If L1 had a pending IRQ/NMI until it executed
12627 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12628 * disallowed (e.g. interrupts disabled), L0 needs to
12629 * evaluate if this pending event should cause an exit from L2
12630 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12631 * intercept EXTERNAL_INTERRUPT).
12632 *
12633 * Usually this would be handled by L0 requesting a
12634 * IRQ/NMI window by setting VMCS accordingly. However,
12635 * this setting was done on VMCS01 and now VMCS02 is active
12636 * instead. Thus, we force L0 to perform pending event
12637 * evaluation by requesting a KVM_REQ_EVENT.
12638 */
12639 if (vmcs01_cpu_exec_ctrl &
12640 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING)) {
12641 kvm_make_request(KVM_REQ_EVENT, vcpu);
12642 }
12643
12644 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012645 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12646 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12647 * returned as far as L1 is concerned. It will only return (and set
12648 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12649 */
12650 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012651
12652fail:
12653 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12654 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12655 leave_guest_mode(vcpu);
12656 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012657 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012658}
12659
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012660/*
12661 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12662 * for running an L2 nested guest.
12663 */
12664static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12665{
12666 struct vmcs12 *vmcs12;
12667 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012668 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012669 u32 exit_qual;
12670 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012671
Kyle Hueyeb277562016-11-29 12:40:39 -080012672 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012673 return 1;
12674
Kyle Hueyeb277562016-11-29 12:40:39 -080012675 if (!nested_vmx_check_vmcs12(vcpu))
12676 goto out;
12677
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012678 vmcs12 = get_vmcs12(vcpu);
12679
Liran Alona6192d42018-06-23 02:35:04 +030012680 /*
12681 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12682 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12683 * rather than RFLAGS.ZF, and no error number is stored to the
12684 * VM-instruction error field.
12685 */
12686 if (vmcs12->hdr.shadow_vmcs) {
12687 nested_vmx_failInvalid(vcpu);
12688 goto out;
12689 }
12690
Abel Gordon012f83c2013-04-18 14:39:25 +030012691 if (enable_shadow_vmcs)
12692 copy_shadow_to_vmcs12(vmx);
12693
Nadav Har'El7c177932011-05-25 23:12:04 +030012694 /*
12695 * The nested entry process starts with enforcing various prerequisites
12696 * on vmcs12 as required by the Intel SDM, and act appropriately when
12697 * they fail: As the SDM explains, some conditions should cause the
12698 * instruction to fail, while others will cause the instruction to seem
12699 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12700 * To speed up the normal (success) code path, we should avoid checking
12701 * for misconfigurations which will anyway be caught by the processor
12702 * when using the merged vmcs02.
12703 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012704 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12705 nested_vmx_failValid(vcpu,
12706 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12707 goto out;
12708 }
12709
Nadav Har'El7c177932011-05-25 23:12:04 +030012710 if (vmcs12->launch_state == launch) {
12711 nested_vmx_failValid(vcpu,
12712 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12713 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012714 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012715 }
12716
Jim Mattsonca0bde22016-11-30 12:03:46 -080012717 ret = check_vmentry_prereqs(vcpu, vmcs12);
12718 if (ret) {
12719 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012720 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012721 }
12722
Nadav Har'El7c177932011-05-25 23:12:04 +030012723 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012724 * After this point, the trap flag no longer triggers a singlestep trap
12725 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12726 * This is not 100% correct; for performance reasons, we delegate most
12727 * of the checks on host state to the processor. If those fail,
12728 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012729 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012730 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012731
Jim Mattsonca0bde22016-11-30 12:03:46 -080012732 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12733 if (ret) {
12734 nested_vmx_entry_failure(vcpu, vmcs12,
12735 EXIT_REASON_INVALID_STATE, exit_qual);
12736 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012737 }
12738
12739 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012740 * We're finally done with prerequisite checking, and can start with
12741 * the nested entry.
12742 */
12743
Jim Mattson6514dc32018-04-26 16:09:12 -070012744 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012745 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012746 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012747 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012748 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012749 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012750 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012751
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012752 /* Hide L1D cache contents from the nested guest. */
12753 vmx->vcpu.arch.l1tf_flush_l1d = true;
12754
Chao Gao135a06c2018-02-11 10:06:30 +080012755 /*
Liran Alon61ada742018-06-23 02:35:08 +030012756 * Must happen outside of enter_vmx_non_root_mode() as it will
12757 * also be used as part of restoring nVMX state for
12758 * snapshot restore (migration).
12759 *
12760 * In this flow, it is assumed that vmcs12 cache was
12761 * trasferred as part of captured nVMX state and should
12762 * therefore not be read from guest memory (which may not
12763 * exist on destination host yet).
12764 */
12765 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12766
12767 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012768 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12769 * by event injection, halt vcpu.
12770 */
12771 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012772 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12773 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012774 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012775 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012776 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012777
12778out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012779 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012780}
12781
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012782/*
12783 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12784 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12785 * This function returns the new value we should put in vmcs12.guest_cr0.
12786 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12787 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12788 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12789 * didn't trap the bit, because if L1 did, so would L0).
12790 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12791 * been modified by L2, and L1 knows it. So just leave the old value of
12792 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12793 * isn't relevant, because if L0 traps this bit it can set it to anything.
12794 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12795 * changed these bits, and therefore they need to be updated, but L0
12796 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12797 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12798 */
12799static inline unsigned long
12800vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12801{
12802 return
12803 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12804 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12805 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12806 vcpu->arch.cr0_guest_owned_bits));
12807}
12808
12809static inline unsigned long
12810vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12811{
12812 return
12813 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12814 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12815 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12816 vcpu->arch.cr4_guest_owned_bits));
12817}
12818
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012819static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12820 struct vmcs12 *vmcs12)
12821{
12822 u32 idt_vectoring;
12823 unsigned int nr;
12824
Wanpeng Li664f8e22017-08-24 03:35:09 -070012825 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012826 nr = vcpu->arch.exception.nr;
12827 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12828
12829 if (kvm_exception_is_soft(nr)) {
12830 vmcs12->vm_exit_instruction_len =
12831 vcpu->arch.event_exit_inst_len;
12832 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12833 } else
12834 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12835
12836 if (vcpu->arch.exception.has_error_code) {
12837 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12838 vmcs12->idt_vectoring_error_code =
12839 vcpu->arch.exception.error_code;
12840 }
12841
12842 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012843 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012844 vmcs12->idt_vectoring_info_field =
12845 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012846 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012847 nr = vcpu->arch.interrupt.nr;
12848 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12849
12850 if (vcpu->arch.interrupt.soft) {
12851 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12852 vmcs12->vm_entry_instruction_len =
12853 vcpu->arch.event_exit_inst_len;
12854 } else
12855 idt_vectoring |= INTR_TYPE_EXT_INTR;
12856
12857 vmcs12->idt_vectoring_info_field = idt_vectoring;
12858 }
12859}
12860
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012861static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12862{
12863 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012864 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012865 bool block_nested_events =
12866 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012867
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012868 if (vcpu->arch.exception.pending &&
12869 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012870 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012871 return -EBUSY;
12872 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012873 return 0;
12874 }
12875
Jan Kiszkaf4124502014-03-07 20:03:13 +010012876 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12877 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012878 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012879 return -EBUSY;
12880 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12881 return 0;
12882 }
12883
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012884 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012885 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012886 return -EBUSY;
12887 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12888 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12889 INTR_INFO_VALID_MASK, 0);
12890 /*
12891 * The NMI-triggered VM exit counts as injection:
12892 * clear this one and block further NMIs.
12893 */
12894 vcpu->arch.nmi_pending = 0;
12895 vmx_set_nmi_mask(vcpu, true);
12896 return 0;
12897 }
12898
12899 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12900 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012901 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012902 return -EBUSY;
12903 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012904 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012905 }
12906
David Hildenbrand6342c502017-01-25 11:58:58 +010012907 vmx_complete_nested_posted_interrupt(vcpu);
12908 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012909}
12910
Sean Christophersond264ee02018-08-27 15:21:12 -070012911static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12912{
12913 to_vmx(vcpu)->req_immediate_exit = true;
12914}
12915
Jan Kiszkaf4124502014-03-07 20:03:13 +010012916static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12917{
12918 ktime_t remaining =
12919 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12920 u64 value;
12921
12922 if (ktime_to_ns(remaining) <= 0)
12923 return 0;
12924
12925 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12926 do_div(value, 1000000);
12927 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12928}
12929
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012930/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012931 * Update the guest state fields of vmcs12 to reflect changes that
12932 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12933 * VM-entry controls is also updated, since this is really a guest
12934 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012935 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012936static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012937{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012938 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12939 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12940
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012941 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12942 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12943 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12944
12945 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12946 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12947 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12948 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12949 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12950 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12951 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12952 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12953 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12954 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12955 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
12956 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
12957 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
12958 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
12959 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
12960 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
12961 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
12962 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
12963 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
12964 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
12965 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
12966 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
12967 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
12968 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
12969 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
12970 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
12971 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
12972 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
12973 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
12974 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
12975 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
12976 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
12977 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
12978 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
12979 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
12980 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
12981
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012982 vmcs12->guest_interruptibility_info =
12983 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
12984 vmcs12->guest_pending_dbg_exceptions =
12985 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010012986 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
12987 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
12988 else
12989 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012990
Jan Kiszkaf4124502014-03-07 20:03:13 +010012991 if (nested_cpu_has_preemption_timer(vmcs12)) {
12992 if (vmcs12->vm_exit_controls &
12993 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
12994 vmcs12->vmx_preemption_timer_value =
12995 vmx_get_preemption_timer_value(vcpu);
12996 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
12997 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080012998
Nadav Har'El3633cfc2013-08-05 11:07:07 +030012999 /*
13000 * In some cases (usually, nested EPT), L2 is allowed to change its
13001 * own CR3 without exiting. If it has changed it, we must keep it.
13002 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13003 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13004 *
13005 * Additionally, restore L2's PDPTR to vmcs12.
13006 */
13007 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013008 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013009 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13010 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13011 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13012 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13013 }
13014
Jim Mattsond281e132017-06-01 12:44:46 -070013015 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013016
Wincy Van608406e2015-02-03 23:57:51 +080013017 if (nested_cpu_has_vid(vmcs12))
13018 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13019
Jan Kiszkac18911a2013-03-13 16:06:41 +010013020 vmcs12->vm_entry_controls =
13021 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013022 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013023
Jan Kiszka2996fca2014-06-16 13:59:43 +020013024 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13025 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13026 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13027 }
13028
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013029 /* TODO: These cannot have changed unless we have MSR bitmaps and
13030 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013031 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013032 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013033 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13034 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013035 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13036 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13037 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013038 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013039 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013040}
13041
13042/*
13043 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13044 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13045 * and this function updates it to reflect the changes to the guest state while
13046 * L2 was running (and perhaps made some exits which were handled directly by L0
13047 * without going back to L1), and to reflect the exit reason.
13048 * Note that we do not have to copy here all VMCS fields, just those that
13049 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13050 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13051 * which already writes to vmcs12 directly.
13052 */
13053static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13054 u32 exit_reason, u32 exit_intr_info,
13055 unsigned long exit_qualification)
13056{
13057 /* update guest state fields: */
13058 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013059
13060 /* update exit information fields: */
13061
Jan Kiszka533558b2014-01-04 18:47:20 +010013062 vmcs12->vm_exit_reason = exit_reason;
13063 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013064 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013065
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013066 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013067 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13068 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13069
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013070 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013071 vmcs12->launch_state = 1;
13072
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013073 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13074 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013075 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013076
13077 /*
13078 * Transfer the event that L0 or L1 may wanted to inject into
13079 * L2 to IDT_VECTORING_INFO_FIELD.
13080 */
13081 vmcs12_save_pending_event(vcpu, vmcs12);
13082 }
13083
13084 /*
13085 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13086 * preserved above and would only end up incorrectly in L1.
13087 */
13088 vcpu->arch.nmi_injected = false;
13089 kvm_clear_exception_queue(vcpu);
13090 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013091}
13092
Wanpeng Li5af41572017-11-05 16:54:49 -080013093static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
13094 struct vmcs12 *vmcs12)
13095{
13096 u32 entry_failure_code;
13097
13098 nested_ept_uninit_mmu_context(vcpu);
13099
13100 /*
13101 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13102 * couldn't have changed.
13103 */
13104 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13105 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13106
13107 if (!enable_ept)
13108 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13109}
13110
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013111/*
13112 * A part of what we need to when the nested L2 guest exits and we want to
13113 * run its L1 parent, is to reset L1's guest state to the host state specified
13114 * in vmcs12.
13115 * This function is to be called not only on normal nested exit, but also on
13116 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13117 * Failures During or After Loading Guest State").
13118 * This function should be called when the active VMCS is L1's (vmcs01).
13119 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013120static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13121 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013122{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013123 struct kvm_segment seg;
13124
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013125 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13126 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013127 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013128 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13129 else
13130 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13131 vmx_set_efer(vcpu, vcpu->arch.efer);
13132
13133 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13134 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013135 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013136 /*
13137 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013138 * actually changed, because vmx_set_cr0 refers to efer set above.
13139 *
13140 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13141 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013142 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013143 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013144 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013145
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013146 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013147 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013148 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013149
Wanpeng Li5af41572017-11-05 16:54:49 -080013150 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013151
Liran Alon6f1e03b2018-05-22 17:16:14 +030013152 /*
13153 * If vmcs01 don't use VPID, CPU flushes TLB on every
13154 * VMEntry/VMExit. Thus, no need to flush TLB.
13155 *
13156 * If vmcs12 uses VPID, TLB entries populated by L2 are
13157 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13158 * with vmx->vpid. Thus, no need to flush TLB.
13159 *
13160 * Therefore, flush TLB only in case vmcs01 uses VPID and
13161 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13162 * are both tagged with vmx->vpid.
13163 */
13164 if (enable_vpid &&
13165 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Wanpeng Lic2ba05c2017-12-12 17:33:03 -080013166 vmx_flush_tlb(vcpu, true);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013167 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013168
13169 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13170 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13171 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13172 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13173 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013174 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13175 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013176
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013177 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13178 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13179 vmcs_write64(GUEST_BNDCFGS, 0);
13180
Jan Kiszka44811c02013-08-04 17:17:27 +020013181 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013182 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013183 vcpu->arch.pat = vmcs12->host_ia32_pat;
13184 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013185 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13186 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13187 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013188
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013189 /* Set L1 segment info according to Intel SDM
13190 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13191 seg = (struct kvm_segment) {
13192 .base = 0,
13193 .limit = 0xFFFFFFFF,
13194 .selector = vmcs12->host_cs_selector,
13195 .type = 11,
13196 .present = 1,
13197 .s = 1,
13198 .g = 1
13199 };
13200 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13201 seg.l = 1;
13202 else
13203 seg.db = 1;
13204 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13205 seg = (struct kvm_segment) {
13206 .base = 0,
13207 .limit = 0xFFFFFFFF,
13208 .type = 3,
13209 .present = 1,
13210 .s = 1,
13211 .db = 1,
13212 .g = 1
13213 };
13214 seg.selector = vmcs12->host_ds_selector;
13215 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13216 seg.selector = vmcs12->host_es_selector;
13217 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13218 seg.selector = vmcs12->host_ss_selector;
13219 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13220 seg.selector = vmcs12->host_fs_selector;
13221 seg.base = vmcs12->host_fs_base;
13222 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13223 seg.selector = vmcs12->host_gs_selector;
13224 seg.base = vmcs12->host_gs_base;
13225 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13226 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013227 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013228 .limit = 0x67,
13229 .selector = vmcs12->host_tr_selector,
13230 .type = 11,
13231 .present = 1
13232 };
13233 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13234
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013235 kvm_set_dr(vcpu, 7, 0x400);
13236 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013237
Wincy Van3af18d92015-02-03 23:49:31 +080013238 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013239 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013240
Wincy Vanff651cb2014-12-11 08:52:58 +030013241 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13242 vmcs12->vm_exit_msr_load_count))
13243 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013244}
13245
13246/*
13247 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13248 * and modify vmcs12 to make it see what it would expect to see there if
13249 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13250 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013251static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13252 u32 exit_intr_info,
13253 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013254{
13255 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013256 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13257
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013258 /* trying to cancel vmlaunch/vmresume is a bug */
13259 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13260
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013261 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013262 * The only expected VM-instruction error is "VM entry with
13263 * invalid control field(s)." Anything else indicates a
13264 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013265 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013266 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13267 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13268
13269 leave_guest_mode(vcpu);
13270
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013271 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13272 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13273
Jim Mattson4f350c62017-09-14 16:31:44 -070013274 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013275 if (exit_reason == -1)
13276 sync_vmcs12(vcpu, vmcs12);
13277 else
13278 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13279 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013280
Liran Alon61ada742018-06-23 02:35:08 +030013281 /*
13282 * Must happen outside of sync_vmcs12() as it will
13283 * also be used to capture vmcs12 cache as part of
13284 * capturing nVMX state for snapshot (migration).
13285 *
13286 * Otherwise, this flush will dirty guest memory at a
13287 * point it is already assumed by user-space to be
13288 * immutable.
13289 */
13290 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13291
Jim Mattson4f350c62017-09-14 16:31:44 -070013292 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13293 vmcs12->vm_exit_msr_store_count))
13294 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013295 }
13296
Jim Mattson4f350c62017-09-14 16:31:44 -070013297 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013298 vm_entry_controls_reset_shadow(vmx);
13299 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013300 vmx_segment_cache_clear(vmx);
13301
Paolo Bonzini93140062016-07-06 13:23:51 +020013302 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013303 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13304 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013305 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013306
Peter Feinerc95ba922016-08-17 09:36:47 -070013307 if (kvm_has_tsc_control)
13308 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013309
Jim Mattson8d860bb2018-05-09 16:56:05 -040013310 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13311 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13312 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013313 } else if (!nested_cpu_has_ept(vmcs12) &&
13314 nested_cpu_has2(vmcs12,
13315 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013316 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013317 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013318
13319 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13320 vmx->host_rsp = 0;
13321
13322 /* Unpin physical memory we referred to in vmcs02 */
13323 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013324 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013325 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013326 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013327 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013328 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013329 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013330 }
Wincy Van705699a2015-02-03 23:58:17 +080013331 if (vmx->nested.pi_desc_page) {
13332 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013333 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013334 vmx->nested.pi_desc_page = NULL;
13335 vmx->nested.pi_desc = NULL;
13336 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013337
13338 /*
Tang Chen38b99172014-09-24 15:57:54 +080013339 * We are now running in L2, mmu_notifier will force to reload the
13340 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13341 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013342 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013343
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013344 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013345 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013346
13347 /* in case we halted in L2 */
13348 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013349
13350 if (likely(!vmx->fail)) {
13351 /*
13352 * TODO: SDM says that with acknowledge interrupt on
13353 * exit, bit 31 of the VM-exit interrupt information
13354 * (valid interrupt) is always set to 1 on
13355 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13356 * need kvm_cpu_has_interrupt(). See the commit
13357 * message for details.
13358 */
13359 if (nested_exit_intr_ack_set(vcpu) &&
13360 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13361 kvm_cpu_has_interrupt(vcpu)) {
13362 int irq = kvm_cpu_get_interrupt(vcpu);
13363 WARN_ON(irq < 0);
13364 vmcs12->vm_exit_intr_info = irq |
13365 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13366 }
13367
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013368 if (exit_reason != -1)
13369 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13370 vmcs12->exit_qualification,
13371 vmcs12->idt_vectoring_info_field,
13372 vmcs12->vm_exit_intr_info,
13373 vmcs12->vm_exit_intr_error_code,
13374 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013375
13376 load_vmcs12_host_state(vcpu, vmcs12);
13377
13378 return;
13379 }
13380
13381 /*
13382 * After an early L2 VM-entry failure, we're now back
13383 * in L1 which thinks it just finished a VMLAUNCH or
13384 * VMRESUME instruction, so we need to set the failure
13385 * flag and the VM-instruction error field of the VMCS
13386 * accordingly.
13387 */
13388 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013389
13390 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13391
Jim Mattson4f350c62017-09-14 16:31:44 -070013392 /*
13393 * The emulated instruction was already skipped in
13394 * nested_vmx_run, but the updated RIP was never
13395 * written back to the vmcs01.
13396 */
13397 skip_emulated_instruction(vcpu);
13398 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013399}
13400
Nadav Har'El7c177932011-05-25 23:12:04 +030013401/*
Jan Kiszka42124922014-01-04 18:47:19 +010013402 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13403 */
13404static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13405{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013406 if (is_guest_mode(vcpu)) {
13407 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013408 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013409 }
Jan Kiszka42124922014-01-04 18:47:19 +010013410 free_nested(to_vmx(vcpu));
13411}
13412
13413/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013414 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13415 * 23.7 "VM-entry failures during or after loading guest state" (this also
13416 * lists the acceptable exit-reason and exit-qualification parameters).
13417 * It should only be called before L2 actually succeeded to run, and when
13418 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13419 */
13420static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13421 struct vmcs12 *vmcs12,
13422 u32 reason, unsigned long qualification)
13423{
13424 load_vmcs12_host_state(vcpu, vmcs12);
13425 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13426 vmcs12->exit_qualification = qualification;
13427 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013428 if (enable_shadow_vmcs)
13429 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013430}
13431
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013432static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13433 struct x86_instruction_info *info,
13434 enum x86_intercept_stage stage)
13435{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013436 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13437 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13438
13439 /*
13440 * RDPID causes #UD if disabled through secondary execution controls.
13441 * Because it is marked as EmulateOnUD, we need to intercept it here.
13442 */
13443 if (info->intercept == x86_intercept_rdtscp &&
13444 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13445 ctxt->exception.vector = UD_VECTOR;
13446 ctxt->exception.error_code_valid = false;
13447 return X86EMUL_PROPAGATE_FAULT;
13448 }
13449
13450 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013451 return X86EMUL_CONTINUE;
13452}
13453
Yunhong Jiang64672c92016-06-13 14:19:59 -070013454#ifdef CONFIG_X86_64
13455/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13456static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13457 u64 divisor, u64 *result)
13458{
13459 u64 low = a << shift, high = a >> (64 - shift);
13460
13461 /* To avoid the overflow on divq */
13462 if (high >= divisor)
13463 return 1;
13464
13465 /* Low hold the result, high hold rem which is discarded */
13466 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13467 "rm" (divisor), "0" (low), "1" (high));
13468 *result = low;
13469
13470 return 0;
13471}
13472
13473static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13474{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013475 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013476 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013477
13478 if (kvm_mwait_in_guest(vcpu->kvm))
13479 return -EOPNOTSUPP;
13480
13481 vmx = to_vmx(vcpu);
13482 tscl = rdtsc();
13483 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13484 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013485 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13486
13487 if (delta_tsc > lapic_timer_advance_cycles)
13488 delta_tsc -= lapic_timer_advance_cycles;
13489 else
13490 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013491
13492 /* Convert to host delta tsc if tsc scaling is enabled */
13493 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13494 u64_shl_div_u64(delta_tsc,
13495 kvm_tsc_scaling_ratio_frac_bits,
13496 vcpu->arch.tsc_scaling_ratio,
13497 &delta_tsc))
13498 return -ERANGE;
13499
13500 /*
13501 * If the delta tsc can't fit in the 32 bit after the multi shift,
13502 * we can't use the preemption timer.
13503 * It's possible that it fits on later vmentries, but checking
13504 * on every vmentry is costly so we just use an hrtimer.
13505 */
13506 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13507 return -ERANGE;
13508
13509 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013510 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013511}
13512
13513static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13514{
Sean Christophersonf459a702018-08-27 15:21:11 -070013515 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013516}
13517#endif
13518
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013519static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013520{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013521 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013522 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013523}
13524
Kai Huang843e4332015-01-28 10:54:28 +080013525static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13526 struct kvm_memory_slot *slot)
13527{
13528 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13529 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13530}
13531
13532static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13533 struct kvm_memory_slot *slot)
13534{
13535 kvm_mmu_slot_set_dirty(kvm, slot);
13536}
13537
13538static void vmx_flush_log_dirty(struct kvm *kvm)
13539{
13540 kvm_flush_pml_buffers(kvm);
13541}
13542
Bandan Dasc5f983f2017-05-05 15:25:14 -040013543static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13544{
13545 struct vmcs12 *vmcs12;
13546 struct vcpu_vmx *vmx = to_vmx(vcpu);
13547 gpa_t gpa;
13548 struct page *page = NULL;
13549 u64 *pml_address;
13550
13551 if (is_guest_mode(vcpu)) {
13552 WARN_ON_ONCE(vmx->nested.pml_full);
13553
13554 /*
13555 * Check if PML is enabled for the nested guest.
13556 * Whether eptp bit 6 is set is already checked
13557 * as part of A/D emulation.
13558 */
13559 vmcs12 = get_vmcs12(vcpu);
13560 if (!nested_cpu_has_pml(vmcs12))
13561 return 0;
13562
Dan Carpenter47698862017-05-10 22:43:17 +030013563 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013564 vmx->nested.pml_full = true;
13565 return 1;
13566 }
13567
13568 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13569
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013570 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13571 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013572 return 0;
13573
13574 pml_address = kmap(page);
13575 pml_address[vmcs12->guest_pml_index--] = gpa;
13576 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013577 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013578 }
13579
13580 return 0;
13581}
13582
Kai Huang843e4332015-01-28 10:54:28 +080013583static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13584 struct kvm_memory_slot *memslot,
13585 gfn_t offset, unsigned long mask)
13586{
13587 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13588}
13589
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013590static void __pi_post_block(struct kvm_vcpu *vcpu)
13591{
13592 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13593 struct pi_desc old, new;
13594 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013595
13596 do {
13597 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013598 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13599 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013600
13601 dest = cpu_physical_id(vcpu->cpu);
13602
13603 if (x2apic_enabled())
13604 new.ndst = dest;
13605 else
13606 new.ndst = (dest << 8) & 0xFF00;
13607
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013608 /* set 'NV' to 'notification vector' */
13609 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013610 } while (cmpxchg64(&pi_desc->control, old.control,
13611 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013612
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013613 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13614 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013615 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013616 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013617 vcpu->pre_pcpu = -1;
13618 }
13619}
13620
Feng Wuefc64402015-09-18 22:29:51 +080013621/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013622 * This routine does the following things for vCPU which is going
13623 * to be blocked if VT-d PI is enabled.
13624 * - Store the vCPU to the wakeup list, so when interrupts happen
13625 * we can find the right vCPU to wake up.
13626 * - Change the Posted-interrupt descriptor as below:
13627 * 'NDST' <-- vcpu->pre_pcpu
13628 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13629 * - If 'ON' is set during this process, which means at least one
13630 * interrupt is posted for this vCPU, we cannot block it, in
13631 * this case, return 1, otherwise, return 0.
13632 *
13633 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013634static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013635{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013636 unsigned int dest;
13637 struct pi_desc old, new;
13638 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13639
13640 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013641 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13642 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013643 return 0;
13644
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013645 WARN_ON(irqs_disabled());
13646 local_irq_disable();
13647 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13648 vcpu->pre_pcpu = vcpu->cpu;
13649 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13650 list_add_tail(&vcpu->blocked_vcpu_list,
13651 &per_cpu(blocked_vcpu_on_cpu,
13652 vcpu->pre_pcpu));
13653 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13654 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013655
13656 do {
13657 old.control = new.control = pi_desc->control;
13658
Feng Wubf9f6ac2015-09-18 22:29:55 +080013659 WARN((pi_desc->sn == 1),
13660 "Warning: SN field of posted-interrupts "
13661 "is set before blocking\n");
13662
13663 /*
13664 * Since vCPU can be preempted during this process,
13665 * vcpu->cpu could be different with pre_pcpu, we
13666 * need to set pre_pcpu as the destination of wakeup
13667 * notification event, then we can find the right vCPU
13668 * to wakeup in wakeup handler if interrupts happen
13669 * when the vCPU is in blocked state.
13670 */
13671 dest = cpu_physical_id(vcpu->pre_pcpu);
13672
13673 if (x2apic_enabled())
13674 new.ndst = dest;
13675 else
13676 new.ndst = (dest << 8) & 0xFF00;
13677
13678 /* set 'NV' to 'wakeup vector' */
13679 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013680 } while (cmpxchg64(&pi_desc->control, old.control,
13681 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013682
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013683 /* We should not block the vCPU if an interrupt is posted for it. */
13684 if (pi_test_on(pi_desc) == 1)
13685 __pi_post_block(vcpu);
13686
13687 local_irq_enable();
13688 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013689}
13690
Yunhong Jiangbc225122016-06-13 14:19:58 -070013691static int vmx_pre_block(struct kvm_vcpu *vcpu)
13692{
13693 if (pi_pre_block(vcpu))
13694 return 1;
13695
Yunhong Jiang64672c92016-06-13 14:19:59 -070013696 if (kvm_lapic_hv_timer_in_use(vcpu))
13697 kvm_lapic_switch_to_sw_timer(vcpu);
13698
Yunhong Jiangbc225122016-06-13 14:19:58 -070013699 return 0;
13700}
13701
13702static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013703{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013704 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013705 return;
13706
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013707 WARN_ON(irqs_disabled());
13708 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013709 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013710 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013711}
13712
Yunhong Jiangbc225122016-06-13 14:19:58 -070013713static void vmx_post_block(struct kvm_vcpu *vcpu)
13714{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013715 if (kvm_x86_ops->set_hv_timer)
13716 kvm_lapic_switch_to_hv_timer(vcpu);
13717
Yunhong Jiangbc225122016-06-13 14:19:58 -070013718 pi_post_block(vcpu);
13719}
13720
Feng Wubf9f6ac2015-09-18 22:29:55 +080013721/*
Feng Wuefc64402015-09-18 22:29:51 +080013722 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13723 *
13724 * @kvm: kvm
13725 * @host_irq: host irq of the interrupt
13726 * @guest_irq: gsi of the interrupt
13727 * @set: set or unset PI
13728 * returns 0 on success, < 0 on failure
13729 */
13730static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13731 uint32_t guest_irq, bool set)
13732{
13733 struct kvm_kernel_irq_routing_entry *e;
13734 struct kvm_irq_routing_table *irq_rt;
13735 struct kvm_lapic_irq irq;
13736 struct kvm_vcpu *vcpu;
13737 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013738 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013739
13740 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013741 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13742 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013743 return 0;
13744
13745 idx = srcu_read_lock(&kvm->irq_srcu);
13746 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013747 if (guest_irq >= irq_rt->nr_rt_entries ||
13748 hlist_empty(&irq_rt->map[guest_irq])) {
13749 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13750 guest_irq, irq_rt->nr_rt_entries);
13751 goto out;
13752 }
Feng Wuefc64402015-09-18 22:29:51 +080013753
13754 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13755 if (e->type != KVM_IRQ_ROUTING_MSI)
13756 continue;
13757 /*
13758 * VT-d PI cannot support posting multicast/broadcast
13759 * interrupts to a vCPU, we still use interrupt remapping
13760 * for these kind of interrupts.
13761 *
13762 * For lowest-priority interrupts, we only support
13763 * those with single CPU as the destination, e.g. user
13764 * configures the interrupts via /proc/irq or uses
13765 * irqbalance to make the interrupts single-CPU.
13766 *
13767 * We will support full lowest-priority interrupt later.
13768 */
13769
Radim Krčmář371313132016-07-12 22:09:27 +020013770 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013771 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13772 /*
13773 * Make sure the IRTE is in remapped mode if
13774 * we don't handle it in posted mode.
13775 */
13776 ret = irq_set_vcpu_affinity(host_irq, NULL);
13777 if (ret < 0) {
13778 printk(KERN_INFO
13779 "failed to back to remapped mode, irq: %u\n",
13780 host_irq);
13781 goto out;
13782 }
13783
Feng Wuefc64402015-09-18 22:29:51 +080013784 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013785 }
Feng Wuefc64402015-09-18 22:29:51 +080013786
13787 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13788 vcpu_info.vector = irq.vector;
13789
hu huajun2698d822018-04-11 15:16:40 +080013790 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013791 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13792
13793 if (set)
13794 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013795 else
Feng Wuefc64402015-09-18 22:29:51 +080013796 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013797
13798 if (ret < 0) {
13799 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13800 __func__);
13801 goto out;
13802 }
13803 }
13804
13805 ret = 0;
13806out:
13807 srcu_read_unlock(&kvm->irq_srcu, idx);
13808 return ret;
13809}
13810
Ashok Rajc45dcc72016-06-22 14:59:56 +080013811static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13812{
13813 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13814 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13815 FEATURE_CONTROL_LMCE;
13816 else
13817 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13818 ~FEATURE_CONTROL_LMCE;
13819}
13820
Ladi Prosek72d7b372017-10-11 16:54:41 +020013821static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13822{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013823 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13824 if (to_vmx(vcpu)->nested.nested_run_pending)
13825 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013826 return 1;
13827}
13828
Ladi Prosek0234bf82017-10-11 16:54:40 +020013829static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13830{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013831 struct vcpu_vmx *vmx = to_vmx(vcpu);
13832
13833 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13834 if (vmx->nested.smm.guest_mode)
13835 nested_vmx_vmexit(vcpu, -1, 0, 0);
13836
13837 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13838 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013839 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013840 return 0;
13841}
13842
13843static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13844{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013845 struct vcpu_vmx *vmx = to_vmx(vcpu);
13846 int ret;
13847
13848 if (vmx->nested.smm.vmxon) {
13849 vmx->nested.vmxon = true;
13850 vmx->nested.smm.vmxon = false;
13851 }
13852
13853 if (vmx->nested.smm.guest_mode) {
13854 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013855 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013856 vcpu->arch.hflags |= HF_SMM_MASK;
13857 if (ret)
13858 return ret;
13859
13860 vmx->nested.smm.guest_mode = false;
13861 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013862 return 0;
13863}
13864
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013865static int enable_smi_window(struct kvm_vcpu *vcpu)
13866{
13867 return 0;
13868}
13869
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013870static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13871 struct kvm_nested_state __user *user_kvm_nested_state,
13872 u32 user_data_size)
13873{
13874 struct vcpu_vmx *vmx;
13875 struct vmcs12 *vmcs12;
13876 struct kvm_nested_state kvm_state = {
13877 .flags = 0,
13878 .format = 0,
13879 .size = sizeof(kvm_state),
13880 .vmx.vmxon_pa = -1ull,
13881 .vmx.vmcs_pa = -1ull,
13882 };
13883
13884 if (!vcpu)
13885 return kvm_state.size + 2 * VMCS12_SIZE;
13886
13887 vmx = to_vmx(vcpu);
13888 vmcs12 = get_vmcs12(vcpu);
13889 if (nested_vmx_allowed(vcpu) &&
13890 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13891 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13892 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13893
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013894 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013895 kvm_state.size += VMCS12_SIZE;
13896
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013897 if (is_guest_mode(vcpu) &&
13898 nested_cpu_has_shadow_vmcs(vmcs12) &&
13899 vmcs12->vmcs_link_pointer != -1ull)
13900 kvm_state.size += VMCS12_SIZE;
13901 }
13902
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013903 if (vmx->nested.smm.vmxon)
13904 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13905
13906 if (vmx->nested.smm.guest_mode)
13907 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13908
13909 if (is_guest_mode(vcpu)) {
13910 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13911
13912 if (vmx->nested.nested_run_pending)
13913 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13914 }
13915 }
13916
13917 if (user_data_size < kvm_state.size)
13918 goto out;
13919
13920 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13921 return -EFAULT;
13922
13923 if (vmx->nested.current_vmptr == -1ull)
13924 goto out;
13925
13926 /*
13927 * When running L2, the authoritative vmcs12 state is in the
13928 * vmcs02. When running L1, the authoritative vmcs12 state is
13929 * in the shadow vmcs linked to vmcs01, unless
13930 * sync_shadow_vmcs is set, in which case, the authoritative
13931 * vmcs12 state is in the vmcs12 already.
13932 */
13933 if (is_guest_mode(vcpu))
13934 sync_vmcs12(vcpu, vmcs12);
13935 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13936 copy_shadow_to_vmcs12(vmx);
13937
13938 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13939 return -EFAULT;
13940
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013941 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13942 vmcs12->vmcs_link_pointer != -1ull) {
13943 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13944 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13945 return -EFAULT;
13946 }
13947
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013948out:
13949 return kvm_state.size;
13950}
13951
13952static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
13953 struct kvm_nested_state __user *user_kvm_nested_state,
13954 struct kvm_nested_state *kvm_state)
13955{
13956 struct vcpu_vmx *vmx = to_vmx(vcpu);
13957 struct vmcs12 *vmcs12;
13958 u32 exit_qual;
13959 int ret;
13960
13961 if (kvm_state->format != 0)
13962 return -EINVAL;
13963
13964 if (!nested_vmx_allowed(vcpu))
13965 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
13966
13967 if (kvm_state->vmx.vmxon_pa == -1ull) {
13968 if (kvm_state->vmx.smm.flags)
13969 return -EINVAL;
13970
13971 if (kvm_state->vmx.vmcs_pa != -1ull)
13972 return -EINVAL;
13973
13974 vmx_leave_nested(vcpu);
13975 return 0;
13976 }
13977
13978 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
13979 return -EINVAL;
13980
13981 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
13982 return -EINVAL;
13983
13984 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
13985 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
13986 return -EINVAL;
13987
13988 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
13989 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
13990 return -EINVAL;
13991
13992 if (kvm_state->vmx.smm.flags &
13993 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
13994 return -EINVAL;
13995
Paolo Bonzini5bea5122018-09-18 15:19:17 +020013996 /*
13997 * SMM temporarily disables VMX, so we cannot be in guest mode,
13998 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
13999 * must be zero.
14000 */
14001 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14002 return -EINVAL;
14003
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014004 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14005 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14006 return -EINVAL;
14007
14008 vmx_leave_nested(vcpu);
14009 if (kvm_state->vmx.vmxon_pa == -1ull)
14010 return 0;
14011
14012 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14013 ret = enter_vmx_operation(vcpu);
14014 if (ret)
14015 return ret;
14016
14017 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14018
14019 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14020 vmx->nested.smm.vmxon = true;
14021 vmx->nested.vmxon = false;
14022
14023 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14024 vmx->nested.smm.guest_mode = true;
14025 }
14026
14027 vmcs12 = get_vmcs12(vcpu);
14028 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14029 return -EFAULT;
14030
Liran Alon392b2f22018-06-23 02:35:01 +030014031 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014032 return -EINVAL;
14033
14034 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14035 return 0;
14036
14037 vmx->nested.nested_run_pending =
14038 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14039
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014040 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14041 vmcs12->vmcs_link_pointer != -1ull) {
14042 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14043 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14044 return -EINVAL;
14045
14046 if (copy_from_user(shadow_vmcs12,
14047 user_kvm_nested_state->data + VMCS12_SIZE,
14048 sizeof(*vmcs12)))
14049 return -EFAULT;
14050
14051 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14052 !shadow_vmcs12->hdr.shadow_vmcs)
14053 return -EINVAL;
14054 }
14055
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014056 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14057 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14058 return -EINVAL;
14059
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014060 vmx->nested.dirty_vmcs12 = true;
14061 ret = enter_vmx_non_root_mode(vcpu, NULL);
14062 if (ret)
14063 return -EINVAL;
14064
14065 return 0;
14066}
14067
Kees Cook404f6aa2016-08-08 16:29:06 -070014068static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014069 .cpu_has_kvm_support = cpu_has_kvm_support,
14070 .disabled_by_bios = vmx_disabled_by_bios,
14071 .hardware_setup = hardware_setup,
14072 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014073 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014074 .hardware_enable = hardware_enable,
14075 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014076 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014077 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014078
Wanpeng Lib31c1142018-03-12 04:53:04 -070014079 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014080 .vm_alloc = vmx_vm_alloc,
14081 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014082
Avi Kivity6aa8b732006-12-10 02:21:36 -080014083 .vcpu_create = vmx_create_vcpu,
14084 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014085 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014086
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014087 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014088 .vcpu_load = vmx_vcpu_load,
14089 .vcpu_put = vmx_vcpu_put,
14090
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014091 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014092 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014093 .get_msr = vmx_get_msr,
14094 .set_msr = vmx_set_msr,
14095 .get_segment_base = vmx_get_segment_base,
14096 .get_segment = vmx_get_segment,
14097 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014098 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014099 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014100 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014101 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014102 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014103 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014104 .set_cr3 = vmx_set_cr3,
14105 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014106 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014107 .get_idt = vmx_get_idt,
14108 .set_idt = vmx_set_idt,
14109 .get_gdt = vmx_get_gdt,
14110 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014111 .get_dr6 = vmx_get_dr6,
14112 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014113 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014114 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014115 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014116 .get_rflags = vmx_get_rflags,
14117 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014118
Avi Kivity6aa8b732006-12-10 02:21:36 -080014119 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014120 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014121
Avi Kivity6aa8b732006-12-10 02:21:36 -080014122 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014123 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014124 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014125 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14126 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014127 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014128 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014129 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014130 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014131 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014132 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014133 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014134 .get_nmi_mask = vmx_get_nmi_mask,
14135 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014136 .enable_nmi_window = enable_nmi_window,
14137 .enable_irq_window = enable_irq_window,
14138 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014139 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014140 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014141 .get_enable_apicv = vmx_get_enable_apicv,
14142 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014143 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014144 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014145 .hwapic_irr_update = vmx_hwapic_irr_update,
14146 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014147 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014148 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14149 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014150
Izik Eiduscbc94022007-10-25 00:29:55 +020014151 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014152 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014153 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014154 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014155
Avi Kivity586f9602010-11-18 13:09:54 +020014156 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014157
Sheng Yang17cc3932010-01-05 19:02:27 +080014158 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014159
14160 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014161
14162 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014163 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014164
14165 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014166
14167 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014168
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014169 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014170 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014171
14172 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014173
14174 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014175 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014176 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014177 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014178 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014179
14180 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014181 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014182
14183 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014184
14185 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14186 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14187 .flush_log_dirty = vmx_flush_log_dirty,
14188 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014189 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014190
Feng Wubf9f6ac2015-09-18 22:29:55 +080014191 .pre_block = vmx_pre_block,
14192 .post_block = vmx_post_block,
14193
Wei Huang25462f72015-06-19 15:45:05 +020014194 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014195
14196 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014197
14198#ifdef CONFIG_X86_64
14199 .set_hv_timer = vmx_set_hv_timer,
14200 .cancel_hv_timer = vmx_cancel_hv_timer,
14201#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014202
14203 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014204
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014205 .get_nested_state = vmx_get_nested_state,
14206 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014207 .get_vmcs12_pages = nested_get_vmcs12_pages,
14208
Ladi Prosek72d7b372017-10-11 16:54:41 +020014209 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014210 .pre_enter_smm = vmx_pre_enter_smm,
14211 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014212 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014213};
14214
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014215static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014216{
14217 if (vmx_l1d_flush_pages) {
14218 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14219 vmx_l1d_flush_pages = NULL;
14220 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014221 /* Restore state so sysfs ignores VMX */
14222 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014223}
14224
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014225static void vmx_exit(void)
14226{
14227#ifdef CONFIG_KEXEC_CORE
14228 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14229 synchronize_rcu();
14230#endif
14231
14232 kvm_exit();
14233
14234#if IS_ENABLED(CONFIG_HYPERV)
14235 if (static_branch_unlikely(&enable_evmcs)) {
14236 int cpu;
14237 struct hv_vp_assist_page *vp_ap;
14238 /*
14239 * Reset everything to support using non-enlightened VMCS
14240 * access later (e.g. when we reload the module with
14241 * enlightened_vmcs=0)
14242 */
14243 for_each_online_cpu(cpu) {
14244 vp_ap = hv_get_vp_assist_page(cpu);
14245
14246 if (!vp_ap)
14247 continue;
14248
14249 vp_ap->current_nested_vmcs = 0;
14250 vp_ap->enlighten_vmentry = 0;
14251 }
14252
14253 static_branch_disable(&enable_evmcs);
14254 }
14255#endif
14256 vmx_cleanup_l1d_flush();
14257}
14258module_exit(vmx_exit);
14259
Avi Kivity6aa8b732006-12-10 02:21:36 -080014260static int __init vmx_init(void)
14261{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014262 int r;
14263
14264#if IS_ENABLED(CONFIG_HYPERV)
14265 /*
14266 * Enlightened VMCS usage should be recommended and the host needs
14267 * to support eVMCS v1 or above. We can also disable eVMCS support
14268 * with module parameter.
14269 */
14270 if (enlightened_vmcs &&
14271 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14272 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14273 KVM_EVMCS_VERSION) {
14274 int cpu;
14275
14276 /* Check that we have assist pages on all online CPUs */
14277 for_each_online_cpu(cpu) {
14278 if (!hv_get_vp_assist_page(cpu)) {
14279 enlightened_vmcs = false;
14280 break;
14281 }
14282 }
14283
14284 if (enlightened_vmcs) {
14285 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14286 static_branch_enable(&enable_evmcs);
14287 }
14288 } else {
14289 enlightened_vmcs = false;
14290 }
14291#endif
14292
14293 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014294 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014295 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014296 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014297
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014298 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014299 * Must be called after kvm_init() so enable_ept is properly set
14300 * up. Hand the parameter mitigation value in which was stored in
14301 * the pre module init parser. If no parameter was given, it will
14302 * contain 'auto' which will be turned into the default 'cond'
14303 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014304 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014305 if (boot_cpu_has(X86_BUG_L1TF)) {
14306 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14307 if (r) {
14308 vmx_exit();
14309 return r;
14310 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014311 }
14312
Dave Young2965faa2015-09-09 15:38:55 -070014313#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014314 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14315 crash_vmclear_local_loaded_vmcss);
14316#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014317 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014318
He, Qingfdef3ad2007-04-30 09:45:24 +030014319 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014320}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014321module_init(vmx_init);