blob: b0f36d82ae7b42e6d09d643fca939153c2bba592 [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
Sean Christopherson3de63472018-07-13 08:42:30 -0700134#define KVM_VM_CR0_ALWAYS_OFF (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;
Liran Alon62cf9bd812018-09-14 03:25:54 +0300859 u64 vmcs01_guest_bndcfgs;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800860
Wanpeng Li5c614b32015-10-13 09:18:36 -0700861 u16 vpid02;
862 u16 last_vpid;
863
Paolo Bonzini6677f3d2018-02-26 13:40:08 +0100864 struct nested_vmx_msrs msrs;
Ladi Prosek72e9cbd2017-10-11 16:54:43 +0200865
866 /* SMM related state */
867 struct {
868 /* in VMX operation on SMM entry? */
869 bool vmxon;
870 /* in guest mode on SMM entry? */
871 bool guest_mode;
872 } smm;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300873};
874
Yang Zhang01e439b2013-04-11 19:25:12 +0800875#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800876#define POSTED_INTR_SN 1
877
Yang Zhang01e439b2013-04-11 19:25:12 +0800878/* Posted-Interrupt Descriptor */
879struct pi_desc {
880 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800881 union {
882 struct {
883 /* bit 256 - Outstanding Notification */
884 u16 on : 1,
885 /* bit 257 - Suppress Notification */
886 sn : 1,
887 /* bit 271:258 - Reserved */
888 rsvd_1 : 14;
889 /* bit 279:272 - Notification Vector */
890 u8 nv;
891 /* bit 287:280 - Reserved */
892 u8 rsvd_2;
893 /* bit 319:288 - Notification Destination */
894 u32 ndst;
895 };
896 u64 control;
897 };
898 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800899} __aligned(64);
900
Yang Zhanga20ed542013-04-11 19:25:15 +0800901static bool pi_test_and_set_on(struct pi_desc *pi_desc)
902{
903 return test_and_set_bit(POSTED_INTR_ON,
904 (unsigned long *)&pi_desc->control);
905}
906
907static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
908{
909 return test_and_clear_bit(POSTED_INTR_ON,
910 (unsigned long *)&pi_desc->control);
911}
912
913static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
914{
915 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
916}
917
Feng Wuebbfc762015-09-18 22:29:46 +0800918static inline void pi_clear_sn(struct pi_desc *pi_desc)
919{
920 return clear_bit(POSTED_INTR_SN,
921 (unsigned long *)&pi_desc->control);
922}
923
924static inline void pi_set_sn(struct pi_desc *pi_desc)
925{
926 return set_bit(POSTED_INTR_SN,
927 (unsigned long *)&pi_desc->control);
928}
929
Paolo Bonziniad361092016-09-20 16:15:05 +0200930static inline void pi_clear_on(struct pi_desc *pi_desc)
931{
932 clear_bit(POSTED_INTR_ON,
933 (unsigned long *)&pi_desc->control);
934}
935
Feng Wuebbfc762015-09-18 22:29:46 +0800936static inline int pi_test_on(struct pi_desc *pi_desc)
937{
938 return test_bit(POSTED_INTR_ON,
939 (unsigned long *)&pi_desc->control);
940}
941
942static inline int pi_test_sn(struct pi_desc *pi_desc)
943{
944 return test_bit(POSTED_INTR_SN,
945 (unsigned long *)&pi_desc->control);
946}
947
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400948struct vmx_msrs {
949 unsigned int nr;
950 struct vmx_msr_entry val[NR_AUTOLOAD_MSRS];
951};
952
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400953struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000954 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300955 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300956 u8 fail;
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100957 u8 msr_bitmap_mode;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300958 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200959 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200960 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300961 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400962 int nmsrs;
963 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800964 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400965#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300966 u64 msr_host_kernel_gs_base;
967 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968#endif
Ashok Raj15d45072018-02-01 22:59:43 +0100969
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100970 u64 arch_capabilities;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +0100971 u64 spec_ctrl;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +0100972
Gleb Natapov2961e8762013-11-25 15:37:13 +0200973 u32 vm_entry_controls_shadow;
974 u32 vm_exit_controls_shadow;
Paolo Bonzini80154d72017-08-24 13:55:35 +0200975 u32 secondary_exec_control;
976
Nadav Har'Eld462b812011-05-24 15:26:10 +0300977 /*
978 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
979 * non-nested (L1) guest, it always points to vmcs01. For a nested
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700980 * guest (L2), it points to a different VMCS. loaded_cpu_state points
981 * to the VMCS whose state is loaded into the CPU registers that only
982 * need to be switched when transitioning to/from the kernel; a NULL
983 * value indicates that host state is loaded.
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984 */
985 struct loaded_vmcs vmcs01;
986 struct loaded_vmcs *loaded_vmcs;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700987 struct loaded_vmcs *loaded_cpu_state;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300988 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300989 struct msr_autoload {
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -0400990 struct vmx_msrs guest;
991 struct vmx_msrs host;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300992 } msr_autoload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -0700993
Avi Kivity9c8cba32007-11-22 11:42:59 +0200994 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300995 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300996 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300997 struct kvm_segment segs[8];
998 } rmode;
999 struct {
1000 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001001 struct kvm_save_segment {
1002 u16 selector;
1003 unsigned long base;
1004 u32 limit;
1005 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03001006 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +03001007 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001008 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +03001009 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02001010
Andi Kleena0861c02009-06-08 17:37:09 +08001011 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001012
Yang Zhang01e439b2013-04-11 19:25:12 +08001013 /* Posted interrupt descriptor */
1014 struct pi_desc pi_desc;
1015
Nadav Har'Elec378ae2011-05-25 23:02:54 +03001016 /* Support for a guest hypervisor (nested VMX) */
1017 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +02001018
1019 /* Dynamic PLE window. */
1020 int ple_window;
1021 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +08001022
Sean Christophersond264ee02018-08-27 15:21:12 -07001023 bool req_immediate_exit;
1024
Kai Huang843e4332015-01-28 10:54:28 +08001025 /* Support for PML */
1026#define PML_ENTITY_NUM 512
1027 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001028
Yunhong Jiang64672c92016-06-13 14:19:59 -07001029 /* apic deadline value in host tsc */
1030 u64 hv_deadline_tsc;
1031
Owen Hofmann2680d6d2016-03-01 13:36:13 -08001032 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001033
Xiao Guangrong1be0e612016-03-22 16:51:18 +08001034 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +08001035
Wanpeng Li74c55932017-11-29 01:31:20 -08001036 unsigned long host_debugctlmsr;
1037
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001038 /*
1039 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
1040 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
1041 * in msr_ia32_feature_control_valid_bits.
1042 */
Haozhong Zhang3b840802016-06-22 14:59:54 +08001043 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +08001044 u64 msr_ia32_feature_control_valid_bits;
Tianyu Lan877ad952018-07-19 08:40:23 +00001045 u64 ept_pointer;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001046};
1047
Avi Kivity2fb92db2011-04-27 19:42:18 +03001048enum segment_cache_field {
1049 SEG_FIELD_SEL = 0,
1050 SEG_FIELD_BASE = 1,
1051 SEG_FIELD_LIMIT = 2,
1052 SEG_FIELD_AR = 3,
1053
1054 SEG_FIELD_NR = 4
1055};
1056
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07001057static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
1058{
1059 return container_of(kvm, struct kvm_vmx, kvm);
1060}
1061
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
1063{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001064 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065}
1066
Feng Wuefc64402015-09-18 22:29:51 +08001067static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
1068{
1069 return &(to_vmx(vcpu)->pi_desc);
1070}
1071
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001072#define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n)))))
Nadav Har'El22bd0352011-05-25 23:05:57 +03001073#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001074#define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name)
1075#define FIELD64(number, name) \
1076 FIELD(number, name), \
1077 [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32)
Nadav Har'El22bd0352011-05-25 23:05:57 +03001078
Abel Gordon4607c2d2013-04-18 14:35:55 +03001079
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001080static u16 shadow_read_only_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001081#define SHADOW_FIELD_RO(x) x,
1082#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001083};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001084static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001085 ARRAY_SIZE(shadow_read_only_fields);
1086
Paolo Bonzini44900ba2017-12-13 12:58:02 +01001087static u16 shadow_read_write_fields[] = {
Paolo Bonzinic9e9dea2017-12-20 13:16:29 +01001088#define SHADOW_FIELD_RW(x) x,
1089#include "vmx_shadow_fields.h"
Abel Gordon4607c2d2013-04-18 14:35:55 +03001090};
Bandan Dasfe2b2012014-04-21 15:20:14 -04001091static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +03001092 ARRAY_SIZE(shadow_read_write_fields);
1093
Mathias Krause772e0312012-08-30 01:30:19 +02001094static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +03001095 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +08001096 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001097 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
1098 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
1099 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
1100 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
1101 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
1102 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
1103 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
1104 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +08001105 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Bandan Dasc5f983f2017-05-05 15:25:14 -04001106 FIELD(GUEST_PML_INDEX, guest_pml_index),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001107 FIELD(HOST_ES_SELECTOR, host_es_selector),
1108 FIELD(HOST_CS_SELECTOR, host_cs_selector),
1109 FIELD(HOST_SS_SELECTOR, host_ss_selector),
1110 FIELD(HOST_DS_SELECTOR, host_ds_selector),
1111 FIELD(HOST_FS_SELECTOR, host_fs_selector),
1112 FIELD(HOST_GS_SELECTOR, host_gs_selector),
1113 FIELD(HOST_TR_SELECTOR, host_tr_selector),
1114 FIELD64(IO_BITMAP_A, io_bitmap_a),
1115 FIELD64(IO_BITMAP_B, io_bitmap_b),
1116 FIELD64(MSR_BITMAP, msr_bitmap),
1117 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
1118 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
1119 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
Jim Mattsonb348e792018-05-01 15:40:27 -07001120 FIELD64(PML_ADDRESS, pml_address),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001121 FIELD64(TSC_OFFSET, tsc_offset),
1122 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
1123 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +08001124 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Bandan Das27c42a12017-08-03 15:54:42 -04001125 FIELD64(VM_FUNCTION_CONTROL, vm_function_control),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001126 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +08001127 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
1128 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
1129 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
1130 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Bandan Das41ab9372017-08-03 15:54:43 -04001131 FIELD64(EPTP_LIST_ADDRESS, eptp_list_address),
Jim Mattsonb348e792018-05-01 15:40:27 -07001132 FIELD64(VMREAD_BITMAP, vmread_bitmap),
1133 FIELD64(VMWRITE_BITMAP, vmwrite_bitmap),
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001134 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001135 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
1136 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
1137 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
1138 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
1139 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
1140 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
1141 FIELD64(GUEST_PDPTR0, guest_pdptr0),
1142 FIELD64(GUEST_PDPTR1, guest_pdptr1),
1143 FIELD64(GUEST_PDPTR2, guest_pdptr2),
1144 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +01001145 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001146 FIELD64(HOST_IA32_PAT, host_ia32_pat),
1147 FIELD64(HOST_IA32_EFER, host_ia32_efer),
1148 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
1149 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
1150 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
1151 FIELD(EXCEPTION_BITMAP, exception_bitmap),
1152 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
1153 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
1154 FIELD(CR3_TARGET_COUNT, cr3_target_count),
1155 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
1156 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
1157 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
1158 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
1159 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
1160 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
1161 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
1162 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
1163 FIELD(TPR_THRESHOLD, tpr_threshold),
1164 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
1165 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
1166 FIELD(VM_EXIT_REASON, vm_exit_reason),
1167 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
1168 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
1169 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
1170 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
1171 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
1172 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
1173 FIELD(GUEST_ES_LIMIT, guest_es_limit),
1174 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
1175 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
1176 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
1177 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
1178 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
1179 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
1180 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
1181 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
1182 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
1183 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
1184 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
1185 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
1186 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
1187 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
1188 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
1189 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
1190 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
1191 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
1192 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
1193 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
1194 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +01001195 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +03001196 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
1197 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
1198 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
1199 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
1200 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
1201 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
1202 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
1203 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
1204 FIELD(EXIT_QUALIFICATION, exit_qualification),
1205 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
1206 FIELD(GUEST_CR0, guest_cr0),
1207 FIELD(GUEST_CR3, guest_cr3),
1208 FIELD(GUEST_CR4, guest_cr4),
1209 FIELD(GUEST_ES_BASE, guest_es_base),
1210 FIELD(GUEST_CS_BASE, guest_cs_base),
1211 FIELD(GUEST_SS_BASE, guest_ss_base),
1212 FIELD(GUEST_DS_BASE, guest_ds_base),
1213 FIELD(GUEST_FS_BASE, guest_fs_base),
1214 FIELD(GUEST_GS_BASE, guest_gs_base),
1215 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
1216 FIELD(GUEST_TR_BASE, guest_tr_base),
1217 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
1218 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
1219 FIELD(GUEST_DR7, guest_dr7),
1220 FIELD(GUEST_RSP, guest_rsp),
1221 FIELD(GUEST_RIP, guest_rip),
1222 FIELD(GUEST_RFLAGS, guest_rflags),
1223 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
1224 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
1225 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
1226 FIELD(HOST_CR0, host_cr0),
1227 FIELD(HOST_CR3, host_cr3),
1228 FIELD(HOST_CR4, host_cr4),
1229 FIELD(HOST_FS_BASE, host_fs_base),
1230 FIELD(HOST_GS_BASE, host_gs_base),
1231 FIELD(HOST_TR_BASE, host_tr_base),
1232 FIELD(HOST_GDTR_BASE, host_gdtr_base),
1233 FIELD(HOST_IDTR_BASE, host_idtr_base),
1234 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
1235 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
1236 FIELD(HOST_RSP, host_rsp),
1237 FIELD(HOST_RIP, host_rip),
1238};
Nadav Har'El22bd0352011-05-25 23:05:57 +03001239
1240static inline short vmcs_field_to_offset(unsigned long field)
1241{
Dan Williams085331d2018-01-31 17:47:03 -08001242 const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table);
1243 unsigned short offset;
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001244 unsigned index;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001245
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001246 if (field >> 15)
Andrew Honig75f139a2018-01-10 10:12:03 -08001247 return -ENOENT;
1248
Jim Mattson58e9ffa2017-12-22 12:13:13 -08001249 index = ROL16(field, 6);
Linus Torvalds15303ba2018-02-10 13:16:35 -08001250 if (index >= size)
Andrew Honig75f139a2018-01-10 10:12:03 -08001251 return -ENOENT;
1252
Linus Torvalds15303ba2018-02-10 13:16:35 -08001253 index = array_index_nospec(index, size);
1254 offset = vmcs_field_to_offset_table[index];
Dan Williams085331d2018-01-31 17:47:03 -08001255 if (offset == 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01001256 return -ENOENT;
Dan Williams085331d2018-01-31 17:47:03 -08001257 return offset;
Nadav Har'El22bd0352011-05-25 23:05:57 +03001258}
1259
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001260static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
1261{
David Matlack4f2777b2016-07-13 17:16:37 -07001262 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03001263}
1264
Liran Alon61ada742018-06-23 02:35:08 +03001265static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu)
1266{
1267 return to_vmx(vcpu)->nested.cached_shadow_vmcs12;
1268}
1269
Peter Feiner995f00a2017-06-30 17:26:32 -07001270static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03001271static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Peter Feiner995f00a2017-06-30 17:26:32 -07001272static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +08001273static bool vmx_xsaves_supported(void);
Orit Wassermanb246dd52012-05-31 14:49:22 +03001274static void vmx_set_segment(struct kvm_vcpu *vcpu,
1275 struct kvm_segment *var, int seg);
1276static void vmx_get_segment(struct kvm_vcpu *vcpu,
1277 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +02001278static bool guest_state_valid(struct kvm_vcpu *vcpu);
1279static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordon16f5b902013-04-18 14:38:25 +03001280static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzinib96fb432017-07-27 12:29:32 +02001281static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu);
1282static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked);
1283static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
1284 u16 error_code);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01001285static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001286static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1287 u32 msr, int type);
Avi Kivity75880a02007-06-20 11:20:04 +03001288
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1290static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001291/*
1292 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1293 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1294 */
1295static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Feng Wubf9f6ac2015-09-18 22:29:55 +08001297/*
1298 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1299 * can find which vCPU should be waken up.
1300 */
1301static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1302static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1303
Radim Krčmář23611332016-09-29 22:41:33 +02001304enum {
Radim Krčmář23611332016-09-29 22:41:33 +02001305 VMX_VMREAD_BITMAP,
1306 VMX_VMWRITE_BITMAP,
1307 VMX_BITMAP_NR
1308};
1309
1310static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
1311
Radim Krčmář23611332016-09-29 22:41:33 +02001312#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
1313#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +03001314
Avi Kivity110312c2010-12-21 12:54:20 +02001315static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001316static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +02001317
Sheng Yang2384d2b2008-01-17 15:14:33 +08001318static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1319static DEFINE_SPINLOCK(vmx_vpid_lock);
1320
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 int size;
1323 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001324 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001326 u32 pin_based_exec_ctrl;
1327 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001328 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329 u32 vmexit_ctrl;
1330 u32 vmentry_ctrl;
Paolo Bonzini13893092018-02-26 13:40:09 +01001331 struct nested_vmx_msrs nested;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333
Hannes Ederefff9e52008-11-28 17:02:06 +01001334static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +08001335 u32 ept;
1336 u32 vpid;
1337} vmx_capability;
1338
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339#define VMX_SEGMENT_FIELD(seg) \
1340 [VCPU_SREG_##seg] = { \
1341 .selector = GUEST_##seg##_SELECTOR, \
1342 .base = GUEST_##seg##_BASE, \
1343 .limit = GUEST_##seg##_LIMIT, \
1344 .ar_bytes = GUEST_##seg##_AR_BYTES, \
1345 }
1346
Mathias Krause772e0312012-08-30 01:30:19 +02001347static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 unsigned selector;
1349 unsigned base;
1350 unsigned limit;
1351 unsigned ar_bytes;
1352} kvm_vmx_segment_fields[] = {
1353 VMX_SEGMENT_FIELD(CS),
1354 VMX_SEGMENT_FIELD(DS),
1355 VMX_SEGMENT_FIELD(ES),
1356 VMX_SEGMENT_FIELD(FS),
1357 VMX_SEGMENT_FIELD(GS),
1358 VMX_SEGMENT_FIELD(SS),
1359 VMX_SEGMENT_FIELD(TR),
1360 VMX_SEGMENT_FIELD(LDTR),
1361};
1362
Avi Kivity26bb0982009-09-07 11:14:12 +03001363static u64 host_efer;
1364
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001365static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1366
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001367/*
Brian Gerst8c065852010-07-17 09:03:26 -04001368 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001369 * away by decrementing the array size.
1370 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001371static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001372#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001373 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001375 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001377
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001378DEFINE_STATIC_KEY_FALSE(enable_evmcs);
1379
1380#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
1381
1382#define KVM_EVMCS_VERSION 1
1383
1384#if IS_ENABLED(CONFIG_HYPERV)
1385static bool __read_mostly enlightened_vmcs = true;
1386module_param(enlightened_vmcs, bool, 0444);
1387
1388static inline void evmcs_write64(unsigned long field, u64 value)
1389{
1390 u16 clean_field;
1391 int offset = get_evmcs_offset(field, &clean_field);
1392
1393 if (offset < 0)
1394 return;
1395
1396 *(u64 *)((char *)current_evmcs + offset) = value;
1397
1398 current_evmcs->hv_clean_fields &= ~clean_field;
1399}
1400
1401static inline void evmcs_write32(unsigned long field, u32 value)
1402{
1403 u16 clean_field;
1404 int offset = get_evmcs_offset(field, &clean_field);
1405
1406 if (offset < 0)
1407 return;
1408
1409 *(u32 *)((char *)current_evmcs + offset) = value;
1410 current_evmcs->hv_clean_fields &= ~clean_field;
1411}
1412
1413static inline void evmcs_write16(unsigned long field, u16 value)
1414{
1415 u16 clean_field;
1416 int offset = get_evmcs_offset(field, &clean_field);
1417
1418 if (offset < 0)
1419 return;
1420
1421 *(u16 *)((char *)current_evmcs + offset) = value;
1422 current_evmcs->hv_clean_fields &= ~clean_field;
1423}
1424
1425static inline u64 evmcs_read64(unsigned long field)
1426{
1427 int offset = get_evmcs_offset(field, NULL);
1428
1429 if (offset < 0)
1430 return 0;
1431
1432 return *(u64 *)((char *)current_evmcs + offset);
1433}
1434
1435static inline u32 evmcs_read32(unsigned long field)
1436{
1437 int offset = get_evmcs_offset(field, NULL);
1438
1439 if (offset < 0)
1440 return 0;
1441
1442 return *(u32 *)((char *)current_evmcs + offset);
1443}
1444
1445static inline u16 evmcs_read16(unsigned long field)
1446{
1447 int offset = get_evmcs_offset(field, NULL);
1448
1449 if (offset < 0)
1450 return 0;
1451
1452 return *(u16 *)((char *)current_evmcs + offset);
1453}
1454
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001455static inline void evmcs_touch_msr_bitmap(void)
1456{
1457 if (unlikely(!current_evmcs))
1458 return;
1459
1460 if (current_evmcs->hv_enlightenments_control.msr_bitmap)
1461 current_evmcs->hv_clean_fields &=
1462 ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;
1463}
1464
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001465static void evmcs_load(u64 phys_addr)
1466{
1467 struct hv_vp_assist_page *vp_ap =
1468 hv_get_vp_assist_page(smp_processor_id());
1469
1470 vp_ap->current_nested_vmcs = phys_addr;
1471 vp_ap->enlighten_vmentry = 1;
1472}
1473
1474static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
1475{
1476 /*
1477 * Enlightened VMCSv1 doesn't support these:
1478 *
1479 * POSTED_INTR_NV = 0x00000002,
1480 * GUEST_INTR_STATUS = 0x00000810,
1481 * APIC_ACCESS_ADDR = 0x00002014,
1482 * POSTED_INTR_DESC_ADDR = 0x00002016,
1483 * EOI_EXIT_BITMAP0 = 0x0000201c,
1484 * EOI_EXIT_BITMAP1 = 0x0000201e,
1485 * EOI_EXIT_BITMAP2 = 0x00002020,
1486 * EOI_EXIT_BITMAP3 = 0x00002022,
1487 */
1488 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
1489 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1490 ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1491 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1492 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1493 vmcs_conf->cpu_based_2nd_exec_ctrl &=
1494 ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
1495
1496 /*
1497 * GUEST_PML_INDEX = 0x00000812,
1498 * PML_ADDRESS = 0x0000200e,
1499 */
1500 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML;
1501
1502 /* VM_FUNCTION_CONTROL = 0x00002018, */
1503 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC;
1504
1505 /*
1506 * EPTP_LIST_ADDRESS = 0x00002024,
1507 * VMREAD_BITMAP = 0x00002026,
1508 * VMWRITE_BITMAP = 0x00002028,
1509 */
1510 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS;
1511
1512 /*
1513 * TSC_MULTIPLIER = 0x00002032,
1514 */
1515 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING;
1516
1517 /*
1518 * PLE_GAP = 0x00004020,
1519 * PLE_WINDOW = 0x00004022,
1520 */
1521 vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1522
1523 /*
1524 * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E,
1525 */
1526 vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
1527
1528 /*
1529 * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808,
1530 * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04,
1531 */
1532 vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
1533 vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
1534
1535 /*
1536 * Currently unsupported in KVM:
1537 * GUEST_IA32_RTIT_CTL = 0x00002814,
1538 */
1539}
Tianyu Lan877ad952018-07-19 08:40:23 +00001540
1541/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1542static void check_ept_pointer_match(struct kvm *kvm)
1543{
1544 struct kvm_vcpu *vcpu;
1545 u64 tmp_eptp = INVALID_PAGE;
1546 int i;
1547
1548 kvm_for_each_vcpu(i, vcpu, kvm) {
1549 if (!VALID_PAGE(tmp_eptp)) {
1550 tmp_eptp = to_vmx(vcpu)->ept_pointer;
1551 } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1552 to_kvm_vmx(kvm)->ept_pointers_match
1553 = EPT_POINTERS_MISMATCH;
1554 return;
1555 }
1556 }
1557
1558 to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1559}
1560
1561static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
1562{
1563 int ret;
1564
1565 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1566
1567 if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1568 check_ept_pointer_match(kvm);
1569
1570 if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1571 ret = -ENOTSUPP;
1572 goto out;
1573 }
1574
1575 ret = hyperv_flush_guest_mapping(
1576 to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer);
1577
1578out:
1579 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1580 return ret;
1581}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001582#else /* !IS_ENABLED(CONFIG_HYPERV) */
1583static inline void evmcs_write64(unsigned long field, u64 value) {}
1584static inline void evmcs_write32(unsigned long field, u32 value) {}
1585static inline void evmcs_write16(unsigned long field, u16 value) {}
1586static inline u64 evmcs_read64(unsigned long field) { return 0; }
1587static inline u32 evmcs_read32(unsigned long field) { return 0; }
1588static inline u16 evmcs_read16(unsigned long field) { return 0; }
1589static inline void evmcs_load(u64 phys_addr) {}
1590static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {}
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02001591static inline void evmcs_touch_msr_bitmap(void) {}
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01001592#endif /* IS_ENABLED(CONFIG_HYPERV) */
1593
Jan Kiszka5bb16012016-02-09 20:14:21 +01001594static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1597 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001598 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1599}
1600
Jan Kiszka6f054852016-02-09 20:15:18 +01001601static inline bool is_debug(u32 intr_info)
1602{
1603 return is_exception_n(intr_info, DB_VECTOR);
1604}
1605
1606static inline bool is_breakpoint(u32 intr_info)
1607{
1608 return is_exception_n(intr_info, BP_VECTOR);
1609}
1610
Jan Kiszka5bb16012016-02-09 20:14:21 +01001611static inline bool is_page_fault(u32 intr_info)
1612{
1613 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614}
1615
Gui Jianfeng31299942010-03-15 17:29:09 +08001616static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001617{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001619}
1620
Liran Alon9e869482018-03-12 13:12:51 +02001621static inline bool is_gp_fault(u32 intr_info)
1622{
1623 return is_exception_n(intr_info, GP_VECTOR);
1624}
1625
Gui Jianfeng31299942010-03-15 17:29:09 +08001626static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001627{
1628 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1629 INTR_INFO_VALID_MASK)) ==
1630 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1631}
1632
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001633/* Undocumented: icebp/int1 */
1634static inline bool is_icebp(u32 intr_info)
1635{
1636 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1637 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1638}
1639
Gui Jianfeng31299942010-03-15 17:29:09 +08001640static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001641{
Sheng Yang04547152009-04-01 15:52:31 +08001642 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001643}
1644
Gui Jianfeng31299942010-03-15 17:29:09 +08001645static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001646{
Sheng Yang04547152009-04-01 15:52:31 +08001647 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001648}
1649
Paolo Bonzini35754c92015-07-29 12:05:37 +02001650static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001651{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001652 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001653}
1654
Gui Jianfeng31299942010-03-15 17:29:09 +08001655static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001656{
Sheng Yang04547152009-04-01 15:52:31 +08001657 return vmcs_config.cpu_based_exec_ctrl &
1658 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001659}
1660
Avi Kivity774ead32007-12-26 13:57:04 +02001661static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001662{
Sheng Yang04547152009-04-01 15:52:31 +08001663 return vmcs_config.cpu_based_2nd_exec_ctrl &
1664 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1665}
1666
Yang Zhang8d146952013-01-25 10:18:50 +08001667static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1668{
1669 return vmcs_config.cpu_based_2nd_exec_ctrl &
1670 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1671}
1672
Yang Zhang83d4c282013-01-25 10:18:49 +08001673static inline bool cpu_has_vmx_apic_register_virt(void)
1674{
1675 return vmcs_config.cpu_based_2nd_exec_ctrl &
1676 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1677}
1678
Yang Zhangc7c9c562013-01-25 10:18:51 +08001679static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1680{
1681 return vmcs_config.cpu_based_2nd_exec_ctrl &
1682 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1683}
1684
Sean Christopherson0b665d32018-08-14 09:33:34 -07001685static inline bool cpu_has_vmx_encls_vmexit(void)
1686{
1687 return vmcs_config.cpu_based_2nd_exec_ctrl &
1688 SECONDARY_EXEC_ENCLS_EXITING;
1689}
1690
Yunhong Jiang64672c92016-06-13 14:19:59 -07001691/*
1692 * Comment's format: document - errata name - stepping - processor name.
1693 * Refer from
1694 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1695 */
1696static u32 vmx_preemption_cpu_tfms[] = {
1697/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
16980x000206E6,
1699/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1700/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1701/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17020x00020652,
1703/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17040x00020655,
1705/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1706/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1707/*
1708 * 320767.pdf - AAP86 - B1 -
1709 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1710 */
17110x000106E5,
1712/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17130x000106A0,
1714/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17150x000106A1,
1716/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17170x000106A4,
1718 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1719 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1720 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17210x000106A5,
1722};
1723
1724static inline bool cpu_has_broken_vmx_preemption_timer(void)
1725{
1726 u32 eax = cpuid_eax(0x00000001), i;
1727
1728 /* Clear the reserved bits */
1729 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001730 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001731 if (eax == vmx_preemption_cpu_tfms[i])
1732 return true;
1733
1734 return false;
1735}
1736
1737static inline bool cpu_has_vmx_preemption_timer(void)
1738{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001739 return vmcs_config.pin_based_exec_ctrl &
1740 PIN_BASED_VMX_PREEMPTION_TIMER;
1741}
1742
Yang Zhang01e439b2013-04-11 19:25:12 +08001743static inline bool cpu_has_vmx_posted_intr(void)
1744{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001745 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1746 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001747}
1748
1749static inline bool cpu_has_vmx_apicv(void)
1750{
1751 return cpu_has_vmx_apic_register_virt() &&
1752 cpu_has_vmx_virtual_intr_delivery() &&
1753 cpu_has_vmx_posted_intr();
1754}
1755
Sheng Yang04547152009-04-01 15:52:31 +08001756static inline bool cpu_has_vmx_flexpriority(void)
1757{
1758 return cpu_has_vmx_tpr_shadow() &&
1759 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001760}
1761
Marcelo Tosattie7997942009-06-11 12:07:40 -03001762static inline bool cpu_has_vmx_ept_execute_only(void)
1763{
Gui Jianfeng31299942010-03-15 17:29:09 +08001764 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001765}
1766
Marcelo Tosattie7997942009-06-11 12:07:40 -03001767static inline bool cpu_has_vmx_ept_2m_page(void)
1768{
Gui Jianfeng31299942010-03-15 17:29:09 +08001769 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001770}
1771
Sheng Yang878403b2010-01-05 19:02:29 +08001772static inline bool cpu_has_vmx_ept_1g_page(void)
1773{
Gui Jianfeng31299942010-03-15 17:29:09 +08001774 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001775}
1776
Sheng Yang4bc9b982010-06-02 14:05:24 +08001777static inline bool cpu_has_vmx_ept_4levels(void)
1778{
1779 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1780}
1781
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001782static inline bool cpu_has_vmx_ept_mt_wb(void)
1783{
1784 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1785}
1786
Yu Zhang855feb62017-08-24 20:27:55 +08001787static inline bool cpu_has_vmx_ept_5levels(void)
1788{
1789 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1790}
1791
Xudong Hao83c3a332012-05-28 19:33:35 +08001792static inline bool cpu_has_vmx_ept_ad_bits(void)
1793{
1794 return vmx_capability.ept & VMX_EPT_AD_BIT;
1795}
1796
Gui Jianfeng31299942010-03-15 17:29:09 +08001797static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001798{
Gui Jianfeng31299942010-03-15 17:29:09 +08001799 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001800}
1801
Gui Jianfeng31299942010-03-15 17:29:09 +08001802static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001803{
Gui Jianfeng31299942010-03-15 17:29:09 +08001804 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001805}
1806
Liran Aloncd9a4912018-05-22 17:16:15 +03001807static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1808{
1809 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1810}
1811
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001812static inline bool cpu_has_vmx_invvpid_single(void)
1813{
1814 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1815}
1816
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001817static inline bool cpu_has_vmx_invvpid_global(void)
1818{
1819 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1820}
1821
Wanpeng Li08d839c2017-03-23 05:30:08 -07001822static inline bool cpu_has_vmx_invvpid(void)
1823{
1824 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1825}
1826
Gui Jianfeng31299942010-03-15 17:29:09 +08001827static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001828{
Sheng Yang04547152009-04-01 15:52:31 +08001829 return vmcs_config.cpu_based_2nd_exec_ctrl &
1830 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001831}
1832
Gui Jianfeng31299942010-03-15 17:29:09 +08001833static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001834{
1835 return vmcs_config.cpu_based_2nd_exec_ctrl &
1836 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1837}
1838
Gui Jianfeng31299942010-03-15 17:29:09 +08001839static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001840{
1841 return vmcs_config.cpu_based_2nd_exec_ctrl &
1842 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1843}
1844
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001845static inline bool cpu_has_vmx_basic_inout(void)
1846{
1847 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1848}
1849
Paolo Bonzini35754c92015-07-29 12:05:37 +02001850static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001851{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001852 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001853}
1854
Gui Jianfeng31299942010-03-15 17:29:09 +08001855static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001856{
Sheng Yang04547152009-04-01 15:52:31 +08001857 return vmcs_config.cpu_based_2nd_exec_ctrl &
1858 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001859}
1860
Gui Jianfeng31299942010-03-15 17:29:09 +08001861static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001862{
1863 return vmcs_config.cpu_based_2nd_exec_ctrl &
1864 SECONDARY_EXEC_RDTSCP;
1865}
1866
Mao, Junjiead756a12012-07-02 01:18:48 +00001867static inline bool cpu_has_vmx_invpcid(void)
1868{
1869 return vmcs_config.cpu_based_2nd_exec_ctrl &
1870 SECONDARY_EXEC_ENABLE_INVPCID;
1871}
1872
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001873static inline bool cpu_has_virtual_nmis(void)
1874{
1875 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1876}
1877
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001878static inline bool cpu_has_vmx_wbinvd_exit(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_WBINVD_EXITING;
1882}
1883
Abel Gordonabc4fc52013-04-18 14:35:25 +03001884static inline bool cpu_has_vmx_shadow_vmcs(void)
1885{
1886 u64 vmx_msr;
1887 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1888 /* check if the cpu supports writing r/o exit information fields */
1889 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1890 return false;
1891
1892 return vmcs_config.cpu_based_2nd_exec_ctrl &
1893 SECONDARY_EXEC_SHADOW_VMCS;
1894}
1895
Kai Huang843e4332015-01-28 10:54:28 +08001896static inline bool cpu_has_vmx_pml(void)
1897{
1898 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1899}
1900
Haozhong Zhang64903d62015-10-20 15:39:09 +08001901static inline bool cpu_has_vmx_tsc_scaling(void)
1902{
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_TSC_SCALING;
1905}
1906
Bandan Das2a499e42017-08-03 15:54:41 -04001907static inline bool cpu_has_vmx_vmfunc(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl &
1910 SECONDARY_EXEC_ENABLE_VMFUNC;
1911}
1912
Sean Christopherson64f7a112018-04-30 10:01:06 -07001913static bool vmx_umip_emulated(void)
1914{
1915 return vmcs_config.cpu_based_2nd_exec_ctrl &
1916 SECONDARY_EXEC_DESC;
1917}
1918
Sheng Yang04547152009-04-01 15:52:31 +08001919static inline bool report_flexpriority(void)
1920{
1921 return flexpriority_enabled;
1922}
1923
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001924static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1925{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001926 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c702017-05-05 11:28:09 -07001927}
1928
Jim Mattsonf4160e42018-05-29 09:11:33 -07001929/*
1930 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1931 * to modify any valid field of the VMCS, or are the VM-exit
1932 * information fields read-only?
1933 */
1934static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1935{
1936 return to_vmx(vcpu)->nested.msrs.misc_low &
1937 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1938}
1939
Marc Orr04473782018-06-20 17:21:29 -07001940static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1941{
1942 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1943}
1944
1945static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1948 CPU_BASED_MONITOR_TRAP_FLAG;
1949}
1950
Liran Alonfa97d7d2018-07-18 14:07:59 +02001951static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1954 SECONDARY_EXEC_SHADOW_VMCS;
1955}
1956
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001957static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1958{
1959 return vmcs12->cpu_based_vm_exec_control & bit;
1960}
1961
1962static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1963{
1964 return (vmcs12->cpu_based_vm_exec_control &
1965 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1966 (vmcs12->secondary_vm_exec_control & bit);
1967}
1968
Jan Kiszkaf4124502014-03-07 20:03:13 +01001969static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1970{
1971 return vmcs12->pin_based_vm_exec_control &
1972 PIN_BASED_VMX_PREEMPTION_TIMER;
1973}
1974
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001975static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1976{
1977 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1978}
1979
1980static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1983}
1984
Nadav Har'El155a97a2013-08-05 11:07:16 +03001985static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1986{
1987 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1988}
1989
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001990static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1991{
Paolo Bonzini3db13482017-08-24 14:48:03 +02001992 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001993}
1994
Bandan Dasc5f983f2017-05-05 15:25:14 -04001995static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
1996{
1997 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
1998}
1999
Wincy Vanf2b93282015-02-03 23:56:03 +08002000static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2001{
2002 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2003}
2004
Wanpeng Li5c614b32015-10-13 09:18:36 -07002005static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2006{
2007 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2008}
2009
Wincy Van82f0dd42015-02-03 23:57:18 +08002010static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2011{
2012 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2013}
2014
Wincy Van608406e2015-02-03 23:57:51 +08002015static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2016{
2017 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2018}
2019
Wincy Van705699a2015-02-03 23:58:17 +08002020static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2021{
2022 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2023}
2024
Bandan Das27c42a12017-08-03 15:54:42 -04002025static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2026{
2027 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2028}
2029
Bandan Das41ab9372017-08-03 15:54:43 -04002030static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2031{
2032 return nested_cpu_has_vmfunc(vmcs12) &&
2033 (vmcs12->vm_function_control &
2034 VMX_VMFUNC_EPTP_SWITCHING);
2035}
2036
Liran Alonf792d272018-06-23 02:35:05 +03002037static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2038{
2039 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2040}
2041
Jim Mattsonef85b672016-12-12 11:01:37 -08002042static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002043{
2044 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002045 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002046}
2047
Jan Kiszka533558b2014-01-04 18:47:20 +01002048static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2049 u32 exit_intr_info,
2050 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002051static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2052 struct vmcs12 *vmcs12,
2053 u32 reason, unsigned long qualification);
2054
Rusty Russell8b9cf982007-07-30 16:31:43 +10002055static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002056{
2057 int i;
2058
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002059 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002060 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002061 return i;
2062 return -1;
2063}
2064
Sheng Yang2384d2b2008-01-17 15:14:33 +08002065static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2066{
2067 struct {
2068 u64 vpid : 16;
2069 u64 rsvd : 48;
2070 u64 gva;
2071 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002072 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002073
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002074 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2075 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2076 : "memory");
2077 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002078}
2079
Sheng Yang14394422008-04-28 12:24:45 +08002080static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2081{
2082 struct {
2083 u64 eptp, gpa;
2084 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002085 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002086
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002087 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2088 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2089 : "memory");
2090 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002091}
2092
Avi Kivity26bb0982009-09-07 11:14:12 +03002093static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002094{
2095 int i;
2096
Rusty Russell8b9cf982007-07-30 16:31:43 +10002097 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002098 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002099 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002100 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002101}
2102
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103static void vmcs_clear(struct vmcs *vmcs)
2104{
2105 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002106 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002107
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002108 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2109 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2110 : "memory");
2111 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2113 vmcs, phys_addr);
2114}
2115
Nadav Har'Eld462b812011-05-24 15:26:10 +03002116static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2117{
2118 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002119 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2120 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002121 loaded_vmcs->cpu = -1;
2122 loaded_vmcs->launched = 0;
2123}
2124
Dongxiao Xu7725b892010-05-11 18:29:38 +08002125static void vmcs_load(struct vmcs *vmcs)
2126{
2127 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002128 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002129
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002130 if (static_branch_unlikely(&enable_evmcs))
2131 return evmcs_load(phys_addr);
2132
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002133 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2134 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2135 : "memory");
2136 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002137 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002138 vmcs, phys_addr);
2139}
2140
Dave Young2965faa2015-09-09 15:38:55 -07002141#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002142/*
2143 * This bitmap is used to indicate whether the vmclear
2144 * operation is enabled on all cpus. All disabled by
2145 * default.
2146 */
2147static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2148
2149static inline void crash_enable_local_vmclear(int cpu)
2150{
2151 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2152}
2153
2154static inline void crash_disable_local_vmclear(int cpu)
2155{
2156 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2157}
2158
2159static inline int crash_local_vmclear_enabled(int cpu)
2160{
2161 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2162}
2163
2164static void crash_vmclear_local_loaded_vmcss(void)
2165{
2166 int cpu = raw_smp_processor_id();
2167 struct loaded_vmcs *v;
2168
2169 if (!crash_local_vmclear_enabled(cpu))
2170 return;
2171
2172 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2173 loaded_vmcss_on_cpu_link)
2174 vmcs_clear(v->vmcs);
2175}
2176#else
2177static inline void crash_enable_local_vmclear(int cpu) { }
2178static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002179#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002180
Nadav Har'Eld462b812011-05-24 15:26:10 +03002181static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002182{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002183 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002184 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185
Nadav Har'Eld462b812011-05-24 15:26:10 +03002186 if (loaded_vmcs->cpu != cpu)
2187 return; /* vcpu migration can race with cpu offline */
2188 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002190 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002191 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002192
2193 /*
2194 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2195 * is before setting loaded_vmcs->vcpu to -1 which is done in
2196 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2197 * then adds the vmcs into percpu list before it is deleted.
2198 */
2199 smp_wmb();
2200
Nadav Har'Eld462b812011-05-24 15:26:10 +03002201 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002202 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203}
2204
Nadav Har'Eld462b812011-05-24 15:26:10 +03002205static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002206{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002207 int cpu = loaded_vmcs->cpu;
2208
2209 if (cpu != -1)
2210 smp_call_function_single(cpu,
2211 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002212}
2213
Junaid Shahidfaff8752018-06-29 13:10:05 -07002214static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2215{
2216 if (vpid == 0)
2217 return true;
2218
2219 if (cpu_has_vmx_invvpid_individual_addr()) {
2220 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2221 return true;
2222 }
2223
2224 return false;
2225}
2226
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002227static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002228{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002229 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002230 return;
2231
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002232 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002233 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002234}
2235
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002236static inline void vpid_sync_vcpu_global(void)
2237{
2238 if (cpu_has_vmx_invvpid_global())
2239 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2240}
2241
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002242static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002243{
2244 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002245 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002246 else
2247 vpid_sync_vcpu_global();
2248}
2249
Sheng Yang14394422008-04-28 12:24:45 +08002250static inline void ept_sync_global(void)
2251{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002252 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002253}
2254
2255static inline void ept_sync_context(u64 eptp)
2256{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002257 if (cpu_has_vmx_invept_context())
2258 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2259 else
2260 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002261}
2262
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002263static __always_inline void vmcs_check16(unsigned long field)
2264{
2265 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2266 "16-bit accessor invalid for 64-bit field");
2267 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2268 "16-bit accessor invalid for 64-bit high field");
2269 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2270 "16-bit accessor invalid for 32-bit high field");
2271 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2272 "16-bit accessor invalid for natural width field");
2273}
2274
2275static __always_inline void vmcs_check32(unsigned long field)
2276{
2277 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2278 "32-bit accessor invalid for 16-bit field");
2279 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2280 "32-bit accessor invalid for natural width field");
2281}
2282
2283static __always_inline void vmcs_check64(unsigned long field)
2284{
2285 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2286 "64-bit accessor invalid for 16-bit field");
2287 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2288 "64-bit accessor invalid for 64-bit high field");
2289 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2290 "64-bit accessor invalid for 32-bit field");
2291 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2292 "64-bit accessor invalid for natural width field");
2293}
2294
2295static __always_inline void vmcs_checkl(unsigned long field)
2296{
2297 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2298 "Natural width accessor invalid for 16-bit field");
2299 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2300 "Natural width accessor invalid for 64-bit field");
2301 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2302 "Natural width accessor invalid for 64-bit high field");
2303 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2304 "Natural width accessor invalid for 32-bit field");
2305}
2306
2307static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308{
Avi Kivity5e520e62011-05-15 10:13:12 -04002309 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310
Avi Kivity5e520e62011-05-15 10:13:12 -04002311 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2312 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002313 return value;
2314}
2315
Avi Kivity96304212011-05-15 10:13:13 -04002316static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002317{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002318 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002319 if (static_branch_unlikely(&enable_evmcs))
2320 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002321 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322}
2323
Avi Kivity96304212011-05-15 10:13:13 -04002324static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002326 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002327 if (static_branch_unlikely(&enable_evmcs))
2328 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
Avi Kivity96304212011-05-15 10:13:13 -04002332static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002334 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002335 if (static_branch_unlikely(&enable_evmcs))
2336 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002337#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002338 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341#endif
2342}
2343
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002344static __always_inline unsigned long vmcs_readl(unsigned long field)
2345{
2346 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002347 if (static_branch_unlikely(&enable_evmcs))
2348 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002349 return __vmcs_readl(field);
2350}
2351
Avi Kivitye52de1b2007-01-05 16:36:56 -08002352static noinline void vmwrite_error(unsigned long field, unsigned long value)
2353{
2354 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2355 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2356 dump_stack();
2357}
2358
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002359static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002360{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002361 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002363 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2364 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002365 if (unlikely(error))
2366 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
2368
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002369static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002371 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002372 if (static_branch_unlikely(&enable_evmcs))
2373 return evmcs_write16(field, value);
2374
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002375 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376}
2377
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002378static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002381 if (static_branch_unlikely(&enable_evmcs))
2382 return evmcs_write32(field, value);
2383
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002384 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385}
2386
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002387static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002390 if (static_branch_unlikely(&enable_evmcs))
2391 return evmcs_write64(field, value);
2392
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002393 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002394#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002396 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397#endif
2398}
2399
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002401{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002402 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002403 if (static_branch_unlikely(&enable_evmcs))
2404 return evmcs_write64(field, value);
2405
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002406 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002407}
2408
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002409static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002410{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2412 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002413 if (static_branch_unlikely(&enable_evmcs))
2414 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2415
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002416 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2417}
2418
2419static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2420{
2421 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2422 "vmcs_set_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);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002427}
2428
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002429static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2430{
2431 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2432}
2433
Gleb Natapov2961e8762013-11-25 15:37:13 +02002434static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2435{
2436 vmcs_write32(VM_ENTRY_CONTROLS, val);
2437 vmx->vm_entry_controls_shadow = val;
2438}
2439
2440static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2441{
2442 if (vmx->vm_entry_controls_shadow != val)
2443 vm_entry_controls_init(vmx, val);
2444}
2445
2446static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2447{
2448 return vmx->vm_entry_controls_shadow;
2449}
2450
2451
2452static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2453{
2454 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2455}
2456
2457static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2458{
2459 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2460}
2461
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002462static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2463{
2464 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2465}
2466
Gleb Natapov2961e8762013-11-25 15:37:13 +02002467static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2468{
2469 vmcs_write32(VM_EXIT_CONTROLS, val);
2470 vmx->vm_exit_controls_shadow = val;
2471}
2472
2473static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2474{
2475 if (vmx->vm_exit_controls_shadow != val)
2476 vm_exit_controls_init(vmx, val);
2477}
2478
2479static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2480{
2481 return vmx->vm_exit_controls_shadow;
2482}
2483
2484
2485static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2486{
2487 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2488}
2489
2490static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2491{
2492 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2493}
2494
Avi Kivity2fb92db2011-04-27 19:42:18 +03002495static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2496{
2497 vmx->segment_cache.bitmask = 0;
2498}
2499
2500static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2501 unsigned field)
2502{
2503 bool ret;
2504 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2505
2506 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2507 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2508 vmx->segment_cache.bitmask = 0;
2509 }
2510 ret = vmx->segment_cache.bitmask & mask;
2511 vmx->segment_cache.bitmask |= mask;
2512 return ret;
2513}
2514
2515static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2516{
2517 u16 *p = &vmx->segment_cache.seg[seg].selector;
2518
2519 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2520 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2521 return *p;
2522}
2523
2524static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2525{
2526 ulong *p = &vmx->segment_cache.seg[seg].base;
2527
2528 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2529 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2530 return *p;
2531}
2532
2533static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2534{
2535 u32 *p = &vmx->segment_cache.seg[seg].limit;
2536
2537 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2538 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2539 return *p;
2540}
2541
2542static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2543{
2544 u32 *p = &vmx->segment_cache.seg[seg].ar;
2545
2546 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2547 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2548 return *p;
2549}
2550
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002551static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2552{
2553 u32 eb;
2554
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002555 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002556 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002557 /*
2558 * Guest access to VMware backdoor ports could legitimately
2559 * trigger #GP because of TSS I/O permission bitmap.
2560 * We intercept those #GP and allow access to them anyway
2561 * as VMware does.
2562 */
2563 if (enable_vmware_backdoor)
2564 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002565 if ((vcpu->guest_debug &
2566 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2567 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2568 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002569 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002570 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002571 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002572 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002573
2574 /* When we are running a nested L2 guest and L1 specified for it a
2575 * certain exception bitmap, we must trap the same exceptions and pass
2576 * them to L1. When running L2, we will only handle the exceptions
2577 * specified above if L1 did not want them.
2578 */
2579 if (is_guest_mode(vcpu))
2580 eb |= get_vmcs12(vcpu)->exception_bitmap;
2581
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002582 vmcs_write32(EXCEPTION_BITMAP, eb);
2583}
2584
Ashok Raj15d45072018-02-01 22:59:43 +01002585/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002586 * Check if MSR is intercepted for currently loaded MSR bitmap.
2587 */
2588static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2589{
2590 unsigned long *msr_bitmap;
2591 int f = sizeof(unsigned long);
2592
2593 if (!cpu_has_vmx_msr_bitmap())
2594 return true;
2595
2596 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2597
2598 if (msr <= 0x1fff) {
2599 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2600 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2601 msr &= 0x1fff;
2602 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2603 }
2604
2605 return true;
2606}
2607
2608/*
Ashok Raj15d45072018-02-01 22:59:43 +01002609 * Check if MSR is intercepted for L01 MSR bitmap.
2610 */
2611static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2612{
2613 unsigned long *msr_bitmap;
2614 int f = sizeof(unsigned long);
2615
2616 if (!cpu_has_vmx_msr_bitmap())
2617 return true;
2618
2619 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2620
2621 if (msr <= 0x1fff) {
2622 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2623 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2624 msr &= 0x1fff;
2625 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2626 }
2627
2628 return true;
2629}
2630
Gleb Natapov2961e8762013-11-25 15:37:13 +02002631static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2632 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002633{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002634 vm_entry_controls_clearbit(vmx, entry);
2635 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002636}
2637
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002638static int find_msr(struct vmx_msrs *m, unsigned int msr)
2639{
2640 unsigned int i;
2641
2642 for (i = 0; i < m->nr; ++i) {
2643 if (m->val[i].index == msr)
2644 return i;
2645 }
2646 return -ENOENT;
2647}
2648
Avi Kivity61d2ef22010-04-28 16:40:38 +03002649static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2650{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002651 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002652 struct msr_autoload *m = &vmx->msr_autoload;
2653
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002654 switch (msr) {
2655 case MSR_EFER:
2656 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002657 clear_atomic_switch_msr_special(vmx,
2658 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002659 VM_EXIT_LOAD_IA32_EFER);
2660 return;
2661 }
2662 break;
2663 case MSR_CORE_PERF_GLOBAL_CTRL:
2664 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002665 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002666 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2667 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2668 return;
2669 }
2670 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002671 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002672 i = find_msr(&m->guest, msr);
2673 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002674 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002675 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002676 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002677 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002678
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002679skip_guest:
2680 i = find_msr(&m->host, msr);
2681 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002682 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002683
2684 --m->host.nr;
2685 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002686 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002687}
2688
Gleb Natapov2961e8762013-11-25 15:37:13 +02002689static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2690 unsigned long entry, unsigned long exit,
2691 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2692 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002693{
2694 vmcs_write64(guest_val_vmcs, guest_val);
2695 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002696 vm_entry_controls_setbit(vmx, entry);
2697 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002698}
2699
Avi Kivity61d2ef22010-04-28 16:40:38 +03002700static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002701 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002702{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002703 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002704 struct msr_autoload *m = &vmx->msr_autoload;
2705
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002706 switch (msr) {
2707 case MSR_EFER:
2708 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002709 add_atomic_switch_msr_special(vmx,
2710 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002711 VM_EXIT_LOAD_IA32_EFER,
2712 GUEST_IA32_EFER,
2713 HOST_IA32_EFER,
2714 guest_val, host_val);
2715 return;
2716 }
2717 break;
2718 case MSR_CORE_PERF_GLOBAL_CTRL:
2719 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002720 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002721 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2722 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2723 GUEST_IA32_PERF_GLOBAL_CTRL,
2724 HOST_IA32_PERF_GLOBAL_CTRL,
2725 guest_val, host_val);
2726 return;
2727 }
2728 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002729 case MSR_IA32_PEBS_ENABLE:
2730 /* PEBS needs a quiescent period after being disabled (to write
2731 * a record). Disabling PEBS through VMX MSR swapping doesn't
2732 * provide that period, so a CPU could write host's record into
2733 * guest's memory.
2734 */
2735 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002736 }
2737
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002738 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002739 if (!entry_only)
2740 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002741
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002742 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002743 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002744 "Can't add msr %x\n", msr);
2745 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002746 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002747 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002748 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002749 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002750 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002751 m->guest.val[i].index = msr;
2752 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002753
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002754 if (entry_only)
2755 return;
2756
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002757 if (j < 0) {
2758 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002759 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002760 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002761 m->host.val[j].index = msr;
2762 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002763}
2764
Avi Kivity92c0d902009-10-29 11:00:16 +02002765static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002766{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002767 u64 guest_efer = vmx->vcpu.arch.efer;
2768 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002769
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002770 if (!enable_ept) {
2771 /*
2772 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2773 * host CPUID is more efficient than testing guest CPUID
2774 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2775 */
2776 if (boot_cpu_has(X86_FEATURE_SMEP))
2777 guest_efer |= EFER_NX;
2778 else if (!(guest_efer & EFER_NX))
2779 ignore_bits |= EFER_NX;
2780 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002781
Avi Kivity51c6cf62007-08-29 03:48:05 +03002782 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002783 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002784 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002785 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002786#ifdef CONFIG_X86_64
2787 ignore_bits |= EFER_LMA | EFER_LME;
2788 /* SCE is meaningful only in long mode on Intel */
2789 if (guest_efer & EFER_LMA)
2790 ignore_bits &= ~(u64)EFER_SCE;
2791#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002792
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002793 /*
2794 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2795 * On CPUs that support "load IA32_EFER", always switch EFER
2796 * atomically, since it's faster than switching it manually.
2797 */
2798 if (cpu_has_load_ia32_efer ||
2799 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002800 if (!(guest_efer & EFER_LMA))
2801 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002802 if (guest_efer != host_efer)
2803 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002804 guest_efer, host_efer, false);
Sean Christopherson02343cf2018-09-26 09:23:43 -07002805 else
2806 clear_atomic_switch_msr(vmx, MSR_EFER);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002807 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002808 } else {
Sean Christopherson02343cf2018-09-26 09:23:43 -07002809 clear_atomic_switch_msr(vmx, MSR_EFER);
2810
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002811 guest_efer &= ~ignore_bits;
2812 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002814 vmx->guest_msrs[efer_offset].data = guest_efer;
2815 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2816
2817 return true;
2818 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002819}
2820
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002821#ifdef CONFIG_X86_32
2822/*
2823 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2824 * VMCS rather than the segment table. KVM uses this helper to figure
2825 * out the current bases to poke them into the VMCS before entry.
2826 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002827static unsigned long segment_base(u16 selector)
2828{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002829 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002830 unsigned long v;
2831
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002832 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002833 return 0;
2834
Thomas Garnier45fc8752017-03-14 10:05:08 -07002835 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002837 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002838 u16 ldt_selector = kvm_read_ldt();
2839
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002840 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002841 return 0;
2842
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002843 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002845 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002846 return v;
2847}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002848#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002849
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002850static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002851{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002852 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002853 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002854#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002855 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002856#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002857 unsigned long fs_base, gs_base;
2858 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002859 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002860
Sean Christophersond264ee02018-08-27 15:21:12 -07002861 vmx->req_immediate_exit = false;
2862
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002863 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002864 return;
2865
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002866 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002867 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002868
Avi Kivity33ed6322007-05-02 16:54:03 +03002869 /*
2870 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2871 * allow segment selectors with cpl > 0 or ti == 1.
2872 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002873 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002874
2875#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002876 savesegment(ds, host_state->ds_sel);
2877 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002878
2879 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002880 if (likely(is_64bit_mm(current->mm))) {
2881 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002882 fs_sel = current->thread.fsindex;
2883 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002884 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002885 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002886 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002887 savesegment(fs, fs_sel);
2888 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002889 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002890 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002891 }
2892
Paolo Bonzini4679b612018-09-24 17:23:01 +02002893 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002894#else
Sean Christophersone368b872018-07-23 12:32:41 -07002895 savesegment(fs, fs_sel);
2896 savesegment(gs, gs_sel);
2897 fs_base = segment_base(fs_sel);
2898 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002899#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002900
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002901 if (unlikely(fs_sel != host_state->fs_sel)) {
2902 if (!(fs_sel & 7))
2903 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2904 else
2905 vmcs_write16(HOST_FS_SELECTOR, 0);
2906 host_state->fs_sel = fs_sel;
2907 }
2908 if (unlikely(gs_sel != host_state->gs_sel)) {
2909 if (!(gs_sel & 7))
2910 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2911 else
2912 vmcs_write16(HOST_GS_SELECTOR, 0);
2913 host_state->gs_sel = gs_sel;
2914 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002915 if (unlikely(fs_base != host_state->fs_base)) {
2916 vmcs_writel(HOST_FS_BASE, fs_base);
2917 host_state->fs_base = fs_base;
2918 }
2919 if (unlikely(gs_base != host_state->gs_base)) {
2920 vmcs_writel(HOST_GS_BASE, gs_base);
2921 host_state->gs_base = gs_base;
2922 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002923
Avi Kivity26bb0982009-09-07 11:14:12 +03002924 for (i = 0; i < vmx->save_nmsrs; ++i)
2925 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002926 vmx->guest_msrs[i].data,
2927 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002928}
2929
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002930static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002931{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002932 struct vmcs_host_state *host_state;
2933
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002934 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002935 return;
2936
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002937 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002938 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002939
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002940 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002941 vmx->loaded_cpu_state = NULL;
2942
Avi Kivityc8770e72010-11-11 12:37:26 +02002943#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002944 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002945#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002946 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2947 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002948#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002949 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002950#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002951 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002952#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002953 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002954 if (host_state->fs_sel & 7)
2955 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002956#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002957 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2958 loadsegment(ds, host_state->ds_sel);
2959 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002960 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002961#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002962 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002963#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002964 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002965#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002966 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002967}
2968
Sean Christopherson678e3152018-07-23 12:32:43 -07002969#ifdef CONFIG_X86_64
2970static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002971{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002972 preempt_disable();
2973 if (vmx->loaded_cpu_state)
2974 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2975 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002976 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002977}
2978
Sean Christopherson678e3152018-07-23 12:32:43 -07002979static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2980{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002981 preempt_disable();
2982 if (vmx->loaded_cpu_state)
2983 wrmsrl(MSR_KERNEL_GS_BASE, data);
2984 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002985 vmx->msr_guest_kernel_gs_base = data;
2986}
2987#endif
2988
Feng Wu28b835d2015-09-18 22:29:54 +08002989static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2990{
2991 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2992 struct pi_desc old, new;
2993 unsigned int dest;
2994
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002995 /*
2996 * In case of hot-plug or hot-unplug, we may have to undo
2997 * vmx_vcpu_pi_put even if there is no assigned device. And we
2998 * always keep PI.NDST up to date for simplicity: it makes the
2999 * code easier, and CPU migration is not a fast path.
3000 */
3001 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003002 return;
3003
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003004 /*
3005 * First handle the simple case where no cmpxchg is necessary; just
3006 * allow posting non-urgent interrupts.
3007 *
3008 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3009 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3010 * expects the VCPU to be on the blocked_vcpu_list that matches
3011 * PI.NDST.
3012 */
3013 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3014 vcpu->cpu == cpu) {
3015 pi_clear_sn(pi_desc);
3016 return;
3017 }
3018
3019 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003020 do {
3021 old.control = new.control = pi_desc->control;
3022
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003023 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003024
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003025 if (x2apic_enabled())
3026 new.ndst = dest;
3027 else
3028 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003029
Feng Wu28b835d2015-09-18 22:29:54 +08003030 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003031 } while (cmpxchg64(&pi_desc->control, old.control,
3032 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003033}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003034
Peter Feinerc95ba922016-08-17 09:36:47 -07003035static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3036{
3037 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3038 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3039}
3040
Avi Kivity6aa8b732006-12-10 02:21:36 -08003041/*
3042 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3043 * vcpu mutex is already taken.
3044 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003045static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003048 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003049
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003050 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003051 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003052 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003053 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003054
3055 /*
3056 * Read loaded_vmcs->cpu should be before fetching
3057 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3058 * See the comments in __loaded_vmcs_clear().
3059 */
3060 smp_rmb();
3061
Nadav Har'Eld462b812011-05-24 15:26:10 +03003062 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3063 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003064 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003065 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003066 }
3067
3068 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3069 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3070 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003071 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003072 }
3073
3074 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003075 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003076 unsigned long sysenter_esp;
3077
3078 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003079
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 /*
3081 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003082 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003084 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003085 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003086 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003088 /*
3089 * VM exits change the host TR limit to 0x67 after a VM
3090 * exit. This is okay, since 0x67 covers everything except
3091 * the IO bitmap and have have code to handle the IO bitmap
3092 * being lost after a VM exit.
3093 */
3094 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3095
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3097 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003098
Nadav Har'Eld462b812011-05-24 15:26:10 +03003099 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003100 }
Feng Wu28b835d2015-09-18 22:29:54 +08003101
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003102 /* Setup TSC multiplier */
3103 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003104 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3105 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003106
Feng Wu28b835d2015-09-18 22:29:54 +08003107 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003108 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003109 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003110}
3111
3112static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3113{
3114 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3115
3116 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003117 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3118 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003119 return;
3120
3121 /* Set SN when the vCPU is preempted */
3122 if (vcpu->preempted)
3123 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124}
3125
3126static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3127{
Feng Wu28b835d2015-09-18 22:29:54 +08003128 vmx_vcpu_pi_put(vcpu);
3129
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003130 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131}
3132
Wanpeng Lif244dee2017-07-20 01:11:54 -07003133static bool emulation_required(struct kvm_vcpu *vcpu)
3134{
3135 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3136}
3137
Avi Kivityedcafe32009-12-30 18:07:40 +02003138static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3139
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003140/*
3141 * Return the cr0 value that a nested guest would read. This is a combination
3142 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3143 * its hypervisor (cr0_read_shadow).
3144 */
3145static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3146{
3147 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3148 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3149}
3150static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3151{
3152 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3153 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3154}
3155
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3157{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003158 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003159
Avi Kivity6de12732011-03-07 12:51:22 +02003160 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3161 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3162 rflags = vmcs_readl(GUEST_RFLAGS);
3163 if (to_vmx(vcpu)->rmode.vm86_active) {
3164 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3165 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3166 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3167 }
3168 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003169 }
Avi Kivity6de12732011-03-07 12:51:22 +02003170 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171}
3172
3173static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3174{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003175 unsigned long old_rflags = vmx_get_rflags(vcpu);
3176
Avi Kivity6de12732011-03-07 12:51:22 +02003177 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3178 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003179 if (to_vmx(vcpu)->rmode.vm86_active) {
3180 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003181 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003182 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003184
3185 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3186 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187}
3188
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003189static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003190{
3191 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3192 int ret = 0;
3193
3194 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003195 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003196 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003197 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003198
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003199 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003200}
3201
3202static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3203{
3204 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3205 u32 interruptibility = interruptibility_old;
3206
3207 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3208
Jan Kiszka48005f62010-02-19 19:38:07 +01003209 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003210 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003211 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003212 interruptibility |= GUEST_INTR_STATE_STI;
3213
3214 if ((interruptibility != interruptibility_old))
3215 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3216}
3217
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3219{
3220 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003222 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003224 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003225
Glauber Costa2809f5d2009-05-12 16:21:05 -04003226 /* skipping an emulated instruction also counts */
3227 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228}
3229
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003230static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3231 unsigned long exit_qual)
3232{
3233 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3234 unsigned int nr = vcpu->arch.exception.nr;
3235 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3236
3237 if (vcpu->arch.exception.has_error_code) {
3238 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3239 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3240 }
3241
3242 if (kvm_exception_is_soft(nr))
3243 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3244 else
3245 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3246
3247 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3248 vmx_get_nmi_mask(vcpu))
3249 intr_info |= INTR_INFO_UNBLOCK_NMI;
3250
3251 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3252}
3253
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003254/*
3255 * KVM wants to inject page-faults which it got to the guest. This function
3256 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003257 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003258static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003259{
3260 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003261 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003262
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003263 if (nr == PF_VECTOR) {
3264 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003265 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003266 return 1;
3267 }
3268 /*
3269 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3270 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3271 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3272 * can be written only when inject_pending_event runs. This should be
3273 * conditional on a new capability---if the capability is disabled,
3274 * kvm_multiple_exception would write the ancillary information to
3275 * CR2 or DR6, for backwards ABI-compatibility.
3276 */
3277 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3278 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003279 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003280 return 1;
3281 }
3282 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003283 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003284 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003285 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003286 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3287 *exit_qual ^= DR6_RTM;
3288 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003289 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003290 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003291 return 1;
3292 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003293 }
3294
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003295 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003296}
3297
Wanpeng Licaa057a2018-03-12 04:53:03 -07003298static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3299{
3300 /*
3301 * Ensure that we clear the HLT state in the VMCS. We don't need to
3302 * explicitly skip the instruction because if the HLT state is set,
3303 * then the instruction is already executing and RIP has already been
3304 * advanced.
3305 */
3306 if (kvm_hlt_in_guest(vcpu->kvm) &&
3307 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3308 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3309}
3310
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003311static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003312{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003313 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003314 unsigned nr = vcpu->arch.exception.nr;
3315 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003316 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003317 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003318
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003319 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003320 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003321 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3322 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003323
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003324 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003325 int inc_eip = 0;
3326 if (kvm_exception_is_soft(nr))
3327 inc_eip = vcpu->arch.event_exit_inst_len;
3328 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003329 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003330 return;
3331 }
3332
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003333 WARN_ON_ONCE(vmx->emulation_required);
3334
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003335 if (kvm_exception_is_soft(nr)) {
3336 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3337 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003338 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3339 } else
3340 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3341
3342 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003343
3344 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003345}
3346
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003347static bool vmx_rdtscp_supported(void)
3348{
3349 return cpu_has_vmx_rdtscp();
3350}
3351
Mao, Junjiead756a12012-07-02 01:18:48 +00003352static bool vmx_invpcid_supported(void)
3353{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003354 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003355}
3356
Avi Kivity6aa8b732006-12-10 02:21:36 -08003357/*
Eddie Donga75beee2007-05-17 18:55:15 +03003358 * Swap MSR entry in host/guest MSR entry array.
3359 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003360static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003361{
Avi Kivity26bb0982009-09-07 11:14:12 +03003362 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003363
3364 tmp = vmx->guest_msrs[to];
3365 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3366 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003367}
3368
3369/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003370 * Set up the vmcs to automatically save and restore system
3371 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3372 * mode, as fiddling with msrs is very expensive.
3373 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003374static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003375{
Avi Kivity26bb0982009-09-07 11:14:12 +03003376 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003377
Eddie Donga75beee2007-05-17 18:55:15 +03003378 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003379#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003380 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003381 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003382 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003383 move_msr_up(vmx, index, save_nmsrs++);
3384 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003385 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003386 move_msr_up(vmx, index, save_nmsrs++);
3387 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003388 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003390 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003391 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003392 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003393 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003394 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003395 * if efer.sce is enabled.
3396 */
Brian Gerst8c065852010-07-17 09:03:26 -04003397 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003398 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003399 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003400 }
Eddie Donga75beee2007-05-17 18:55:15 +03003401#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003402 index = __find_msr_index(vmx, MSR_EFER);
3403 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003404 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003405
Avi Kivity26bb0982009-09-07 11:14:12 +03003406 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003407
Yang Zhang8d146952013-01-25 10:18:50 +08003408 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003409 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003410}
3411
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003412static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003414 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003416 if (is_guest_mode(vcpu) &&
3417 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3418 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3419
3420 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421}
3422
3423/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003424 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003426static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003428 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003429 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003430 * We're here if L1 chose not to trap WRMSR to TSC. According
3431 * to the spec, this should set L1's TSC; The offset that L1
3432 * set for L2 remains unchanged, and still needs to be added
3433 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003434 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003435 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003436 /* recalculate vmcs02.TSC_OFFSET: */
3437 vmcs12 = get_vmcs12(vcpu);
3438 vmcs_write64(TSC_OFFSET, offset +
3439 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3440 vmcs12->tsc_offset : 0));
3441 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003442 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3443 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003444 vmcs_write64(TSC_OFFSET, offset);
3445 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446}
3447
Nadav Har'El801d3422011-05-25 23:02:23 +03003448/*
3449 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3450 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3451 * all guests if the "nested" module option is off, and can also be disabled
3452 * for a single guest by disabling its VMX cpuid bit.
3453 */
3454static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3455{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003456 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003457}
3458
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003460 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3461 * returned for the various VMX controls MSRs when nested VMX is enabled.
3462 * The same values should also be used to verify that vmcs12 control fields are
3463 * valid during nested entry from L1 to L2.
3464 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3465 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3466 * bit in the high half is on if the corresponding bit in the control field
3467 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003468 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003469static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003470{
Paolo Bonzini13893092018-02-26 13:40:09 +01003471 if (!nested) {
3472 memset(msrs, 0, sizeof(*msrs));
3473 return;
3474 }
3475
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003476 /*
3477 * Note that as a general rule, the high half of the MSRs (bits in
3478 * the control fields which may be 1) should be initialized by the
3479 * intersection of the underlying hardware's MSR (i.e., features which
3480 * can be supported) and the list of features we want to expose -
3481 * because they are known to be properly supported in our code.
3482 * Also, usually, the low half of the MSRs (bits which must be 1) can
3483 * be set to 0, meaning that L1 may turn off any of these bits. The
3484 * reason is that if one of these bits is necessary, it will appear
3485 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3486 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003487 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003488 * These rules have exceptions below.
3489 */
3490
3491 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003492 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003493 msrs->pinbased_ctls_low,
3494 msrs->pinbased_ctls_high);
3495 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003496 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003497 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003498 PIN_BASED_EXT_INTR_MASK |
3499 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003500 PIN_BASED_VIRTUAL_NMIS |
3501 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003502 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003503 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003504 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003505
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003506 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003507 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003508 msrs->exit_ctls_low,
3509 msrs->exit_ctls_high);
3510 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003511 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003512
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003513 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003515 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003516#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01003517 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003518 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003519 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003520 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003521 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3522
Jan Kiszka2996fca2014-06-16 13:59:43 +02003523 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003525
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003526 /* entry controls */
3527 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003528 msrs->entry_ctls_low,
3529 msrs->entry_ctls_high);
3530 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003531 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003532 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003533#ifdef CONFIG_X86_64
3534 VM_ENTRY_IA32E_MODE |
3535#endif
3536 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003537 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003538 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003539
Jan Kiszka2996fca2014-06-16 13:59:43 +02003540 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003541 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003542
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003543 /* cpu-based controls */
3544 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003545 msrs->procbased_ctls_low,
3546 msrs->procbased_ctls_high);
3547 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003548 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003549 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003550 CPU_BASED_VIRTUAL_INTR_PENDING |
3551 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003552 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3553 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3554 CPU_BASED_CR3_STORE_EXITING |
3555#ifdef CONFIG_X86_64
3556 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3557#endif
3558 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003559 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3560 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3561 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3562 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003563 /*
3564 * We can allow some features even when not supported by the
3565 * hardware. For example, L1 can specify an MSR bitmap - and we
3566 * can use it to avoid exits to L1 - even when L0 runs L2
3567 * without MSR bitmaps.
3568 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003569 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003570 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003571 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003572
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003573 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003574 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003575 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3576
Paolo Bonzini80154d72017-08-24 13:55:35 +02003577 /*
3578 * secondary cpu-based controls. Do not include those that
3579 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3580 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003581 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003582 msrs->secondary_ctls_low,
3583 msrs->secondary_ctls_high);
3584 msrs->secondary_ctls_low = 0;
3585 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003586 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003587 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003588 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003589 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003590 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003591
Liran Alon32c7acf2018-06-23 02:35:11 +03003592 /*
3593 * We can emulate "VMCS shadowing," even if the hardware
3594 * doesn't support it.
3595 */
3596 msrs->secondary_ctls_high |=
3597 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003598
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003599 if (enable_ept) {
3600 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003601 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003602 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003603 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003604 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003605 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003606 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003607 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003608 msrs->ept_caps &= vmx_capability.ept;
3609 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003610 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3611 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003612 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003613 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003614 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003615 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003616 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003617 }
Nadav Har'Elafa61f72013-08-07 14:59:22 +02003618
Bandan Das27c42a12017-08-03 15:54:42 -04003619 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003620 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003621 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003622 /*
3623 * Advertise EPTP switching unconditionally
3624 * since we emulate it
3625 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003626 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003627 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003628 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003629 }
3630
Paolo Bonzinief697a72016-03-18 16:58:38 +01003631 /*
3632 * Old versions of KVM use the single-context version without
3633 * checking for support, so declare that it is supported even
3634 * though it is treated as global context. The alternative is
3635 * not failing the single-context invvpid, and it is worse.
3636 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003637 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003639 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003640 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003641 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003642 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003643
Radim Krčmář0790ec12015-03-17 14:02:32 +01003644 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003645 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003646 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3647
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003648 if (flexpriority_enabled)
3649 msrs->secondary_ctls_high |=
3650 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3651
Jan Kiszkac18911a2013-03-13 16:06:41 +01003652 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003653 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003654 msrs->misc_low,
3655 msrs->misc_high);
3656 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3657 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003658 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003659 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf4124502014-03-07 20:03:13 +01003660 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003661 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003662
3663 /*
3664 * This MSR reports some information about VMX support. We
3665 * should return information about the VMX we emulate for the
3666 * guest, and the VMCS structure we give it - not about the
3667 * VMX support of the underlying hardware.
3668 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003669 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003670 VMCS12_REVISION |
3671 VMX_BASIC_TRUE_CTLS |
3672 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3673 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3674
3675 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003676 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003677
3678 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003679 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003680 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3681 * We picked the standard core2 setting.
3682 */
3683#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3684#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003685 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3686 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003687
3688 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003689 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3690 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003691
3692 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003693 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003694}
3695
David Matlack38991522016-11-29 18:14:08 -08003696/*
3697 * if fixed0[i] == 1: val[i] must be 1
3698 * if fixed1[i] == 0: val[i] must be 0
3699 */
3700static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3701{
3702 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003703}
3704
3705static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3706{
David Matlack38991522016-11-29 18:14:08 -08003707 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003708}
3709
3710static inline u64 vmx_control_msr(u32 low, u32 high)
3711{
3712 return low | ((u64)high << 32);
3713}
3714
David Matlack62cc6b9d2016-11-29 18:14:07 -08003715static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3716{
3717 superset &= mask;
3718 subset &= mask;
3719
3720 return (superset | subset) == superset;
3721}
3722
3723static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3724{
3725 const u64 feature_and_reserved =
3726 /* feature (except bit 48; see below) */
3727 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3728 /* reserved */
3729 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003730 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003731
3732 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3733 return -EINVAL;
3734
3735 /*
3736 * KVM does not emulate a version of VMX that constrains physical
3737 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3738 */
3739 if (data & BIT_ULL(48))
3740 return -EINVAL;
3741
3742 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3743 vmx_basic_vmcs_revision_id(data))
3744 return -EINVAL;
3745
3746 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3747 return -EINVAL;
3748
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003749 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003750 return 0;
3751}
3752
3753static int
3754vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3755{
3756 u64 supported;
3757 u32 *lowp, *highp;
3758
3759 switch (msr_index) {
3760 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003761 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3762 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003763 break;
3764 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003765 lowp = &vmx->nested.msrs.procbased_ctls_low;
3766 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003767 break;
3768 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003769 lowp = &vmx->nested.msrs.exit_ctls_low;
3770 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003771 break;
3772 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003773 lowp = &vmx->nested.msrs.entry_ctls_low;
3774 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003775 break;
3776 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003777 lowp = &vmx->nested.msrs.secondary_ctls_low;
3778 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003779 break;
3780 default:
3781 BUG();
3782 }
3783
3784 supported = vmx_control_msr(*lowp, *highp);
3785
3786 /* Check must-be-1 bits are still 1. */
3787 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3788 return -EINVAL;
3789
3790 /* Check must-be-0 bits are still 0. */
3791 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3792 return -EINVAL;
3793
3794 *lowp = data;
3795 *highp = data >> 32;
3796 return 0;
3797}
3798
3799static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3800{
3801 const u64 feature_and_reserved_bits =
3802 /* feature */
3803 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3804 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3805 /* reserved */
3806 GENMASK_ULL(13, 9) | BIT_ULL(31);
3807 u64 vmx_misc;
3808
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003809 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3810 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003811
3812 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3813 return -EINVAL;
3814
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003815 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003816 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3817 vmx_misc_preemption_timer_rate(data) !=
3818 vmx_misc_preemption_timer_rate(vmx_misc))
3819 return -EINVAL;
3820
3821 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3822 return -EINVAL;
3823
3824 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3825 return -EINVAL;
3826
3827 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3828 return -EINVAL;
3829
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003830 vmx->nested.msrs.misc_low = data;
3831 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003832
3833 /*
3834 * If L1 has read-only VM-exit information fields, use the
3835 * less permissive vmx_vmwrite_bitmap to specify write
3836 * permissions for the shadow VMCS.
3837 */
3838 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3839 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3840
David Matlack62cc6b9d2016-11-29 18:14:07 -08003841 return 0;
3842}
3843
3844static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3845{
3846 u64 vmx_ept_vpid_cap;
3847
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003848 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3849 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003850
3851 /* Every bit is either reserved or a feature bit. */
3852 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3853 return -EINVAL;
3854
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003855 vmx->nested.msrs.ept_caps = data;
3856 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003857 return 0;
3858}
3859
3860static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3861{
3862 u64 *msr;
3863
3864 switch (msr_index) {
3865 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003866 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003867 break;
3868 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003869 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003870 break;
3871 default:
3872 BUG();
3873 }
3874
3875 /*
3876 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3877 * must be 1 in the restored value.
3878 */
3879 if (!is_bitwise_subset(data, *msr, -1ULL))
3880 return -EINVAL;
3881
3882 *msr = data;
3883 return 0;
3884}
3885
3886/*
3887 * Called when userspace is restoring VMX MSRs.
3888 *
3889 * Returns 0 on success, non-0 otherwise.
3890 */
3891static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3892{
3893 struct vcpu_vmx *vmx = to_vmx(vcpu);
3894
Jim Mattsona943ac52018-05-29 09:11:32 -07003895 /*
3896 * Don't allow changes to the VMX capability MSRs while the vCPU
3897 * is in VMX operation.
3898 */
3899 if (vmx->nested.vmxon)
3900 return -EBUSY;
3901
David Matlack62cc6b9d2016-11-29 18:14:07 -08003902 switch (msr_index) {
3903 case MSR_IA32_VMX_BASIC:
3904 return vmx_restore_vmx_basic(vmx, data);
3905 case MSR_IA32_VMX_PINBASED_CTLS:
3906 case MSR_IA32_VMX_PROCBASED_CTLS:
3907 case MSR_IA32_VMX_EXIT_CTLS:
3908 case MSR_IA32_VMX_ENTRY_CTLS:
3909 /*
3910 * The "non-true" VMX capability MSRs are generated from the
3911 * "true" MSRs, so we do not support restoring them directly.
3912 *
3913 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3914 * should restore the "true" MSRs with the must-be-1 bits
3915 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3916 * DEFAULT SETTINGS".
3917 */
3918 return -EINVAL;
3919 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3920 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3921 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3922 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3923 case MSR_IA32_VMX_PROCBASED_CTLS2:
3924 return vmx_restore_control_msr(vmx, msr_index, data);
3925 case MSR_IA32_VMX_MISC:
3926 return vmx_restore_vmx_misc(vmx, data);
3927 case MSR_IA32_VMX_CR0_FIXED0:
3928 case MSR_IA32_VMX_CR4_FIXED0:
3929 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3930 case MSR_IA32_VMX_CR0_FIXED1:
3931 case MSR_IA32_VMX_CR4_FIXED1:
3932 /*
3933 * These MSRs are generated based on the vCPU's CPUID, so we
3934 * do not support restoring them directly.
3935 */
3936 return -EINVAL;
3937 case MSR_IA32_VMX_EPT_VPID_CAP:
3938 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3939 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003940 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003941 return 0;
3942 default:
3943 /*
3944 * The rest of the VMX capability MSRs do not support restore.
3945 */
3946 return -EINVAL;
3947 }
3948}
3949
Jan Kiszkacae50132014-01-04 18:47:22 +01003950/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003951static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003952{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003953 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003954 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003955 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003956 break;
3957 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3958 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003959 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003960 msrs->pinbased_ctls_low,
3961 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003962 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3963 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003964 break;
3965 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3966 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003967 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003968 msrs->procbased_ctls_low,
3969 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003970 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3971 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003972 break;
3973 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3974 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003975 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003976 msrs->exit_ctls_low,
3977 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003978 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3979 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003980 break;
3981 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3982 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003983 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003984 msrs->entry_ctls_low,
3985 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003986 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3987 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003988 break;
3989 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003990 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003991 msrs->misc_low,
3992 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003993 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003995 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003996 break;
3997 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003998 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
4000 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004010 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004011 msrs->secondary_ctls_low,
4012 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004013 break;
4014 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004015 *pdata = msrs->ept_caps |
4016 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004017 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004018 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004019 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004020 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004021 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004022 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004023 }
4024
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004025 return 0;
4026}
4027
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004028static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4029 uint64_t val)
4030{
4031 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4032
4033 return !(val & ~valid_bits);
4034}
4035
Tom Lendacky801e4592018-02-21 13:39:51 -06004036static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4037{
Paolo Bonzini13893092018-02-26 13:40:09 +01004038 switch (msr->index) {
4039 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4040 if (!nested)
4041 return 1;
4042 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4043 default:
4044 return 1;
4045 }
4046
4047 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004048}
4049
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004050/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004051 * Reads an msr value (of 'msr_index') into 'pdata'.
4052 * Returns 0 on success, non-0 otherwise.
4053 * Assumes vcpu_load() was already called.
4054 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004055static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004057 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004058 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004060 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004061#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004063 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004064 break;
4065 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004067 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004068 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004069 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004070 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004071#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004073 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004074 case MSR_IA32_SPEC_CTRL:
4075 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004076 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4077 return 1;
4078
4079 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4080 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004081 case MSR_IA32_ARCH_CAPABILITIES:
4082 if (!msr_info->host_initiated &&
4083 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4084 return 1;
4085 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4086 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004088 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 break;
4090 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004091 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092 break;
4093 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004094 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004096 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004097 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004098 (!msr_info->host_initiated &&
4099 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004100 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004101 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004102 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004103 case MSR_IA32_MCG_EXT_CTL:
4104 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004105 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004106 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004107 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004108 msr_info->data = vcpu->arch.mcg_ext_ctl;
4109 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004110 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004111 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004112 break;
4113 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4114 if (!nested_vmx_allowed(vcpu))
4115 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004116 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4117 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004118 case MSR_IA32_XSS:
4119 if (!vmx_xsaves_supported())
4120 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004121 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004122 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004123 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004124 if (!msr_info->host_initiated &&
4125 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004126 return 1;
4127 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004129 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004130 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004131 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004132 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004134 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135 }
4136
Avi Kivity6aa8b732006-12-10 02:21:36 -08004137 return 0;
4138}
4139
Jan Kiszkacae50132014-01-04 18:47:22 +01004140static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4141
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142/*
4143 * Writes msr value into into the appropriate "register".
4144 * Returns 0 on success, non-0 otherwise.
4145 * Assumes vcpu_load() was already called.
4146 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004147static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004149 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004150 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004151 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004152 u32 msr_index = msr_info->index;
4153 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004154
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004156 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004157 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004158 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004159#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004161 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004162 vmcs_writel(GUEST_FS_BASE, data);
4163 break;
4164 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004165 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 vmcs_writel(GUEST_GS_BASE, data);
4167 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004168 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004169 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004170 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171#endif
4172 case MSR_IA32_SYSENTER_CS:
4173 vmcs_write32(GUEST_SYSENTER_CS, data);
4174 break;
4175 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004176 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177 break;
4178 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004179 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004180 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004181 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004182 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004183 (!msr_info->host_initiated &&
4184 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004185 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004186 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004187 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004189 vmcs_write64(GUEST_BNDCFGS, data);
4190 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004191 case MSR_IA32_SPEC_CTRL:
4192 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004193 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4194 return 1;
4195
4196 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004197 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004198 return 1;
4199
4200 vmx->spec_ctrl = data;
4201
4202 if (!data)
4203 break;
4204
4205 /*
4206 * For non-nested:
4207 * When it's written (to non-zero) for the first time, pass
4208 * it through.
4209 *
4210 * For nested:
4211 * The handling of the MSR bitmap for L2 guests is done in
4212 * nested_vmx_merge_msr_bitmap. We should not touch the
4213 * vmcs02.msr_bitmap here since it gets completely overwritten
4214 * in the merging. We update the vmcs01 here for L1 as well
4215 * since it will end up touching the MSR anyway now.
4216 */
4217 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4218 MSR_IA32_SPEC_CTRL,
4219 MSR_TYPE_RW);
4220 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004221 case MSR_IA32_PRED_CMD:
4222 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004223 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4224 return 1;
4225
4226 if (data & ~PRED_CMD_IBPB)
4227 return 1;
4228
4229 if (!data)
4230 break;
4231
4232 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4233
4234 /*
4235 * For non-nested:
4236 * When it's written (to non-zero) for the first time, pass
4237 * it through.
4238 *
4239 * For nested:
4240 * The handling of the MSR bitmap for L2 guests is done in
4241 * nested_vmx_merge_msr_bitmap. We should not touch the
4242 * vmcs02.msr_bitmap here since it gets completely overwritten
4243 * in the merging.
4244 */
4245 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4246 MSR_TYPE_W);
4247 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004248 case MSR_IA32_ARCH_CAPABILITIES:
4249 if (!msr_info->host_initiated)
4250 return 1;
4251 vmx->arch_capabilities = data;
4252 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004253 case MSR_IA32_CR_PAT:
4254 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004255 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4256 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004257 vmcs_write64(GUEST_IA32_PAT, data);
4258 vcpu->arch.pat = data;
4259 break;
4260 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004261 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004262 break;
Will Auldba904632012-11-29 12:42:50 -08004263 case MSR_IA32_TSC_ADJUST:
4264 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004265 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004266 case MSR_IA32_MCG_EXT_CTL:
4267 if ((!msr_info->host_initiated &&
4268 !(to_vmx(vcpu)->msr_ia32_feature_control &
4269 FEATURE_CONTROL_LMCE)) ||
4270 (data & ~MCG_EXT_CTL_LMCE_EN))
4271 return 1;
4272 vcpu->arch.mcg_ext_ctl = data;
4273 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004274 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004275 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004276 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004277 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4278 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004279 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004280 if (msr_info->host_initiated && data == 0)
4281 vmx_leave_nested(vcpu);
4282 break;
4283 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004284 if (!msr_info->host_initiated)
4285 return 1; /* they are read-only */
4286 if (!nested_vmx_allowed(vcpu))
4287 return 1;
4288 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004289 case MSR_IA32_XSS:
4290 if (!vmx_xsaves_supported())
4291 return 1;
4292 /*
4293 * The only supported bit as of Skylake is bit 8, but
4294 * it is not supported on KVM.
4295 */
4296 if (data != 0)
4297 return 1;
4298 vcpu->arch.ia32_xss = data;
4299 if (vcpu->arch.ia32_xss != host_xss)
4300 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004301 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004302 else
4303 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4304 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004305 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004306 if (!msr_info->host_initiated &&
4307 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004308 return 1;
4309 /* Check reserved bit, higher 32 bits should be zero */
4310 if ((data >> 32) != 0)
4311 return 1;
4312 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004314 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004315 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004316 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004317 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004318 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4319 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004320 ret = kvm_set_shared_msr(msr->index, msr->data,
4321 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004322 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004323 if (ret)
4324 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004325 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004326 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004328 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 }
4330
Eddie Dong2cc51562007-05-21 07:28:09 +03004331 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004332}
4333
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004334static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004336 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4337 switch (reg) {
4338 case VCPU_REGS_RSP:
4339 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4340 break;
4341 case VCPU_REGS_RIP:
4342 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4343 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004344 case VCPU_EXREG_PDPTR:
4345 if (enable_ept)
4346 ept_save_pdptrs(vcpu);
4347 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004348 default:
4349 break;
4350 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351}
4352
Avi Kivity6aa8b732006-12-10 02:21:36 -08004353static __init int cpu_has_kvm_support(void)
4354{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004355 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004356}
4357
4358static __init int vmx_disabled_by_bios(void)
4359{
4360 u64 msr;
4361
4362 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004363 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004364 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004365 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4366 && tboot_enabled())
4367 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004368 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004369 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004370 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004371 && !tboot_enabled()) {
4372 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004373 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004374 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004375 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004376 /* launched w/o TXT and VMX disabled */
4377 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4378 && !tboot_enabled())
4379 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004380 }
4381
4382 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383}
4384
Dongxiao Xu7725b892010-05-11 18:29:38 +08004385static void kvm_cpu_vmxon(u64 addr)
4386{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004387 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004388 intel_pt_handle_vmx(1);
4389
Dongxiao Xu7725b892010-05-11 18:29:38 +08004390 asm volatile (ASM_VMX_VMXON_RAX
4391 : : "a"(&addr), "m"(addr)
4392 : "memory", "cc");
4393}
4394
Radim Krčmář13a34e02014-08-28 15:13:03 +02004395static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396{
4397 int cpu = raw_smp_processor_id();
4398 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004399 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004401 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004402 return -EBUSY;
4403
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004404 /*
4405 * This can happen if we hot-added a CPU but failed to allocate
4406 * VP assist page for it.
4407 */
4408 if (static_branch_unlikely(&enable_evmcs) &&
4409 !hv_get_vp_assist_page(cpu))
4410 return -EFAULT;
4411
Nadav Har'Eld462b812011-05-24 15:26:10 +03004412 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004413 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4414 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004415
4416 /*
4417 * Now we can enable the vmclear operation in kdump
4418 * since the loaded_vmcss_on_cpu list on this cpu
4419 * has been initialized.
4420 *
4421 * Though the cpu is not in VMX operation now, there
4422 * is no problem to enable the vmclear operation
4423 * for the loaded_vmcss_on_cpu list is empty!
4424 */
4425 crash_enable_local_vmclear(cpu);
4426
Avi Kivity6aa8b732006-12-10 02:21:36 -08004427 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004428
4429 test_bits = FEATURE_CONTROL_LOCKED;
4430 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4431 if (tboot_enabled())
4432 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4433
4434 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004435 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004436 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4437 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004438 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004439 if (enable_ept)
4440 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004441
4442 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443}
4444
Nadav Har'Eld462b812011-05-24 15:26:10 +03004445static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004446{
4447 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004448 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004449
Nadav Har'Eld462b812011-05-24 15:26:10 +03004450 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4451 loaded_vmcss_on_cpu_link)
4452 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004453}
4454
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004455
4456/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4457 * tricks.
4458 */
4459static void kvm_cpu_vmxoff(void)
4460{
4461 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004462
4463 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004464 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004465}
4466
Radim Krčmář13a34e02014-08-28 15:13:03 +02004467static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004468{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004469 vmclear_local_loaded_vmcss();
4470 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471}
4472
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004473static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004474 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475{
4476 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004477 u32 ctl = ctl_min | ctl_opt;
4478
4479 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4480
4481 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4482 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4483
4484 /* Ensure minimum (required) set of control bits are supported. */
4485 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004486 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004487
4488 *result = ctl;
4489 return 0;
4490}
4491
Avi Kivity110312c2010-12-21 12:54:20 +02004492static __init bool allow_1_setting(u32 msr, u32 ctl)
4493{
4494 u32 vmx_msr_low, vmx_msr_high;
4495
4496 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4497 return vmx_msr_high & ctl;
4498}
4499
Yang, Sheng002c7f72007-07-31 14:23:01 +03004500static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004501{
4502 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004503 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004504 u32 _pin_based_exec_control = 0;
4505 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004506 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004507 u32 _vmexit_control = 0;
4508 u32 _vmentry_control = 0;
4509
Paolo Bonzini13893092018-02-26 13:40:09 +01004510 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304511 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004512#ifdef CONFIG_X86_64
4513 CPU_BASED_CR8_LOAD_EXITING |
4514 CPU_BASED_CR8_STORE_EXITING |
4515#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004516 CPU_BASED_CR3_LOAD_EXITING |
4517 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e22017-12-12 16:44:21 +08004518 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004519 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004520 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004521 CPU_BASED_MWAIT_EXITING |
4522 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004523 CPU_BASED_INVLPG_EXITING |
4524 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004525
Sheng Yangf78e0e22007-10-29 09:40:42 +08004526 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004527 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004528 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004529 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4530 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004531 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004532#ifdef CONFIG_X86_64
4533 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4534 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4535 ~CPU_BASED_CR8_STORE_EXITING;
4536#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004538 min2 = 0;
4539 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004540 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004541 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004542 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004543 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004544 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004545 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004546 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004547 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004548 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004549 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004550 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004551 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004552 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004553 SECONDARY_EXEC_RDSEED_EXITING |
4554 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004555 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004556 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004557 SECONDARY_EXEC_ENABLE_VMFUNC |
4558 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004559 if (adjust_vmx_controls(min2, opt2,
4560 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004561 &_cpu_based_2nd_exec_control) < 0)
4562 return -EIO;
4563 }
4564#ifndef CONFIG_X86_64
4565 if (!(_cpu_based_2nd_exec_control &
4566 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4567 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4568#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004569
4570 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4571 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004572 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004573 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4574 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004575
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004576 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4577 &vmx_capability.ept, &vmx_capability.vpid);
4578
Sheng Yangd56f5462008-04-25 10:13:16 +08004579 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004580 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4581 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004582 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4583 CPU_BASED_CR3_STORE_EXITING |
4584 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004585 } else if (vmx_capability.ept) {
4586 vmx_capability.ept = 0;
4587 pr_warn_once("EPT CAP should not exist if not support "
4588 "1-setting enable EPT VM-execution control\n");
4589 }
4590 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4591 vmx_capability.vpid) {
4592 vmx_capability.vpid = 0;
4593 pr_warn_once("VPID CAP should not exist if not support "
4594 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004595 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004596
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004597 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004598#ifdef CONFIG_X86_64
4599 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4600#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004601 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004602 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004603 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4604 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004605 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004606
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004607 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4608 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4609 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004610 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4611 &_pin_based_exec_control) < 0)
4612 return -EIO;
4613
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004614 if (cpu_has_broken_vmx_preemption_timer())
4615 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004616 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004617 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004618 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4619
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004620 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004621 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004622 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4623 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004624 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004625
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004626 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004627
4628 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4629 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004630 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004631
4632#ifdef CONFIG_X86_64
4633 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4634 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004635 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004636#endif
4637
4638 /* Require Write-Back (WB) memory type for VMCS accesses. */
4639 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004640 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004641
Yang, Sheng002c7f72007-07-31 14:23:01 +03004642 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004643 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004644 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004645
Liran Alon2307af12018-06-29 22:59:04 +03004646 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004647
Yang, Sheng002c7f72007-07-31 14:23:01 +03004648 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4649 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004650 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004651 vmcs_conf->vmexit_ctrl = _vmexit_control;
4652 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004653
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004654 if (static_branch_unlikely(&enable_evmcs))
4655 evmcs_sanitize_exec_ctrls(vmcs_conf);
4656
Avi Kivity110312c2010-12-21 12:54:20 +02004657 cpu_has_load_ia32_efer =
4658 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4659 VM_ENTRY_LOAD_IA32_EFER)
4660 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4661 VM_EXIT_LOAD_IA32_EFER);
4662
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004663 cpu_has_load_perf_global_ctrl =
4664 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4665 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4666 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4667 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4668
4669 /*
4670 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004671 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004672 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4673 *
4674 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4675 *
4676 * AAK155 (model 26)
4677 * AAP115 (model 30)
4678 * AAT100 (model 37)
4679 * BC86,AAY89,BD102 (model 44)
4680 * BA97 (model 46)
4681 *
4682 */
4683 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4684 switch (boot_cpu_data.x86_model) {
4685 case 26:
4686 case 30:
4687 case 37:
4688 case 44:
4689 case 46:
4690 cpu_has_load_perf_global_ctrl = false;
4691 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4692 "does not work properly. Using workaround\n");
4693 break;
4694 default:
4695 break;
4696 }
4697 }
4698
Borislav Petkov782511b2016-04-04 22:25:03 +02004699 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004700 rdmsrl(MSR_IA32_XSS, host_xss);
4701
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004702 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004703}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704
Liran Alon491a6032018-06-23 02:35:12 +03004705static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706{
4707 int node = cpu_to_node(cpu);
4708 struct page *pages;
4709 struct vmcs *vmcs;
4710
Vlastimil Babka96db8002015-09-08 15:03:50 -07004711 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712 if (!pages)
4713 return NULL;
4714 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004715 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004716
4717 /* KVM supports Enlightened VMCS v1 only */
4718 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004719 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004720 else
Liran Alon392b2f22018-06-23 02:35:01 +03004721 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004722
Liran Alon491a6032018-06-23 02:35:12 +03004723 if (shadow)
4724 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004725 return vmcs;
4726}
4727
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728static void free_vmcs(struct vmcs *vmcs)
4729{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004730 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731}
4732
Nadav Har'Eld462b812011-05-24 15:26:10 +03004733/*
4734 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4735 */
4736static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4737{
4738 if (!loaded_vmcs->vmcs)
4739 return;
4740 loaded_vmcs_clear(loaded_vmcs);
4741 free_vmcs(loaded_vmcs->vmcs);
4742 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004743 if (loaded_vmcs->msr_bitmap)
4744 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004745 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004746}
4747
Liran Alon491a6032018-06-23 02:35:12 +03004748static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004749{
Liran Alon491a6032018-06-23 02:35:12 +03004750 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004751}
4752
4753static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4754{
Liran Alon491a6032018-06-23 02:35:12 +03004755 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004756 if (!loaded_vmcs->vmcs)
4757 return -ENOMEM;
4758
4759 loaded_vmcs->shadow_vmcs = NULL;
4760 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004761
4762 if (cpu_has_vmx_msr_bitmap()) {
4763 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4764 if (!loaded_vmcs->msr_bitmap)
4765 goto out_vmcs;
4766 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004767
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004768 if (IS_ENABLED(CONFIG_HYPERV) &&
4769 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004770 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4771 struct hv_enlightened_vmcs *evmcs =
4772 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4773
4774 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4775 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004776 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004777
4778 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4779
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004780 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004781
4782out_vmcs:
4783 free_loaded_vmcs(loaded_vmcs);
4784 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004785}
4786
Sam Ravnborg39959582007-06-01 00:47:13 -07004787static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004788{
4789 int cpu;
4790
Zachary Amsden3230bb42009-09-29 11:38:37 -10004791 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004792 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004793 per_cpu(vmxarea, cpu) = NULL;
4794 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795}
4796
Jim Mattsond37f4262017-12-22 12:12:16 -08004797enum vmcs_field_width {
4798 VMCS_FIELD_WIDTH_U16 = 0,
4799 VMCS_FIELD_WIDTH_U64 = 1,
4800 VMCS_FIELD_WIDTH_U32 = 2,
4801 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004802};
4803
Jim Mattsond37f4262017-12-22 12:12:16 -08004804static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004805{
4806 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004807 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004808 return (field >> 13) & 0x3 ;
4809}
4810
4811static inline int vmcs_field_readonly(unsigned long field)
4812{
4813 return (((field >> 10) & 0x3) == 1);
4814}
4815
Bandan Dasfe2b2012014-04-21 15:20:14 -04004816static void init_vmcs_shadow_fields(void)
4817{
4818 int i, j;
4819
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004820 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4821 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004822 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004823 (i + 1 == max_shadow_read_only_fields ||
4824 shadow_read_only_fields[i + 1] != field + 1))
4825 pr_err("Missing field from shadow_read_only_field %x\n",
4826 field + 1);
4827
4828 clear_bit(field, vmx_vmread_bitmap);
4829#ifdef CONFIG_X86_64
4830 if (field & 1)
4831 continue;
4832#endif
4833 if (j < i)
4834 shadow_read_only_fields[j] = field;
4835 j++;
4836 }
4837 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004838
4839 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004840 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004841 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004842 (i + 1 == max_shadow_read_write_fields ||
4843 shadow_read_write_fields[i + 1] != field + 1))
4844 pr_err("Missing field from shadow_read_write_field %x\n",
4845 field + 1);
4846
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004847 /*
4848 * PML and the preemption timer can be emulated, but the
4849 * processor cannot vmwrite to fields that don't exist
4850 * on bare metal.
4851 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004852 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004853 case GUEST_PML_INDEX:
4854 if (!cpu_has_vmx_pml())
4855 continue;
4856 break;
4857 case VMX_PREEMPTION_TIMER_VALUE:
4858 if (!cpu_has_vmx_preemption_timer())
4859 continue;
4860 break;
4861 case GUEST_INTR_STATUS:
4862 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004863 continue;
4864 break;
4865 default:
4866 break;
4867 }
4868
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004869 clear_bit(field, vmx_vmwrite_bitmap);
4870 clear_bit(field, vmx_vmread_bitmap);
4871#ifdef CONFIG_X86_64
4872 if (field & 1)
4873 continue;
4874#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004875 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004876 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004877 j++;
4878 }
4879 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004880}
4881
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882static __init int alloc_kvm_area(void)
4883{
4884 int cpu;
4885
Zachary Amsden3230bb42009-09-29 11:38:37 -10004886 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004887 struct vmcs *vmcs;
4888
Liran Alon491a6032018-06-23 02:35:12 +03004889 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004890 if (!vmcs) {
4891 free_kvm_area();
4892 return -ENOMEM;
4893 }
4894
Liran Alon2307af12018-06-29 22:59:04 +03004895 /*
4896 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4897 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4898 * revision_id reported by MSR_IA32_VMX_BASIC.
4899 *
4900 * However, even though not explictly documented by
4901 * TLFS, VMXArea passed as VMXON argument should
4902 * still be marked with revision_id reported by
4903 * physical CPU.
4904 */
4905 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004906 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004907
Avi Kivity6aa8b732006-12-10 02:21:36 -08004908 per_cpu(vmxarea, cpu) = vmcs;
4909 }
4910 return 0;
4911}
4912
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004913static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004914 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004915{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004916 if (!emulate_invalid_guest_state) {
4917 /*
4918 * CS and SS RPL should be equal during guest entry according
4919 * to VMX spec, but in reality it is not always so. Since vcpu
4920 * is in the middle of the transition from real mode to
4921 * protected mode it is safe to assume that RPL 0 is a good
4922 * default value.
4923 */
4924 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004925 save->selector &= ~SEGMENT_RPL_MASK;
4926 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004927 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004928 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004929 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004930}
4931
4932static void enter_pmode(struct kvm_vcpu *vcpu)
4933{
4934 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004935 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936
Gleb Natapovd99e4152012-12-20 16:57:45 +02004937 /*
4938 * Update real mode segment cache. It may be not up-to-date if sement
4939 * register was written while vcpu was in a guest mode.
4940 */
4941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4945 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4946 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4947
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004948 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004949
Avi Kivity2fb92db2011-04-27 19:42:18 +03004950 vmx_segment_cache_clear(vmx);
4951
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004952 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953
4954 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004955 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4956 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957 vmcs_writel(GUEST_RFLAGS, flags);
4958
Rusty Russell66aee912007-07-17 23:34:16 +10004959 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4960 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004961
4962 update_exception_bitmap(vcpu);
4963
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004964 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4965 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4966 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4967 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4968 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4969 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004970}
4971
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004972static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004973{
Mathias Krause772e0312012-08-30 01:30:19 +02004974 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004975 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004976
Gleb Natapovd99e4152012-12-20 16:57:45 +02004977 var.dpl = 0x3;
4978 if (seg == VCPU_SREG_CS)
4979 var.type = 0x3;
4980
4981 if (!emulate_invalid_guest_state) {
4982 var.selector = var.base >> 4;
4983 var.base = var.base & 0xffff0;
4984 var.limit = 0xffff;
4985 var.g = 0;
4986 var.db = 0;
4987 var.present = 1;
4988 var.s = 1;
4989 var.l = 0;
4990 var.unusable = 0;
4991 var.type = 0x3;
4992 var.avl = 0;
4993 if (save->base & 0xf)
4994 printk_once(KERN_WARNING "kvm: segment base is not "
4995 "paragraph aligned when entering "
4996 "protected mode (seg=%d)", seg);
4997 }
4998
4999 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005000 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005001 vmcs_write32(sf->limit, var.limit);
5002 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005003}
5004
5005static void enter_rmode(struct kvm_vcpu *vcpu)
5006{
5007 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005008 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005009 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005011 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005016 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005018
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005019 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005020
Gleb Natapov776e58e2011-03-13 12:34:27 +02005021 /*
5022 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005023 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005024 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005025 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005026 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5027 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005028
Avi Kivity2fb92db2011-04-27 19:42:18 +03005029 vmx_segment_cache_clear(vmx);
5030
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005031 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005033 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5034
5035 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005036 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005038 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039
5040 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005041 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042 update_exception_bitmap(vcpu);
5043
Gleb Natapovd99e4152012-12-20 16:57:45 +02005044 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5045 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5046 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5047 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5048 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5049 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005050
Eddie Dong8668a3c2007-10-10 14:26:45 +08005051 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005052}
5053
Amit Shah401d10d2009-02-20 22:53:37 +05305054static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5055{
5056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005057 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5058
5059 if (!msr)
5060 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305061
Avi Kivityf6801df2010-01-21 15:31:50 +02005062 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305063 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005064 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305065 msr->data = efer;
5066 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005067 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305068
5069 msr->data = efer & ~EFER_LME;
5070 }
5071 setup_msrs(vmx);
5072}
5073
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005074#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075
5076static void enter_lmode(struct kvm_vcpu *vcpu)
5077{
5078 u32 guest_tr_ar;
5079
Avi Kivity2fb92db2011-04-27 19:42:18 +03005080 vmx_segment_cache_clear(to_vmx(vcpu));
5081
Avi Kivity6aa8b732006-12-10 02:21:36 -08005082 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005083 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005084 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5085 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005086 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005087 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5088 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089 }
Avi Kivityda38f432010-07-06 11:30:49 +03005090 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091}
5092
5093static void exit_lmode(struct kvm_vcpu *vcpu)
5094{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005095 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005096 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005097}
5098
5099#endif
5100
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005101static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5102 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005103{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005104 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005105 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5106 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005107 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005108 } else {
5109 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005110 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005111}
5112
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005113static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005114{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005115 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005116}
5117
Junaid Shahidfaff8752018-06-29 13:10:05 -07005118static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5119{
5120 int vpid = to_vmx(vcpu)->vpid;
5121
5122 if (!vpid_sync_vcpu_addr(vpid, addr))
5123 vpid_sync_context(vpid);
5124
5125 /*
5126 * If VPIDs are not supported or enabled, then the above is a no-op.
5127 * But we don't really need a TLB flush in that case anyway, because
5128 * each VM entry/exit includes an implicit flush when VPID is 0.
5129 */
5130}
5131
Avi Kivitye8467fd2009-12-29 18:43:06 +02005132static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5133{
5134 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5135
5136 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5137 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5138}
5139
Avi Kivityaff48ba2010-12-05 18:56:11 +02005140static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5141{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005142 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005143 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5144 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5145}
5146
Anthony Liguori25c4c272007-04-27 09:29:21 +03005147static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005148{
Avi Kivityfc78f512009-12-07 12:16:48 +02005149 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5150
5151 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5152 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005153}
5154
Sheng Yang14394422008-04-28 12:24:45 +08005155static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5156{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005157 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5158
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005159 if (!test_bit(VCPU_EXREG_PDPTR,
5160 (unsigned long *)&vcpu->arch.regs_dirty))
5161 return;
5162
Sheng Yang14394422008-04-28 12:24:45 +08005163 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005164 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5165 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5166 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5167 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005168 }
5169}
5170
Avi Kivity8f5d5492009-05-31 18:41:29 +03005171static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5172{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005173 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5174
Avi Kivity8f5d5492009-05-31 18:41:29 +03005175 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005176 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5177 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5178 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5179 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005180 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005181
5182 __set_bit(VCPU_EXREG_PDPTR,
5183 (unsigned long *)&vcpu->arch.regs_avail);
5184 __set_bit(VCPU_EXREG_PDPTR,
5185 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005186}
5187
David Matlack38991522016-11-29 18:14:08 -08005188static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5189{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005190 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5191 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005192 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5193
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005194 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005195 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5196 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5197 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5198
5199 return fixed_bits_valid(val, fixed0, fixed1);
5200}
5201
5202static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5203{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005204 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5205 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005206
5207 return fixed_bits_valid(val, fixed0, fixed1);
5208}
5209
5210static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5211{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005212 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5213 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005214
5215 return fixed_bits_valid(val, fixed0, fixed1);
5216}
5217
5218/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5219#define nested_guest_cr4_valid nested_cr4_valid
5220#define nested_host_cr4_valid nested_cr4_valid
5221
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005222static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005223
5224static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5225 unsigned long cr0,
5226 struct kvm_vcpu *vcpu)
5227{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005228 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5229 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005230 if (!(cr0 & X86_CR0_PG)) {
5231 /* From paging/starting to nonpaging */
5232 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005233 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005234 (CPU_BASED_CR3_LOAD_EXITING |
5235 CPU_BASED_CR3_STORE_EXITING));
5236 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005237 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005238 } else if (!is_paging(vcpu)) {
5239 /* From nonpaging to paging */
5240 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005241 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005242 ~(CPU_BASED_CR3_LOAD_EXITING |
5243 CPU_BASED_CR3_STORE_EXITING));
5244 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005245 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005246 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005247
5248 if (!(cr0 & X86_CR0_WP))
5249 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005250}
5251
Avi Kivity6aa8b732006-12-10 02:21:36 -08005252static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5253{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005254 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005255 unsigned long hw_cr0;
5256
Sean Christopherson3de63472018-07-13 08:42:30 -07005257 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005258 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005259 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005260 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005261 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005262
Gleb Natapov218e7632013-01-21 15:36:45 +02005263 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5264 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005265
Gleb Natapov218e7632013-01-21 15:36:45 +02005266 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5267 enter_rmode(vcpu);
5268 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005269
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005270#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005271 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005272 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005274 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275 exit_lmode(vcpu);
5276 }
5277#endif
5278
Sean Christophersonb4d18512018-03-05 12:04:40 -08005279 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005280 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5281
Avi Kivity6aa8b732006-12-10 02:21:36 -08005282 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005283 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005284 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005285
5286 /* depends on vcpu->arch.cr0 to be set to a new value */
5287 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288}
5289
Yu Zhang855feb62017-08-24 20:27:55 +08005290static int get_ept_level(struct kvm_vcpu *vcpu)
5291{
5292 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5293 return 5;
5294 return 4;
5295}
5296
Peter Feiner995f00a2017-06-30 17:26:32 -07005297static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005298{
Yu Zhang855feb62017-08-24 20:27:55 +08005299 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005300
Yu Zhang855feb62017-08-24 20:27:55 +08005301 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005302
Peter Feiner995f00a2017-06-30 17:26:32 -07005303 if (enable_ept_ad_bits &&
5304 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005305 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005306 eptp |= (root_hpa & PAGE_MASK);
5307
5308 return eptp;
5309}
5310
Avi Kivity6aa8b732006-12-10 02:21:36 -08005311static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5312{
Tianyu Lan877ad952018-07-19 08:40:23 +00005313 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005314 unsigned long guest_cr3;
5315 u64 eptp;
5316
5317 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005318 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005319 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005320 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005321
5322 if (kvm_x86_ops->tlb_remote_flush) {
5323 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5324 to_vmx(vcpu)->ept_pointer = eptp;
5325 to_kvm_vmx(kvm)->ept_pointers_match
5326 = EPT_POINTERS_CHECK;
5327 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5328 }
5329
Sean Christophersone90008d2018-03-05 12:04:37 -08005330 if (enable_unrestricted_guest || is_paging(vcpu) ||
5331 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005332 guest_cr3 = kvm_read_cr3(vcpu);
5333 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005334 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005335 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005336 }
5337
Sheng Yang14394422008-04-28 12:24:45 +08005338 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005339}
5340
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005341static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005342{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005343 /*
5344 * Pass through host's Machine Check Enable value to hw_cr4, which
5345 * is in force while we are in guest mode. Do not let guests control
5346 * this bit, even if host CR4.MCE == 0.
5347 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005348 unsigned long hw_cr4;
5349
5350 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5351 if (enable_unrestricted_guest)
5352 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5353 else if (to_vmx(vcpu)->rmode.vm86_active)
5354 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5355 else
5356 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005357
Sean Christopherson64f7a112018-04-30 10:01:06 -07005358 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5359 if (cr4 & X86_CR4_UMIP) {
5360 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005361 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005362 hw_cr4 &= ~X86_CR4_UMIP;
5363 } else if (!is_guest_mode(vcpu) ||
5364 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5365 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5366 SECONDARY_EXEC_DESC);
5367 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005368
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005369 if (cr4 & X86_CR4_VMXE) {
5370 /*
5371 * To use VMXON (and later other VMX instructions), a guest
5372 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5373 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005374 * is here. We operate under the default treatment of SMM,
5375 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005376 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005377 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005378 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005379 }
David Matlack38991522016-11-29 18:14:08 -08005380
5381 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005382 return 1;
5383
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005384 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005385
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005386 if (!enable_unrestricted_guest) {
5387 if (enable_ept) {
5388 if (!is_paging(vcpu)) {
5389 hw_cr4 &= ~X86_CR4_PAE;
5390 hw_cr4 |= X86_CR4_PSE;
5391 } else if (!(cr4 & X86_CR4_PAE)) {
5392 hw_cr4 &= ~X86_CR4_PAE;
5393 }
5394 }
5395
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005396 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005397 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5398 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5399 * to be manually disabled when guest switches to non-paging
5400 * mode.
5401 *
5402 * If !enable_unrestricted_guest, the CPU is always running
5403 * with CR0.PG=1 and CR4 needs to be modified.
5404 * If enable_unrestricted_guest, the CPU automatically
5405 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005406 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005407 if (!is_paging(vcpu))
5408 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5409 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005410
Sheng Yang14394422008-04-28 12:24:45 +08005411 vmcs_writel(CR4_READ_SHADOW, cr4);
5412 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005413 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005414}
5415
Avi Kivity6aa8b732006-12-10 02:21:36 -08005416static void vmx_get_segment(struct kvm_vcpu *vcpu,
5417 struct kvm_segment *var, int seg)
5418{
Avi Kivitya9179492011-01-03 14:28:52 +02005419 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005420 u32 ar;
5421
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005422 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005423 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005424 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005425 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005426 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005427 var->base = vmx_read_guest_seg_base(vmx, seg);
5428 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5429 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005430 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005431 var->base = vmx_read_guest_seg_base(vmx, seg);
5432 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5433 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5434 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005435 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005436 var->type = ar & 15;
5437 var->s = (ar >> 4) & 1;
5438 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005439 /*
5440 * Some userspaces do not preserve unusable property. Since usable
5441 * segment has to be present according to VMX spec we can use present
5442 * property to amend userspace bug by making unusable segment always
5443 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5444 * segment as unusable.
5445 */
5446 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005447 var->avl = (ar >> 12) & 1;
5448 var->l = (ar >> 13) & 1;
5449 var->db = (ar >> 14) & 1;
5450 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005451}
5452
Avi Kivitya9179492011-01-03 14:28:52 +02005453static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5454{
Avi Kivitya9179492011-01-03 14:28:52 +02005455 struct kvm_segment s;
5456
5457 if (to_vmx(vcpu)->rmode.vm86_active) {
5458 vmx_get_segment(vcpu, &s, seg);
5459 return s.base;
5460 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005461 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005462}
5463
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005464static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005465{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005466 struct vcpu_vmx *vmx = to_vmx(vcpu);
5467
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005468 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005469 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005470 else {
5471 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005472 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005473 }
Avi Kivity69c73022011-03-07 15:26:44 +02005474}
5475
Avi Kivity653e3102007-05-07 10:55:37 +03005476static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005478 u32 ar;
5479
Avi Kivityf0495f92012-06-07 17:06:10 +03005480 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005481 ar = 1 << 16;
5482 else {
5483 ar = var->type & 15;
5484 ar |= (var->s & 1) << 4;
5485 ar |= (var->dpl & 3) << 5;
5486 ar |= (var->present & 1) << 7;
5487 ar |= (var->avl & 1) << 12;
5488 ar |= (var->l & 1) << 13;
5489 ar |= (var->db & 1) << 14;
5490 ar |= (var->g & 1) << 15;
5491 }
Avi Kivity653e3102007-05-07 10:55:37 +03005492
5493 return ar;
5494}
5495
5496static void vmx_set_segment(struct kvm_vcpu *vcpu,
5497 struct kvm_segment *var, int seg)
5498{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005499 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005500 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005501
Avi Kivity2fb92db2011-04-27 19:42:18 +03005502 vmx_segment_cache_clear(vmx);
5503
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005504 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5505 vmx->rmode.segs[seg] = *var;
5506 if (seg == VCPU_SREG_TR)
5507 vmcs_write16(sf->selector, var->selector);
5508 else if (var->s)
5509 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005510 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005511 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005512
Avi Kivity653e3102007-05-07 10:55:37 +03005513 vmcs_writel(sf->base, var->base);
5514 vmcs_write32(sf->limit, var->limit);
5515 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005516
5517 /*
5518 * Fix the "Accessed" bit in AR field of segment registers for older
5519 * qemu binaries.
5520 * IA32 arch specifies that at the time of processor reset the
5521 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005522 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005523 * state vmexit when "unrestricted guest" mode is turned on.
5524 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5525 * tree. Newer qemu binaries with that qemu fix would not need this
5526 * kvm hack.
5527 */
5528 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005529 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005530
Gleb Natapovf924d662012-12-12 19:10:55 +02005531 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005532
5533out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005534 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535}
5536
Avi Kivity6aa8b732006-12-10 02:21:36 -08005537static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5538{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005539 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005540
5541 *db = (ar >> 14) & 1;
5542 *l = (ar >> 13) & 1;
5543}
5544
Gleb Natapov89a27f42010-02-16 10:51:48 +02005545static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005547 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5548 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005549}
5550
Gleb Natapov89a27f42010-02-16 10:51:48 +02005551static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005553 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5554 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555}
5556
Gleb Natapov89a27f42010-02-16 10:51:48 +02005557static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005559 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5560 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561}
5562
Gleb Natapov89a27f42010-02-16 10:51:48 +02005563static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005565 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5566 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567}
5568
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005569static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5570{
5571 struct kvm_segment var;
5572 u32 ar;
5573
5574 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005575 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005576 if (seg == VCPU_SREG_CS)
5577 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005578 ar = vmx_segment_access_rights(&var);
5579
5580 if (var.base != (var.selector << 4))
5581 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005582 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005583 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005584 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005585 return false;
5586
5587 return true;
5588}
5589
5590static bool code_segment_valid(struct kvm_vcpu *vcpu)
5591{
5592 struct kvm_segment cs;
5593 unsigned int cs_rpl;
5594
5595 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005596 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005597
Avi Kivity1872a3f2009-01-04 23:26:52 +02005598 if (cs.unusable)
5599 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005600 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005601 return false;
5602 if (!cs.s)
5603 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005604 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005605 if (cs.dpl > cs_rpl)
5606 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005607 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005608 if (cs.dpl != cs_rpl)
5609 return false;
5610 }
5611 if (!cs.present)
5612 return false;
5613
5614 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5615 return true;
5616}
5617
5618static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5619{
5620 struct kvm_segment ss;
5621 unsigned int ss_rpl;
5622
5623 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005624 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005625
Avi Kivity1872a3f2009-01-04 23:26:52 +02005626 if (ss.unusable)
5627 return true;
5628 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005629 return false;
5630 if (!ss.s)
5631 return false;
5632 if (ss.dpl != ss_rpl) /* DPL != RPL */
5633 return false;
5634 if (!ss.present)
5635 return false;
5636
5637 return true;
5638}
5639
5640static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5641{
5642 struct kvm_segment var;
5643 unsigned int rpl;
5644
5645 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005646 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005647
Avi Kivity1872a3f2009-01-04 23:26:52 +02005648 if (var.unusable)
5649 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005650 if (!var.s)
5651 return false;
5652 if (!var.present)
5653 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005654 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005655 if (var.dpl < rpl) /* DPL < RPL */
5656 return false;
5657 }
5658
5659 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5660 * rights flags
5661 */
5662 return true;
5663}
5664
5665static bool tr_valid(struct kvm_vcpu *vcpu)
5666{
5667 struct kvm_segment tr;
5668
5669 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5670
Avi Kivity1872a3f2009-01-04 23:26:52 +02005671 if (tr.unusable)
5672 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005673 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005674 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005675 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005676 return false;
5677 if (!tr.present)
5678 return false;
5679
5680 return true;
5681}
5682
5683static bool ldtr_valid(struct kvm_vcpu *vcpu)
5684{
5685 struct kvm_segment ldtr;
5686
5687 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5688
Avi Kivity1872a3f2009-01-04 23:26:52 +02005689 if (ldtr.unusable)
5690 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005691 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005692 return false;
5693 if (ldtr.type != 2)
5694 return false;
5695 if (!ldtr.present)
5696 return false;
5697
5698 return true;
5699}
5700
5701static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5702{
5703 struct kvm_segment cs, ss;
5704
5705 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5706 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5707
Nadav Amitb32a9912015-03-29 16:33:04 +03005708 return ((cs.selector & SEGMENT_RPL_MASK) ==
5709 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005710}
5711
5712/*
5713 * Check if guest state is valid. Returns true if valid, false if
5714 * not.
5715 * We assume that registers are always usable
5716 */
5717static bool guest_state_valid(struct kvm_vcpu *vcpu)
5718{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005719 if (enable_unrestricted_guest)
5720 return true;
5721
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005722 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005723 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005724 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5725 return false;
5726 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5727 return false;
5728 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5729 return false;
5730 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5731 return false;
5732 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5733 return false;
5734 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5735 return false;
5736 } else {
5737 /* protected mode guest state checks */
5738 if (!cs_ss_rpl_check(vcpu))
5739 return false;
5740 if (!code_segment_valid(vcpu))
5741 return false;
5742 if (!stack_segment_valid(vcpu))
5743 return false;
5744 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5745 return false;
5746 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5747 return false;
5748 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5749 return false;
5750 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5751 return false;
5752 if (!tr_valid(vcpu))
5753 return false;
5754 if (!ldtr_valid(vcpu))
5755 return false;
5756 }
5757 /* TODO:
5758 * - Add checks on RIP
5759 * - Add checks on RFLAGS
5760 */
5761
5762 return true;
5763}
5764
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005765static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5766{
5767 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5768}
5769
Mike Dayd77c26f2007-10-08 09:02:08 -04005770static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005771{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005772 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005773 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005774 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005775
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005776 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005777 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005778 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5779 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005780 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005781 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005782 r = kvm_write_guest_page(kvm, fn++, &data,
5783 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005784 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005785 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005786 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5787 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005788 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005789 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5790 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005791 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005792 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005793 r = kvm_write_guest_page(kvm, fn, &data,
5794 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5795 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005796out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005797 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005798 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799}
5800
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005801static int init_rmode_identity_map(struct kvm *kvm)
5802{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005803 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005804 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005805 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005806 u32 tmp;
5807
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005808 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005809 mutex_lock(&kvm->slots_lock);
5810
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005811 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005812 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005813
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005814 if (!kvm_vmx->ept_identity_map_addr)
5815 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5816 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005817
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005818 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005819 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005820 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005821 goto out2;
5822
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005823 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005824 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5825 if (r < 0)
5826 goto out;
5827 /* Set up identity-mapping pagetable for EPT in real mode */
5828 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5829 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5830 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5831 r = kvm_write_guest_page(kvm, identity_map_pfn,
5832 &tmp, i * sizeof(tmp), sizeof(tmp));
5833 if (r < 0)
5834 goto out;
5835 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005836 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005837
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005838out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005839 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005840
5841out2:
5842 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005843 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005844}
5845
Avi Kivity6aa8b732006-12-10 02:21:36 -08005846static void seg_setup(int seg)
5847{
Mathias Krause772e0312012-08-30 01:30:19 +02005848 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005849 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005850
5851 vmcs_write16(sf->selector, 0);
5852 vmcs_writel(sf->base, 0);
5853 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005854 ar = 0x93;
5855 if (seg == VCPU_SREG_CS)
5856 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005857
5858 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005859}
5860
Sheng Yangf78e0e22007-10-29 09:40:42 +08005861static int alloc_apic_access_page(struct kvm *kvm)
5862{
Xiao Guangrong44841412012-09-07 14:14:20 +08005863 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005864 int r = 0;
5865
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005866 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005867 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005868 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005869 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5870 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005871 if (r)
5872 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005873
Tang Chen73a6d942014-09-11 13:38:00 +08005874 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005875 if (is_error_page(page)) {
5876 r = -EFAULT;
5877 goto out;
5878 }
5879
Tang Chenc24ae0d2014-09-24 15:57:58 +08005880 /*
5881 * Do not pin the page in memory, so that memory hot-unplug
5882 * is able to migrate it.
5883 */
5884 put_page(page);
5885 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005886out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005887 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005888 return r;
5889}
5890
Wanpeng Li991e7a02015-09-16 17:30:05 +08005891static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005892{
5893 int vpid;
5894
Avi Kivity919818a2009-03-23 18:01:29 +02005895 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005896 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005897 spin_lock(&vmx_vpid_lock);
5898 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005899 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005900 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005901 else
5902 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005903 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005904 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005905}
5906
Wanpeng Li991e7a02015-09-16 17:30:05 +08005907static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005908{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005909 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005910 return;
5911 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005912 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005913 spin_unlock(&vmx_vpid_lock);
5914}
5915
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005916static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5917 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005918{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005919 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005920
5921 if (!cpu_has_vmx_msr_bitmap())
5922 return;
5923
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005924 if (static_branch_unlikely(&enable_evmcs))
5925 evmcs_touch_msr_bitmap();
5926
Sheng Yang25c5f222008-03-28 13:18:56 +08005927 /*
5928 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5929 * have the write-low and read-high bitmap offsets the wrong way round.
5930 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5931 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005932 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005933 if (type & MSR_TYPE_R)
5934 /* read-low */
5935 __clear_bit(msr, msr_bitmap + 0x000 / f);
5936
5937 if (type & MSR_TYPE_W)
5938 /* write-low */
5939 __clear_bit(msr, msr_bitmap + 0x800 / f);
5940
Sheng Yang25c5f222008-03-28 13:18:56 +08005941 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5942 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005943 if (type & MSR_TYPE_R)
5944 /* read-high */
5945 __clear_bit(msr, msr_bitmap + 0x400 / f);
5946
5947 if (type & MSR_TYPE_W)
5948 /* write-high */
5949 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5950
5951 }
5952}
5953
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005954static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5955 u32 msr, int type)
5956{
5957 int f = sizeof(unsigned long);
5958
5959 if (!cpu_has_vmx_msr_bitmap())
5960 return;
5961
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005962 if (static_branch_unlikely(&enable_evmcs))
5963 evmcs_touch_msr_bitmap();
5964
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005965 /*
5966 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5967 * have the write-low and read-high bitmap offsets the wrong way round.
5968 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5969 */
5970 if (msr <= 0x1fff) {
5971 if (type & MSR_TYPE_R)
5972 /* read-low */
5973 __set_bit(msr, msr_bitmap + 0x000 / f);
5974
5975 if (type & MSR_TYPE_W)
5976 /* write-low */
5977 __set_bit(msr, msr_bitmap + 0x800 / f);
5978
5979 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5980 msr &= 0x1fff;
5981 if (type & MSR_TYPE_R)
5982 /* read-high */
5983 __set_bit(msr, msr_bitmap + 0x400 / f);
5984
5985 if (type & MSR_TYPE_W)
5986 /* write-high */
5987 __set_bit(msr, msr_bitmap + 0xc00 / f);
5988
5989 }
5990}
5991
5992static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5993 u32 msr, int type, bool value)
5994{
5995 if (value)
5996 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5997 else
5998 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5999}
6000
Wincy Vanf2b93282015-02-03 23:56:03 +08006001/*
6002 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6003 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6004 */
6005static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6006 unsigned long *msr_bitmap_nested,
6007 u32 msr, int type)
6008{
6009 int f = sizeof(unsigned long);
6010
Wincy Vanf2b93282015-02-03 23:56:03 +08006011 /*
6012 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6013 * have the write-low and read-high bitmap offsets the wrong way round.
6014 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6015 */
6016 if (msr <= 0x1fff) {
6017 if (type & MSR_TYPE_R &&
6018 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6019 /* read-low */
6020 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6021
6022 if (type & MSR_TYPE_W &&
6023 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6024 /* write-low */
6025 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6026
6027 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6028 msr &= 0x1fff;
6029 if (type & MSR_TYPE_R &&
6030 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6031 /* read-high */
6032 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6033
6034 if (type & MSR_TYPE_W &&
6035 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6036 /* write-high */
6037 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6038
6039 }
6040}
6041
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006042static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006043{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006044 u8 mode = 0;
6045
6046 if (cpu_has_secondary_exec_ctrls() &&
6047 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6048 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6049 mode |= MSR_BITMAP_MODE_X2APIC;
6050 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6051 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6052 }
6053
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006054 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006055}
6056
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006057#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6058
6059static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6060 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006061{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006062 int msr;
6063
6064 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6065 unsigned word = msr / BITS_PER_LONG;
6066 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6067 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006068 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006069
6070 if (mode & MSR_BITMAP_MODE_X2APIC) {
6071 /*
6072 * TPR reads and writes can be virtualized even if virtual interrupt
6073 * delivery is not in use.
6074 */
6075 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6076 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6077 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6078 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6079 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6080 }
6081 }
6082}
6083
6084static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6085{
6086 struct vcpu_vmx *vmx = to_vmx(vcpu);
6087 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6088 u8 mode = vmx_msr_bitmap_mode(vcpu);
6089 u8 changed = mode ^ vmx->msr_bitmap_mode;
6090
6091 if (!changed)
6092 return;
6093
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006094 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6095 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6096
6097 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006098}
6099
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006100static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006101{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006102 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006103}
6104
David Matlackc9f04402017-08-01 14:00:40 -07006105static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6106{
6107 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6108 gfn_t gfn;
6109
6110 /*
6111 * Don't need to mark the APIC access page dirty; it is never
6112 * written to by the CPU during APIC virtualization.
6113 */
6114
6115 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6116 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6117 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6118 }
6119
6120 if (nested_cpu_has_posted_intr(vmcs12)) {
6121 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6122 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6123 }
6124}
6125
6126
David Hildenbrand6342c502017-01-25 11:58:58 +01006127static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006128{
6129 struct vcpu_vmx *vmx = to_vmx(vcpu);
6130 int max_irr;
6131 void *vapic_page;
6132 u16 status;
6133
David Matlackc9f04402017-08-01 14:00:40 -07006134 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6135 return;
Wincy Van705699a2015-02-03 23:58:17 +08006136
David Matlackc9f04402017-08-01 14:00:40 -07006137 vmx->nested.pi_pending = false;
6138 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6139 return;
Wincy Van705699a2015-02-03 23:58:17 +08006140
David Matlackc9f04402017-08-01 14:00:40 -07006141 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6142 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006143 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006144 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6145 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006146 kunmap(vmx->nested.virtual_apic_page);
6147
6148 status = vmcs_read16(GUEST_INTR_STATUS);
6149 if ((u8)max_irr > ((u8)status & 0xff)) {
6150 status &= ~0xff;
6151 status |= (u8)max_irr;
6152 vmcs_write16(GUEST_INTR_STATUS, status);
6153 }
6154 }
David Matlackc9f04402017-08-01 14:00:40 -07006155
6156 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006157}
6158
Paolo Bonzini7e712682018-10-03 13:44:26 +02006159static u8 vmx_get_rvi(void)
6160{
6161 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6162}
6163
Liran Alone6c67d82018-09-04 10:56:52 +03006164static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6165{
6166 struct vcpu_vmx *vmx = to_vmx(vcpu);
6167 void *vapic_page;
6168 u32 vppr;
6169 int rvi;
6170
6171 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6172 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6173 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6174 return false;
6175
Paolo Bonzini7e712682018-10-03 13:44:26 +02006176 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006177
6178 vapic_page = kmap(vmx->nested.virtual_apic_page);
6179 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6180 kunmap(vmx->nested.virtual_apic_page);
6181
6182 return ((rvi & 0xf0) > (vppr & 0xf0));
6183}
6184
Wincy Van06a55242017-04-28 13:13:59 +08006185static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6186 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006187{
6188#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006189 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6190
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006191 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006192 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006193 * The vector of interrupt to be delivered to vcpu had
6194 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006195 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006196 * Following cases will be reached in this block, and
6197 * we always send a notification event in all cases as
6198 * explained below.
6199 *
6200 * Case 1: vcpu keeps in non-root mode. Sending a
6201 * notification event posts the interrupt to vcpu.
6202 *
6203 * Case 2: vcpu exits to root mode and is still
6204 * runnable. PIR will be synced to vIRR before the
6205 * next vcpu entry. Sending a notification event in
6206 * this case has no effect, as vcpu is not in root
6207 * mode.
6208 *
6209 * Case 3: vcpu exits to root mode and is blocked.
6210 * vcpu_block() has already synced PIR to vIRR and
6211 * never blocks vcpu if vIRR is not cleared. Therefore,
6212 * a blocked vcpu here does not wait for any requested
6213 * interrupts in PIR, and sending a notification event
6214 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006215 */
Feng Wu28b835d2015-09-18 22:29:54 +08006216
Wincy Van06a55242017-04-28 13:13:59 +08006217 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006218 return true;
6219 }
6220#endif
6221 return false;
6222}
6223
Wincy Van705699a2015-02-03 23:58:17 +08006224static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6225 int vector)
6226{
6227 struct vcpu_vmx *vmx = to_vmx(vcpu);
6228
6229 if (is_guest_mode(vcpu) &&
6230 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006231 /*
6232 * If a posted intr is not recognized by hardware,
6233 * we will accomplish it in the next vmentry.
6234 */
6235 vmx->nested.pi_pending = true;
6236 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006237 /* the PIR and ON have been set by L1. */
6238 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6239 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006240 return 0;
6241 }
6242 return -1;
6243}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006244/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006245 * Send interrupt to vcpu via posted interrupt way.
6246 * 1. If target vcpu is running(non-root mode), send posted interrupt
6247 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6248 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6249 * interrupt from PIR in next vmentry.
6250 */
6251static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6252{
6253 struct vcpu_vmx *vmx = to_vmx(vcpu);
6254 int r;
6255
Wincy Van705699a2015-02-03 23:58:17 +08006256 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6257 if (!r)
6258 return;
6259
Yang Zhanga20ed542013-04-11 19:25:15 +08006260 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6261 return;
6262
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006263 /* If a previous notification has sent the IPI, nothing to do. */
6264 if (pi_test_and_set_on(&vmx->pi_desc))
6265 return;
6266
Wincy Van06a55242017-04-28 13:13:59 +08006267 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006268 kvm_vcpu_kick(vcpu);
6269}
6270
Avi Kivity6aa8b732006-12-10 02:21:36 -08006271/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006272 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6273 * will not change in the lifetime of the guest.
6274 * Note that host-state that does change is set elsewhere. E.g., host-state
6275 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6276 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006277static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006278{
6279 u32 low32, high32;
6280 unsigned long tmpl;
6281 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006282 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006283
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006284 cr0 = read_cr0();
6285 WARN_ON(cr0 & X86_CR0_TS);
6286 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006287
6288 /*
6289 * Save the most likely value for this task's CR3 in the VMCS.
6290 * We can't use __get_current_cr3_fast() because we're not atomic.
6291 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006292 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006293 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006294 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006295
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006296 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006297 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006298 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006299 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006300
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006301 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006302#ifdef CONFIG_X86_64
6303 /*
6304 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006305 * vmx_prepare_switch_to_host(), in case userspace uses
6306 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006307 */
6308 vmcs_write16(HOST_DS_SELECTOR, 0);
6309 vmcs_write16(HOST_ES_SELECTOR, 0);
6310#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006311 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6312 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006313#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006314 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6315 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6316
Juergen Gross87930012017-09-04 12:25:27 +02006317 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006318 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006319 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006320
Avi Kivity83287ea422012-09-16 15:10:57 +03006321 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006322
6323 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6324 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6325 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6326 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6327
6328 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6329 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6330 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6331 }
6332}
6333
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006334static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6335{
6336 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6337 if (enable_ept)
6338 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006339 if (is_guest_mode(&vmx->vcpu))
6340 vmx->vcpu.arch.cr4_guest_owned_bits &=
6341 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006342 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6343}
6344
Yang Zhang01e439b2013-04-11 19:25:12 +08006345static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6346{
6347 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6348
Andrey Smetanind62caab2015-11-10 15:36:33 +03006349 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006350 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006351
6352 if (!enable_vnmi)
6353 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6354
Yunhong Jiang64672c92016-06-13 14:19:59 -07006355 /* Enable the preemption timer dynamically */
6356 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006357 return pin_based_exec_ctrl;
6358}
6359
Andrey Smetanind62caab2015-11-10 15:36:33 +03006360static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6361{
6362 struct vcpu_vmx *vmx = to_vmx(vcpu);
6363
6364 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006365 if (cpu_has_secondary_exec_ctrls()) {
6366 if (kvm_vcpu_apicv_active(vcpu))
6367 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6368 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6369 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6370 else
6371 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6372 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6373 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6374 }
6375
6376 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006377 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006378}
6379
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006380static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6381{
6382 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006383
6384 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6385 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6386
Paolo Bonzini35754c92015-07-29 12:05:37 +02006387 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006388 exec_control &= ~CPU_BASED_TPR_SHADOW;
6389#ifdef CONFIG_X86_64
6390 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6391 CPU_BASED_CR8_LOAD_EXITING;
6392#endif
6393 }
6394 if (!enable_ept)
6395 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6396 CPU_BASED_CR3_LOAD_EXITING |
6397 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006398 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6399 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6400 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006401 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6402 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006403 return exec_control;
6404}
6405
Jim Mattson45ec3682017-08-23 16:32:04 -07006406static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006407{
Jim Mattson45ec3682017-08-23 16:32:04 -07006408 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006409 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006410}
6411
Jim Mattson75f4fc82017-08-23 16:32:03 -07006412static bool vmx_rdseed_supported(void)
6413{
6414 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006415 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006416}
6417
Paolo Bonzini80154d72017-08-24 13:55:35 +02006418static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006419{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006420 struct kvm_vcpu *vcpu = &vmx->vcpu;
6421
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006422 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006423
Paolo Bonzini80154d72017-08-24 13:55:35 +02006424 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006425 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6426 if (vmx->vpid == 0)
6427 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6428 if (!enable_ept) {
6429 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6430 enable_unrestricted_guest = 0;
6431 }
6432 if (!enable_unrestricted_guest)
6433 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006434 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006435 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006436 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006437 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6438 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006439 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006440
6441 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6442 * in vmx_set_cr4. */
6443 exec_control &= ~SECONDARY_EXEC_DESC;
6444
Abel Gordonabc4fc52013-04-18 14:35:25 +03006445 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6446 (handle_vmptrld).
6447 We can NOT enable shadow_vmcs here because we don't have yet
6448 a current VMCS12
6449 */
6450 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006451
6452 if (!enable_pml)
6453 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006454
Paolo Bonzini3db13482017-08-24 14:48:03 +02006455 if (vmx_xsaves_supported()) {
6456 /* Exposing XSAVES only when XSAVE is exposed */
6457 bool xsaves_enabled =
6458 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6459 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6460
6461 if (!xsaves_enabled)
6462 exec_control &= ~SECONDARY_EXEC_XSAVES;
6463
6464 if (nested) {
6465 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006466 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006467 SECONDARY_EXEC_XSAVES;
6468 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006469 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006470 ~SECONDARY_EXEC_XSAVES;
6471 }
6472 }
6473
Paolo Bonzini80154d72017-08-24 13:55:35 +02006474 if (vmx_rdtscp_supported()) {
6475 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6476 if (!rdtscp_enabled)
6477 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6478
6479 if (nested) {
6480 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006481 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006482 SECONDARY_EXEC_RDTSCP;
6483 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006484 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006485 ~SECONDARY_EXEC_RDTSCP;
6486 }
6487 }
6488
6489 if (vmx_invpcid_supported()) {
6490 /* Exposing INVPCID only when PCID is exposed */
6491 bool invpcid_enabled =
6492 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6493 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6494
6495 if (!invpcid_enabled) {
6496 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6497 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6498 }
6499
6500 if (nested) {
6501 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006502 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006503 SECONDARY_EXEC_ENABLE_INVPCID;
6504 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006505 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006506 ~SECONDARY_EXEC_ENABLE_INVPCID;
6507 }
6508 }
6509
Jim Mattson45ec3682017-08-23 16:32:04 -07006510 if (vmx_rdrand_supported()) {
6511 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6512 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006513 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006514
6515 if (nested) {
6516 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006517 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006518 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006519 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006520 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006521 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006522 }
6523 }
6524
Jim Mattson75f4fc82017-08-23 16:32:03 -07006525 if (vmx_rdseed_supported()) {
6526 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6527 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006528 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006529
6530 if (nested) {
6531 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006532 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006533 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006534 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006535 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006536 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006537 }
6538 }
6539
Paolo Bonzini80154d72017-08-24 13:55:35 +02006540 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006541}
6542
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006543static void ept_set_mmio_spte_mask(void)
6544{
6545 /*
6546 * EPT Misconfigurations can be generated if the value of bits 2:0
6547 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006548 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006549 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6550 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006551}
6552
Wanpeng Lif53cd632014-12-02 19:14:58 +08006553#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006554/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006555 * Sets up the vmcs for emulated real mode.
6556 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006557static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006558{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006559 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006560
Abel Gordon4607c2d2013-04-18 14:35:55 +03006561 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006562 /*
6563 * At vCPU creation, "VMWRITE to any supported field
6564 * in the VMCS" is supported, so use the more
6565 * permissive vmx_vmread_bitmap to specify both read
6566 * and write permissions for the shadow VMCS.
6567 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006568 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006569 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006570 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006571 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006572 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006573
Avi Kivity6aa8b732006-12-10 02:21:36 -08006574 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6575
Avi Kivity6aa8b732006-12-10 02:21:36 -08006576 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006577 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006578 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006579
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006580 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006581
Dan Williamsdfa169b2016-06-02 11:17:24 -07006582 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006583 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006584 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006585 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006586 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006587
Andrey Smetanind62caab2015-11-10 15:36:33 +03006588 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006589 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6590 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6591 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6592 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6593
6594 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006595
Li RongQing0bcf2612015-12-03 13:29:34 +08006596 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006597 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006598 }
6599
Wanpeng Lib31c1142018-03-12 04:53:04 -07006600 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006601 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006602 vmx->ple_window = ple_window;
6603 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006604 }
6605
Xiao Guangrongc3707952011-07-12 03:28:04 +08006606 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6607 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006608 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6609
Avi Kivity9581d442010-10-19 16:46:55 +02006610 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6611 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006612 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006613 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6614 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006615
Bandan Das2a499e42017-08-03 15:54:41 -04006616 if (cpu_has_vmx_vmfunc())
6617 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6618
Eddie Dong2cc51562007-05-21 07:28:09 +03006619 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6620 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006621 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006622 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006623 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006624
Radim Krčmář74545702015-04-27 15:11:25 +02006625 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6626 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006627
Paolo Bonzini03916db2014-07-24 14:21:57 +02006628 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006629 u32 index = vmx_msr_index[i];
6630 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006631 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006632
6633 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6634 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006635 if (wrmsr_safe(index, data_low, data_high) < 0)
6636 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006637 vmx->guest_msrs[j].index = i;
6638 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006639 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006640 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006641 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006643 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006644
6645 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006646
6647 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006648 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006649
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006650 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6651 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6652
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006653 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006654
Wanpeng Lif53cd632014-12-02 19:14:58 +08006655 if (vmx_xsaves_supported())
6656 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6657
Peter Feiner4e595162016-07-07 14:49:58 -07006658 if (enable_pml) {
6659 ASSERT(vmx->pml_pg);
6660 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6661 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6662 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006663
6664 if (cpu_has_vmx_encls_vmexit())
6665 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006666}
6667
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006668static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006669{
6670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006671 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006672 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006673
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006674 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006675 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006676
Wanpeng Li518e7b92018-02-28 14:03:31 +08006677 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006678 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006679 kvm_set_cr8(vcpu, 0);
6680
6681 if (!init_event) {
6682 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6683 MSR_IA32_APICBASE_ENABLE;
6684 if (kvm_vcpu_is_reset_bsp(vcpu))
6685 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6686 apic_base_msr.host_initiated = true;
6687 kvm_set_apic_base(vcpu, &apic_base_msr);
6688 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006689
Avi Kivity2fb92db2011-04-27 19:42:18 +03006690 vmx_segment_cache_clear(vmx);
6691
Avi Kivity5706be02008-08-20 15:07:31 +03006692 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006693 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006694 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006695
6696 seg_setup(VCPU_SREG_DS);
6697 seg_setup(VCPU_SREG_ES);
6698 seg_setup(VCPU_SREG_FS);
6699 seg_setup(VCPU_SREG_GS);
6700 seg_setup(VCPU_SREG_SS);
6701
6702 vmcs_write16(GUEST_TR_SELECTOR, 0);
6703 vmcs_writel(GUEST_TR_BASE, 0);
6704 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6705 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6706
6707 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6708 vmcs_writel(GUEST_LDTR_BASE, 0);
6709 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6710 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6711
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006712 if (!init_event) {
6713 vmcs_write32(GUEST_SYSENTER_CS, 0);
6714 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6715 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6716 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6717 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006718
Wanpeng Lic37c2872017-11-20 14:52:21 -08006719 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006720 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006721
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006722 vmcs_writel(GUEST_GDTR_BASE, 0);
6723 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6724
6725 vmcs_writel(GUEST_IDTR_BASE, 0);
6726 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6727
Anthony Liguori443381a2010-12-06 10:53:38 -06006728 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006729 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006730 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006731 if (kvm_mpx_supported())
6732 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006733
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006734 setup_msrs(vmx);
6735
Avi Kivity6aa8b732006-12-10 02:21:36 -08006736 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6737
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006738 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006739 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006740 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006741 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006742 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006743 vmcs_write32(TPR_THRESHOLD, 0);
6744 }
6745
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006746 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006747
Sheng Yang2384d2b2008-01-17 15:14:33 +08006748 if (vmx->vpid != 0)
6749 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6750
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006751 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006752 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006753 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006754 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006755 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006756
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006757 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006758
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006759 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006760 if (init_event)
6761 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006762}
6763
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006764/*
6765 * In nested virtualization, check if L1 asked to exit on external interrupts.
6766 * For most existing hypervisors, this will always return true.
6767 */
6768static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6769{
6770 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6771 PIN_BASED_EXT_INTR_MASK;
6772}
6773
Bandan Das77b0f5d2014-04-19 18:17:45 -04006774/*
6775 * In nested virtualization, check if L1 has set
6776 * VM_EXIT_ACK_INTR_ON_EXIT
6777 */
6778static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6779{
6780 return get_vmcs12(vcpu)->vm_exit_controls &
6781 VM_EXIT_ACK_INTR_ON_EXIT;
6782}
6783
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006784static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6785{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006786 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006787}
6788
Jan Kiszkac9a79532014-03-07 20:03:15 +01006789static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006790{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006791 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6792 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006793}
6794
Jan Kiszkac9a79532014-03-07 20:03:15 +01006795static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006796{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006797 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006798 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006799 enable_irq_window(vcpu);
6800 return;
6801 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006802
Paolo Bonzini47c01522016-12-19 11:44:07 +01006803 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6804 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006805}
6806
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006807static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006808{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006810 uint32_t intr;
6811 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006812
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006813 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006814
Avi Kivityfa89a812008-09-01 15:57:51 +03006815 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006816 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006817 int inc_eip = 0;
6818 if (vcpu->arch.interrupt.soft)
6819 inc_eip = vcpu->arch.event_exit_inst_len;
6820 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006821 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006822 return;
6823 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006824 intr = irq | INTR_INFO_VALID_MASK;
6825 if (vcpu->arch.interrupt.soft) {
6826 intr |= INTR_TYPE_SOFT_INTR;
6827 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6828 vmx->vcpu.arch.event_exit_inst_len);
6829 } else
6830 intr |= INTR_TYPE_EXT_INTR;
6831 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006832
6833 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006834}
6835
Sheng Yangf08864b2008-05-15 18:23:25 +08006836static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6837{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006838 struct vcpu_vmx *vmx = to_vmx(vcpu);
6839
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006840 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006841 /*
6842 * Tracking the NMI-blocked state in software is built upon
6843 * finding the next open IRQ window. This, in turn, depends on
6844 * well-behaving guests: They have to keep IRQs disabled at
6845 * least as long as the NMI handler runs. Otherwise we may
6846 * cause NMI nesting, maybe breaking the guest. But as this is
6847 * highly unlikely, we can live with the residual risk.
6848 */
6849 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6850 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6851 }
6852
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006853 ++vcpu->stat.nmi_injections;
6854 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006855
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006856 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006857 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006858 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006859 return;
6860 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006861
Sheng Yangf08864b2008-05-15 18:23:25 +08006862 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6863 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006864
6865 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006866}
6867
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006868static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6869{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006870 struct vcpu_vmx *vmx = to_vmx(vcpu);
6871 bool masked;
6872
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006873 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006874 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006875 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006876 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006877 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6878 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6879 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006880}
6881
6882static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6883{
6884 struct vcpu_vmx *vmx = to_vmx(vcpu);
6885
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006886 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006887 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6888 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6889 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6890 }
6891 } else {
6892 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6893 if (masked)
6894 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6895 GUEST_INTR_STATE_NMI);
6896 else
6897 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6898 GUEST_INTR_STATE_NMI);
6899 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006900}
6901
Jan Kiszka2505dc92013-04-14 12:12:47 +02006902static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6903{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006904 if (to_vmx(vcpu)->nested.nested_run_pending)
6905 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006906
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006907 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006908 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6909 return 0;
6910
Jan Kiszka2505dc92013-04-14 12:12:47 +02006911 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6912 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6913 | GUEST_INTR_STATE_NMI));
6914}
6915
Gleb Natapov78646122009-03-23 12:12:11 +02006916static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6917{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006918 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6919 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006920 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6921 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006922}
6923
Izik Eiduscbc94022007-10-25 00:29:55 +02006924static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6925{
6926 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006927
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006928 if (enable_unrestricted_guest)
6929 return 0;
6930
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006931 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6932 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006933 if (ret)
6934 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006935 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006936 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006937}
6938
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006939static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6940{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006941 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006942 return 0;
6943}
6944
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006945static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006946{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006947 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006948 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01006949 /*
6950 * Update instruction length as we may reinject the exception
6951 * from user space while in guest debugging mode.
6952 */
6953 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6954 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006955 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006956 return false;
6957 /* fall through */
6958 case DB_VECTOR:
6959 if (vcpu->guest_debug &
6960 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6961 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006962 /* fall through */
6963 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006964 case OF_VECTOR:
6965 case BR_VECTOR:
6966 case UD_VECTOR:
6967 case DF_VECTOR:
6968 case SS_VECTOR:
6969 case GP_VECTOR:
6970 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006971 return true;
6972 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006973 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006974 return false;
6975}
6976
6977static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6978 int vec, u32 err_code)
6979{
6980 /*
6981 * Instruction with address size override prefix opcode 0x67
6982 * Cause the #SS fault with 0 error code in VM86 mode.
6983 */
6984 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006985 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006986 if (vcpu->arch.halt_request) {
6987 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006988 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006989 }
6990 return 1;
6991 }
6992 return 0;
6993 }
6994
6995 /*
6996 * Forward all other exceptions that are valid in real mode.
6997 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6998 * the required debugging infrastructure rework.
6999 */
7000 kvm_queue_exception(vcpu, vec);
7001 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007002}
7003
Andi Kleena0861c02009-06-08 17:37:09 +08007004/*
7005 * Trigger machine check on the host. We assume all the MSRs are already set up
7006 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7007 * We pass a fake environment to the machine check handler because we want
7008 * the guest to be always treated like user space, no matter what context
7009 * it used internally.
7010 */
7011static void kvm_machine_check(void)
7012{
7013#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7014 struct pt_regs regs = {
7015 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7016 .flags = X86_EFLAGS_IF,
7017 };
7018
7019 do_machine_check(&regs, 0);
7020#endif
7021}
7022
Avi Kivity851ba692009-08-24 11:10:17 +03007023static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007024{
7025 /* already handled by vcpu_run */
7026 return 1;
7027}
7028
Avi Kivity851ba692009-08-24 11:10:17 +03007029static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030{
Avi Kivity1155f762007-11-22 11:30:47 +02007031 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007032 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007033 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007034 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007035 u32 vect_info;
7036 enum emulation_result er;
7037
Avi Kivity1155f762007-11-22 11:30:47 +02007038 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007039 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007040
Andi Kleena0861c02009-06-08 17:37:09 +08007041 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007042 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007043
Jim Mattsonef85b672016-12-12 11:01:37 -08007044 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007045 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007046
Wanpeng Li082d06e2018-04-03 16:28:48 -07007047 if (is_invalid_opcode(intr_info))
7048 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007049
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007051 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007052 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007053
Liran Alon9e869482018-03-12 13:12:51 +02007054 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7055 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007056 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007057 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7058 if (er == EMULATE_USER_EXIT)
7059 return 0;
7060 else if (er != EMULATE_DONE)
7061 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7062 return 1;
7063 }
7064
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007065 /*
7066 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7067 * MMIO, it is better to report an internal error.
7068 * See the comments in vmx_handle_exit.
7069 */
7070 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7071 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7072 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7073 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007074 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007075 vcpu->run->internal.data[0] = vect_info;
7076 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007077 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007078 return 0;
7079 }
7080
Avi Kivity6aa8b732006-12-10 02:21:36 -08007081 if (is_page_fault(intr_info)) {
7082 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007083 /* EPT won't cause page fault directly */
7084 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007085 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007086 }
7087
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007088 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007089
7090 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7091 return handle_rmode_exception(vcpu, ex_no, error_code);
7092
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007093 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007094 case AC_VECTOR:
7095 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7096 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007097 case DB_VECTOR:
7098 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7099 if (!(vcpu->guest_debug &
7100 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007101 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007102 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007103 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007104 skip_emulated_instruction(vcpu);
7105
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007106 kvm_queue_exception(vcpu, DB_VECTOR);
7107 return 1;
7108 }
7109 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7110 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7111 /* fall through */
7112 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01007113 /*
7114 * Update instruction length as we may reinject #BP from
7115 * user space while in guest debugging mode. Reading it for
7116 * #DB as well causes no harm, it is not used in that case.
7117 */
7118 vmx->vcpu.arch.event_exit_inst_len =
7119 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007120 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007121 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007122 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7123 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007124 break;
7125 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007126 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7127 kvm_run->ex.exception = ex_no;
7128 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007129 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007130 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007131 return 0;
7132}
7133
Avi Kivity851ba692009-08-24 11:10:17 +03007134static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007136 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007137 return 1;
7138}
7139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007141{
Avi Kivity851ba692009-08-24 11:10:17 +03007142 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007143 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007144 return 0;
7145}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007146
Avi Kivity851ba692009-08-24 11:10:17 +03007147static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007148{
He, Qingbfdaab02007-09-12 14:18:28 +08007149 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007150 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007151 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007152
He, Qingbfdaab02007-09-12 14:18:28 +08007153 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007154 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007155
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007156 ++vcpu->stat.io_exits;
7157
Sean Christopherson432baf62018-03-08 08:57:26 -08007158 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007159 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007160
7161 port = exit_qualification >> 16;
7162 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007163 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007164
Sean Christophersondca7f122018-03-08 08:57:27 -08007165 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007166}
7167
Ingo Molnar102d8322007-02-19 14:37:47 +02007168static void
7169vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7170{
7171 /*
7172 * Patch in the VMCALL instruction:
7173 */
7174 hypercall[0] = 0x0f;
7175 hypercall[1] = 0x01;
7176 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007177}
7178
Guo Chao0fa06072012-06-28 15:16:19 +08007179/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007180static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7181{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007182 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007183 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7184 unsigned long orig_val = val;
7185
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007186 /*
7187 * We get here when L2 changed cr0 in a way that did not change
7188 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007189 * but did change L0 shadowed bits. So we first calculate the
7190 * effective cr0 value that L1 would like to write into the
7191 * hardware. It consists of the L2-owned bits from the new
7192 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007193 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007194 val = (val & ~vmcs12->cr0_guest_host_mask) |
7195 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7196
David Matlack38991522016-11-29 18:14:08 -08007197 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007198 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007199
7200 if (kvm_set_cr0(vcpu, val))
7201 return 1;
7202 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007203 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007204 } else {
7205 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007206 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007207 return 1;
David Matlack38991522016-11-29 18:14:08 -08007208
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007209 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007210 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007211}
7212
7213static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7214{
7215 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007216 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7217 unsigned long orig_val = val;
7218
7219 /* analogously to handle_set_cr0 */
7220 val = (val & ~vmcs12->cr4_guest_host_mask) |
7221 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7222 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007223 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007224 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007225 return 0;
7226 } else
7227 return kvm_set_cr4(vcpu, val);
7228}
7229
Paolo Bonzini0367f202016-07-12 10:44:55 +02007230static int handle_desc(struct kvm_vcpu *vcpu)
7231{
7232 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007233 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007234}
7235
Avi Kivity851ba692009-08-24 11:10:17 +03007236static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007238 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007239 int cr;
7240 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007241 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007242 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007243
He, Qingbfdaab02007-09-12 14:18:28 +08007244 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007245 cr = exit_qualification & 15;
7246 reg = (exit_qualification >> 8) & 15;
7247 switch ((exit_qualification >> 4) & 3) {
7248 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007249 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007250 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007251 switch (cr) {
7252 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007253 err = handle_set_cr0(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 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007256 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007257 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007258 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007259 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007260 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007261 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007262 case 8: {
7263 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007264 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007265 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007266 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007267 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007268 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007269 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007270 return ret;
7271 /*
7272 * TODO: we might be squashing a
7273 * KVM_GUESTDBG_SINGLESTEP-triggered
7274 * KVM_EXIT_DEBUG here.
7275 */
Avi Kivity851ba692009-08-24 11:10:17 +03007276 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007277 return 0;
7278 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007279 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007280 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007281 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007282 WARN_ONCE(1, "Guest should always own CR0.TS");
7283 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007284 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007285 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007286 case 1: /*mov from cr*/
7287 switch (cr) {
7288 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007289 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007290 val = kvm_read_cr3(vcpu);
7291 kvm_register_write(vcpu, reg, val);
7292 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007293 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007294 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007295 val = kvm_get_cr8(vcpu);
7296 kvm_register_write(vcpu, reg, val);
7297 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007298 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007299 }
7300 break;
7301 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007302 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007303 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007304 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305
Kyle Huey6affcbe2016-11-29 12:40:40 -08007306 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007307 default:
7308 break;
7309 }
Avi Kivity851ba692009-08-24 11:10:17 +03007310 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007311 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312 (int)(exit_qualification >> 4) & 3, cr);
7313 return 0;
7314}
7315
Avi Kivity851ba692009-08-24 11:10:17 +03007316static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007317{
He, Qingbfdaab02007-09-12 14:18:28 +08007318 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007319 int dr, dr7, reg;
7320
7321 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7322 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7323
7324 /* First, if DR does not exist, trigger UD */
7325 if (!kvm_require_dr(vcpu, dr))
7326 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007327
Jan Kiszkaf2483412010-01-20 18:20:20 +01007328 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007329 if (!kvm_require_cpl(vcpu, 0))
7330 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007331 dr7 = vmcs_readl(GUEST_DR7);
7332 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007333 /*
7334 * As the vm-exit takes precedence over the debug trap, we
7335 * need to emulate the latter, either for the host or the
7336 * guest debugging itself.
7337 */
7338 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007339 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007340 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007341 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007342 vcpu->run->debug.arch.exception = DB_VECTOR;
7343 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007344 return 0;
7345 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007346 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007347 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007348 kvm_queue_exception(vcpu, DB_VECTOR);
7349 return 1;
7350 }
7351 }
7352
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007353 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007354 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7355 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007356
7357 /*
7358 * No more DR vmexits; force a reload of the debug registers
7359 * and reenter on this instruction. The next vmexit will
7360 * retrieve the full state of the debug registers.
7361 */
7362 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7363 return 1;
7364 }
7365
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007366 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7367 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007368 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007369
7370 if (kvm_get_dr(vcpu, dr, &val))
7371 return 1;
7372 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007373 } else
Nadav Amit57773922014-06-18 17:19:23 +03007374 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007375 return 1;
7376
Kyle Huey6affcbe2016-11-29 12:40:40 -08007377 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007378}
7379
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007380static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7381{
7382 return vcpu->arch.dr6;
7383}
7384
7385static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7386{
7387}
7388
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007389static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7390{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007391 get_debugreg(vcpu->arch.db[0], 0);
7392 get_debugreg(vcpu->arch.db[1], 1);
7393 get_debugreg(vcpu->arch.db[2], 2);
7394 get_debugreg(vcpu->arch.db[3], 3);
7395 get_debugreg(vcpu->arch.dr6, 6);
7396 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7397
7398 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007399 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007400}
7401
Gleb Natapov020df072010-04-13 10:05:23 +03007402static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7403{
7404 vmcs_writel(GUEST_DR7, val);
7405}
7406
Avi Kivity851ba692009-08-24 11:10:17 +03007407static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007408{
Kyle Huey6a908b62016-11-29 12:40:37 -08007409 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007410}
7411
Avi Kivity851ba692009-08-24 11:10:17 +03007412static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007413{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007414 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007415 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007416
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007417 msr_info.index = ecx;
7418 msr_info.host_initiated = false;
7419 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007420 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007421 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007422 return 1;
7423 }
7424
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007425 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007426
Avi Kivity6aa8b732006-12-10 02:21:36 -08007427 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007428 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7429 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007430 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007431}
7432
Avi Kivity851ba692009-08-24 11:10:17 +03007433static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007434{
Will Auld8fe8ab42012-11-29 12:42:12 -08007435 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007436 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7437 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7438 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007439
Will Auld8fe8ab42012-11-29 12:42:12 -08007440 msr.data = data;
7441 msr.index = ecx;
7442 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007443 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007444 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007445 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007446 return 1;
7447 }
7448
Avi Kivity59200272010-01-25 19:47:02 +02007449 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007450 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007451}
7452
Avi Kivity851ba692009-08-24 11:10:17 +03007453static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007454{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007455 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007456 return 1;
7457}
7458
Avi Kivity851ba692009-08-24 11:10:17 +03007459static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007460{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007461 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7462 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007463
Avi Kivity3842d132010-07-27 12:30:24 +03007464 kvm_make_request(KVM_REQ_EVENT, vcpu);
7465
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007466 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467 return 1;
7468}
7469
Avi Kivity851ba692009-08-24 11:10:17 +03007470static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007471{
Avi Kivityd3bef152007-06-05 15:53:05 +03007472 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007473}
7474
Avi Kivity851ba692009-08-24 11:10:17 +03007475static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007476{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007477 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007478}
7479
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007480static int handle_invd(struct kvm_vcpu *vcpu)
7481{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007482 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007483}
7484
Avi Kivity851ba692009-08-24 11:10:17 +03007485static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007486{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007487 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007488
7489 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007490 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007491}
7492
Avi Kivityfee84b02011-11-10 14:57:25 +02007493static int handle_rdpmc(struct kvm_vcpu *vcpu)
7494{
7495 int err;
7496
7497 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007498 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007499}
7500
Avi Kivity851ba692009-08-24 11:10:17 +03007501static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007502{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007503 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007504}
7505
Dexuan Cui2acf9232010-06-10 11:27:12 +08007506static int handle_xsetbv(struct kvm_vcpu *vcpu)
7507{
7508 u64 new_bv = kvm_read_edx_eax(vcpu);
7509 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7510
7511 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007512 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007513 return 1;
7514}
7515
Wanpeng Lif53cd632014-12-02 19:14:58 +08007516static int handle_xsaves(struct kvm_vcpu *vcpu)
7517{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007518 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007519 WARN(1, "this should never happen\n");
7520 return 1;
7521}
7522
7523static int handle_xrstors(struct kvm_vcpu *vcpu)
7524{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007525 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007526 WARN(1, "this should never happen\n");
7527 return 1;
7528}
7529
Avi Kivity851ba692009-08-24 11:10:17 +03007530static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007531{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007532 if (likely(fasteoi)) {
7533 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7534 int access_type, offset;
7535
7536 access_type = exit_qualification & APIC_ACCESS_TYPE;
7537 offset = exit_qualification & APIC_ACCESS_OFFSET;
7538 /*
7539 * Sane guest uses MOV to write EOI, with written value
7540 * not cared. So make a short-circuit here by avoiding
7541 * heavy instruction emulation.
7542 */
7543 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7544 (offset == APIC_EOI)) {
7545 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007546 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007547 }
7548 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007549 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007550}
7551
Yang Zhangc7c9c562013-01-25 10:18:51 +08007552static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7553{
7554 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7555 int vector = exit_qualification & 0xff;
7556
7557 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7558 kvm_apic_set_eoi_accelerated(vcpu, vector);
7559 return 1;
7560}
7561
Yang Zhang83d4c282013-01-25 10:18:49 +08007562static int handle_apic_write(struct kvm_vcpu *vcpu)
7563{
7564 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7565 u32 offset = exit_qualification & 0xfff;
7566
7567 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7568 kvm_apic_write_nodecode(vcpu, offset);
7569 return 1;
7570}
7571
Avi Kivity851ba692009-08-24 11:10:17 +03007572static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007573{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007574 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007575 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007576 bool has_error_code = false;
7577 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007578 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007579 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007580
7581 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007582 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007583 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007584
7585 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7586
7587 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007588 if (reason == TASK_SWITCH_GATE && idt_v) {
7589 switch (type) {
7590 case INTR_TYPE_NMI_INTR:
7591 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007592 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007593 break;
7594 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007595 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007596 kvm_clear_interrupt_queue(vcpu);
7597 break;
7598 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007599 if (vmx->idt_vectoring_info &
7600 VECTORING_INFO_DELIVER_CODE_MASK) {
7601 has_error_code = true;
7602 error_code =
7603 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7604 }
7605 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007606 case INTR_TYPE_SOFT_EXCEPTION:
7607 kvm_clear_exception_queue(vcpu);
7608 break;
7609 default:
7610 break;
7611 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007612 }
Izik Eidus37817f22008-03-24 23:14:53 +02007613 tss_selector = exit_qualification;
7614
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007615 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7616 type != INTR_TYPE_EXT_INTR &&
7617 type != INTR_TYPE_NMI_INTR))
7618 skip_emulated_instruction(vcpu);
7619
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007620 if (kvm_task_switch(vcpu, tss_selector,
7621 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7622 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007623 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7624 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7625 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007626 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007627 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007628
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007629 /*
7630 * TODO: What about debug traps on tss switch?
7631 * Are we supposed to inject them and update dr6?
7632 */
7633
7634 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007635}
7636
Avi Kivity851ba692009-08-24 11:10:17 +03007637static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007638{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007639 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007640 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007641 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007642
Sheng Yangf9c617f2009-03-25 10:08:52 +08007643 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007644
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007645 /*
7646 * EPT violation happened while executing iret from NMI,
7647 * "blocked by NMI" bit has to be set before next VM entry.
7648 * There are errata that may cause this bit to not be set:
7649 * AAK134, BY25.
7650 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007651 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007652 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007653 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007654 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7655
Sheng Yang14394422008-04-28 12:24:45 +08007656 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007657 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007658
Junaid Shahid27959a42016-12-06 16:46:10 -08007659 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007660 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007661 ? PFERR_USER_MASK : 0;
7662 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007663 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007664 ? PFERR_WRITE_MASK : 0;
7665 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007666 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007667 ? PFERR_FETCH_MASK : 0;
7668 /* ept page table entry is present? */
7669 error_code |= (exit_qualification &
7670 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7671 EPT_VIOLATION_EXECUTABLE))
7672 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007673
Paolo Bonzinieebed242016-11-28 14:39:58 +01007674 error_code |= (exit_qualification & 0x100) != 0 ?
7675 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007676
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007677 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007678 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007679}
7680
Avi Kivity851ba692009-08-24 11:10:17 +03007681static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007682{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007683 gpa_t gpa;
7684
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007685 /*
7686 * A nested guest cannot optimize MMIO vmexits, because we have an
7687 * nGPA here instead of the required GPA.
7688 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007689 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007690 if (!is_guest_mode(vcpu) &&
7691 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007692 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007693 /*
7694 * Doing kvm_skip_emulated_instruction() depends on undefined
7695 * behavior: Intel's manual doesn't mandate
7696 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7697 * occurs and while on real hardware it was observed to be set,
7698 * other hypervisors (namely Hyper-V) don't set it, we end up
7699 * advancing IP with some random value. Disable fast mmio when
7700 * running nested and keep it for real hardware in hope that
7701 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7702 */
7703 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7704 return kvm_skip_emulated_instruction(vcpu);
7705 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007706 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007707 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007708 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007709
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007710 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007711}
7712
Avi Kivity851ba692009-08-24 11:10:17 +03007713static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007714{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007715 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007716 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7717 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007718 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007719 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007720
7721 return 1;
7722}
7723
Mohammed Gamal80ced182009-09-01 12:48:18 +02007724static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007725{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007726 struct vcpu_vmx *vmx = to_vmx(vcpu);
7727 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007728 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007729 u32 cpu_exec_ctrl;
7730 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007731 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007732
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007733 /*
7734 * We should never reach the point where we are emulating L2
7735 * due to invalid guest state as that means we incorrectly
7736 * allowed a nested VMEntry with an invalid vmcs12.
7737 */
7738 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7739
Avi Kivity49e9d552010-09-19 14:34:08 +02007740 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7741 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007742
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007743 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007744 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007745 return handle_interrupt_window(&vmx->vcpu);
7746
Radim Krčmář72875d82017-04-26 22:32:19 +02007747 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007748 return 1;
7749
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007750 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007751
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007752 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007753 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007754 ret = 0;
7755 goto out;
7756 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007757
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007758 if (err != EMULATE_DONE)
7759 goto emulation_error;
7760
7761 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7762 vcpu->arch.exception.pending)
7763 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007764
Gleb Natapov8d76c492013-05-08 18:38:44 +03007765 if (vcpu->arch.halt_request) {
7766 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007767 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007768 goto out;
7769 }
7770
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007771 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007772 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007773 if (need_resched())
7774 schedule();
7775 }
7776
Mohammed Gamal80ced182009-09-01 12:48:18 +02007777out:
7778 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007779
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007780emulation_error:
7781 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7782 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7783 vcpu->run->internal.ndata = 0;
7784 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007785}
7786
7787static void grow_ple_window(struct kvm_vcpu *vcpu)
7788{
7789 struct vcpu_vmx *vmx = to_vmx(vcpu);
7790 int old = vmx->ple_window;
7791
Babu Mogerc8e88712018-03-16 16:37:24 -04007792 vmx->ple_window = __grow_ple_window(old, ple_window,
7793 ple_window_grow,
7794 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007795
7796 if (vmx->ple_window != old)
7797 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007798
7799 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007800}
7801
7802static void shrink_ple_window(struct kvm_vcpu *vcpu)
7803{
7804 struct vcpu_vmx *vmx = to_vmx(vcpu);
7805 int old = vmx->ple_window;
7806
Babu Mogerc8e88712018-03-16 16:37:24 -04007807 vmx->ple_window = __shrink_ple_window(old, ple_window,
7808 ple_window_shrink,
7809 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007810
7811 if (vmx->ple_window != old)
7812 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007813
7814 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007815}
7816
7817/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007818 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7819 */
7820static void wakeup_handler(void)
7821{
7822 struct kvm_vcpu *vcpu;
7823 int cpu = smp_processor_id();
7824
7825 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7826 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7827 blocked_vcpu_list) {
7828 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7829
7830 if (pi_test_on(pi_desc) == 1)
7831 kvm_vcpu_kick(vcpu);
7832 }
7833 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7834}
7835
Peng Haoe01bca22018-04-07 05:47:32 +08007836static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007837{
7838 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7839 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7840 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7841 0ull, VMX_EPT_EXECUTABLE_MASK,
7842 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007843 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007844
7845 ept_set_mmio_spte_mask();
7846 kvm_enable_tdp();
7847}
7848
Tiejun Chenf2c76482014-10-28 10:14:47 +08007849static __init int hardware_setup(void)
7850{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007851 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007852 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007853
7854 rdmsrl_safe(MSR_EFER, &host_efer);
7855
7856 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7857 kvm_define_shared_msr(i, vmx_msr_index[i]);
7858
Radim Krčmář23611332016-09-29 22:41:33 +02007859 for (i = 0; i < VMX_BITMAP_NR; i++) {
7860 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7861 if (!vmx_bitmap[i])
7862 goto out;
7863 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007864
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007865 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7866 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7867
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007868 if (setup_vmcs_config(&vmcs_config) < 0) {
7869 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007870 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007871 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007872
7873 if (boot_cpu_has(X86_FEATURE_NX))
7874 kvm_enable_efer_bits(EFER_NX);
7875
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007876 if (boot_cpu_has(X86_FEATURE_MPX)) {
7877 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7878 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7879 }
7880
Wanpeng Li08d839c2017-03-23 05:30:08 -07007881 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7882 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007883 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007884
Tiejun Chenf2c76482014-10-28 10:14:47 +08007885 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007886 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007887 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007888 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007889 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007890
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007891 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007892 enable_ept_ad_bits = 0;
7893
Wanpeng Li8ad81822017-10-09 15:51:53 -07007894 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007895 enable_unrestricted_guest = 0;
7896
Paolo Bonziniad15a292015-01-30 16:18:49 +01007897 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007898 flexpriority_enabled = 0;
7899
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007900 if (!cpu_has_virtual_nmis())
7901 enable_vnmi = 0;
7902
Paolo Bonziniad15a292015-01-30 16:18:49 +01007903 /*
7904 * set_apic_access_page_addr() is used to reload apic access
7905 * page upon invalidation. No need to do anything if not
7906 * using the APIC_ACCESS_ADDR VMCS field.
7907 */
7908 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007909 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007910
7911 if (!cpu_has_vmx_tpr_shadow())
7912 kvm_x86_ops->update_cr8_intercept = NULL;
7913
7914 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7915 kvm_disable_largepages();
7916
Tianyu Lan877ad952018-07-19 08:40:23 +00007917#if IS_ENABLED(CONFIG_HYPERV)
7918 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7919 && enable_ept)
7920 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7921#endif
7922
Wanpeng Li0f107682017-09-28 18:06:24 -07007923 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007924 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007925 ple_window = 0;
7926 ple_window_grow = 0;
7927 ple_window_max = 0;
7928 ple_window_shrink = 0;
7929 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007930
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007931 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007932 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007933 kvm_x86_ops->sync_pir_to_irr = NULL;
7934 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007935
Haozhong Zhang64903d62015-10-20 15:39:09 +08007936 if (cpu_has_vmx_tsc_scaling()) {
7937 kvm_has_tsc_control = true;
7938 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7939 kvm_tsc_scaling_ratio_frac_bits = 48;
7940 }
7941
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007942 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7943
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007944 if (enable_ept)
7945 vmx_enable_tdp();
7946 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007947 kvm_disable_tdp();
7948
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007949 if (!nested) {
7950 kvm_x86_ops->get_nested_state = NULL;
7951 kvm_x86_ops->set_nested_state = NULL;
7952 }
7953
Kai Huang843e4332015-01-28 10:54:28 +08007954 /*
7955 * Only enable PML when hardware supports PML feature, and both EPT
7956 * and EPT A/D bit features are enabled -- PML depends on them to work.
7957 */
7958 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7959 enable_pml = 0;
7960
7961 if (!enable_pml) {
7962 kvm_x86_ops->slot_enable_log_dirty = NULL;
7963 kvm_x86_ops->slot_disable_log_dirty = NULL;
7964 kvm_x86_ops->flush_log_dirty = NULL;
7965 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7966 }
7967
Sean Christophersond264ee02018-08-27 15:21:12 -07007968 if (!cpu_has_vmx_preemption_timer())
7969 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7970
Yunhong Jiang64672c92016-06-13 14:19:59 -07007971 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7972 u64 vmx_msr;
7973
7974 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7975 cpu_preemption_timer_multi =
7976 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7977 } else {
7978 kvm_x86_ops->set_hv_timer = NULL;
7979 kvm_x86_ops->cancel_hv_timer = NULL;
7980 }
7981
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007982 if (!cpu_has_vmx_shadow_vmcs())
7983 enable_shadow_vmcs = 0;
7984 if (enable_shadow_vmcs)
7985 init_vmcs_shadow_fields();
7986
Feng Wubf9f6ac2015-09-18 22:29:55 +08007987 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007988 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007989
Ashok Rajc45dcc72016-06-22 14:59:56 +08007990 kvm_mce_cap_supported |= MCG_LMCE_P;
7991
Tiejun Chenf2c76482014-10-28 10:14:47 +08007992 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007993
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007994out:
Radim Krčmář23611332016-09-29 22:41:33 +02007995 for (i = 0; i < VMX_BITMAP_NR; i++)
7996 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007997
7998 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007999}
8000
8001static __exit void hardware_unsetup(void)
8002{
Radim Krčmář23611332016-09-29 22:41:33 +02008003 int i;
8004
8005 for (i = 0; i < VMX_BITMAP_NR; i++)
8006 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008007
Tiejun Chenf2c76482014-10-28 10:14:47 +08008008 free_kvm_area();
8009}
8010
Avi Kivity6aa8b732006-12-10 02:21:36 -08008011/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008012 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8013 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8014 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008015static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008016{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008017 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008018 grow_ple_window(vcpu);
8019
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008020 /*
8021 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8022 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8023 * never set PAUSE_EXITING and just set PLE if supported,
8024 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8025 */
8026 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008027 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008028}
8029
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008030static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008031{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008032 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008033}
8034
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008035static int handle_mwait(struct kvm_vcpu *vcpu)
8036{
8037 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8038 return handle_nop(vcpu);
8039}
8040
Jim Mattson45ec3682017-08-23 16:32:04 -07008041static int handle_invalid_op(struct kvm_vcpu *vcpu)
8042{
8043 kvm_queue_exception(vcpu, UD_VECTOR);
8044 return 1;
8045}
8046
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008047static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8048{
8049 return 1;
8050}
8051
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008052static int handle_monitor(struct kvm_vcpu *vcpu)
8053{
8054 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8055 return handle_nop(vcpu);
8056}
8057
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008058/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008059 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8060 * set the success or error code of an emulated VMX instruction, as specified
8061 * by Vol 2B, VMX Instruction Reference, "Conventions".
8062 */
8063static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8064{
8065 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8066 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8067 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8068}
8069
8070static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8071{
8072 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8073 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8074 X86_EFLAGS_SF | X86_EFLAGS_OF))
8075 | X86_EFLAGS_CF);
8076}
8077
Abel Gordon145c28d2013-04-18 14:36:55 +03008078static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008079 u32 vm_instruction_error)
8080{
8081 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8082 /*
8083 * failValid writes the error number to the current VMCS, which
8084 * can't be done there isn't a current VMCS.
8085 */
8086 nested_vmx_failInvalid(vcpu);
8087 return;
8088 }
8089 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8090 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8091 X86_EFLAGS_SF | X86_EFLAGS_OF))
8092 | X86_EFLAGS_ZF);
8093 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8094 /*
8095 * We don't need to force a shadow sync because
8096 * VM_INSTRUCTION_ERROR is not shadowed
8097 */
8098}
Abel Gordon145c28d2013-04-18 14:36:55 +03008099
Wincy Vanff651cb2014-12-11 08:52:58 +03008100static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8101{
8102 /* TODO: not to reset guest simply here. */
8103 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008104 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008105}
8106
Jan Kiszkaf4124502014-03-07 20:03:13 +01008107static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8108{
8109 struct vcpu_vmx *vmx =
8110 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8111
8112 vmx->nested.preemption_timer_expired = true;
8113 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8114 kvm_vcpu_kick(&vmx->vcpu);
8115
8116 return HRTIMER_NORESTART;
8117}
8118
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008119/*
Bandan Das19677e32014-05-06 02:19:15 -04008120 * Decode the memory-address operand of a vmx instruction, as recorded on an
8121 * exit caused by such an instruction (run by a guest hypervisor).
8122 * On success, returns 0. When the operand is invalid, returns 1 and throws
8123 * #UD or #GP.
8124 */
8125static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8126 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008127 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008128{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008129 gva_t off;
8130 bool exn;
8131 struct kvm_segment s;
8132
Bandan Das19677e32014-05-06 02:19:15 -04008133 /*
8134 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8135 * Execution", on an exit, vmx_instruction_info holds most of the
8136 * addressing components of the operand. Only the displacement part
8137 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8138 * For how an actual address is calculated from all these components,
8139 * refer to Vol. 1, "Operand Addressing".
8140 */
8141 int scaling = vmx_instruction_info & 3;
8142 int addr_size = (vmx_instruction_info >> 7) & 7;
8143 bool is_reg = vmx_instruction_info & (1u << 10);
8144 int seg_reg = (vmx_instruction_info >> 15) & 7;
8145 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8146 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8147 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8148 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8149
8150 if (is_reg) {
8151 kvm_queue_exception(vcpu, UD_VECTOR);
8152 return 1;
8153 }
8154
8155 /* Addr = segment_base + offset */
8156 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008158 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008159 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008160 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008161 off += kvm_register_read(vcpu, index_reg)<<scaling;
8162 vmx_get_segment(vcpu, &s, seg_reg);
8163 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008164
8165 if (addr_size == 1) /* 32 bit */
8166 *ret &= 0xffffffff;
8167
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008168 /* Checks for #GP/#SS exceptions. */
8169 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008170 if (is_long_mode(vcpu)) {
8171 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8172 * non-canonical form. This is the only check on the memory
8173 * destination for long mode!
8174 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008175 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008176 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008177 /* Protected mode: apply checks for segment validity in the
8178 * following order:
8179 * - segment type check (#GP(0) may be thrown)
8180 * - usability check (#GP(0)/#SS(0))
8181 * - limit check (#GP(0)/#SS(0))
8182 */
8183 if (wr)
8184 /* #GP(0) if the destination operand is located in a
8185 * read-only data segment or any code segment.
8186 */
8187 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8188 else
8189 /* #GP(0) if the source operand is located in an
8190 * execute-only code segment
8191 */
8192 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008193 if (exn) {
8194 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8195 return 1;
8196 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008197 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8198 */
8199 exn = (s.unusable != 0);
8200 /* Protected mode: #GP(0)/#SS(0) if the memory
8201 * operand is outside the segment limit.
8202 */
8203 exn = exn || (off + sizeof(u64) > s.limit);
8204 }
8205 if (exn) {
8206 kvm_queue_exception_e(vcpu,
8207 seg_reg == VCPU_SREG_SS ?
8208 SS_VECTOR : GP_VECTOR,
8209 0);
8210 return 1;
8211 }
8212
Bandan Das19677e32014-05-06 02:19:15 -04008213 return 0;
8214}
8215
Radim Krčmářcbf71272017-05-19 15:48:51 +02008216static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008217{
8218 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008219 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008220
8221 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008222 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008223 return 1;
8224
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008225 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008226 kvm_inject_page_fault(vcpu, &e);
8227 return 1;
8228 }
8229
Bandan Das3573e222014-05-06 02:19:16 -04008230 return 0;
8231}
8232
Liran Alonabfc52c2018-06-23 02:35:13 +03008233/*
8234 * Allocate a shadow VMCS and associate it with the currently loaded
8235 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8236 * VMCS is also VMCLEARed, so that it is ready for use.
8237 */
8238static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8239{
8240 struct vcpu_vmx *vmx = to_vmx(vcpu);
8241 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8242
8243 /*
8244 * We should allocate a shadow vmcs for vmcs01 only when L1
8245 * executes VMXON and free it when L1 executes VMXOFF.
8246 * As it is invalid to execute VMXON twice, we shouldn't reach
8247 * here when vmcs01 already have an allocated shadow vmcs.
8248 */
8249 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8250
8251 if (!loaded_vmcs->shadow_vmcs) {
8252 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8253 if (loaded_vmcs->shadow_vmcs)
8254 vmcs_clear(loaded_vmcs->shadow_vmcs);
8255 }
8256 return loaded_vmcs->shadow_vmcs;
8257}
8258
Jim Mattsone29acc52016-11-30 12:03:43 -08008259static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8260{
8261 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008262 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008263
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008264 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8265 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008266 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008267
8268 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8269 if (!vmx->nested.cached_vmcs12)
8270 goto out_cached_vmcs12;
8271
Liran Alon61ada742018-06-23 02:35:08 +03008272 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8273 if (!vmx->nested.cached_shadow_vmcs12)
8274 goto out_cached_shadow_vmcs12;
8275
Liran Alonabfc52c2018-06-23 02:35:13 +03008276 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8277 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008278
Jim Mattsone29acc52016-11-30 12:03:43 -08008279 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8280 HRTIMER_MODE_REL_PINNED);
8281 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8282
Roman Kagan63aff652018-07-19 21:59:07 +03008283 vmx->nested.vpid02 = allocate_vpid();
8284
Jim Mattsone29acc52016-11-30 12:03:43 -08008285 vmx->nested.vmxon = true;
8286 return 0;
8287
8288out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008289 kfree(vmx->nested.cached_shadow_vmcs12);
8290
8291out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008292 kfree(vmx->nested.cached_vmcs12);
8293
8294out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008295 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008296
Jim Mattsonde3a0022017-11-27 17:22:25 -06008297out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008298 return -ENOMEM;
8299}
8300
Bandan Das3573e222014-05-06 02:19:16 -04008301/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008302 * Emulate the VMXON instruction.
8303 * Currently, we just remember that VMX is active, and do not save or even
8304 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8305 * do not currently need to store anything in that guest-allocated memory
8306 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8307 * argument is different from the VMXON pointer (which the spec says they do).
8308 */
8309static int handle_vmon(struct kvm_vcpu *vcpu)
8310{
Jim Mattsone29acc52016-11-30 12:03:43 -08008311 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008312 gpa_t vmptr;
8313 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008314 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008315 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8316 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008317
Jim Mattson70f3aac2017-04-26 08:53:46 -07008318 /*
8319 * The Intel VMX Instruction Reference lists a bunch of bits that are
8320 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8321 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8322 * Otherwise, we should fail with #UD. But most faulting conditions
8323 * have already been checked by hardware, prior to the VM-exit for
8324 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8325 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008326 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008327 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008328 kvm_queue_exception(vcpu, UD_VECTOR);
8329 return 1;
8330 }
8331
Felix Wilhelm727ba742018-06-11 09:43:44 +02008332 /* CPL=0 must be checked manually. */
8333 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008334 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008335 return 1;
8336 }
8337
Abel Gordon145c28d2013-04-18 14:36:55 +03008338 if (vmx->nested.vmxon) {
8339 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008340 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008341 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008342
Haozhong Zhang3b840802016-06-22 14:59:54 +08008343 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008344 != VMXON_NEEDED_FEATURES) {
8345 kvm_inject_gp(vcpu, 0);
8346 return 1;
8347 }
8348
Radim Krčmářcbf71272017-05-19 15:48:51 +02008349 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008350 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008351
8352 /*
8353 * SDM 3: 24.11.5
8354 * The first 4 bytes of VMXON region contain the supported
8355 * VMCS revision identifier
8356 *
8357 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8358 * which replaces physical address width with 32
8359 */
8360 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8361 nested_vmx_failInvalid(vcpu);
8362 return kvm_skip_emulated_instruction(vcpu);
8363 }
8364
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008365 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8366 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008367 nested_vmx_failInvalid(vcpu);
8368 return kvm_skip_emulated_instruction(vcpu);
8369 }
8370 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8371 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008372 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008373 nested_vmx_failInvalid(vcpu);
8374 return kvm_skip_emulated_instruction(vcpu);
8375 }
8376 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008377 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008378
8379 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008380 ret = enter_vmx_operation(vcpu);
8381 if (ret)
8382 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008383
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008384 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008385 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008386}
8387
8388/*
8389 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8390 * for running VMX instructions (except VMXON, whose prerequisites are
8391 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008392 * Note that many of these exceptions have priority over VM exits, so they
8393 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008394 */
8395static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8396{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008397 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008398 kvm_queue_exception(vcpu, UD_VECTOR);
8399 return 0;
8400 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008401
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008402 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008403 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008404 return 0;
8405 }
8406
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008407 return 1;
8408}
8409
David Matlack8ca44e82017-08-01 14:00:39 -07008410static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8411{
8412 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8413 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8414}
8415
Abel Gordone7953d72013-04-18 14:37:55 +03008416static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8417{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008418 if (vmx->nested.current_vmptr == -1ull)
8419 return;
8420
Abel Gordon012f83c2013-04-18 14:39:25 +03008421 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008422 /* copy to memory all shadowed fields in case
8423 they were modified */
8424 copy_shadow_to_vmcs12(vmx);
8425 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008426 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008427 }
Wincy Van705699a2015-02-03 23:58:17 +08008428 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008429
8430 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008431 kvm_vcpu_write_guest_page(&vmx->vcpu,
8432 vmx->nested.current_vmptr >> PAGE_SHIFT,
8433 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008434
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008435 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008436}
8437
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008438/*
8439 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8440 * just stops using VMX.
8441 */
8442static void free_nested(struct vcpu_vmx *vmx)
8443{
Wanpeng Lib7455822017-11-22 14:04:00 -08008444 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008445 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008446
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008447 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008448 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008449 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008450 vmx->nested.posted_intr_nv = -1;
8451 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008452 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008453 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008454 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8455 free_vmcs(vmx->vmcs01.shadow_vmcs);
8456 vmx->vmcs01.shadow_vmcs = NULL;
8457 }
David Matlack4f2777b2016-07-13 17:16:37 -07008458 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008459 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008460 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008461 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008462 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008463 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008464 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008465 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008466 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008467 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008468 }
Wincy Van705699a2015-02-03 23:58:17 +08008469 if (vmx->nested.pi_desc_page) {
8470 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008471 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008472 vmx->nested.pi_desc_page = NULL;
8473 vmx->nested.pi_desc = NULL;
8474 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008475
Jim Mattsonde3a0022017-11-27 17:22:25 -06008476 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008477}
8478
8479/* Emulate the VMXOFF instruction */
8480static int handle_vmoff(struct kvm_vcpu *vcpu)
8481{
8482 if (!nested_vmx_check_permission(vcpu))
8483 return 1;
8484 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008485 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008486 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008487}
8488
Nadav Har'El27d6c862011-05-25 23:06:59 +03008489/* Emulate the VMCLEAR instruction */
8490static int handle_vmclear(struct kvm_vcpu *vcpu)
8491{
8492 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008493 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008494 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008495
8496 if (!nested_vmx_check_permission(vcpu))
8497 return 1;
8498
Radim Krčmářcbf71272017-05-19 15:48:51 +02008499 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008500 return 1;
8501
Radim Krčmářcbf71272017-05-19 15:48:51 +02008502 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8503 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8504 return kvm_skip_emulated_instruction(vcpu);
8505 }
8506
8507 if (vmptr == vmx->nested.vmxon_ptr) {
8508 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8509 return kvm_skip_emulated_instruction(vcpu);
8510 }
8511
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008512 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008513 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008514
Jim Mattson587d7e722017-03-02 12:41:48 -08008515 kvm_vcpu_write_guest(vcpu,
8516 vmptr + offsetof(struct vmcs12, launch_state),
8517 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008518
Nadav Har'El27d6c862011-05-25 23:06:59 +03008519 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008520 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008521}
8522
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008523static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8524
8525/* Emulate the VMLAUNCH instruction */
8526static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8527{
8528 return nested_vmx_run(vcpu, true);
8529}
8530
8531/* Emulate the VMRESUME instruction */
8532static int handle_vmresume(struct kvm_vcpu *vcpu)
8533{
8534
8535 return nested_vmx_run(vcpu, false);
8536}
8537
Nadav Har'El49f705c2011-05-25 23:08:30 +03008538/*
8539 * Read a vmcs12 field. Since these can have varying lengths and we return
8540 * one type, we chose the biggest type (u64) and zero-extend the return value
8541 * to that size. Note that the caller, handle_vmread, might need to use only
8542 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8543 * 64-bit fields are to be returned).
8544 */
Liran Alone2536742018-06-23 02:35:02 +03008545static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008546 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008547{
8548 short offset = vmcs_field_to_offset(field);
8549 char *p;
8550
8551 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008552 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553
Liran Alone2536742018-06-23 02:35:02 +03008554 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008555
Jim Mattsond37f4262017-12-22 12:12:16 -08008556 switch (vmcs_field_width(field)) {
8557 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008558 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008559 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008560 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008562 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008563 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008565 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008566 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008569 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008570 WARN_ON(1);
8571 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008572 }
8573}
8574
Abel Gordon20b97fe2013-04-18 14:36:25 +03008575
Liran Alone2536742018-06-23 02:35:02 +03008576static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008577 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008579 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008580 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008581 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008582
Jim Mattsond37f4262017-12-22 12:12:16 -08008583 switch (vmcs_field_width(field)) {
8584 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008585 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008586 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008587 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008588 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008589 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008590 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008592 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008593 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008596 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008597 WARN_ON(1);
8598 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008599 }
8600
8601}
8602
Jim Mattsonf4160e42018-05-29 09:11:33 -07008603/*
8604 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8605 * they have been modified by the L1 guest. Note that the "read-only"
8606 * VM-exit information fields are actually writable if the vCPU is
8607 * configured to support "VMWRITE to any supported field in the VMCS."
8608 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008609static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8610{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008611 const u16 *fields[] = {
8612 shadow_read_write_fields,
8613 shadow_read_only_fields
8614 };
8615 const int max_fields[] = {
8616 max_shadow_read_write_fields,
8617 max_shadow_read_only_fields
8618 };
8619 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008620 unsigned long field;
8621 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008622 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008623
Jan Kiszka282da872014-10-08 18:05:39 +02008624 preempt_disable();
8625
Abel Gordon16f5b902013-04-18 14:38:25 +03008626 vmcs_load(shadow_vmcs);
8627
Jim Mattsonf4160e42018-05-29 09:11:33 -07008628 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8629 for (i = 0; i < max_fields[q]; i++) {
8630 field = fields[q][i];
8631 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008632 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008633 }
8634 /*
8635 * Skip the VM-exit information fields if they are read-only.
8636 */
8637 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8638 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008639 }
8640
8641 vmcs_clear(shadow_vmcs);
8642 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008643
8644 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008645}
8646
Abel Gordonc3114422013-04-18 14:38:55 +03008647static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8648{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008649 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008650 shadow_read_write_fields,
8651 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008652 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008653 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008654 max_shadow_read_write_fields,
8655 max_shadow_read_only_fields
8656 };
8657 int i, q;
8658 unsigned long field;
8659 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008660 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008661
8662 vmcs_load(shadow_vmcs);
8663
Mathias Krausec2bae892013-06-26 20:36:21 +02008664 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008665 for (i = 0; i < max_fields[q]; i++) {
8666 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008667 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008668 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008669 }
8670 }
8671
8672 vmcs_clear(shadow_vmcs);
8673 vmcs_load(vmx->loaded_vmcs->vmcs);
8674}
8675
Nadav Har'El49f705c2011-05-25 23:08:30 +03008676/*
8677 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8678 * used before) all generate the same failure when it is missing.
8679 */
8680static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8681{
8682 struct vcpu_vmx *vmx = to_vmx(vcpu);
8683 if (vmx->nested.current_vmptr == -1ull) {
8684 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008685 return 0;
8686 }
8687 return 1;
8688}
8689
8690static int handle_vmread(struct kvm_vcpu *vcpu)
8691{
8692 unsigned long field;
8693 u64 field_value;
8694 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8695 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8696 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008697 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008698
Kyle Hueyeb277562016-11-29 12:40:39 -08008699 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008700 return 1;
8701
Kyle Huey6affcbe2016-11-29 12:40:40 -08008702 if (!nested_vmx_check_vmcs12(vcpu))
8703 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008704
Liran Alon6d894f42018-06-23 02:35:09 +03008705 if (!is_guest_mode(vcpu))
8706 vmcs12 = get_vmcs12(vcpu);
8707 else {
8708 /*
8709 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8710 * to shadowed-field sets the ALU flags for VMfailInvalid.
8711 */
8712 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8713 nested_vmx_failInvalid(vcpu);
8714 return kvm_skip_emulated_instruction(vcpu);
8715 }
8716 vmcs12 = get_shadow_vmcs12(vcpu);
8717 }
8718
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008720 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008721 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008722 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008724 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008725 }
8726 /*
8727 * Now copy part of this value to register or memory, as requested.
8728 * Note that the number of bits actually copied is 32 or 64 depending
8729 * on the guest's mode (32 or 64 bit), not on the given field's length.
8730 */
8731 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008732 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008733 field_value);
8734 } else {
8735 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008736 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008737 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008738 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008739 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8740 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008741 }
8742
8743 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008744 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008745}
8746
8747
8748static int handle_vmwrite(struct kvm_vcpu *vcpu)
8749{
8750 unsigned long field;
8751 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008752 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008753 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8754 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008755
Nadav Har'El49f705c2011-05-25 23:08:30 +03008756 /* The value to write might be 32 or 64 bits, depending on L1's long
8757 * mode, and eventually we need to write that into a field of several
8758 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008759 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008760 * bits into the vmcs12 field.
8761 */
8762 u64 field_value = 0;
8763 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008764 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008765
Kyle Hueyeb277562016-11-29 12:40:39 -08008766 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008767 return 1;
8768
Kyle Huey6affcbe2016-11-29 12:40:40 -08008769 if (!nested_vmx_check_vmcs12(vcpu))
8770 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008771
Nadav Har'El49f705c2011-05-25 23:08:30 +03008772 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008773 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008774 (((vmx_instruction_info) >> 3) & 0xf));
8775 else {
8776 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008777 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008778 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008779 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8780 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008781 kvm_inject_page_fault(vcpu, &e);
8782 return 1;
8783 }
8784 }
8785
8786
Nadav Amit27e6fb52014-06-18 17:19:26 +03008787 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008788 /*
8789 * If the vCPU supports "VMWRITE to any supported field in the
8790 * VMCS," then the "read-only" fields are actually read/write.
8791 */
8792 if (vmcs_field_readonly(field) &&
8793 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008794 nested_vmx_failValid(vcpu,
8795 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008796 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008797 }
8798
Liran Alon6d894f42018-06-23 02:35:09 +03008799 if (!is_guest_mode(vcpu))
8800 vmcs12 = get_vmcs12(vcpu);
8801 else {
8802 /*
8803 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8804 * to shadowed-field sets the ALU flags for VMfailInvalid.
8805 */
8806 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8807 nested_vmx_failInvalid(vcpu);
8808 return kvm_skip_emulated_instruction(vcpu);
8809 }
8810 vmcs12 = get_shadow_vmcs12(vcpu);
8811
8812 }
8813
8814 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008815 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008816 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008817 }
8818
Liran Alon6d894f42018-06-23 02:35:09 +03008819 /*
8820 * Do not track vmcs12 dirty-state if in guest-mode
8821 * as we actually dirty shadow vmcs12 instead of vmcs12.
8822 */
8823 if (!is_guest_mode(vcpu)) {
8824 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008825#define SHADOW_FIELD_RW(x) case x:
8826#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008827 /*
8828 * The fields that can be updated by L1 without a vmexit are
8829 * always updated in the vmcs02, the others go down the slow
8830 * path of prepare_vmcs02.
8831 */
8832 break;
8833 default:
8834 vmx->nested.dirty_vmcs12 = true;
8835 break;
8836 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008837 }
8838
Nadav Har'El49f705c2011-05-25 23:08:30 +03008839 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008840 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008841}
8842
Jim Mattsona8bc2842016-11-30 12:03:44 -08008843static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8844{
8845 vmx->nested.current_vmptr = vmptr;
8846 if (enable_shadow_vmcs) {
8847 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8848 SECONDARY_EXEC_SHADOW_VMCS);
8849 vmcs_write64(VMCS_LINK_POINTER,
8850 __pa(vmx->vmcs01.shadow_vmcs));
8851 vmx->nested.sync_shadow_vmcs = true;
8852 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008853 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008854}
8855
Nadav Har'El63846662011-05-25 23:07:29 +03008856/* Emulate the VMPTRLD instruction */
8857static int handle_vmptrld(struct kvm_vcpu *vcpu)
8858{
8859 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008860 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008861
8862 if (!nested_vmx_check_permission(vcpu))
8863 return 1;
8864
Radim Krčmářcbf71272017-05-19 15:48:51 +02008865 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008866 return 1;
8867
Radim Krčmářcbf71272017-05-19 15:48:51 +02008868 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8869 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8870 return kvm_skip_emulated_instruction(vcpu);
8871 }
8872
8873 if (vmptr == vmx->nested.vmxon_ptr) {
8874 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8875 return kvm_skip_emulated_instruction(vcpu);
8876 }
8877
Nadav Har'El63846662011-05-25 23:07:29 +03008878 if (vmx->nested.current_vmptr != vmptr) {
8879 struct vmcs12 *new_vmcs12;
8880 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008881 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8882 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008883 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008884 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008885 }
8886 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008887 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008888 (new_vmcs12->hdr.shadow_vmcs &&
8889 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008890 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008891 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008892 nested_vmx_failValid(vcpu,
8893 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008894 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008895 }
Nadav Har'El63846662011-05-25 23:07:29 +03008896
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008897 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008898 /*
8899 * Load VMCS12 from guest memory since it is not already
8900 * cached.
8901 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008902 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8903 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008904 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008905
Jim Mattsona8bc2842016-11-30 12:03:44 -08008906 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008907 }
8908
8909 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008910 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008911}
8912
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008913/* Emulate the VMPTRST instruction */
8914static int handle_vmptrst(struct kvm_vcpu *vcpu)
8915{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008916 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8917 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8918 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008919 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008920 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008921
8922 if (!nested_vmx_check_permission(vcpu))
8923 return 1;
8924
Sean Christopherson0a06d422018-07-19 10:31:00 -07008925 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008926 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008927 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008928 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8929 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008930 kvm_inject_page_fault(vcpu, &e);
8931 return 1;
8932 }
8933 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008934 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008935}
8936
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008937/* Emulate the INVEPT instruction */
8938static int handle_invept(struct kvm_vcpu *vcpu)
8939{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008940 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008941 u32 vmx_instruction_info, types;
8942 unsigned long type;
8943 gva_t gva;
8944 struct x86_exception e;
8945 struct {
8946 u64 eptp, gpa;
8947 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008948
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008949 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008950 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008951 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008952 kvm_queue_exception(vcpu, UD_VECTOR);
8953 return 1;
8954 }
8955
8956 if (!nested_vmx_check_permission(vcpu))
8957 return 1;
8958
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008960 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008962 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008963
Jim Mattson85c856b2016-10-26 08:38:38 -07008964 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008965 nested_vmx_failValid(vcpu,
8966 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008967 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008968 }
8969
8970 /* According to the Intel VMX instruction reference, the memory
8971 * operand is read even if it isn't needed (e.g., for type==global)
8972 */
8973 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008974 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008975 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008976 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008977 kvm_inject_page_fault(vcpu, &e);
8978 return 1;
8979 }
8980
8981 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008982 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008983 /*
8984 * TODO: track mappings and invalidate
8985 * single context requests appropriately
8986 */
8987 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008988 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008989 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008990 nested_vmx_succeed(vcpu);
8991 break;
8992 default:
8993 BUG_ON(1);
8994 break;
8995 }
8996
Kyle Huey6affcbe2016-11-29 12:40:40 -08008997 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008998}
8999
Liran Alon3d5bdae2018-10-08 23:42:18 +03009000static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9001{
9002 struct vcpu_vmx *vmx = to_vmx(vcpu);
9003
9004 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9005}
9006
Petr Matouseka642fc32014-09-23 20:22:30 +02009007static int handle_invvpid(struct kvm_vcpu *vcpu)
9008{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009009 struct vcpu_vmx *vmx = to_vmx(vcpu);
9010 u32 vmx_instruction_info;
9011 unsigned long type, types;
9012 gva_t gva;
9013 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009014 struct {
9015 u64 vpid;
9016 u64 gla;
9017 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009018 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009019
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009020 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009021 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009022 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009023 kvm_queue_exception(vcpu, UD_VECTOR);
9024 return 1;
9025 }
9026
9027 if (!nested_vmx_check_permission(vcpu))
9028 return 1;
9029
9030 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9031 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9032
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009033 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009034 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009035
Jim Mattson85c856b2016-10-26 08:38:38 -07009036 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009037 nested_vmx_failValid(vcpu,
9038 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009039 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009040 }
9041
9042 /* according to the intel vmx instruction reference, the memory
9043 * operand is read even if it isn't needed (e.g., for type==global)
9044 */
9045 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9046 vmx_instruction_info, false, &gva))
9047 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009048 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009049 kvm_inject_page_fault(vcpu, &e);
9050 return 1;
9051 }
Jim Mattson40352602017-06-28 09:37:37 -07009052 if (operand.vpid >> 16) {
9053 nested_vmx_failValid(vcpu,
9054 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9055 return kvm_skip_emulated_instruction(vcpu);
9056 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009057
Liran Alon3d5bdae2018-10-08 23:42:18 +03009058 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009059 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009060 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009061 if (!operand.vpid ||
9062 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009063 nested_vmx_failValid(vcpu,
9064 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9065 return kvm_skip_emulated_instruction(vcpu);
9066 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009067 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009068 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009069 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009070 } else
Liran Alon327c0722018-10-08 23:42:19 +03009071 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009072 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009073 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009074 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009075 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009076 nested_vmx_failValid(vcpu,
9077 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009078 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009079 }
Liran Alon327c0722018-10-08 23:42:19 +03009080 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009081 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009082 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009083 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009084 break;
9085 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009086 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009087 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009088 }
9089
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009090 nested_vmx_succeed(vcpu);
9091
Kyle Huey6affcbe2016-11-29 12:40:40 -08009092 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009093}
9094
Junaid Shahideb4b2482018-06-27 14:59:14 -07009095static int handle_invpcid(struct kvm_vcpu *vcpu)
9096{
9097 u32 vmx_instruction_info;
9098 unsigned long type;
9099 bool pcid_enabled;
9100 gva_t gva;
9101 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009102 unsigned i;
9103 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009104 struct {
9105 u64 pcid;
9106 u64 gla;
9107 } operand;
9108
9109 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9110 kvm_queue_exception(vcpu, UD_VECTOR);
9111 return 1;
9112 }
9113
9114 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9115 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9116
9117 if (type > 3) {
9118 kvm_inject_gp(vcpu, 0);
9119 return 1;
9120 }
9121
9122 /* According to the Intel instruction reference, the memory operand
9123 * is read even if it isn't needed (e.g., for type==all)
9124 */
9125 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9126 vmx_instruction_info, false, &gva))
9127 return 1;
9128
9129 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9130 kvm_inject_page_fault(vcpu, &e);
9131 return 1;
9132 }
9133
9134 if (operand.pcid >> 12 != 0) {
9135 kvm_inject_gp(vcpu, 0);
9136 return 1;
9137 }
9138
9139 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9140
9141 switch (type) {
9142 case INVPCID_TYPE_INDIV_ADDR:
9143 if ((!pcid_enabled && (operand.pcid != 0)) ||
9144 is_noncanonical_address(operand.gla, vcpu)) {
9145 kvm_inject_gp(vcpu, 0);
9146 return 1;
9147 }
9148 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9149 return kvm_skip_emulated_instruction(vcpu);
9150
9151 case INVPCID_TYPE_SINGLE_CTXT:
9152 if (!pcid_enabled && (operand.pcid != 0)) {
9153 kvm_inject_gp(vcpu, 0);
9154 return 1;
9155 }
9156
9157 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9158 kvm_mmu_sync_roots(vcpu);
9159 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9160 }
9161
Junaid Shahidb94742c2018-06-27 14:59:20 -07009162 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9163 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9164 == operand.pcid)
9165 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009166
Junaid Shahidb94742c2018-06-27 14:59:20 -07009167 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009168 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009169 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009170 * given PCID, then nothing needs to be done here because a
9171 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009172 */
9173
9174 return kvm_skip_emulated_instruction(vcpu);
9175
9176 case INVPCID_TYPE_ALL_NON_GLOBAL:
9177 /*
9178 * Currently, KVM doesn't mark global entries in the shadow
9179 * page tables, so a non-global flush just degenerates to a
9180 * global flush. If needed, we could optimize this later by
9181 * keeping track of global entries in shadow page tables.
9182 */
9183
9184 /* fall-through */
9185 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9186 kvm_mmu_unload(vcpu);
9187 return kvm_skip_emulated_instruction(vcpu);
9188
9189 default:
9190 BUG(); /* We have already checked above that type <= 3 */
9191 }
9192}
9193
Kai Huang843e4332015-01-28 10:54:28 +08009194static int handle_pml_full(struct kvm_vcpu *vcpu)
9195{
9196 unsigned long exit_qualification;
9197
9198 trace_kvm_pml_full(vcpu->vcpu_id);
9199
9200 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9201
9202 /*
9203 * PML buffer FULL happened while executing iret from NMI,
9204 * "blocked by NMI" bit has to be set before next VM entry.
9205 */
9206 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009207 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009208 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9209 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9210 GUEST_INTR_STATE_NMI);
9211
9212 /*
9213 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9214 * here.., and there's no userspace involvement needed for PML.
9215 */
9216 return 1;
9217}
9218
Yunhong Jiang64672c92016-06-13 14:19:59 -07009219static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9220{
Sean Christophersond264ee02018-08-27 15:21:12 -07009221 if (!to_vmx(vcpu)->req_immediate_exit)
9222 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009223 return 1;
9224}
9225
Bandan Das41ab9372017-08-03 15:54:43 -04009226static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9227{
9228 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009229 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9230
9231 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009232 switch (address & VMX_EPTP_MT_MASK) {
9233 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009234 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009235 return false;
9236 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009237 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009238 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009239 return false;
9240 break;
9241 default:
9242 return false;
9243 }
9244
David Hildenbrandbb97a012017-08-10 23:15:28 +02009245 /* only 4 levels page-walk length are valid */
9246 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009247 return false;
9248
9249 /* Reserved bits should not be set */
9250 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9251 return false;
9252
9253 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009254 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009255 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009256 return false;
9257 }
9258
9259 return true;
9260}
9261
9262static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9263 struct vmcs12 *vmcs12)
9264{
9265 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9266 u64 address;
9267 bool accessed_dirty;
9268 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9269
9270 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9271 !nested_cpu_has_ept(vmcs12))
9272 return 1;
9273
9274 if (index >= VMFUNC_EPTP_ENTRIES)
9275 return 1;
9276
9277
9278 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9279 &address, index * 8, 8))
9280 return 1;
9281
David Hildenbrandbb97a012017-08-10 23:15:28 +02009282 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009283
9284 /*
9285 * If the (L2) guest does a vmfunc to the currently
9286 * active ept pointer, we don't have to do anything else
9287 */
9288 if (vmcs12->ept_pointer != address) {
9289 if (!valid_ept_address(vcpu, address))
9290 return 1;
9291
9292 kvm_mmu_unload(vcpu);
9293 mmu->ept_ad = accessed_dirty;
9294 mmu->base_role.ad_disabled = !accessed_dirty;
9295 vmcs12->ept_pointer = address;
9296 /*
9297 * TODO: Check what's the correct approach in case
9298 * mmu reload fails. Currently, we just let the next
9299 * reload potentially fail
9300 */
9301 kvm_mmu_reload(vcpu);
9302 }
9303
9304 return 0;
9305}
9306
Bandan Das2a499e42017-08-03 15:54:41 -04009307static int handle_vmfunc(struct kvm_vcpu *vcpu)
9308{
Bandan Das27c42a12017-08-03 15:54:42 -04009309 struct vcpu_vmx *vmx = to_vmx(vcpu);
9310 struct vmcs12 *vmcs12;
9311 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9312
9313 /*
9314 * VMFUNC is only supported for nested guests, but we always enable the
9315 * secondary control for simplicity; for non-nested mode, fake that we
9316 * didn't by injecting #UD.
9317 */
9318 if (!is_guest_mode(vcpu)) {
9319 kvm_queue_exception(vcpu, UD_VECTOR);
9320 return 1;
9321 }
9322
9323 vmcs12 = get_vmcs12(vcpu);
9324 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9325 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009326
9327 switch (function) {
9328 case 0:
9329 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9330 goto fail;
9331 break;
9332 default:
9333 goto fail;
9334 }
9335 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009336
9337fail:
9338 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9339 vmcs_read32(VM_EXIT_INTR_INFO),
9340 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009341 return 1;
9342}
9343
Sean Christopherson0b665d32018-08-14 09:33:34 -07009344static int handle_encls(struct kvm_vcpu *vcpu)
9345{
9346 /*
9347 * SGX virtualization is not yet supported. There is no software
9348 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9349 * to prevent the guest from executing ENCLS.
9350 */
9351 kvm_queue_exception(vcpu, UD_VECTOR);
9352 return 1;
9353}
9354
Nadav Har'El0140cae2011-05-25 23:06:28 +03009355/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009356 * The exit handlers return 1 if the exit was handled fully and guest execution
9357 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9358 * to be done to userspace and return 0.
9359 */
Mathias Krause772e0312012-08-30 01:30:19 +02009360static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009361 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9362 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009363 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009364 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009365 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009366 [EXIT_REASON_CR_ACCESS] = handle_cr,
9367 [EXIT_REASON_DR_ACCESS] = handle_dr,
9368 [EXIT_REASON_CPUID] = handle_cpuid,
9369 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9370 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9371 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9372 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009373 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009374 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009375 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009376 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009377 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009378 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009379 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009380 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009381 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009382 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009383 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009384 [EXIT_REASON_VMOFF] = handle_vmoff,
9385 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009386 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9387 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009388 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009389 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009390 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009391 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009392 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009393 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009394 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9395 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009396 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9397 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009398 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009399 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009400 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009401 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009402 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009403 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009404 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009405 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009406 [EXIT_REASON_XSAVES] = handle_xsaves,
9407 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009408 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009409 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009410 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009411 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009412 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009413};
9414
9415static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009416 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009417
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009418static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9419 struct vmcs12 *vmcs12)
9420{
9421 unsigned long exit_qualification;
9422 gpa_t bitmap, last_bitmap;
9423 unsigned int port;
9424 int size;
9425 u8 b;
9426
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009427 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009428 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009429
9430 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9431
9432 port = exit_qualification >> 16;
9433 size = (exit_qualification & 7) + 1;
9434
9435 last_bitmap = (gpa_t)-1;
9436 b = -1;
9437
9438 while (size > 0) {
9439 if (port < 0x8000)
9440 bitmap = vmcs12->io_bitmap_a;
9441 else if (port < 0x10000)
9442 bitmap = vmcs12->io_bitmap_b;
9443 else
Joe Perches1d804d02015-03-30 16:46:09 -07009444 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009445 bitmap += (port & 0x7fff) / 8;
9446
9447 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009448 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009449 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009450 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009451 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009452
9453 port++;
9454 size--;
9455 last_bitmap = bitmap;
9456 }
9457
Joe Perches1d804d02015-03-30 16:46:09 -07009458 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009459}
9460
Nadav Har'El644d7112011-05-25 23:12:35 +03009461/*
9462 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9463 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9464 * disinterest in the current event (read or write a specific MSR) by using an
9465 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9466 */
9467static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9468 struct vmcs12 *vmcs12, u32 exit_reason)
9469{
9470 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9471 gpa_t bitmap;
9472
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009473 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009474 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009475
9476 /*
9477 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9478 * for the four combinations of read/write and low/high MSR numbers.
9479 * First we need to figure out which of the four to use:
9480 */
9481 bitmap = vmcs12->msr_bitmap;
9482 if (exit_reason == EXIT_REASON_MSR_WRITE)
9483 bitmap += 2048;
9484 if (msr_index >= 0xc0000000) {
9485 msr_index -= 0xc0000000;
9486 bitmap += 1024;
9487 }
9488
9489 /* Then read the msr_index'th bit from this bitmap: */
9490 if (msr_index < 1024*8) {
9491 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009492 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009493 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009494 return 1 & (b >> (msr_index & 7));
9495 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009496 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009497}
9498
9499/*
9500 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9501 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9502 * intercept (via guest_host_mask etc.) the current event.
9503 */
9504static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9505 struct vmcs12 *vmcs12)
9506{
9507 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9508 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009509 int reg;
9510 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009511
9512 switch ((exit_qualification >> 4) & 3) {
9513 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009514 reg = (exit_qualification >> 8) & 15;
9515 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009516 switch (cr) {
9517 case 0:
9518 if (vmcs12->cr0_guest_host_mask &
9519 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009520 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009521 break;
9522 case 3:
9523 if ((vmcs12->cr3_target_count >= 1 &&
9524 vmcs12->cr3_target_value0 == val) ||
9525 (vmcs12->cr3_target_count >= 2 &&
9526 vmcs12->cr3_target_value1 == val) ||
9527 (vmcs12->cr3_target_count >= 3 &&
9528 vmcs12->cr3_target_value2 == val) ||
9529 (vmcs12->cr3_target_count >= 4 &&
9530 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009531 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009532 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009533 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009534 break;
9535 case 4:
9536 if (vmcs12->cr4_guest_host_mask &
9537 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009538 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009539 break;
9540 case 8:
9541 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009542 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009543 break;
9544 }
9545 break;
9546 case 2: /* clts */
9547 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9548 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009549 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009550 break;
9551 case 1: /* mov from cr */
9552 switch (cr) {
9553 case 3:
9554 if (vmcs12->cpu_based_vm_exec_control &
9555 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009556 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009557 break;
9558 case 8:
9559 if (vmcs12->cpu_based_vm_exec_control &
9560 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009561 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009562 break;
9563 }
9564 break;
9565 case 3: /* lmsw */
9566 /*
9567 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9568 * cr0. Other attempted changes are ignored, with no exit.
9569 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009570 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009571 if (vmcs12->cr0_guest_host_mask & 0xe &
9572 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009573 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009574 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9575 !(vmcs12->cr0_read_shadow & 0x1) &&
9576 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009577 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009578 break;
9579 }
Joe Perches1d804d02015-03-30 16:46:09 -07009580 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009581}
9582
Liran Alona7cde482018-06-23 02:35:10 +03009583static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9584 struct vmcs12 *vmcs12, gpa_t bitmap)
9585{
9586 u32 vmx_instruction_info;
9587 unsigned long field;
9588 u8 b;
9589
9590 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9591 return true;
9592
9593 /* Decode instruction info and find the field to access */
9594 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9595 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9596
9597 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9598 if (field >> 15)
9599 return true;
9600
9601 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9602 return true;
9603
9604 return 1 & (b >> (field & 7));
9605}
9606
Nadav Har'El644d7112011-05-25 23:12:35 +03009607/*
9608 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9609 * should handle it ourselves in L0 (and then continue L2). Only call this
9610 * when in is_guest_mode (L2).
9611 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009612static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009613{
Nadav Har'El644d7112011-05-25 23:12:35 +03009614 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9615 struct vcpu_vmx *vmx = to_vmx(vcpu);
9616 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9617
Jim Mattson4f350c62017-09-14 16:31:44 -07009618 if (vmx->nested.nested_run_pending)
9619 return false;
9620
9621 if (unlikely(vmx->fail)) {
9622 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9623 vmcs_read32(VM_INSTRUCTION_ERROR));
9624 return true;
9625 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009626
David Matlackc9f04402017-08-01 14:00:40 -07009627 /*
9628 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009629 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9630 * Page). The CPU may write to these pages via their host
9631 * physical address while L2 is running, bypassing any
9632 * address-translation-based dirty tracking (e.g. EPT write
9633 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009634 *
9635 * Mark them dirty on every exit from L2 to prevent them from
9636 * getting out of sync with dirty tracking.
9637 */
9638 nested_mark_vmcs12_pages_dirty(vcpu);
9639
Jim Mattson4f350c62017-09-14 16:31:44 -07009640 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9641 vmcs_readl(EXIT_QUALIFICATION),
9642 vmx->idt_vectoring_info,
9643 intr_info,
9644 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9645 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009646
9647 switch (exit_reason) {
9648 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009649 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009650 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009651 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009652 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009653 else if (is_debug(intr_info) &&
9654 vcpu->guest_debug &
9655 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9656 return false;
9657 else if (is_breakpoint(intr_info) &&
9658 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9659 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009660 return vmcs12->exception_bitmap &
9661 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9662 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009663 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009665 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009667 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009668 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009669 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009671 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009672 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009673 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009674 case EXIT_REASON_HLT:
9675 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9676 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009677 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009678 case EXIT_REASON_INVLPG:
9679 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9680 case EXIT_REASON_RDPMC:
9681 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009682 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009683 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009684 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009685 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009686 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009687 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009688 case EXIT_REASON_VMREAD:
9689 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9690 vmcs12->vmread_bitmap);
9691 case EXIT_REASON_VMWRITE:
9692 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9693 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009694 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9695 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009696 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009697 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009698 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009699 /*
9700 * VMX instructions trap unconditionally. This allows L1 to
9701 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9702 */
Joe Perches1d804d02015-03-30 16:46:09 -07009703 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009704 case EXIT_REASON_CR_ACCESS:
9705 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9706 case EXIT_REASON_DR_ACCESS:
9707 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9708 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009709 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009710 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9711 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009712 case EXIT_REASON_MSR_READ:
9713 case EXIT_REASON_MSR_WRITE:
9714 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9715 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009716 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009717 case EXIT_REASON_MWAIT_INSTRUCTION:
9718 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009719 case EXIT_REASON_MONITOR_TRAP_FLAG:
9720 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009721 case EXIT_REASON_MONITOR_INSTRUCTION:
9722 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9723 case EXIT_REASON_PAUSE_INSTRUCTION:
9724 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9725 nested_cpu_has2(vmcs12,
9726 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9727 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009728 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009729 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009730 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009731 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009732 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009733 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009734 /*
9735 * The controls for "virtualize APIC accesses," "APIC-
9736 * register virtualization," and "virtual-interrupt
9737 * delivery" only come from vmcs12.
9738 */
Joe Perches1d804d02015-03-30 16:46:09 -07009739 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009740 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009741 /*
9742 * L0 always deals with the EPT violation. If nested EPT is
9743 * used, and the nested mmu code discovers that the address is
9744 * missing in the guest EPT table (EPT12), the EPT violation
9745 * will be injected with nested_ept_inject_page_fault()
9746 */
Joe Perches1d804d02015-03-30 16:46:09 -07009747 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009748 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009749 /*
9750 * L2 never uses directly L1's EPT, but rather L0's own EPT
9751 * table (shadow on EPT) or a merged EPT table that L0 built
9752 * (EPT on EPT). So any problems with the structure of the
9753 * table is L0's fault.
9754 */
Joe Perches1d804d02015-03-30 16:46:09 -07009755 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009756 case EXIT_REASON_INVPCID:
9757 return
9758 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9759 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009760 case EXIT_REASON_WBINVD:
9761 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9762 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009763 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009764 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9765 /*
9766 * This should never happen, since it is not possible to
9767 * set XSS to a non-zero value---neither in L1 nor in L2.
9768 * If if it were, XSS would have to be checked against
9769 * the XSS exit bitmap in vmcs12.
9770 */
9771 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009772 case EXIT_REASON_PREEMPTION_TIMER:
9773 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009774 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009775 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009776 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009777 case EXIT_REASON_VMFUNC:
9778 /* VM functions are emulated through L2->L0 vmexits. */
9779 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009780 case EXIT_REASON_ENCLS:
9781 /* SGX is never exposed to L1 */
9782 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009783 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009784 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009785 }
9786}
9787
Paolo Bonzini7313c692017-07-27 10:31:25 +02009788static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9789{
9790 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9791
9792 /*
9793 * At this point, the exit interruption info in exit_intr_info
9794 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9795 * we need to query the in-kernel LAPIC.
9796 */
9797 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9798 if ((exit_intr_info &
9799 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9800 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9801 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9802 vmcs12->vm_exit_intr_error_code =
9803 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9804 }
9805
9806 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9807 vmcs_readl(EXIT_QUALIFICATION));
9808 return 1;
9809}
9810
Avi Kivity586f9602010-11-18 13:09:54 +02009811static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9812{
9813 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9814 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9815}
9816
Kai Huanga3eaa862015-11-04 13:46:05 +08009817static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009818{
Kai Huanga3eaa862015-11-04 13:46:05 +08009819 if (vmx->pml_pg) {
9820 __free_page(vmx->pml_pg);
9821 vmx->pml_pg = NULL;
9822 }
Kai Huang843e4332015-01-28 10:54:28 +08009823}
9824
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009825static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009826{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009827 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009828 u64 *pml_buf;
9829 u16 pml_idx;
9830
9831 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9832
9833 /* Do nothing if PML buffer is empty */
9834 if (pml_idx == (PML_ENTITY_NUM - 1))
9835 return;
9836
9837 /* PML index always points to next available PML buffer entity */
9838 if (pml_idx >= PML_ENTITY_NUM)
9839 pml_idx = 0;
9840 else
9841 pml_idx++;
9842
9843 pml_buf = page_address(vmx->pml_pg);
9844 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9845 u64 gpa;
9846
9847 gpa = pml_buf[pml_idx];
9848 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009849 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009850 }
9851
9852 /* reset PML index */
9853 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9854}
9855
9856/*
9857 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9858 * Called before reporting dirty_bitmap to userspace.
9859 */
9860static void kvm_flush_pml_buffers(struct kvm *kvm)
9861{
9862 int i;
9863 struct kvm_vcpu *vcpu;
9864 /*
9865 * We only need to kick vcpu out of guest mode here, as PML buffer
9866 * is flushed at beginning of all VMEXITs, and it's obvious that only
9867 * vcpus running in guest are possible to have unflushed GPAs in PML
9868 * buffer.
9869 */
9870 kvm_for_each_vcpu(i, vcpu, kvm)
9871 kvm_vcpu_kick(vcpu);
9872}
9873
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009874static void vmx_dump_sel(char *name, uint32_t sel)
9875{
9876 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009877 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009878 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9879 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9880 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9881}
9882
9883static void vmx_dump_dtsel(char *name, uint32_t limit)
9884{
9885 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9886 name, vmcs_read32(limit),
9887 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9888}
9889
9890static void dump_vmcs(void)
9891{
9892 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9893 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9894 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9895 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9896 u32 secondary_exec_control = 0;
9897 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009898 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009899 int i, n;
9900
9901 if (cpu_has_secondary_exec_ctrls())
9902 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9903
9904 pr_err("*** Guest State ***\n");
9905 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9906 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9907 vmcs_readl(CR0_GUEST_HOST_MASK));
9908 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9909 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9910 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9911 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9912 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9913 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009914 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9915 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9916 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9917 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009918 }
9919 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9920 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9921 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9922 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9923 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9924 vmcs_readl(GUEST_SYSENTER_ESP),
9925 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9926 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9927 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9928 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9929 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9930 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9931 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9932 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9933 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9934 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9935 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9936 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9937 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009938 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9939 efer, vmcs_read64(GUEST_IA32_PAT));
9940 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9941 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009942 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009943 if (cpu_has_load_perf_global_ctrl &&
9944 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009945 pr_err("PerfGlobCtl = 0x%016llx\n",
9946 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009947 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009948 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009949 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9950 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9951 vmcs_read32(GUEST_ACTIVITY_STATE));
9952 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9953 pr_err("InterruptStatus = %04x\n",
9954 vmcs_read16(GUEST_INTR_STATUS));
9955
9956 pr_err("*** Host State ***\n");
9957 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9958 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9959 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9960 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9961 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9962 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9963 vmcs_read16(HOST_TR_SELECTOR));
9964 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9965 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9966 vmcs_readl(HOST_TR_BASE));
9967 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9968 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9969 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9970 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9971 vmcs_readl(HOST_CR4));
9972 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9973 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9974 vmcs_read32(HOST_IA32_SYSENTER_CS),
9975 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9976 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009977 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9978 vmcs_read64(HOST_IA32_EFER),
9979 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009980 if (cpu_has_load_perf_global_ctrl &&
9981 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009982 pr_err("PerfGlobCtl = 0x%016llx\n",
9983 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009984
9985 pr_err("*** Control State ***\n");
9986 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9987 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9988 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9989 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9990 vmcs_read32(EXCEPTION_BITMAP),
9991 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9992 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9993 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9994 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9995 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9996 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9997 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9998 vmcs_read32(VM_EXIT_INTR_INFO),
9999 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10000 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10001 pr_err(" reason=%08x qualification=%016lx\n",
10002 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10003 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10004 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10005 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010006 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010007 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010008 pr_err("TSC Multiplier = 0x%016llx\n",
10009 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010010 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10011 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10012 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10013 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10014 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010015 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010016 n = vmcs_read32(CR3_TARGET_COUNT);
10017 for (i = 0; i + 1 < n; i += 4)
10018 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10019 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10020 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10021 if (i < n)
10022 pr_err("CR3 target%u=%016lx\n",
10023 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10024 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10025 pr_err("PLE Gap=%08x Window=%08x\n",
10026 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10027 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10028 pr_err("Virtual processor ID = 0x%04x\n",
10029 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10030}
10031
Avi Kivity6aa8b732006-12-10 02:21:36 -080010032/*
10033 * The guest has exited. See if we can fix it or if we need userspace
10034 * assistance.
10035 */
Avi Kivity851ba692009-08-24 11:10:17 +030010036static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010037{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010038 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010039 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010040 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010041
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010042 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10043
Kai Huang843e4332015-01-28 10:54:28 +080010044 /*
10045 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10046 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10047 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10048 * mode as if vcpus is in root mode, the PML buffer must has been
10049 * flushed already.
10050 */
10051 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010052 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010053
Mohammed Gamal80ced182009-09-01 12:48:18 +020010054 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010055 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010056 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010057
Paolo Bonzini7313c692017-07-27 10:31:25 +020010058 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10059 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010060
Mohammed Gamal51207022010-05-31 22:40:54 +030010061 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010062 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010063 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10064 vcpu->run->fail_entry.hardware_entry_failure_reason
10065 = exit_reason;
10066 return 0;
10067 }
10068
Avi Kivity29bd8a72007-09-10 17:27:03 +030010069 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010070 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10071 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010072 = vmcs_read32(VM_INSTRUCTION_ERROR);
10073 return 0;
10074 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010075
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010076 /*
10077 * Note:
10078 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10079 * delivery event since it indicates guest is accessing MMIO.
10080 * The vm-exit can be triggered again after return to guest that
10081 * will cause infinite loop.
10082 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010083 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010084 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010085 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010086 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010087 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10088 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10089 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010090 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010091 vcpu->run->internal.data[0] = vectoring_info;
10092 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010093 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10094 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10095 vcpu->run->internal.ndata++;
10096 vcpu->run->internal.data[3] =
10097 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10098 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010099 return 0;
10100 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010101
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010102 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010103 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10104 if (vmx_interrupt_allowed(vcpu)) {
10105 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10106 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10107 vcpu->arch.nmi_pending) {
10108 /*
10109 * This CPU don't support us in finding the end of an
10110 * NMI-blocked window if the guest runs with IRQs
10111 * disabled. So we pull the trigger after 1 s of
10112 * futile waiting, but inform the user about this.
10113 */
10114 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10115 "state on VCPU %d after 1 s timeout\n",
10116 __func__, vcpu->vcpu_id);
10117 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10118 }
10119 }
10120
Avi Kivity6aa8b732006-12-10 02:21:36 -080010121 if (exit_reason < kvm_vmx_max_exit_handlers
10122 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010123 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010124 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010125 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10126 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010127 kvm_queue_exception(vcpu, UD_VECTOR);
10128 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010129 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010130}
10131
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010132/*
10133 * Software based L1D cache flush which is used when microcode providing
10134 * the cache control MSR is not loaded.
10135 *
10136 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10137 * flush it is required to read in 64 KiB because the replacement algorithm
10138 * is not exactly LRU. This could be sized at runtime via topology
10139 * information but as all relevant affected CPUs have 32KiB L1D cache size
10140 * there is no point in doing so.
10141 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010142static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010143{
10144 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010145
10146 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010147 * This code is only executed when the the flush mode is 'cond' or
10148 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010149 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010150 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010151 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010152
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010153 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010154 * Clear the per-vcpu flush bit, it gets set again
10155 * either from vcpu_run() or from one of the unsafe
10156 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010157 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010158 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010159 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010160
10161 /*
10162 * Clear the per-cpu flush bit, it gets set again from
10163 * the interrupt handlers.
10164 */
10165 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10166 kvm_clear_cpu_l1tf_flush_l1d();
10167
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010168 if (!flush_l1d)
10169 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010170 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010171
10172 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010173
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010174 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10175 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10176 return;
10177 }
10178
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010179 asm volatile(
10180 /* First ensure the pages are in the TLB */
10181 "xorl %%eax, %%eax\n"
10182 ".Lpopulate_tlb:\n\t"
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 $4096, %%eax\n\t"
10185 "cmpl %%eax, %[size]\n\t"
10186 "jne .Lpopulate_tlb\n\t"
10187 "xorl %%eax, %%eax\n\t"
10188 "cpuid\n\t"
10189 /* Now fill the cache */
10190 "xorl %%eax, %%eax\n"
10191 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010192 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010193 "addl $64, %%eax\n\t"
10194 "cmpl %%eax, %[size]\n\t"
10195 "jne .Lfill_cache\n\t"
10196 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010197 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010198 [size] "r" (size)
10199 : "eax", "ebx", "ecx", "edx");
10200}
10201
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010202static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010203{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010204 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10205
10206 if (is_guest_mode(vcpu) &&
10207 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10208 return;
10209
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010210 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010211 vmcs_write32(TPR_THRESHOLD, 0);
10212 return;
10213 }
10214
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010215 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010216}
10217
Jim Mattson8d860bb2018-05-09 16:56:05 -040010218static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010219{
10220 u32 sec_exec_control;
10221
Jim Mattson8d860bb2018-05-09 16:56:05 -040010222 if (!lapic_in_kernel(vcpu))
10223 return;
10224
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010225 if (!flexpriority_enabled &&
10226 !cpu_has_vmx_virtualize_x2apic_mode())
10227 return;
10228
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010229 /* Postpone execution until vmcs01 is the current VMCS. */
10230 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010231 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010232 return;
10233 }
10234
Yang Zhang8d146952013-01-25 10:18:50 +080010235 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010236 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10237 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010238
Jim Mattson8d860bb2018-05-09 16:56:05 -040010239 switch (kvm_get_apic_mode(vcpu)) {
10240 case LAPIC_MODE_INVALID:
10241 WARN_ONCE(true, "Invalid local APIC state");
10242 case LAPIC_MODE_DISABLED:
10243 break;
10244 case LAPIC_MODE_XAPIC:
10245 if (flexpriority_enabled) {
10246 sec_exec_control |=
10247 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10248 vmx_flush_tlb(vcpu, true);
10249 }
10250 break;
10251 case LAPIC_MODE_X2APIC:
10252 if (cpu_has_vmx_virtualize_x2apic_mode())
10253 sec_exec_control |=
10254 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10255 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010256 }
10257 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10258
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010259 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010260}
10261
Tang Chen38b99172014-09-24 15:57:54 +080010262static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10263{
Jim Mattsonab5df312018-05-09 17:02:03 -040010264 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010265 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010266 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010267 }
Tang Chen38b99172014-09-24 15:57:54 +080010268}
10269
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010270static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010271{
10272 u16 status;
10273 u8 old;
10274
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010275 if (max_isr == -1)
10276 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010277
10278 status = vmcs_read16(GUEST_INTR_STATUS);
10279 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010280 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010281 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010282 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010283 vmcs_write16(GUEST_INTR_STATUS, status);
10284 }
10285}
10286
10287static void vmx_set_rvi(int vector)
10288{
10289 u16 status;
10290 u8 old;
10291
Wei Wang4114c272014-11-05 10:53:43 +080010292 if (vector == -1)
10293 vector = 0;
10294
Yang Zhangc7c9c562013-01-25 10:18:51 +080010295 status = vmcs_read16(GUEST_INTR_STATUS);
10296 old = (u8)status & 0xff;
10297 if ((u8)vector != old) {
10298 status &= ~0xff;
10299 status |= (u8)vector;
10300 vmcs_write16(GUEST_INTR_STATUS, status);
10301 }
10302}
10303
10304static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10305{
Liran Alon851c1a182017-12-24 18:12:56 +020010306 /*
10307 * When running L2, updating RVI is only relevant when
10308 * vmcs12 virtual-interrupt-delivery enabled.
10309 * However, it can be enabled only when L1 also
10310 * intercepts external-interrupts and in that case
10311 * we should not update vmcs02 RVI but instead intercept
10312 * interrupt. Therefore, do nothing when running L2.
10313 */
10314 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010315 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010316}
10317
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010318static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010319{
10320 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010321 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010322 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010323
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010324 WARN_ON(!vcpu->arch.apicv_active);
10325 if (pi_test_on(&vmx->pi_desc)) {
10326 pi_clear_on(&vmx->pi_desc);
10327 /*
10328 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10329 * But on x86 this is just a compiler barrier anyway.
10330 */
10331 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010332 max_irr_updated =
10333 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10334
10335 /*
10336 * If we are running L2 and L1 has a new pending interrupt
10337 * which can be injected, we should re-evaluate
10338 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010339 * If L1 intercepts external-interrupts, we should
10340 * exit from L2 to L1. Otherwise, interrupt should be
10341 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010342 */
Liran Alon851c1a182017-12-24 18:12:56 +020010343 if (is_guest_mode(vcpu) && max_irr_updated) {
10344 if (nested_exit_on_intr(vcpu))
10345 kvm_vcpu_exiting_guest_mode(vcpu);
10346 else
10347 kvm_make_request(KVM_REQ_EVENT, vcpu);
10348 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010349 } else {
10350 max_irr = kvm_lapic_find_highest_irr(vcpu);
10351 }
10352 vmx_hwapic_irr_update(vcpu, max_irr);
10353 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010354}
10355
Paolo Bonzini7e712682018-10-03 13:44:26 +020010356static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10357{
10358 u8 rvi = vmx_get_rvi();
10359 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10360
10361 return ((rvi & 0xf0) > (vppr & 0xf0));
10362}
10363
Andrey Smetanin63086302015-11-10 15:36:32 +030010364static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010365{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010366 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010367 return;
10368
Yang Zhangc7c9c562013-01-25 10:18:51 +080010369 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10370 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10371 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10372 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10373}
10374
Paolo Bonzini967235d2016-12-19 14:03:45 +010010375static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10376{
10377 struct vcpu_vmx *vmx = to_vmx(vcpu);
10378
10379 pi_clear_on(&vmx->pi_desc);
10380 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10381}
10382
Avi Kivity51aa01d2010-07-20 14:31:20 +030010383static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010384{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010385 u32 exit_intr_info = 0;
10386 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010387
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010388 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10389 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010390 return;
10391
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010392 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10393 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10394 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010395
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010396 /* if exit due to PF check for async PF */
10397 if (is_page_fault(exit_intr_info))
10398 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10399
Andi Kleena0861c02009-06-08 17:37:09 +080010400 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010401 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10402 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010403 kvm_machine_check();
10404
Gleb Natapov20f65982009-05-11 13:35:55 +030010405 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010406 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010407 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010408 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010409 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010410 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010411}
Gleb Natapov20f65982009-05-11 13:35:55 +030010412
Yang Zhanga547c6d2013-04-11 19:25:10 +080010413static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10414{
10415 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10416
Yang Zhanga547c6d2013-04-11 19:25:10 +080010417 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10418 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10419 unsigned int vector;
10420 unsigned long entry;
10421 gate_desc *desc;
10422 struct vcpu_vmx *vmx = to_vmx(vcpu);
10423#ifdef CONFIG_X86_64
10424 unsigned long tmp;
10425#endif
10426
10427 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10428 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010429 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010430 asm volatile(
10431#ifdef CONFIG_X86_64
10432 "mov %%" _ASM_SP ", %[sp]\n\t"
10433 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10434 "push $%c[ss]\n\t"
10435 "push %[sp]\n\t"
10436#endif
10437 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010438 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010439 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010440 :
10441#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010442 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010443#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010444 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010445 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010446 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010447 [ss]"i"(__KERNEL_DS),
10448 [cs]"i"(__KERNEL_CS)
10449 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010450 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010451}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010452STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010453
Tom Lendackybc226f02018-05-10 22:06:39 +020010454static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010455{
Tom Lendackybc226f02018-05-10 22:06:39 +020010456 switch (index) {
10457 case MSR_IA32_SMBASE:
10458 /*
10459 * We cannot do SMM unless we can run the guest in big
10460 * real mode.
10461 */
10462 return enable_unrestricted_guest || emulate_invalid_guest_state;
10463 case MSR_AMD64_VIRT_SPEC_CTRL:
10464 /* This is AMD only. */
10465 return false;
10466 default:
10467 return true;
10468 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010469}
10470
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010471static bool vmx_mpx_supported(void)
10472{
10473 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10474 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10475}
10476
Wanpeng Li55412b22014-12-02 19:21:30 +080010477static bool vmx_xsaves_supported(void)
10478{
10479 return vmcs_config.cpu_based_2nd_exec_ctrl &
10480 SECONDARY_EXEC_XSAVES;
10481}
10482
Avi Kivity51aa01d2010-07-20 14:31:20 +030010483static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10484{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010485 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010486 bool unblock_nmi;
10487 u8 vector;
10488 bool idtv_info_valid;
10489
10490 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010491
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010492 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010493 if (vmx->loaded_vmcs->nmi_known_unmasked)
10494 return;
10495 /*
10496 * Can't use vmx->exit_intr_info since we're not sure what
10497 * the exit reason is.
10498 */
10499 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10500 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10501 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10502 /*
10503 * SDM 3: 27.7.1.2 (September 2008)
10504 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10505 * a guest IRET fault.
10506 * SDM 3: 23.2.2 (September 2008)
10507 * Bit 12 is undefined in any of the following cases:
10508 * If the VM exit sets the valid bit in the IDT-vectoring
10509 * information field.
10510 * If the VM exit is due to a double fault.
10511 */
10512 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10513 vector != DF_VECTOR && !idtv_info_valid)
10514 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10515 GUEST_INTR_STATE_NMI);
10516 else
10517 vmx->loaded_vmcs->nmi_known_unmasked =
10518 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10519 & GUEST_INTR_STATE_NMI);
10520 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10521 vmx->loaded_vmcs->vnmi_blocked_time +=
10522 ktime_to_ns(ktime_sub(ktime_get(),
10523 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010524}
10525
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010526static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010527 u32 idt_vectoring_info,
10528 int instr_len_field,
10529 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010530{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010531 u8 vector;
10532 int type;
10533 bool idtv_info_valid;
10534
10535 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010536
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010537 vcpu->arch.nmi_injected = false;
10538 kvm_clear_exception_queue(vcpu);
10539 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010540
10541 if (!idtv_info_valid)
10542 return;
10543
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010544 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010545
Avi Kivity668f6122008-07-02 09:28:55 +030010546 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10547 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010548
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010549 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010550 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010551 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010552 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010553 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010554 * Clear bit "block by NMI" before VM entry if a NMI
10555 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010556 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010557 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010558 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010559 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010560 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010561 /* fall through */
10562 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010563 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010564 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010565 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010566 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010567 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010568 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010569 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010570 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010571 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010572 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010573 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010574 break;
10575 default:
10576 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010577 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010578}
10579
Avi Kivity83422e12010-07-20 14:43:23 +030010580static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10581{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010582 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010583 VM_EXIT_INSTRUCTION_LEN,
10584 IDT_VECTORING_ERROR_CODE);
10585}
10586
Avi Kivityb463a6f2010-07-20 15:06:17 +030010587static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10588{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010589 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010590 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10591 VM_ENTRY_INSTRUCTION_LEN,
10592 VM_ENTRY_EXCEPTION_ERROR_CODE);
10593
10594 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10595}
10596
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010597static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10598{
10599 int i, nr_msrs;
10600 struct perf_guest_switch_msr *msrs;
10601
10602 msrs = perf_guest_get_msrs(&nr_msrs);
10603
10604 if (!msrs)
10605 return;
10606
10607 for (i = 0; i < nr_msrs; i++)
10608 if (msrs[i].host == msrs[i].guest)
10609 clear_atomic_switch_msr(vmx, msrs[i].msr);
10610 else
10611 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010612 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010613}
10614
Sean Christophersonf459a702018-08-27 15:21:11 -070010615static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10616{
10617 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10618 if (!vmx->loaded_vmcs->hv_timer_armed)
10619 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10620 PIN_BASED_VMX_PREEMPTION_TIMER);
10621 vmx->loaded_vmcs->hv_timer_armed = true;
10622}
10623
10624static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010625{
10626 struct vcpu_vmx *vmx = to_vmx(vcpu);
10627 u64 tscl;
10628 u32 delta_tsc;
10629
Sean Christophersond264ee02018-08-27 15:21:12 -070010630 if (vmx->req_immediate_exit) {
10631 vmx_arm_hv_timer(vmx, 0);
10632 return;
10633 }
10634
Sean Christophersonf459a702018-08-27 15:21:11 -070010635 if (vmx->hv_deadline_tsc != -1) {
10636 tscl = rdtsc();
10637 if (vmx->hv_deadline_tsc > tscl)
10638 /* set_hv_timer ensures the delta fits in 32-bits */
10639 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10640 cpu_preemption_timer_multi);
10641 else
10642 delta_tsc = 0;
10643
10644 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010645 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010646 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010647
Sean Christophersonf459a702018-08-27 15:21:11 -070010648 if (vmx->loaded_vmcs->hv_timer_armed)
10649 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10650 PIN_BASED_VMX_PREEMPTION_TIMER);
10651 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010652}
10653
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010654static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010655{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010656 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010657 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010658
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010659 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010660 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010661 vmx->loaded_vmcs->soft_vnmi_blocked))
10662 vmx->loaded_vmcs->entry_time = ktime_get();
10663
Avi Kivity104f2262010-11-18 13:12:52 +020010664 /* Don't enter VMX if guest state is invalid, let the exit handler
10665 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010666 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010667 return;
10668
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010669 if (vmx->ple_window_dirty) {
10670 vmx->ple_window_dirty = false;
10671 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10672 }
10673
Abel Gordon012f83c2013-04-18 14:39:25 +030010674 if (vmx->nested.sync_shadow_vmcs) {
10675 copy_vmcs12_to_shadow(vmx);
10676 vmx->nested.sync_shadow_vmcs = false;
10677 }
10678
Avi Kivity104f2262010-11-18 13:12:52 +020010679 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10680 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10681 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10682 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10683
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010684 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010685 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010686 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010687 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010688 }
10689
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010690 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010691 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010692 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010693 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010694 }
10695
Avi Kivity104f2262010-11-18 13:12:52 +020010696 /* When single-stepping over STI and MOV SS, we must clear the
10697 * corresponding interruptibility bits in the guest state. Otherwise
10698 * vmentry fails as it then expects bit 14 (BS) in pending debug
10699 * exceptions being set, but that's not correct for the guest debugging
10700 * case. */
10701 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10702 vmx_set_interrupt_shadow(vcpu, 0);
10703
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010704 if (static_cpu_has(X86_FEATURE_PKU) &&
10705 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10706 vcpu->arch.pkru != vmx->host_pkru)
10707 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010708
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010709 atomic_switch_perf_msrs(vmx);
10710
Sean Christophersonf459a702018-08-27 15:21:11 -070010711 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010712
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010713 /*
10714 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10715 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10716 * is no need to worry about the conditional branch over the wrmsr
10717 * being speculatively taken.
10718 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010719 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010720
Nadav Har'Eld462b812011-05-24 15:26:10 +030010721 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010722
10723 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10724 (unsigned long)&current_evmcs->host_rsp : 0;
10725
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010726 if (static_branch_unlikely(&vmx_l1d_should_flush))
10727 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010728
Avi Kivity104f2262010-11-18 13:12:52 +020010729 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010730 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010731 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10732 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10733 "push %%" _ASM_CX " \n\t"
10734 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010735 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010736 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010737 /* Avoid VMWRITE when Enlightened VMCS is in use */
10738 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10739 "jz 2f \n\t"
10740 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10741 "jmp 1f \n\t"
10742 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010743 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +030010744 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010745 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010746 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10747 "mov %%cr2, %%" _ASM_DX " \n\t"
10748 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010749 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010750 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010751 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010752 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010753 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010754 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010755 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10756 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10757 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10758 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10759 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10760 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010761#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010762 "mov %c[r8](%0), %%r8 \n\t"
10763 "mov %c[r9](%0), %%r9 \n\t"
10764 "mov %c[r10](%0), %%r10 \n\t"
10765 "mov %c[r11](%0), %%r11 \n\t"
10766 "mov %c[r12](%0), %%r12 \n\t"
10767 "mov %c[r13](%0), %%r13 \n\t"
10768 "mov %c[r14](%0), %%r14 \n\t"
10769 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010770#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010771 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010772
Avi Kivity6aa8b732006-12-10 02:21:36 -080010773 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010774 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010775 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010776 "jmp 2f \n\t"
10777 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10778 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010779 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010780 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010781 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010782 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010783 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10784 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10785 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10786 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10787 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10788 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10789 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010790#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010791 "mov %%r8, %c[r8](%0) \n\t"
10792 "mov %%r9, %c[r9](%0) \n\t"
10793 "mov %%r10, %c[r10](%0) \n\t"
10794 "mov %%r11, %c[r11](%0) \n\t"
10795 "mov %%r12, %c[r12](%0) \n\t"
10796 "mov %%r13, %c[r13](%0) \n\t"
10797 "mov %%r14, %c[r14](%0) \n\t"
10798 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010799 "xor %%r8d, %%r8d \n\t"
10800 "xor %%r9d, %%r9d \n\t"
10801 "xor %%r10d, %%r10d \n\t"
10802 "xor %%r11d, %%r11d \n\t"
10803 "xor %%r12d, %%r12d \n\t"
10804 "xor %%r13d, %%r13d \n\t"
10805 "xor %%r14d, %%r14d \n\t"
10806 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010807#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010808 "mov %%cr2, %%" _ASM_AX " \n\t"
10809 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010810
Jim Mattson0cb5b302018-01-03 14:31:38 -080010811 "xor %%eax, %%eax \n\t"
10812 "xor %%ebx, %%ebx \n\t"
10813 "xor %%esi, %%esi \n\t"
10814 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010815 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010816 ".pushsection .rodata \n\t"
10817 ".global vmx_return \n\t"
10818 "vmx_return: " _ASM_PTR " 2b \n\t"
10819 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010820 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010821 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010822 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +030010823 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010824 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10825 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10826 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10827 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10828 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10829 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10830 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010831#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010832 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10833 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10834 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10835 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10836 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10837 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10838 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10839 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010840#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010841 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10842 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010843 : "cc", "memory"
10844#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010845 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010846 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010847#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010848 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010849#endif
10850 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010851
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010852 /*
10853 * We do not use IBRS in the kernel. If this vCPU has used the
10854 * SPEC_CTRL MSR it may have left it on; save the value and
10855 * turn it off. This is much more efficient than blindly adding
10856 * it to the atomic save/restore list. Especially as the former
10857 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10858 *
10859 * For non-nested case:
10860 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10861 * save it.
10862 *
10863 * For nested case:
10864 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10865 * save it.
10866 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010867 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010868 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010869
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010870 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010871
David Woodhouse117cc7a2018-01-12 11:11:27 +000010872 /* Eliminate branch target predictions from guest mode */
10873 vmexit_fill_RSB();
10874
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010875 /* All fields are clean at this point */
10876 if (static_branch_unlikely(&enable_evmcs))
10877 current_evmcs->hv_clean_fields |=
10878 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10879
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010880 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010881 if (vmx->host_debugctlmsr)
10882 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010883
Avi Kivityaa67f602012-08-01 16:48:03 +030010884#ifndef CONFIG_X86_64
10885 /*
10886 * The sysexit path does not restore ds/es, so we must set them to
10887 * a reasonable value ourselves.
10888 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010889 * We can't defer this to vmx_prepare_switch_to_host() since that
10890 * function may be executed in interrupt context, which saves and
10891 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010892 */
10893 loadsegment(ds, __USER_DS);
10894 loadsegment(es, __USER_DS);
10895#endif
10896
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010897 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010898 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010899 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010900 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010901 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010902 vcpu->arch.regs_dirty = 0;
10903
Gleb Natapove0b890d2013-09-25 12:51:33 +030010904 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010905 * eager fpu is enabled if PKEY is supported and CR4 is switched
10906 * back on host, so it is safe to read guest PKRU from current
10907 * XSAVE.
10908 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010909 if (static_cpu_has(X86_FEATURE_PKU) &&
10910 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10911 vcpu->arch.pkru = __read_pkru();
10912 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010913 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010914 }
10915
Gleb Natapove0b890d2013-09-25 12:51:33 +030010916 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010917 vmx->idt_vectoring_info = 0;
10918
10919 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10920 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10921 return;
10922
10923 vmx->loaded_vmcs->launched = 1;
10924 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010925
Avi Kivity51aa01d2010-07-20 14:31:20 +030010926 vmx_complete_atomic_exit(vmx);
10927 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010928 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010929}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010930STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010931
Sean Christopherson434a1e92018-03-20 12:17:18 -070010932static struct kvm *vmx_vm_alloc(void)
10933{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010934 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010935 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010936}
10937
10938static void vmx_vm_free(struct kvm *kvm)
10939{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010940 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010941}
10942
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010943static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010944{
10945 struct vcpu_vmx *vmx = to_vmx(vcpu);
10946 int cpu;
10947
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010948 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010949 return;
10950
10951 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010952 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010953 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010954 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010955 put_cpu();
Sean Christophersonb7031fd2018-09-26 09:23:42 -070010956
10957 vm_entry_controls_reset_shadow(vmx);
10958 vm_exit_controls_reset_shadow(vmx);
10959 vmx_segment_cache_clear(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010960}
10961
Jim Mattson2f1fe812016-07-08 15:36:06 -070010962/*
10963 * Ensure that the current vmcs of the logical processor is the
10964 * vmcs01 of the vcpu before calling free_nested().
10965 */
10966static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10967{
10968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010969
Christoffer Dallec7660c2017-12-04 21:35:23 +010010970 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010971 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010972 free_nested(vmx);
10973 vcpu_put(vcpu);
10974}
10975
Avi Kivity6aa8b732006-12-10 02:21:36 -080010976static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10977{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010978 struct vcpu_vmx *vmx = to_vmx(vcpu);
10979
Kai Huang843e4332015-01-28 10:54:28 +080010980 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010981 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010982 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010983 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010984 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010985 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010986 kfree(vmx->guest_msrs);
10987 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010988 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010989}
10990
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010991static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010992{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010993 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010994 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010995 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010996 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010997
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010998 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010999 return ERR_PTR(-ENOMEM);
11000
Wanpeng Li991e7a02015-09-16 17:30:05 +080011001 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011002
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011003 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11004 if (err)
11005 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011006
Peter Feiner4e595162016-07-07 14:49:58 -070011007 err = -ENOMEM;
11008
11009 /*
11010 * If PML is turned on, failure on enabling PML just results in failure
11011 * of creating the vcpu, therefore we can simplify PML logic (by
11012 * avoiding dealing with cases, such as enabling PML partially on vcpus
11013 * for the guest, etc.
11014 */
11015 if (enable_pml) {
11016 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11017 if (!vmx->pml_pg)
11018 goto uninit_vcpu;
11019 }
11020
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011021 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011022 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11023 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011024
Peter Feiner4e595162016-07-07 14:49:58 -070011025 if (!vmx->guest_msrs)
11026 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011027
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011028 err = alloc_loaded_vmcs(&vmx->vmcs01);
11029 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011030 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011031
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011032 msr_bitmap = vmx->vmcs01.msr_bitmap;
11033 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11034 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11035 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11036 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11037 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11038 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11039 vmx->msr_bitmap_mode = 0;
11040
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011041 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011042 cpu = get_cpu();
11043 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011044 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011045 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011046 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011047 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011048 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011049 err = alloc_apic_access_page(kvm);
11050 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011051 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011052 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011053
Sean Christophersone90008d2018-03-05 12:04:37 -080011054 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011055 err = init_rmode_identity_map(kvm);
11056 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011057 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011058 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011059
Roman Kagan63aff652018-07-19 21:59:07 +030011060 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011061 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11062 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011063
Wincy Van705699a2015-02-03 23:58:17 +080011064 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011065 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011066
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011067 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11068
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011069 /*
11070 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11071 * or POSTED_INTR_WAKEUP_VECTOR.
11072 */
11073 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11074 vmx->pi_desc.sn = 1;
11075
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011076 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011077
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011078free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011079 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011080free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011081 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011082free_pml:
11083 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011084uninit_vcpu:
11085 kvm_vcpu_uninit(&vmx->vcpu);
11086free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011087 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011088 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011089 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011090}
11091
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011092#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"
11093#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 -040011094
Wanpeng Lib31c1142018-03-12 04:53:04 -070011095static int vmx_vm_init(struct kvm *kvm)
11096{
Tianyu Lan877ad952018-07-19 08:40:23 +000011097 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11098
Wanpeng Lib31c1142018-03-12 04:53:04 -070011099 if (!ple_gap)
11100 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011101
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011102 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11103 switch (l1tf_mitigation) {
11104 case L1TF_MITIGATION_OFF:
11105 case L1TF_MITIGATION_FLUSH_NOWARN:
11106 /* 'I explicitly don't care' is set */
11107 break;
11108 case L1TF_MITIGATION_FLUSH:
11109 case L1TF_MITIGATION_FLUSH_NOSMT:
11110 case L1TF_MITIGATION_FULL:
11111 /*
11112 * Warn upon starting the first VM in a potentially
11113 * insecure environment.
11114 */
11115 if (cpu_smt_control == CPU_SMT_ENABLED)
11116 pr_warn_once(L1TF_MSG_SMT);
11117 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11118 pr_warn_once(L1TF_MSG_L1D);
11119 break;
11120 case L1TF_MITIGATION_FULL_FORCE:
11121 /* Flush is enforced */
11122 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011123 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011124 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011125 return 0;
11126}
11127
Yang, Sheng002c7f72007-07-31 14:23:01 +030011128static void __init vmx_check_processor_compat(void *rtn)
11129{
11130 struct vmcs_config vmcs_conf;
11131
11132 *(int *)rtn = 0;
11133 if (setup_vmcs_config(&vmcs_conf) < 0)
11134 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011135 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011136 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11137 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11138 smp_processor_id());
11139 *(int *)rtn = -EIO;
11140 }
11141}
11142
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011143static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011144{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011145 u8 cache;
11146 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011147
Sheng Yang522c68c2009-04-27 20:35:43 +080011148 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011149 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011150 * 2. EPT with VT-d:
11151 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011152 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011153 * b. VT-d with snooping control feature: snooping control feature of
11154 * VT-d engine can guarantee the cache correctness. Just set it
11155 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011156 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011157 * consistent with host MTRR
11158 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011159 if (is_mmio) {
11160 cache = MTRR_TYPE_UNCACHABLE;
11161 goto exit;
11162 }
11163
11164 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011165 ipat = VMX_EPT_IPAT_BIT;
11166 cache = MTRR_TYPE_WRBACK;
11167 goto exit;
11168 }
11169
11170 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11171 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011172 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011173 cache = MTRR_TYPE_WRBACK;
11174 else
11175 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011176 goto exit;
11177 }
11178
Xiao Guangrongff536042015-06-15 16:55:22 +080011179 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011180
11181exit:
11182 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011183}
11184
Sheng Yang17cc3932010-01-05 19:02:27 +080011185static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011186{
Sheng Yang878403b2010-01-05 19:02:29 +080011187 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11188 return PT_DIRECTORY_LEVEL;
11189 else
11190 /* For shadow and EPT supported 1GB page */
11191 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011192}
11193
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011194static void vmcs_set_secondary_exec_control(u32 new_ctl)
11195{
11196 /*
11197 * These bits in the secondary execution controls field
11198 * are dynamic, the others are mostly based on the hypervisor
11199 * architecture and the guest's CPUID. Do not touch the
11200 * dynamic bits.
11201 */
11202 u32 mask =
11203 SECONDARY_EXEC_SHADOW_VMCS |
11204 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011205 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11206 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011207
11208 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11209
11210 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11211 (new_ctl & ~mask) | (cur_ctl & mask));
11212}
11213
David Matlack8322ebb2016-11-29 18:14:09 -080011214/*
11215 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11216 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11217 */
11218static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11219{
11220 struct vcpu_vmx *vmx = to_vmx(vcpu);
11221 struct kvm_cpuid_entry2 *entry;
11222
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011223 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11224 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011225
11226#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11227 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011228 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011229} while (0)
11230
11231 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11232 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11233 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11234 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11235 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11236 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11237 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11238 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11239 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11240 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11241 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11242 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11243 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11244 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11245 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11246
11247 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11248 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11249 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11250 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11251 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011252 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011253
11254#undef cr4_fixed1_update
11255}
11256
Liran Alon5f76f6f2018-09-14 03:25:52 +030011257static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11258{
11259 struct vcpu_vmx *vmx = to_vmx(vcpu);
11260
11261 if (kvm_mpx_supported()) {
11262 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11263
11264 if (mpx_enabled) {
11265 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11266 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11267 } else {
11268 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11269 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11270 }
11271 }
11272}
11273
Sheng Yang0e851882009-12-18 16:48:46 +080011274static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11275{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011276 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011277
Paolo Bonzini80154d72017-08-24 13:55:35 +020011278 if (cpu_has_secondary_exec_ctrls()) {
11279 vmx_compute_secondary_exec_control(vmx);
11280 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011281 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011282
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011283 if (nested_vmx_allowed(vcpu))
11284 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11285 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11286 else
11287 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11288 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011289
Liran Alon5f76f6f2018-09-14 03:25:52 +030011290 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011291 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011292 nested_vmx_entry_exit_ctls_update(vcpu);
11293 }
Sheng Yang0e851882009-12-18 16:48:46 +080011294}
11295
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011296static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11297{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011298 if (func == 1 && nested)
11299 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011300}
11301
Yang Zhang25d92082013-08-06 12:00:32 +030011302static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11303 struct x86_exception *fault)
11304{
Jan Kiszka533558b2014-01-04 18:47:20 +010011305 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011306 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011307 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011308 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011309
Bandan Dasc5f983f2017-05-05 15:25:14 -040011310 if (vmx->nested.pml_full) {
11311 exit_reason = EXIT_REASON_PML_FULL;
11312 vmx->nested.pml_full = false;
11313 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11314 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011315 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011316 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011317 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011318
11319 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011320 vmcs12->guest_physical_address = fault->address;
11321}
11322
Peter Feiner995f00a2017-06-30 17:26:32 -070011323static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11324{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011325 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011326}
11327
Nadav Har'El155a97a2013-08-05 11:07:16 +030011328/* Callbacks for nested_ept_init_mmu_context: */
11329
11330static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11331{
11332 /* return the page table to be shadowed - in our case, EPT12 */
11333 return get_vmcs12(vcpu)->ept_pointer;
11334}
11335
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011336static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011337{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011338 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011339
Paolo Bonziniad896af2013-10-02 16:56:14 +020011340 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011341 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011342 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011343 nested_ept_ad_enabled(vcpu),
11344 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011345 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11346 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11347 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11348
11349 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011350}
11351
11352static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11353{
11354 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11355}
11356
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011357static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11358 u16 error_code)
11359{
11360 bool inequality, bit;
11361
11362 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11363 inequality =
11364 (error_code & vmcs12->page_fault_error_code_mask) !=
11365 vmcs12->page_fault_error_code_match;
11366 return inequality ^ bit;
11367}
11368
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011369static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11370 struct x86_exception *fault)
11371{
11372 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11373
11374 WARN_ON(!is_guest_mode(vcpu));
11375
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011376 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11377 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011378 vmcs12->vm_exit_intr_error_code = fault->error_code;
11379 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11380 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11381 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11382 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011383 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011384 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011385 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011386}
11387
Paolo Bonzinic9923842017-12-13 14:16:30 +010011388static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11389 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011390
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011391static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011392{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011393 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011394 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011395 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011396 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011397
11398 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011399 /*
11400 * Translate L1 physical address to host physical
11401 * address for vmcs02. Keep the page pinned, so this
11402 * physical address remains valid. We keep a reference
11403 * to it so we can release it later.
11404 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011405 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011406 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011407 vmx->nested.apic_access_page = NULL;
11408 }
11409 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011410 /*
11411 * If translation failed, no matter: This feature asks
11412 * to exit when accessing the given address, and if it
11413 * can never be accessed, this feature won't do
11414 * anything anyway.
11415 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011416 if (!is_error_page(page)) {
11417 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011418 hpa = page_to_phys(vmx->nested.apic_access_page);
11419 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11420 } else {
11421 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11422 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11423 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011424 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011425
11426 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011427 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011428 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011429 vmx->nested.virtual_apic_page = NULL;
11430 }
11431 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011432
11433 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011434 * If translation failed, VM entry will fail because
11435 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11436 * Failing the vm entry is _not_ what the processor
11437 * does but it's basically the only possibility we
11438 * have. We could still enter the guest if CR8 load
11439 * exits are enabled, CR8 store exits are enabled, and
11440 * virtualize APIC access is disabled; in this case
11441 * the processor would never use the TPR shadow and we
11442 * could simply clear the bit from the execution
11443 * control. But such a configuration is useless, so
11444 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011445 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011446 if (!is_error_page(page)) {
11447 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011448 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11449 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11450 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011451 }
11452
Wincy Van705699a2015-02-03 23:58:17 +080011453 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011454 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11455 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011456 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011457 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011458 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011459 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11460 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011461 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011462 vmx->nested.pi_desc_page = page;
11463 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011464 vmx->nested.pi_desc =
11465 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11466 (unsigned long)(vmcs12->posted_intr_desc_addr &
11467 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011468 vmcs_write64(POSTED_INTR_DESC_ADDR,
11469 page_to_phys(vmx->nested.pi_desc_page) +
11470 (unsigned long)(vmcs12->posted_intr_desc_addr &
11471 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011472 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011473 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011474 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11475 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011476 else
11477 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11478 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011479}
11480
Jan Kiszkaf4124502014-03-07 20:03:13 +010011481static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11482{
11483 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11484 struct vcpu_vmx *vmx = to_vmx(vcpu);
11485
Sean Christopherson4c008122018-08-27 15:21:10 -070011486 /*
11487 * A timer value of zero is architecturally guaranteed to cause
11488 * a VMExit prior to executing any instructions in the guest.
11489 */
11490 if (preemption_timeout == 0) {
Jan Kiszkaf4124502014-03-07 20:03:13 +010011491 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11492 return;
11493 }
11494
Sean Christopherson4c008122018-08-27 15:21:10 -070011495 if (vcpu->arch.virtual_tsc_khz == 0)
11496 return;
11497
Jan Kiszkaf4124502014-03-07 20:03:13 +010011498 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11499 preemption_timeout *= 1000000;
11500 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11501 hrtimer_start(&vmx->nested.preemption_timer,
11502 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11503}
11504
Jim Mattson56a20512017-07-06 16:33:06 -070011505static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11506 struct vmcs12 *vmcs12)
11507{
11508 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11509 return 0;
11510
11511 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11512 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11513 return -EINVAL;
11514
11515 return 0;
11516}
11517
Wincy Van3af18d92015-02-03 23:49:31 +080011518static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11519 struct vmcs12 *vmcs12)
11520{
Wincy Van3af18d92015-02-03 23:49:31 +080011521 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11522 return 0;
11523
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011524 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011525 return -EINVAL;
11526
11527 return 0;
11528}
11529
Jim Mattson712b12d2017-08-24 13:24:47 -070011530static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11531 struct vmcs12 *vmcs12)
11532{
11533 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11534 return 0;
11535
11536 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11537 return -EINVAL;
11538
11539 return 0;
11540}
11541
Wincy Van3af18d92015-02-03 23:49:31 +080011542/*
11543 * Merge L0's and L1's MSR bitmap, return false to indicate that
11544 * we do not use the hardware.
11545 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011546static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11547 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011548{
Wincy Van82f0dd42015-02-03 23:57:18 +080011549 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011550 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011551 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011552 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011553 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011554 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011555 *
11556 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11557 * ensures that we do not accidentally generate an L02 MSR bitmap
11558 * from the L12 MSR bitmap that is too permissive.
11559 * 2. That L1 or L2s have actually used the MSR. This avoids
11560 * unnecessarily merging of the bitmap if the MSR is unused. This
11561 * works properly because we only update the L01 MSR bitmap lazily.
11562 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11563 * updated to reflect this when L1 (or its L2s) actually write to
11564 * the MSR.
11565 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011566 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11567 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011568
Paolo Bonzinic9923842017-12-13 14:16:30 +010011569 /* Nothing to do if the MSR bitmap is not in use. */
11570 if (!cpu_has_vmx_msr_bitmap() ||
11571 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11572 return false;
11573
Ashok Raj15d45072018-02-01 22:59:43 +010011574 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011575 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011576 return false;
11577
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011578 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11579 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011580 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011581
Radim Krčmářd048c092016-08-08 20:16:22 +020011582 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011583 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11584 /*
11585 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11586 * just lets the processor take the value from the virtual-APIC page;
11587 * take those 256 bits directly from the L1 bitmap.
11588 */
11589 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11590 unsigned word = msr / BITS_PER_LONG;
11591 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11592 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011593 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011594 } else {
11595 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11596 unsigned word = msr / BITS_PER_LONG;
11597 msr_bitmap_l0[word] = ~0;
11598 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11599 }
11600 }
11601
11602 nested_vmx_disable_intercept_for_msr(
11603 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011604 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011605 MSR_TYPE_W);
11606
11607 if (nested_cpu_has_vid(vmcs12)) {
11608 nested_vmx_disable_intercept_for_msr(
11609 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011610 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011611 MSR_TYPE_W);
11612 nested_vmx_disable_intercept_for_msr(
11613 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011614 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011615 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011616 }
Ashok Raj15d45072018-02-01 22:59:43 +010011617
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011618 if (spec_ctrl)
11619 nested_vmx_disable_intercept_for_msr(
11620 msr_bitmap_l1, msr_bitmap_l0,
11621 MSR_IA32_SPEC_CTRL,
11622 MSR_TYPE_R | MSR_TYPE_W);
11623
Ashok Raj15d45072018-02-01 22:59:43 +010011624 if (pred_cmd)
11625 nested_vmx_disable_intercept_for_msr(
11626 msr_bitmap_l1, msr_bitmap_l0,
11627 MSR_IA32_PRED_CMD,
11628 MSR_TYPE_W);
11629
Wincy Vanf2b93282015-02-03 23:56:03 +080011630 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011631 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011632
11633 return true;
11634}
11635
Liran Alon61ada742018-06-23 02:35:08 +030011636static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11637 struct vmcs12 *vmcs12)
11638{
11639 struct vmcs12 *shadow;
11640 struct page *page;
11641
11642 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11643 vmcs12->vmcs_link_pointer == -1ull)
11644 return;
11645
11646 shadow = get_shadow_vmcs12(vcpu);
11647 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11648
11649 memcpy(shadow, kmap(page), VMCS12_SIZE);
11650
11651 kunmap(page);
11652 kvm_release_page_clean(page);
11653}
11654
11655static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11656 struct vmcs12 *vmcs12)
11657{
11658 struct vcpu_vmx *vmx = to_vmx(vcpu);
11659
11660 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11661 vmcs12->vmcs_link_pointer == -1ull)
11662 return;
11663
11664 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11665 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11666}
11667
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011668static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11669 struct vmcs12 *vmcs12)
11670{
11671 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11672 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11673 return -EINVAL;
11674 else
11675 return 0;
11676}
11677
Wincy Vanf2b93282015-02-03 23:56:03 +080011678static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11679 struct vmcs12 *vmcs12)
11680{
Wincy Van82f0dd42015-02-03 23:57:18 +080011681 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011682 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011683 !nested_cpu_has_vid(vmcs12) &&
11684 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011685 return 0;
11686
11687 /*
11688 * If virtualize x2apic mode is enabled,
11689 * virtualize apic access must be disabled.
11690 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011691 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11692 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011693 return -EINVAL;
11694
Wincy Van608406e2015-02-03 23:57:51 +080011695 /*
11696 * If virtual interrupt delivery is enabled,
11697 * we must exit on external interrupts.
11698 */
11699 if (nested_cpu_has_vid(vmcs12) &&
11700 !nested_exit_on_intr(vcpu))
11701 return -EINVAL;
11702
Wincy Van705699a2015-02-03 23:58:17 +080011703 /*
11704 * bits 15:8 should be zero in posted_intr_nv,
11705 * the descriptor address has been already checked
11706 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011707 *
11708 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011709 */
11710 if (nested_cpu_has_posted_intr(vmcs12) &&
11711 (!nested_cpu_has_vid(vmcs12) ||
11712 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011713 (vmcs12->posted_intr_nv & 0xff00) ||
11714 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11715 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011716 return -EINVAL;
11717
Wincy Vanf2b93282015-02-03 23:56:03 +080011718 /* tpr shadow is needed by all apicv features. */
11719 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11720 return -EINVAL;
11721
11722 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011723}
11724
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011725static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11726 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011727 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011728{
Liran Alone2536742018-06-23 02:35:02 +030011729 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011730 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011731 u64 count, addr;
11732
Liran Alone2536742018-06-23 02:35:02 +030011733 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11734 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735 WARN_ON(1);
11736 return -EINVAL;
11737 }
11738 if (count == 0)
11739 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011740 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011741 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11742 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011743 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011744 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11745 addr_field, maxphyaddr, count, addr);
11746 return -EINVAL;
11747 }
11748 return 0;
11749}
11750
11751static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11752 struct vmcs12 *vmcs12)
11753{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011754 if (vmcs12->vm_exit_msr_load_count == 0 &&
11755 vmcs12->vm_exit_msr_store_count == 0 &&
11756 vmcs12->vm_entry_msr_load_count == 0)
11757 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011758 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011759 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011760 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011761 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011762 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011763 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011764 return -EINVAL;
11765 return 0;
11766}
11767
Bandan Dasc5f983f2017-05-05 15:25:14 -040011768static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11769 struct vmcs12 *vmcs12)
11770{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011771 if (!nested_cpu_has_pml(vmcs12))
11772 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011773
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011774 if (!nested_cpu_has_ept(vmcs12) ||
11775 !page_address_valid(vcpu, vmcs12->pml_address))
11776 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011777
11778 return 0;
11779}
11780
Liran Alona8a7c022018-06-23 02:35:06 +030011781static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11782 struct vmcs12 *vmcs12)
11783{
11784 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11785 return 0;
11786
11787 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11788 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11789 return -EINVAL;
11790
11791 return 0;
11792}
11793
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011794static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11795 struct vmx_msr_entry *e)
11796{
11797 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011798 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011799 return -EINVAL;
11800 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11801 e->index == MSR_IA32_UCODE_REV)
11802 return -EINVAL;
11803 if (e->reserved != 0)
11804 return -EINVAL;
11805 return 0;
11806}
11807
11808static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11809 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011810{
11811 if (e->index == MSR_FS_BASE ||
11812 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011813 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11814 nested_vmx_msr_check_common(vcpu, e))
11815 return -EINVAL;
11816 return 0;
11817}
11818
11819static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11820 struct vmx_msr_entry *e)
11821{
11822 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11823 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011824 return -EINVAL;
11825 return 0;
11826}
11827
11828/*
11829 * Load guest's/host's msr at nested entry/exit.
11830 * return 0 for success, entry index for failure.
11831 */
11832static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11833{
11834 u32 i;
11835 struct vmx_msr_entry e;
11836 struct msr_data msr;
11837
11838 msr.host_initiated = false;
11839 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011840 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11841 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011842 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011843 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11844 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011845 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011846 }
11847 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011848 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011849 "%s check failed (%u, 0x%x, 0x%x)\n",
11850 __func__, i, e.index, e.reserved);
11851 goto fail;
11852 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011853 msr.index = e.index;
11854 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011855 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011856 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011857 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11858 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011859 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011860 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011861 }
11862 return 0;
11863fail:
11864 return i + 1;
11865}
11866
11867static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11868{
11869 u32 i;
11870 struct vmx_msr_entry e;
11871
11872 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011873 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011874 if (kvm_vcpu_read_guest(vcpu,
11875 gpa + i * sizeof(e),
11876 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011877 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011878 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11879 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011880 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011881 }
11882 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011883 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011884 "%s check failed (%u, 0x%x, 0x%x)\n",
11885 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011886 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011887 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011888 msr_info.host_initiated = false;
11889 msr_info.index = e.index;
11890 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011891 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011892 "%s cannot read MSR (%u, 0x%x)\n",
11893 __func__, i, e.index);
11894 return -EINVAL;
11895 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011896 if (kvm_vcpu_write_guest(vcpu,
11897 gpa + i * sizeof(e) +
11898 offsetof(struct vmx_msr_entry, value),
11899 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011900 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011901 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011902 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011903 return -EINVAL;
11904 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011905 }
11906 return 0;
11907}
11908
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011909static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11910{
11911 unsigned long invalid_mask;
11912
11913 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11914 return (val & invalid_mask) == 0;
11915}
11916
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011917/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011918 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11919 * emulating VM entry into a guest with EPT enabled.
11920 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11921 * is assigned to entry_failure_code on failure.
11922 */
11923static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011924 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011925{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011926 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011927 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011928 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11929 return 1;
11930 }
11931
11932 /*
11933 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11934 * must not be dereferenced.
11935 */
11936 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11937 !nested_ept) {
11938 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11939 *entry_failure_code = ENTRY_FAIL_PDPTE;
11940 return 1;
11941 }
11942 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011943 }
11944
Junaid Shahid50c28f22018-06-27 14:59:11 -070011945 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011946 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011947
11948 vcpu->arch.cr3 = cr3;
11949 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11950
11951 kvm_init_mmu(vcpu, false);
11952
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011953 return 0;
11954}
11955
Liran Alonefebf0a2018-10-08 23:42:20 +030011956/*
11957 * Returns if KVM is able to config CPU to tag TLB entries
11958 * populated by L2 differently than TLB entries populated
11959 * by L1.
11960 *
11961 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11962 *
11963 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11964 * with different VPID (L1 entries are tagged with vmx->vpid
11965 * while L2 entries are tagged with vmx->nested.vpid02).
11966 */
11967static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11968{
11969 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11970
11971 return nested_cpu_has_ept(vmcs12) ||
11972 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11973}
11974
Sean Christopherson3df5c372018-09-26 09:23:44 -070011975static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)
11976{
11977 if (vmx->nested.nested_run_pending &&
11978 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
11979 return vmcs12->guest_ia32_efer;
11980 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
11981 return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME);
11982 else
11983 return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME);
11984}
11985
Jim Mattson6514dc32018-04-26 16:09:12 -070011986static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011987{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011988 struct vcpu_vmx *vmx = to_vmx(vcpu);
11989
11990 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11991 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11992 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11993 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11994 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11995 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11996 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11997 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11998 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11999 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
12000 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
12001 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
12002 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
12003 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
12004 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
12005 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
12006 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
12007 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
12008 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
12009 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
12010 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
12011 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
12012 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
12013 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
12014 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
12015 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
12016 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12017 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12018 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12019 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12020 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012021
12022 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12023 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12024 vmcs12->guest_pending_dbg_exceptions);
12025 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12026 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12027
12028 if (nested_cpu_has_xsaves(vmcs12))
12029 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12030 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12031
12032 if (cpu_has_vmx_posted_intr())
12033 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12034
12035 /*
12036 * Whether page-faults are trapped is determined by a combination of
12037 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12038 * If enable_ept, L0 doesn't care about page faults and we should
12039 * set all of these to L1's desires. However, if !enable_ept, L0 does
12040 * care about (at least some) page faults, and because it is not easy
12041 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12042 * to exit on each and every L2 page fault. This is done by setting
12043 * MASK=MATCH=0 and (see below) EB.PF=1.
12044 * Note that below we don't need special code to set EB.PF beyond the
12045 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12046 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12047 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12048 */
12049 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12050 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12051 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12052 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12053
12054 /* All VMFUNCs are currently emulated through L0 vmexits. */
12055 if (cpu_has_vmx_vmfunc())
12056 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12057
12058 if (cpu_has_vmx_apicv()) {
12059 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12060 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12061 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12062 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12063 }
12064
12065 /*
12066 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12067 * Some constant fields are set here by vmx_set_constant_host_state().
12068 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012069 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12070 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012071 */
12072 vmx_set_constant_host_state(vmx);
12073
12074 /*
12075 * Set the MSR load/store lists to match L0's settings.
12076 */
12077 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012078 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12079 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12080 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12081 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012082
12083 set_cr4_guest_host_mask(vmx);
12084
Liran Alon62cf9bd812018-09-14 03:25:54 +030012085 if (kvm_mpx_supported()) {
12086 if (vmx->nested.nested_run_pending &&
12087 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12088 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12089 else
12090 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12091 }
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012092
12093 if (enable_vpid) {
12094 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12095 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12096 else
12097 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12098 }
12099
12100 /*
12101 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12102 */
12103 if (enable_ept) {
12104 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12105 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12106 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12107 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12108 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012109
12110 if (cpu_has_vmx_msr_bitmap())
12111 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012112}
12113
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012114/*
12115 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12116 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012117 * 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 +030012118 * guest in a way that will both be appropriate to L1's requests, and our
12119 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12120 * function also has additional necessary side-effects, like setting various
12121 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012122 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12123 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012124 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012125static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012126 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012127{
12128 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012129 u32 exec_control, vmcs12_exec_ctrl;
Sean Christopherson3df5c372018-09-26 09:23:44 -070012130 u64 guest_efer;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012131
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012132 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012133 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012134 vmx->nested.dirty_vmcs12 = false;
12135 }
12136
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012137 /*
12138 * First, the fields that are shadowed. This must be kept in sync
12139 * with vmx_shadow_fields.h.
12140 */
12141
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012142 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012143 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012144 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012145 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12146 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012147
Jim Mattson6514dc32018-04-26 16:09:12 -070012148 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012149 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012150 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12151 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12152 } else {
12153 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12154 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12155 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012156 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012157 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12158 vmcs12->vm_entry_intr_info_field);
12159 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12160 vmcs12->vm_entry_exception_error_code);
12161 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12162 vmcs12->vm_entry_instruction_len);
12163 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12164 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012165 vmx->loaded_vmcs->nmi_known_unmasked =
12166 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012167 } else {
12168 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12169 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012170 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012171
Jan Kiszkaf4124502014-03-07 20:03:13 +010012172 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012173
Sean Christophersonf459a702018-08-27 15:21:11 -070012174 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini93140062016-07-06 13:23:51 +020012175 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012176 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12177 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini93140062016-07-06 13:23:51 +020012178
12179 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012180 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012181 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12182 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012183 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012184 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012185 }
Wincy Van705699a2015-02-03 23:58:17 +080012186
Jan Kiszkaf4124502014-03-07 20:03:13 +010012187 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012188
Jan Kiszkaf4124502014-03-07 20:03:13 +010012189 vmx->nested.preemption_timer_expired = false;
12190 if (nested_cpu_has_preemption_timer(vmcs12))
12191 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012192
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012193 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012194 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012195
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012196 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012197 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012198 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012199 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012200 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012201 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012202 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12203 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012204 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012205 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12206 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12207 ~SECONDARY_EXEC_ENABLE_PML;
12208 exec_control |= vmcs12_exec_ctrl;
12209 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012210
Liran Alon32c7acf2018-06-23 02:35:11 +030012211 /* VMCS shadowing for L2 is emulated for now */
12212 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12213
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012214 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012215 vmcs_write16(GUEST_INTR_STATUS,
12216 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012217
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012218 /*
12219 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12220 * nested_get_vmcs12_pages will either fix it up or
12221 * remove the VM execution control.
12222 */
12223 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12224 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12225
Sean Christopherson0b665d32018-08-14 09:33:34 -070012226 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12227 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12228
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012229 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12230 }
12231
Jim Mattson83bafef2016-10-04 10:48:38 -070012232 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012233 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12234 * entry, but only if the current (host) sp changed from the value
12235 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12236 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12237 * here we just force the write to happen on entry.
12238 */
12239 vmx->host_rsp = 0;
12240
12241 exec_control = vmx_exec_control(vmx); /* L0's desires */
12242 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12243 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12244 exec_control &= ~CPU_BASED_TPR_SHADOW;
12245 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012246
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012247 /*
12248 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12249 * nested_get_vmcs12_pages can't fix it up, the illegal value
12250 * will result in a VM entry failure.
12251 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012252 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012253 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012254 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012255 } else {
12256#ifdef CONFIG_X86_64
12257 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12258 CPU_BASED_CR8_STORE_EXITING;
12259#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012260 }
12261
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012262 /*
Quan Xu8eb73e22017-12-12 16:44:21 +080012263 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12264 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012265 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012266 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12267 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12268
12269 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12270
12271 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12272 * bitwise-or of what L1 wants to trap for L2, and what we want to
12273 * trap. Note that CR0.TS also needs updating - we do this later.
12274 */
12275 update_exception_bitmap(vcpu);
12276 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12277 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12278
Sean Christopherson3df5c372018-09-26 09:23:44 -070012279 /*
12280 * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE
12281 * are emulated by vmx_set_efer(), below, but speculate on the
12282 * related bits (if supported by the CPU) in the hope that we can
12283 * avoid VMWrites during vmx_set_efer().
12284 */
12285 guest_efer = nested_vmx_calc_efer(vmx, vmcs12);
12286
Nadav Har'El8049d652013-08-05 11:07:06 +030012287 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12288 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12289 * bits are further modified by vmx_set_efer() below.
12290 */
Sean Christopherson3df5c372018-09-26 09:23:44 -070012291 exec_control = vmcs_config.vmexit_ctrl;
12292 if (cpu_has_load_ia32_efer && guest_efer != host_efer)
12293 exec_control |= VM_EXIT_LOAD_IA32_EFER;
12294 vm_exit_controls_init(vmx, exec_control);
Nadav Har'El8049d652013-08-05 11:07:06 +030012295
Sean Christopherson3df5c372018-09-26 09:23:44 -070012296 exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) &
12297 ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER;
12298 if (cpu_has_load_ia32_efer) {
12299 if (guest_efer & EFER_LMA)
12300 exec_control |= VM_ENTRY_IA32E_MODE;
12301 if (guest_efer != host_efer)
12302 exec_control |= VM_ENTRY_LOAD_IA32_EFER;
12303 }
12304 vm_entry_controls_init(vmx, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012305
Jim Mattson6514dc32018-04-26 16:09:12 -070012306 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012307 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012308 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012309 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012310 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012311 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012312 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012313
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012314 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12315
Peter Feinerc95ba922016-08-17 09:36:47 -070012316 if (kvm_has_tsc_control)
12317 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012318
12319 if (enable_vpid) {
12320 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012321 * There is no direct mapping between vpid02 and vpid12, the
12322 * vpid02 is per-vCPU for L0 and reused while the value of
12323 * vpid12 is changed w/ one invvpid during nested vmentry.
12324 * The vpid12 is allocated by L1 for L2, so it will not
12325 * influence global bitmap(for vpid01 and vpid02 allocation)
12326 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012327 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012328 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012329 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12330 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012331 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012332 }
12333 } else {
Liran Alon14389212018-10-08 23:42:17 +030012334 /*
12335 * If L1 use EPT, then L0 needs to execute INVEPT on
12336 * EPTP02 instead of EPTP01. Therefore, delay TLB
12337 * flush until vmcs02->eptp is fully updated by
12338 * KVM_REQ_LOAD_CR3. Note that this assumes
12339 * KVM_REQ_TLB_FLUSH is evaluated after
12340 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12341 */
12342 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012343 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012344 }
12345
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012346 if (enable_pml) {
12347 /*
12348 * Conceptually we want to copy the PML address and index from
12349 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12350 * since we always flush the log on each vmexit, this happens
12351 * to be equivalent to simply resetting the fields in vmcs02.
12352 */
12353 ASSERT(vmx->pml_pg);
12354 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12355 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12356 }
12357
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012358 if (nested_cpu_has_ept(vmcs12))
12359 nested_ept_init_mmu_context(vcpu);
12360 else if (nested_cpu_has2(vmcs12,
12361 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012362 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012363
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012364 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012365 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12366 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012367 * The CR0_READ_SHADOW is what L2 should have expected to read given
12368 * the specifications by L1; It's not enough to take
12369 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12370 * have more bits than L1 expected.
12371 */
12372 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12373 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12374
12375 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12376 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12377
Sean Christopherson3df5c372018-09-26 09:23:44 -070012378 vcpu->arch.efer = guest_efer;
12379 /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
David Matlack5a6a9742016-11-29 18:14:10 -080012380 vmx_set_efer(vcpu, vcpu->arch.efer);
12381
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012382 /*
12383 * Guest state is invalid and unrestricted guest is disabled,
12384 * which means L1 attempted VMEntry to L2 with invalid state.
12385 * Fail the VMEntry.
12386 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012387 if (vmx->emulation_required) {
12388 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012389 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012390 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012391
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012392 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012393 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012394 entry_failure_code))
12395 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012396
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012397 if (!enable_ept)
12398 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12399
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012400 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12401 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012402 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012403}
12404
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012405static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12406{
12407 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12408 nested_cpu_has_virtual_nmis(vmcs12))
12409 return -EINVAL;
12410
12411 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12412 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12413 return -EINVAL;
12414
12415 return 0;
12416}
12417
Jim Mattsonca0bde22016-11-30 12:03:46 -080012418static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12419{
12420 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012421 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012422
12423 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12424 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12425 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12426
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012427 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12428 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12429
Jim Mattson56a20512017-07-06 16:33:06 -070012430 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12431 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12432
Jim Mattsonca0bde22016-11-30 12:03:46 -080012433 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12434 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12435
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012436 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12437 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12438
Jim Mattson712b12d2017-08-24 13:24:47 -070012439 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12440 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12441
Jim Mattsonca0bde22016-11-30 12:03:46 -080012442 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12443 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12444
12445 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12446 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12447
Bandan Dasc5f983f2017-05-05 15:25:14 -040012448 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12449 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12450
Liran Alona8a7c022018-06-23 02:35:06 +030012451 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12452 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12453
Jim Mattsonca0bde22016-11-30 12:03:46 -080012454 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012455 vmx->nested.msrs.procbased_ctls_low,
12456 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012457 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12458 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012459 vmx->nested.msrs.secondary_ctls_low,
12460 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012461 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012462 vmx->nested.msrs.pinbased_ctls_low,
12463 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012464 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012465 vmx->nested.msrs.exit_ctls_low,
12466 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012467 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012468 vmx->nested.msrs.entry_ctls_low,
12469 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012470 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12471
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012472 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012473 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12474
Bandan Das41ab9372017-08-03 15:54:43 -040012475 if (nested_cpu_has_vmfunc(vmcs12)) {
12476 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012477 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012478 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12479
12480 if (nested_cpu_has_eptp_switching(vmcs12)) {
12481 if (!nested_cpu_has_ept(vmcs12) ||
12482 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12483 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12484 }
12485 }
Bandan Das27c42a12017-08-03 15:54:42 -040012486
Jim Mattsonc7c2c702017-05-05 11:28:09 -070012487 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12488 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12489
Jim Mattsonca0bde22016-11-30 12:03:46 -080012490 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12491 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12492 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12493 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12494
Marc Orr04473782018-06-20 17:21:29 -070012495 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012496 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12497 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12498 * the values of the LMA and LME bits in the field must each be that of
12499 * the host address-space size VM-exit control.
12500 */
12501 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12502 ia32e = (vmcs12->vm_exit_controls &
12503 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12504 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12505 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12506 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12507 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12508 }
12509
12510 /*
Marc Orr04473782018-06-20 17:21:29 -070012511 * From the Intel SDM, volume 3:
12512 * Fields relevant to VM-entry event injection must be set properly.
12513 * These fields are the VM-entry interruption-information field, the
12514 * VM-entry exception error code, and the VM-entry instruction length.
12515 */
12516 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12517 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12518 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12519 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12520 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12521 bool should_have_error_code;
12522 bool urg = nested_cpu_has2(vmcs12,
12523 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12524 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12525
12526 /* VM-entry interruption-info field: interruption type */
12527 if (intr_type == INTR_TYPE_RESERVED ||
12528 (intr_type == INTR_TYPE_OTHER_EVENT &&
12529 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12530 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12531
12532 /* VM-entry interruption-info field: vector */
12533 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12534 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12535 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12536 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12537
12538 /* VM-entry interruption-info field: deliver error code */
12539 should_have_error_code =
12540 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12541 x86_exception_has_error_code(vector);
12542 if (has_error_code != should_have_error_code)
12543 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12544
12545 /* VM-entry exception error code */
12546 if (has_error_code &&
12547 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12548 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12549
12550 /* VM-entry interruption-info field: reserved bits */
12551 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12552 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12553
12554 /* VM-entry instruction length */
12555 switch (intr_type) {
12556 case INTR_TYPE_SOFT_EXCEPTION:
12557 case INTR_TYPE_SOFT_INTR:
12558 case INTR_TYPE_PRIV_SW_EXCEPTION:
12559 if ((vmcs12->vm_entry_instruction_len > 15) ||
12560 (vmcs12->vm_entry_instruction_len == 0 &&
12561 !nested_cpu_has_zero_length_injection(vcpu)))
12562 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12563 }
12564 }
12565
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012566 if (nested_cpu_has_ept(vmcs12) &&
12567 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12568 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12569
Jim Mattsonca0bde22016-11-30 12:03:46 -080012570 return 0;
12571}
12572
Liran Alonf145d902018-06-23 02:35:07 +030012573static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12574 struct vmcs12 *vmcs12)
12575{
12576 int r;
12577 struct page *page;
12578 struct vmcs12 *shadow;
12579
12580 if (vmcs12->vmcs_link_pointer == -1ull)
12581 return 0;
12582
12583 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12584 return -EINVAL;
12585
12586 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12587 if (is_error_page(page))
12588 return -EINVAL;
12589
12590 r = 0;
12591 shadow = kmap(page);
12592 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12593 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12594 r = -EINVAL;
12595 kunmap(page);
12596 kvm_release_page_clean(page);
12597 return r;
12598}
12599
Jim Mattsonca0bde22016-11-30 12:03:46 -080012600static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12601 u32 *exit_qual)
12602{
12603 bool ia32e;
12604
12605 *exit_qual = ENTRY_FAIL_DEFAULT;
12606
12607 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12608 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12609 return 1;
12610
Liran Alonf145d902018-06-23 02:35:07 +030012611 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012612 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12613 return 1;
12614 }
12615
12616 /*
12617 * If the load IA32_EFER VM-entry control is 1, the following checks
12618 * are performed on the field for the IA32_EFER MSR:
12619 * - Bits reserved in the IA32_EFER MSR must be 0.
12620 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12621 * the IA-32e mode guest VM-exit control. It must also be identical
12622 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12623 * CR0.PG) is 1.
12624 */
12625 if (to_vmx(vcpu)->nested.nested_run_pending &&
12626 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12627 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12628 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12629 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12630 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12631 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12632 return 1;
12633 }
12634
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012635 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12636 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12637 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12638 return 1;
12639
Jim Mattsonca0bde22016-11-30 12:03:46 -080012640 return 0;
12641}
12642
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012643/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012644 * If exit_qual is NULL, this is being called from state restore (either RSM
12645 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012646 */
Sean Christophersond63907d2018-09-26 09:23:45 -070012647static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012648{
12649 struct vcpu_vmx *vmx = to_vmx(vcpu);
12650 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012651 bool from_vmentry = !!exit_qual;
12652 u32 dummy_exit_qual;
Paolo Bonzini7e712682018-10-03 13:44:26 +020012653 bool evaluate_pending_interrupts;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012654 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012655
Paolo Bonzini7e712682018-10-03 13:44:26 +020012656 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12657 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12658 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12659 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012660
Jim Mattson858e25c2016-11-30 12:03:47 -080012661 enter_guest_mode(vcpu);
12662
12663 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12664 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012665 if (kvm_mpx_supported() &&
12666 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12667 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012668
Jim Mattsonde3a0022017-11-27 17:22:25 -060012669 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012670
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012671 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12672 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12673
12674 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012675 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012676 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012677
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012678 if (from_vmentry) {
12679 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012680
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012681 r = EXIT_REASON_MSR_LOAD_FAIL;
12682 *exit_qual = nested_vmx_load_msr(vcpu,
12683 vmcs12->vm_entry_msr_load_addr,
12684 vmcs12->vm_entry_msr_load_count);
12685 if (*exit_qual)
12686 goto fail;
12687 } else {
12688 /*
12689 * The MMU is not initialized to point at the right entities yet and
12690 * "get pages" would need to read data from the guest (i.e. we will
12691 * need to perform gpa to hpa translation). Request a call
12692 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12693 * have already been set at vmentry time and should not be reset.
12694 */
12695 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12696 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012697
Jim Mattson858e25c2016-11-30 12:03:47 -080012698 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012699 * If L1 had a pending IRQ/NMI until it executed
12700 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12701 * disallowed (e.g. interrupts disabled), L0 needs to
12702 * evaluate if this pending event should cause an exit from L2
12703 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12704 * intercept EXTERNAL_INTERRUPT).
12705 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012706 * Usually this would be handled by the processor noticing an
12707 * IRQ/NMI window request, or checking RVI during evaluation of
12708 * pending virtual interrupts. However, this setting was done
12709 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12710 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012711 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012712 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012713 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012714
12715 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012716 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12717 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12718 * returned as far as L1 is concerned. It will only return (and set
12719 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12720 */
12721 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012722
12723fail:
12724 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12725 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12726 leave_guest_mode(vcpu);
12727 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012728 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012729}
12730
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012731/*
12732 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12733 * for running an L2 nested guest.
12734 */
12735static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12736{
12737 struct vmcs12 *vmcs12;
12738 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012739 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012740 u32 exit_qual;
12741 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012742
Kyle Hueyeb277562016-11-29 12:40:39 -080012743 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012744 return 1;
12745
Kyle Hueyeb277562016-11-29 12:40:39 -080012746 if (!nested_vmx_check_vmcs12(vcpu))
12747 goto out;
12748
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012749 vmcs12 = get_vmcs12(vcpu);
12750
Liran Alona6192d42018-06-23 02:35:04 +030012751 /*
12752 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12753 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12754 * rather than RFLAGS.ZF, and no error number is stored to the
12755 * VM-instruction error field.
12756 */
12757 if (vmcs12->hdr.shadow_vmcs) {
12758 nested_vmx_failInvalid(vcpu);
12759 goto out;
12760 }
12761
Abel Gordon012f83c2013-04-18 14:39:25 +030012762 if (enable_shadow_vmcs)
12763 copy_shadow_to_vmcs12(vmx);
12764
Nadav Har'El7c177932011-05-25 23:12:04 +030012765 /*
12766 * The nested entry process starts with enforcing various prerequisites
12767 * on vmcs12 as required by the Intel SDM, and act appropriately when
12768 * they fail: As the SDM explains, some conditions should cause the
12769 * instruction to fail, while others will cause the instruction to seem
12770 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12771 * To speed up the normal (success) code path, we should avoid checking
12772 * for misconfigurations which will anyway be caught by the processor
12773 * when using the merged vmcs02.
12774 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012775 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12776 nested_vmx_failValid(vcpu,
12777 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12778 goto out;
12779 }
12780
Nadav Har'El7c177932011-05-25 23:12:04 +030012781 if (vmcs12->launch_state == launch) {
12782 nested_vmx_failValid(vcpu,
12783 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12784 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012785 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012786 }
12787
Jim Mattsonca0bde22016-11-30 12:03:46 -080012788 ret = check_vmentry_prereqs(vcpu, vmcs12);
12789 if (ret) {
12790 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012791 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012792 }
12793
Nadav Har'El7c177932011-05-25 23:12:04 +030012794 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012795 * After this point, the trap flag no longer triggers a singlestep trap
12796 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12797 * This is not 100% correct; for performance reasons, we delegate most
12798 * of the checks on host state to the processor. If those fail,
12799 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012800 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012801 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012802
Jim Mattsonca0bde22016-11-30 12:03:46 -080012803 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12804 if (ret) {
12805 nested_vmx_entry_failure(vcpu, vmcs12,
12806 EXIT_REASON_INVALID_STATE, exit_qual);
12807 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012808 }
12809
12810 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012811 * We're finally done with prerequisite checking, and can start with
12812 * the nested entry.
12813 */
12814
Jim Mattson6514dc32018-04-26 16:09:12 -070012815 vmx->nested.nested_run_pending = 1;
Sean Christophersond63907d2018-09-26 09:23:45 -070012816 ret = nested_vmx_enter_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012817 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012818 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012819 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012820 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012821 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012822
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012823 /* Hide L1D cache contents from the nested guest. */
12824 vmx->vcpu.arch.l1tf_flush_l1d = true;
12825
Chao Gao135a06c2018-02-11 10:06:30 +080012826 /*
Sean Christophersond63907d2018-09-26 09:23:45 -070012827 * Must happen outside of nested_vmx_enter_non_root_mode() as it will
Liran Alon61ada742018-06-23 02:35:08 +030012828 * also be used as part of restoring nVMX state for
12829 * snapshot restore (migration).
12830 *
12831 * In this flow, it is assumed that vmcs12 cache was
12832 * trasferred as part of captured nVMX state and should
12833 * therefore not be read from guest memory (which may not
12834 * exist on destination host yet).
12835 */
12836 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12837
12838 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012839 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12840 * by event injection, halt vcpu.
12841 */
12842 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012843 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12844 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012845 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012846 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012847 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012848
12849out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012850 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012851}
12852
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012853/*
12854 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12855 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12856 * This function returns the new value we should put in vmcs12.guest_cr0.
12857 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12858 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12859 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12860 * didn't trap the bit, because if L1 did, so would L0).
12861 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12862 * been modified by L2, and L1 knows it. So just leave the old value of
12863 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12864 * isn't relevant, because if L0 traps this bit it can set it to anything.
12865 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12866 * changed these bits, and therefore they need to be updated, but L0
12867 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12868 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12869 */
12870static inline unsigned long
12871vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12872{
12873 return
12874 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12875 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12876 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12877 vcpu->arch.cr0_guest_owned_bits));
12878}
12879
12880static inline unsigned long
12881vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12882{
12883 return
12884 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12885 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12886 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12887 vcpu->arch.cr4_guest_owned_bits));
12888}
12889
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012890static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12891 struct vmcs12 *vmcs12)
12892{
12893 u32 idt_vectoring;
12894 unsigned int nr;
12895
Wanpeng Li664f8e22017-08-24 03:35:09 -070012896 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012897 nr = vcpu->arch.exception.nr;
12898 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12899
12900 if (kvm_exception_is_soft(nr)) {
12901 vmcs12->vm_exit_instruction_len =
12902 vcpu->arch.event_exit_inst_len;
12903 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12904 } else
12905 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12906
12907 if (vcpu->arch.exception.has_error_code) {
12908 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12909 vmcs12->idt_vectoring_error_code =
12910 vcpu->arch.exception.error_code;
12911 }
12912
12913 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012914 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012915 vmcs12->idt_vectoring_info_field =
12916 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012917 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012918 nr = vcpu->arch.interrupt.nr;
12919 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12920
12921 if (vcpu->arch.interrupt.soft) {
12922 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12923 vmcs12->vm_entry_instruction_len =
12924 vcpu->arch.event_exit_inst_len;
12925 } else
12926 idt_vectoring |= INTR_TYPE_EXT_INTR;
12927
12928 vmcs12->idt_vectoring_info_field = idt_vectoring;
12929 }
12930}
12931
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012932static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12933{
12934 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012935 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012936 bool block_nested_events =
12937 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012938
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012939 if (vcpu->arch.exception.pending &&
12940 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012941 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012942 return -EBUSY;
12943 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012944 return 0;
12945 }
12946
Jan Kiszkaf4124502014-03-07 20:03:13 +010012947 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12948 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012949 if (block_nested_events)
Jan Kiszkaf4124502014-03-07 20:03:13 +010012950 return -EBUSY;
12951 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12952 return 0;
12953 }
12954
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012955 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012956 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012957 return -EBUSY;
12958 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12959 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12960 INTR_INFO_VALID_MASK, 0);
12961 /*
12962 * The NMI-triggered VM exit counts as injection:
12963 * clear this one and block further NMIs.
12964 */
12965 vcpu->arch.nmi_pending = 0;
12966 vmx_set_nmi_mask(vcpu, true);
12967 return 0;
12968 }
12969
12970 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12971 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012972 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012973 return -EBUSY;
12974 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012975 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012976 }
12977
David Hildenbrand6342c502017-01-25 11:58:58 +010012978 vmx_complete_nested_posted_interrupt(vcpu);
12979 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012980}
12981
Sean Christophersond264ee02018-08-27 15:21:12 -070012982static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12983{
12984 to_vmx(vcpu)->req_immediate_exit = true;
12985}
12986
Jan Kiszkaf4124502014-03-07 20:03:13 +010012987static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12988{
12989 ktime_t remaining =
12990 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12991 u64 value;
12992
12993 if (ktime_to_ns(remaining) <= 0)
12994 return 0;
12995
12996 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12997 do_div(value, 1000000);
12998 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12999}
13000
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013001/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013002 * Update the guest state fields of vmcs12 to reflect changes that
13003 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
13004 * VM-entry controls is also updated, since this is really a guest
13005 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013006 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013007static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013008{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013009 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
13010 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
13011
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013012 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
13013 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
13014 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
13015
13016 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
13017 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
13018 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
13019 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
13020 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
13021 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
13022 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
13023 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13024 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13025 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13026 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13027 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13028 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13029 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13030 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13031 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13032 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13033 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13034 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13035 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13036 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13037 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13038 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13039 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13040 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13041 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13042 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13043 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13044 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13045 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13046 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13047 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13048 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13049 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13050 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13051 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13052
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013053 vmcs12->guest_interruptibility_info =
13054 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13055 vmcs12->guest_pending_dbg_exceptions =
13056 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013057 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13058 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13059 else
13060 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013061
Jan Kiszkaf4124502014-03-07 20:03:13 +010013062 if (nested_cpu_has_preemption_timer(vmcs12)) {
13063 if (vmcs12->vm_exit_controls &
13064 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13065 vmcs12->vmx_preemption_timer_value =
13066 vmx_get_preemption_timer_value(vcpu);
13067 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13068 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013069
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013070 /*
13071 * In some cases (usually, nested EPT), L2 is allowed to change its
13072 * own CR3 without exiting. If it has changed it, we must keep it.
13073 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13074 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13075 *
13076 * Additionally, restore L2's PDPTR to vmcs12.
13077 */
13078 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013079 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013080 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13081 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13082 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13083 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13084 }
13085
Jim Mattsond281e132017-06-01 12:44:46 -070013086 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013087
Wincy Van608406e2015-02-03 23:57:51 +080013088 if (nested_cpu_has_vid(vmcs12))
13089 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13090
Jan Kiszkac18911a2013-03-13 16:06:41 +010013091 vmcs12->vm_entry_controls =
13092 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013093 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013094
Jan Kiszka2996fca2014-06-16 13:59:43 +020013095 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13096 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13097 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13098 }
13099
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013100 /* TODO: These cannot have changed unless we have MSR bitmaps and
13101 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013102 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013103 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013104 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13105 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013106 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13107 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13108 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013109 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013110 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013111}
13112
13113/*
13114 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13115 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13116 * and this function updates it to reflect the changes to the guest state while
13117 * L2 was running (and perhaps made some exits which were handled directly by L0
13118 * without going back to L1), and to reflect the exit reason.
13119 * Note that we do not have to copy here all VMCS fields, just those that
13120 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13121 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13122 * which already writes to vmcs12 directly.
13123 */
13124static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13125 u32 exit_reason, u32 exit_intr_info,
13126 unsigned long exit_qualification)
13127{
13128 /* update guest state fields: */
13129 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013130
13131 /* update exit information fields: */
13132
Jan Kiszka533558b2014-01-04 18:47:20 +010013133 vmcs12->vm_exit_reason = exit_reason;
13134 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013135 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013136
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013137 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013138 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13139 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13140
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013141 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013142 vmcs12->launch_state = 1;
13143
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013144 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13145 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013146 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013147
13148 /*
13149 * Transfer the event that L0 or L1 may wanted to inject into
13150 * L2 to IDT_VECTORING_INFO_FIELD.
13151 */
13152 vmcs12_save_pending_event(vcpu, vmcs12);
13153 }
13154
13155 /*
13156 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13157 * preserved above and would only end up incorrectly in L1.
13158 */
13159 vcpu->arch.nmi_injected = false;
13160 kvm_clear_exception_queue(vcpu);
13161 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013162}
13163
13164/*
13165 * A part of what we need to when the nested L2 guest exits and we want to
13166 * run its L1 parent, is to reset L1's guest state to the host state specified
13167 * in vmcs12.
13168 * This function is to be called not only on normal nested exit, but also on
13169 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13170 * Failures During or After Loading Guest State").
13171 * This function should be called when the active VMCS is L1's (vmcs01).
13172 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013173static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13174 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013175{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013176 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013177 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013178
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013179 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13180 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013181 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013182 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13183 else
13184 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13185 vmx_set_efer(vcpu, vcpu->arch.efer);
13186
13187 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13188 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013189 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013190 /*
13191 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013192 * actually changed, because vmx_set_cr0 refers to efer set above.
13193 *
13194 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13195 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013196 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013197 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4db2013-09-03 21:11:45 +020013198 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013199
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013200 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013201 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013202 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013203
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013204 nested_ept_uninit_mmu_context(vcpu);
13205
13206 /*
13207 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13208 * couldn't have changed.
13209 */
13210 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13211 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13212
13213 if (!enable_ept)
13214 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013215
Liran Alon6f1e03b2018-05-22 17:16:14 +030013216 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013217 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013218 * VMEntry/VMExit. Thus, no need to flush TLB.
13219 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013220 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13221 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013222 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013223 * Otherwise, we can preserve TLB entries as long as we are
13224 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013225 *
13226 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13227 * and therefore we request the TLB flush to happen only after VMCS EPTP
13228 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013229 */
13230 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013231 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013232 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013233 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013234
13235 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13236 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13237 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13238 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13239 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013240 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13241 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013242
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013243 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13244 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13245 vmcs_write64(GUEST_BNDCFGS, 0);
13246
Jan Kiszka44811c02013-08-04 17:17:27 +020013247 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013248 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013249 vcpu->arch.pat = vmcs12->host_ia32_pat;
13250 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013251 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13252 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13253 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013254
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013255 /* Set L1 segment info according to Intel SDM
13256 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13257 seg = (struct kvm_segment) {
13258 .base = 0,
13259 .limit = 0xFFFFFFFF,
13260 .selector = vmcs12->host_cs_selector,
13261 .type = 11,
13262 .present = 1,
13263 .s = 1,
13264 .g = 1
13265 };
13266 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13267 seg.l = 1;
13268 else
13269 seg.db = 1;
13270 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13271 seg = (struct kvm_segment) {
13272 .base = 0,
13273 .limit = 0xFFFFFFFF,
13274 .type = 3,
13275 .present = 1,
13276 .s = 1,
13277 .db = 1,
13278 .g = 1
13279 };
13280 seg.selector = vmcs12->host_ds_selector;
13281 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13282 seg.selector = vmcs12->host_es_selector;
13283 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13284 seg.selector = vmcs12->host_ss_selector;
13285 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13286 seg.selector = vmcs12->host_fs_selector;
13287 seg.base = vmcs12->host_fs_base;
13288 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13289 seg.selector = vmcs12->host_gs_selector;
13290 seg.base = vmcs12->host_gs_base;
13291 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13292 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013293 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013294 .limit = 0x67,
13295 .selector = vmcs12->host_tr_selector,
13296 .type = 11,
13297 .present = 1
13298 };
13299 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13300
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013301 kvm_set_dr(vcpu, 7, 0x400);
13302 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013303
Wincy Van3af18d92015-02-03 23:49:31 +080013304 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013305 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013306
Wincy Vanff651cb2014-12-11 08:52:58 +030013307 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13308 vmcs12->vm_exit_msr_load_count))
13309 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013310}
13311
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013312static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13313{
13314 struct shared_msr_entry *efer_msr;
13315 unsigned int i;
13316
13317 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13318 return vmcs_read64(GUEST_IA32_EFER);
13319
13320 if (cpu_has_load_ia32_efer)
13321 return host_efer;
13322
13323 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13324 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13325 return vmx->msr_autoload.guest.val[i].value;
13326 }
13327
13328 efer_msr = find_msr_entry(vmx, MSR_EFER);
13329 if (efer_msr)
13330 return efer_msr->data;
13331
13332 return host_efer;
13333}
13334
13335static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13336{
13337 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13338 struct vcpu_vmx *vmx = to_vmx(vcpu);
13339 struct vmx_msr_entry g, h;
13340 struct msr_data msr;
13341 gpa_t gpa;
13342 u32 i, j;
13343
13344 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13345
13346 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13347 /*
13348 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13349 * as vmcs01.GUEST_DR7 contains a userspace defined value
13350 * and vcpu->arch.dr7 is not squirreled away before the
13351 * nested VMENTER (not worth adding a variable in nested_vmx).
13352 */
13353 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13354 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13355 else
13356 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13357 }
13358
13359 /*
13360 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13361 * handle a variety of side effects to KVM's software model.
13362 */
13363 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13364
13365 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13366 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13367
13368 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13369 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13370
13371 nested_ept_uninit_mmu_context(vcpu);
13372 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13373 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13374
13375 /*
13376 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13377 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13378 * VMFail, like everything else we just need to ensure our
13379 * software model is up-to-date.
13380 */
13381 ept_save_pdptrs(vcpu);
13382
13383 kvm_mmu_reset_context(vcpu);
13384
13385 if (cpu_has_vmx_msr_bitmap())
13386 vmx_update_msr_bitmap(vcpu);
13387
13388 /*
13389 * This nasty bit of open coding is a compromise between blindly
13390 * loading L1's MSRs using the exit load lists (incorrect emulation
13391 * of VMFail), leaving the nested VM's MSRs in the software model
13392 * (incorrect behavior) and snapshotting the modified MSRs (too
13393 * expensive since the lists are unbound by hardware). For each
13394 * MSR that was (prematurely) loaded from the nested VMEntry load
13395 * list, reload it from the exit load list if it exists and differs
13396 * from the guest value. The intent is to stuff host state as
13397 * silently as possible, not to fully process the exit load list.
13398 */
13399 msr.host_initiated = false;
13400 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13401 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13402 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13403 pr_debug_ratelimited(
13404 "%s read MSR index failed (%u, 0x%08llx)\n",
13405 __func__, i, gpa);
13406 goto vmabort;
13407 }
13408
13409 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13410 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13411 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13412 pr_debug_ratelimited(
13413 "%s read MSR failed (%u, 0x%08llx)\n",
13414 __func__, j, gpa);
13415 goto vmabort;
13416 }
13417 if (h.index != g.index)
13418 continue;
13419 if (h.value == g.value)
13420 break;
13421
13422 if (nested_vmx_load_msr_check(vcpu, &h)) {
13423 pr_debug_ratelimited(
13424 "%s check failed (%u, 0x%x, 0x%x)\n",
13425 __func__, j, h.index, h.reserved);
13426 goto vmabort;
13427 }
13428
13429 msr.index = h.index;
13430 msr.data = h.value;
13431 if (kvm_set_msr(vcpu, &msr)) {
13432 pr_debug_ratelimited(
13433 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13434 __func__, j, h.index, h.value);
13435 goto vmabort;
13436 }
13437 }
13438 }
13439
13440 return;
13441
13442vmabort:
13443 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13444}
13445
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013446/*
13447 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13448 * and modify vmcs12 to make it see what it would expect to see there if
13449 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13450 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013451static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13452 u32 exit_intr_info,
13453 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013454{
13455 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013456 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13457
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013458 /* trying to cancel vmlaunch/vmresume is a bug */
13459 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13460
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013461 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013462 * The only expected VM-instruction error is "VM entry with
13463 * invalid control field(s)." Anything else indicates a
13464 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013465 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013466 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13467 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13468
13469 leave_guest_mode(vcpu);
13470
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013471 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13472 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13473
Jim Mattson4f350c62017-09-14 16:31:44 -070013474 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013475 if (exit_reason == -1)
13476 sync_vmcs12(vcpu, vmcs12);
13477 else
13478 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13479 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013480
Liran Alon61ada742018-06-23 02:35:08 +030013481 /*
13482 * Must happen outside of sync_vmcs12() as it will
13483 * also be used to capture vmcs12 cache as part of
13484 * capturing nVMX state for snapshot (migration).
13485 *
13486 * Otherwise, this flush will dirty guest memory at a
13487 * point it is already assumed by user-space to be
13488 * immutable.
13489 */
13490 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13491
Jim Mattson4f350c62017-09-14 16:31:44 -070013492 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13493 vmcs12->vm_exit_msr_store_count))
13494 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013495 }
13496
Jim Mattson4f350c62017-09-14 16:31:44 -070013497 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013498
Paolo Bonzini93140062016-07-06 13:23:51 +020013499 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013500 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13501 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013502 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013503
Peter Feinerc95ba922016-08-17 09:36:47 -070013504 if (kvm_has_tsc_control)
13505 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013506
Jim Mattson8d860bb2018-05-09 16:56:05 -040013507 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13508 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13509 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013510 } else if (!nested_cpu_has_ept(vmcs12) &&
13511 nested_cpu_has2(vmcs12,
13512 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013513 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013514 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013515
13516 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13517 vmx->host_rsp = 0;
13518
13519 /* Unpin physical memory we referred to in vmcs02 */
13520 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013521 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013522 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013523 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013524 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013525 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013526 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013527 }
Wincy Van705699a2015-02-03 23:58:17 +080013528 if (vmx->nested.pi_desc_page) {
13529 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013530 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013531 vmx->nested.pi_desc_page = NULL;
13532 vmx->nested.pi_desc = NULL;
13533 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013534
13535 /*
Tang Chen38b99172014-09-24 15:57:54 +080013536 * We are now running in L2, mmu_notifier will force to reload the
13537 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13538 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013539 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013540
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013541 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013542 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013543
13544 /* in case we halted in L2 */
13545 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013546
13547 if (likely(!vmx->fail)) {
13548 /*
13549 * TODO: SDM says that with acknowledge interrupt on
13550 * exit, bit 31 of the VM-exit interrupt information
13551 * (valid interrupt) is always set to 1 on
13552 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13553 * need kvm_cpu_has_interrupt(). See the commit
13554 * message for details.
13555 */
13556 if (nested_exit_intr_ack_set(vcpu) &&
13557 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13558 kvm_cpu_has_interrupt(vcpu)) {
13559 int irq = kvm_cpu_get_interrupt(vcpu);
13560 WARN_ON(irq < 0);
13561 vmcs12->vm_exit_intr_info = irq |
13562 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13563 }
13564
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013565 if (exit_reason != -1)
13566 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13567 vmcs12->exit_qualification,
13568 vmcs12->idt_vectoring_info_field,
13569 vmcs12->vm_exit_intr_info,
13570 vmcs12->vm_exit_intr_error_code,
13571 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013572
13573 load_vmcs12_host_state(vcpu, vmcs12);
13574
13575 return;
13576 }
13577
13578 /*
13579 * After an early L2 VM-entry failure, we're now back
13580 * in L1 which thinks it just finished a VMLAUNCH or
13581 * VMRESUME instruction, so we need to set the failure
13582 * flag and the VM-instruction error field of the VMCS
13583 * accordingly.
13584 */
13585 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013586
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013587 /*
13588 * Restore L1's host state to KVM's software model. We're here
13589 * because a consistency check was caught by hardware, which
13590 * means some amount of guest state has been propagated to KVM's
13591 * model and needs to be unwound to the host's state.
13592 */
13593 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013594
Jim Mattson4f350c62017-09-14 16:31:44 -070013595 /*
13596 * The emulated instruction was already skipped in
13597 * nested_vmx_run, but the updated RIP was never
13598 * written back to the vmcs01.
13599 */
13600 skip_emulated_instruction(vcpu);
13601 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013602}
13603
Nadav Har'El7c177932011-05-25 23:12:04 +030013604/*
Jan Kiszka42124922014-01-04 18:47:19 +010013605 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13606 */
13607static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13608{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013609 if (is_guest_mode(vcpu)) {
13610 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013611 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013612 }
Jan Kiszka42124922014-01-04 18:47:19 +010013613 free_nested(to_vmx(vcpu));
13614}
13615
13616/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013617 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13618 * 23.7 "VM-entry failures during or after loading guest state" (this also
13619 * lists the acceptable exit-reason and exit-qualification parameters).
13620 * It should only be called before L2 actually succeeded to run, and when
13621 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13622 */
13623static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13624 struct vmcs12 *vmcs12,
13625 u32 reason, unsigned long qualification)
13626{
13627 load_vmcs12_host_state(vcpu, vmcs12);
13628 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13629 vmcs12->exit_qualification = qualification;
13630 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013631 if (enable_shadow_vmcs)
13632 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013633}
13634
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013635static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13636 struct x86_instruction_info *info,
13637 enum x86_intercept_stage stage)
13638{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013639 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13640 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13641
13642 /*
13643 * RDPID causes #UD if disabled through secondary execution controls.
13644 * Because it is marked as EmulateOnUD, we need to intercept it here.
13645 */
13646 if (info->intercept == x86_intercept_rdtscp &&
13647 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13648 ctxt->exception.vector = UD_VECTOR;
13649 ctxt->exception.error_code_valid = false;
13650 return X86EMUL_PROPAGATE_FAULT;
13651 }
13652
13653 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013654 return X86EMUL_CONTINUE;
13655}
13656
Yunhong Jiang64672c92016-06-13 14:19:59 -070013657#ifdef CONFIG_X86_64
13658/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13659static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13660 u64 divisor, u64 *result)
13661{
13662 u64 low = a << shift, high = a >> (64 - shift);
13663
13664 /* To avoid the overflow on divq */
13665 if (high >= divisor)
13666 return 1;
13667
13668 /* Low hold the result, high hold rem which is discarded */
13669 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13670 "rm" (divisor), "0" (low), "1" (high));
13671 *result = low;
13672
13673 return 0;
13674}
13675
13676static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13677{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013678 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013679 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013680
13681 if (kvm_mwait_in_guest(vcpu->kvm))
13682 return -EOPNOTSUPP;
13683
13684 vmx = to_vmx(vcpu);
13685 tscl = rdtsc();
13686 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13687 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013688 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13689
13690 if (delta_tsc > lapic_timer_advance_cycles)
13691 delta_tsc -= lapic_timer_advance_cycles;
13692 else
13693 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013694
13695 /* Convert to host delta tsc if tsc scaling is enabled */
13696 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13697 u64_shl_div_u64(delta_tsc,
13698 kvm_tsc_scaling_ratio_frac_bits,
13699 vcpu->arch.tsc_scaling_ratio,
13700 &delta_tsc))
13701 return -ERANGE;
13702
13703 /*
13704 * If the delta tsc can't fit in the 32 bit after the multi shift,
13705 * we can't use the preemption timer.
13706 * It's possible that it fits on later vmentries, but checking
13707 * on every vmentry is costly so we just use an hrtimer.
13708 */
13709 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13710 return -ERANGE;
13711
13712 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013713 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013714}
13715
13716static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13717{
Sean Christophersonf459a702018-08-27 15:21:11 -070013718 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013719}
13720#endif
13721
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013722static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013723{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013724 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013725 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013726}
13727
Kai Huang843e4332015-01-28 10:54:28 +080013728static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13729 struct kvm_memory_slot *slot)
13730{
13731 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13732 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13733}
13734
13735static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13736 struct kvm_memory_slot *slot)
13737{
13738 kvm_mmu_slot_set_dirty(kvm, slot);
13739}
13740
13741static void vmx_flush_log_dirty(struct kvm *kvm)
13742{
13743 kvm_flush_pml_buffers(kvm);
13744}
13745
Bandan Dasc5f983f2017-05-05 15:25:14 -040013746static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13747{
13748 struct vmcs12 *vmcs12;
13749 struct vcpu_vmx *vmx = to_vmx(vcpu);
13750 gpa_t gpa;
13751 struct page *page = NULL;
13752 u64 *pml_address;
13753
13754 if (is_guest_mode(vcpu)) {
13755 WARN_ON_ONCE(vmx->nested.pml_full);
13756
13757 /*
13758 * Check if PML is enabled for the nested guest.
13759 * Whether eptp bit 6 is set is already checked
13760 * as part of A/D emulation.
13761 */
13762 vmcs12 = get_vmcs12(vcpu);
13763 if (!nested_cpu_has_pml(vmcs12))
13764 return 0;
13765
Dan Carpenter47698862017-05-10 22:43:17 +030013766 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013767 vmx->nested.pml_full = true;
13768 return 1;
13769 }
13770
13771 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13772
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013773 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13774 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013775 return 0;
13776
13777 pml_address = kmap(page);
13778 pml_address[vmcs12->guest_pml_index--] = gpa;
13779 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013780 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013781 }
13782
13783 return 0;
13784}
13785
Kai Huang843e4332015-01-28 10:54:28 +080013786static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13787 struct kvm_memory_slot *memslot,
13788 gfn_t offset, unsigned long mask)
13789{
13790 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13791}
13792
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013793static void __pi_post_block(struct kvm_vcpu *vcpu)
13794{
13795 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13796 struct pi_desc old, new;
13797 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013798
13799 do {
13800 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013801 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13802 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013803
13804 dest = cpu_physical_id(vcpu->cpu);
13805
13806 if (x2apic_enabled())
13807 new.ndst = dest;
13808 else
13809 new.ndst = (dest << 8) & 0xFF00;
13810
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013811 /* set 'NV' to 'notification vector' */
13812 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013813 } while (cmpxchg64(&pi_desc->control, old.control,
13814 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013815
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013816 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13817 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013818 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013819 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013820 vcpu->pre_pcpu = -1;
13821 }
13822}
13823
Feng Wuefc64402015-09-18 22:29:51 +080013824/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013825 * This routine does the following things for vCPU which is going
13826 * to be blocked if VT-d PI is enabled.
13827 * - Store the vCPU to the wakeup list, so when interrupts happen
13828 * we can find the right vCPU to wake up.
13829 * - Change the Posted-interrupt descriptor as below:
13830 * 'NDST' <-- vcpu->pre_pcpu
13831 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13832 * - If 'ON' is set during this process, which means at least one
13833 * interrupt is posted for this vCPU, we cannot block it, in
13834 * this case, return 1, otherwise, return 0.
13835 *
13836 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013837static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013838{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013839 unsigned int dest;
13840 struct pi_desc old, new;
13841 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13842
13843 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013844 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13845 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013846 return 0;
13847
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013848 WARN_ON(irqs_disabled());
13849 local_irq_disable();
13850 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13851 vcpu->pre_pcpu = vcpu->cpu;
13852 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13853 list_add_tail(&vcpu->blocked_vcpu_list,
13854 &per_cpu(blocked_vcpu_on_cpu,
13855 vcpu->pre_pcpu));
13856 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13857 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013858
13859 do {
13860 old.control = new.control = pi_desc->control;
13861
Feng Wubf9f6ac2015-09-18 22:29:55 +080013862 WARN((pi_desc->sn == 1),
13863 "Warning: SN field of posted-interrupts "
13864 "is set before blocking\n");
13865
13866 /*
13867 * Since vCPU can be preempted during this process,
13868 * vcpu->cpu could be different with pre_pcpu, we
13869 * need to set pre_pcpu as the destination of wakeup
13870 * notification event, then we can find the right vCPU
13871 * to wakeup in wakeup handler if interrupts happen
13872 * when the vCPU is in blocked state.
13873 */
13874 dest = cpu_physical_id(vcpu->pre_pcpu);
13875
13876 if (x2apic_enabled())
13877 new.ndst = dest;
13878 else
13879 new.ndst = (dest << 8) & 0xFF00;
13880
13881 /* set 'NV' to 'wakeup vector' */
13882 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013883 } while (cmpxchg64(&pi_desc->control, old.control,
13884 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013885
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013886 /* We should not block the vCPU if an interrupt is posted for it. */
13887 if (pi_test_on(pi_desc) == 1)
13888 __pi_post_block(vcpu);
13889
13890 local_irq_enable();
13891 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013892}
13893
Yunhong Jiangbc225122016-06-13 14:19:58 -070013894static int vmx_pre_block(struct kvm_vcpu *vcpu)
13895{
13896 if (pi_pre_block(vcpu))
13897 return 1;
13898
Yunhong Jiang64672c92016-06-13 14:19:59 -070013899 if (kvm_lapic_hv_timer_in_use(vcpu))
13900 kvm_lapic_switch_to_sw_timer(vcpu);
13901
Yunhong Jiangbc225122016-06-13 14:19:58 -070013902 return 0;
13903}
13904
13905static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013906{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013907 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013908 return;
13909
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013910 WARN_ON(irqs_disabled());
13911 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013912 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013913 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013914}
13915
Yunhong Jiangbc225122016-06-13 14:19:58 -070013916static void vmx_post_block(struct kvm_vcpu *vcpu)
13917{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013918 if (kvm_x86_ops->set_hv_timer)
13919 kvm_lapic_switch_to_hv_timer(vcpu);
13920
Yunhong Jiangbc225122016-06-13 14:19:58 -070013921 pi_post_block(vcpu);
13922}
13923
Feng Wubf9f6ac2015-09-18 22:29:55 +080013924/*
Feng Wuefc64402015-09-18 22:29:51 +080013925 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13926 *
13927 * @kvm: kvm
13928 * @host_irq: host irq of the interrupt
13929 * @guest_irq: gsi of the interrupt
13930 * @set: set or unset PI
13931 * returns 0 on success, < 0 on failure
13932 */
13933static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13934 uint32_t guest_irq, bool set)
13935{
13936 struct kvm_kernel_irq_routing_entry *e;
13937 struct kvm_irq_routing_table *irq_rt;
13938 struct kvm_lapic_irq irq;
13939 struct kvm_vcpu *vcpu;
13940 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013941 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013942
13943 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013944 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13945 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013946 return 0;
13947
13948 idx = srcu_read_lock(&kvm->irq_srcu);
13949 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013950 if (guest_irq >= irq_rt->nr_rt_entries ||
13951 hlist_empty(&irq_rt->map[guest_irq])) {
13952 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13953 guest_irq, irq_rt->nr_rt_entries);
13954 goto out;
13955 }
Feng Wuefc64402015-09-18 22:29:51 +080013956
13957 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13958 if (e->type != KVM_IRQ_ROUTING_MSI)
13959 continue;
13960 /*
13961 * VT-d PI cannot support posting multicast/broadcast
13962 * interrupts to a vCPU, we still use interrupt remapping
13963 * for these kind of interrupts.
13964 *
13965 * For lowest-priority interrupts, we only support
13966 * those with single CPU as the destination, e.g. user
13967 * configures the interrupts via /proc/irq or uses
13968 * irqbalance to make the interrupts single-CPU.
13969 *
13970 * We will support full lowest-priority interrupt later.
13971 */
13972
Radim Krčmář371313132016-07-12 22:09:27 +020013973 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013974 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13975 /*
13976 * Make sure the IRTE is in remapped mode if
13977 * we don't handle it in posted mode.
13978 */
13979 ret = irq_set_vcpu_affinity(host_irq, NULL);
13980 if (ret < 0) {
13981 printk(KERN_INFO
13982 "failed to back to remapped mode, irq: %u\n",
13983 host_irq);
13984 goto out;
13985 }
13986
Feng Wuefc64402015-09-18 22:29:51 +080013987 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013988 }
Feng Wuefc64402015-09-18 22:29:51 +080013989
13990 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13991 vcpu_info.vector = irq.vector;
13992
hu huajun2698d822018-04-11 15:16:40 +080013993 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013994 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13995
13996 if (set)
13997 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013998 else
Feng Wuefc64402015-09-18 22:29:51 +080013999 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080014000
14001 if (ret < 0) {
14002 printk(KERN_INFO "%s: failed to update PI IRTE\n",
14003 __func__);
14004 goto out;
14005 }
14006 }
14007
14008 ret = 0;
14009out:
14010 srcu_read_unlock(&kvm->irq_srcu, idx);
14011 return ret;
14012}
14013
Ashok Rajc45dcc72016-06-22 14:59:56 +080014014static void vmx_setup_mce(struct kvm_vcpu *vcpu)
14015{
14016 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
14017 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
14018 FEATURE_CONTROL_LMCE;
14019 else
14020 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14021 ~FEATURE_CONTROL_LMCE;
14022}
14023
Ladi Prosek72d7b372017-10-11 16:54:41 +020014024static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14025{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014026 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14027 if (to_vmx(vcpu)->nested.nested_run_pending)
14028 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014029 return 1;
14030}
14031
Ladi Prosek0234bf82017-10-11 16:54:40 +020014032static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14033{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014034 struct vcpu_vmx *vmx = to_vmx(vcpu);
14035
14036 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14037 if (vmx->nested.smm.guest_mode)
14038 nested_vmx_vmexit(vcpu, -1, 0, 0);
14039
14040 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14041 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014042 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014043 return 0;
14044}
14045
14046static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14047{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014048 struct vcpu_vmx *vmx = to_vmx(vcpu);
14049 int ret;
14050
14051 if (vmx->nested.smm.vmxon) {
14052 vmx->nested.vmxon = true;
14053 vmx->nested.smm.vmxon = false;
14054 }
14055
14056 if (vmx->nested.smm.guest_mode) {
14057 vcpu->arch.hflags &= ~HF_SMM_MASK;
Sean Christophersond63907d2018-09-26 09:23:45 -070014058 ret = nested_vmx_enter_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014059 vcpu->arch.hflags |= HF_SMM_MASK;
14060 if (ret)
14061 return ret;
14062
14063 vmx->nested.smm.guest_mode = false;
14064 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014065 return 0;
14066}
14067
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014068static int enable_smi_window(struct kvm_vcpu *vcpu)
14069{
14070 return 0;
14071}
14072
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014073static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14074 struct kvm_nested_state __user *user_kvm_nested_state,
14075 u32 user_data_size)
14076{
14077 struct vcpu_vmx *vmx;
14078 struct vmcs12 *vmcs12;
14079 struct kvm_nested_state kvm_state = {
14080 .flags = 0,
14081 .format = 0,
14082 .size = sizeof(kvm_state),
14083 .vmx.vmxon_pa = -1ull,
14084 .vmx.vmcs_pa = -1ull,
14085 };
14086
14087 if (!vcpu)
14088 return kvm_state.size + 2 * VMCS12_SIZE;
14089
14090 vmx = to_vmx(vcpu);
14091 vmcs12 = get_vmcs12(vcpu);
14092 if (nested_vmx_allowed(vcpu) &&
14093 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14094 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14095 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14096
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014097 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014098 kvm_state.size += VMCS12_SIZE;
14099
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014100 if (is_guest_mode(vcpu) &&
14101 nested_cpu_has_shadow_vmcs(vmcs12) &&
14102 vmcs12->vmcs_link_pointer != -1ull)
14103 kvm_state.size += VMCS12_SIZE;
14104 }
14105
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014106 if (vmx->nested.smm.vmxon)
14107 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14108
14109 if (vmx->nested.smm.guest_mode)
14110 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14111
14112 if (is_guest_mode(vcpu)) {
14113 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14114
14115 if (vmx->nested.nested_run_pending)
14116 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14117 }
14118 }
14119
14120 if (user_data_size < kvm_state.size)
14121 goto out;
14122
14123 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14124 return -EFAULT;
14125
14126 if (vmx->nested.current_vmptr == -1ull)
14127 goto out;
14128
14129 /*
14130 * When running L2, the authoritative vmcs12 state is in the
14131 * vmcs02. When running L1, the authoritative vmcs12 state is
14132 * in the shadow vmcs linked to vmcs01, unless
14133 * sync_shadow_vmcs is set, in which case, the authoritative
14134 * vmcs12 state is in the vmcs12 already.
14135 */
14136 if (is_guest_mode(vcpu))
14137 sync_vmcs12(vcpu, vmcs12);
14138 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14139 copy_shadow_to_vmcs12(vmx);
14140
14141 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14142 return -EFAULT;
14143
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014144 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14145 vmcs12->vmcs_link_pointer != -1ull) {
14146 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14147 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14148 return -EFAULT;
14149 }
14150
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014151out:
14152 return kvm_state.size;
14153}
14154
14155static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14156 struct kvm_nested_state __user *user_kvm_nested_state,
14157 struct kvm_nested_state *kvm_state)
14158{
14159 struct vcpu_vmx *vmx = to_vmx(vcpu);
14160 struct vmcs12 *vmcs12;
14161 u32 exit_qual;
14162 int ret;
14163
14164 if (kvm_state->format != 0)
14165 return -EINVAL;
14166
14167 if (!nested_vmx_allowed(vcpu))
14168 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14169
14170 if (kvm_state->vmx.vmxon_pa == -1ull) {
14171 if (kvm_state->vmx.smm.flags)
14172 return -EINVAL;
14173
14174 if (kvm_state->vmx.vmcs_pa != -1ull)
14175 return -EINVAL;
14176
14177 vmx_leave_nested(vcpu);
14178 return 0;
14179 }
14180
14181 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14182 return -EINVAL;
14183
14184 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14185 return -EINVAL;
14186
14187 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14188 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14189 return -EINVAL;
14190
14191 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14192 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14193 return -EINVAL;
14194
14195 if (kvm_state->vmx.smm.flags &
14196 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14197 return -EINVAL;
14198
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014199 /*
14200 * SMM temporarily disables VMX, so we cannot be in guest mode,
14201 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14202 * must be zero.
14203 */
14204 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14205 return -EINVAL;
14206
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014207 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14208 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14209 return -EINVAL;
14210
14211 vmx_leave_nested(vcpu);
14212 if (kvm_state->vmx.vmxon_pa == -1ull)
14213 return 0;
14214
14215 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14216 ret = enter_vmx_operation(vcpu);
14217 if (ret)
14218 return ret;
14219
14220 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14221
14222 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14223 vmx->nested.smm.vmxon = true;
14224 vmx->nested.vmxon = false;
14225
14226 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14227 vmx->nested.smm.guest_mode = true;
14228 }
14229
14230 vmcs12 = get_vmcs12(vcpu);
14231 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14232 return -EFAULT;
14233
Liran Alon392b2f22018-06-23 02:35:01 +030014234 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014235 return -EINVAL;
14236
14237 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14238 return 0;
14239
14240 vmx->nested.nested_run_pending =
14241 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14242
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014243 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14244 vmcs12->vmcs_link_pointer != -1ull) {
14245 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14246 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14247 return -EINVAL;
14248
14249 if (copy_from_user(shadow_vmcs12,
14250 user_kvm_nested_state->data + VMCS12_SIZE,
14251 sizeof(*vmcs12)))
14252 return -EFAULT;
14253
14254 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14255 !shadow_vmcs12->hdr.shadow_vmcs)
14256 return -EINVAL;
14257 }
14258
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014259 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14260 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14261 return -EINVAL;
14262
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014263 vmx->nested.dirty_vmcs12 = true;
Sean Christophersond63907d2018-09-26 09:23:45 -070014264 ret = nested_vmx_enter_non_root_mode(vcpu, NULL);
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014265 if (ret)
14266 return -EINVAL;
14267
14268 return 0;
14269}
14270
Kees Cook404f6aa2016-08-08 16:29:06 -070014271static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014272 .cpu_has_kvm_support = cpu_has_kvm_support,
14273 .disabled_by_bios = vmx_disabled_by_bios,
14274 .hardware_setup = hardware_setup,
14275 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014276 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014277 .hardware_enable = hardware_enable,
14278 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014279 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014280 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014281
Wanpeng Lib31c1142018-03-12 04:53:04 -070014282 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014283 .vm_alloc = vmx_vm_alloc,
14284 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014285
Avi Kivity6aa8b732006-12-10 02:21:36 -080014286 .vcpu_create = vmx_create_vcpu,
14287 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014288 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014289
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014290 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014291 .vcpu_load = vmx_vcpu_load,
14292 .vcpu_put = vmx_vcpu_put,
14293
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014294 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014295 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014296 .get_msr = vmx_get_msr,
14297 .set_msr = vmx_set_msr,
14298 .get_segment_base = vmx_get_segment_base,
14299 .get_segment = vmx_get_segment,
14300 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014301 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014302 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014303 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014304 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014305 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014306 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014307 .set_cr3 = vmx_set_cr3,
14308 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014309 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014310 .get_idt = vmx_get_idt,
14311 .set_idt = vmx_set_idt,
14312 .get_gdt = vmx_get_gdt,
14313 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014314 .get_dr6 = vmx_get_dr6,
14315 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014316 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014317 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014318 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014319 .get_rflags = vmx_get_rflags,
14320 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014321
Avi Kivity6aa8b732006-12-10 02:21:36 -080014322 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014323 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014324
Avi Kivity6aa8b732006-12-10 02:21:36 -080014325 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014326 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014327 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014328 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14329 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014330 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014331 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014332 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014333 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014334 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014335 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014336 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014337 .get_nmi_mask = vmx_get_nmi_mask,
14338 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014339 .enable_nmi_window = enable_nmi_window,
14340 .enable_irq_window = enable_irq_window,
14341 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014342 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014343 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014344 .get_enable_apicv = vmx_get_enable_apicv,
14345 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014346 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014347 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014348 .hwapic_irr_update = vmx_hwapic_irr_update,
14349 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014350 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014351 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14352 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014353
Izik Eiduscbc94022007-10-25 00:29:55 +020014354 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014355 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014356 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014357 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014358
Avi Kivity586f9602010-11-18 13:09:54 +020014359 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014360
Sheng Yang17cc3932010-01-05 19:02:27 +080014361 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014362
14363 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014364
14365 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014366 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014367
14368 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014369
14370 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014371
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014372 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014373 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014374
14375 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014376
14377 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014378 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014379 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014380 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014381 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014382
14383 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014384 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014385
14386 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014387
14388 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14389 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14390 .flush_log_dirty = vmx_flush_log_dirty,
14391 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014392 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014393
Feng Wubf9f6ac2015-09-18 22:29:55 +080014394 .pre_block = vmx_pre_block,
14395 .post_block = vmx_post_block,
14396
Wei Huang25462f72015-06-19 15:45:05 +020014397 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014398
14399 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014400
14401#ifdef CONFIG_X86_64
14402 .set_hv_timer = vmx_set_hv_timer,
14403 .cancel_hv_timer = vmx_cancel_hv_timer,
14404#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014405
14406 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014407
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014408 .get_nested_state = vmx_get_nested_state,
14409 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014410 .get_vmcs12_pages = nested_get_vmcs12_pages,
14411
Ladi Prosek72d7b372017-10-11 16:54:41 +020014412 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014413 .pre_enter_smm = vmx_pre_enter_smm,
14414 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014415 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014416};
14417
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014418static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014419{
14420 if (vmx_l1d_flush_pages) {
14421 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14422 vmx_l1d_flush_pages = NULL;
14423 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014424 /* Restore state so sysfs ignores VMX */
14425 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014426}
14427
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014428static void vmx_exit(void)
14429{
14430#ifdef CONFIG_KEXEC_CORE
14431 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14432 synchronize_rcu();
14433#endif
14434
14435 kvm_exit();
14436
14437#if IS_ENABLED(CONFIG_HYPERV)
14438 if (static_branch_unlikely(&enable_evmcs)) {
14439 int cpu;
14440 struct hv_vp_assist_page *vp_ap;
14441 /*
14442 * Reset everything to support using non-enlightened VMCS
14443 * access later (e.g. when we reload the module with
14444 * enlightened_vmcs=0)
14445 */
14446 for_each_online_cpu(cpu) {
14447 vp_ap = hv_get_vp_assist_page(cpu);
14448
14449 if (!vp_ap)
14450 continue;
14451
14452 vp_ap->current_nested_vmcs = 0;
14453 vp_ap->enlighten_vmentry = 0;
14454 }
14455
14456 static_branch_disable(&enable_evmcs);
14457 }
14458#endif
14459 vmx_cleanup_l1d_flush();
14460}
14461module_exit(vmx_exit);
14462
Avi Kivity6aa8b732006-12-10 02:21:36 -080014463static int __init vmx_init(void)
14464{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014465 int r;
14466
14467#if IS_ENABLED(CONFIG_HYPERV)
14468 /*
14469 * Enlightened VMCS usage should be recommended and the host needs
14470 * to support eVMCS v1 or above. We can also disable eVMCS support
14471 * with module parameter.
14472 */
14473 if (enlightened_vmcs &&
14474 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14475 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14476 KVM_EVMCS_VERSION) {
14477 int cpu;
14478
14479 /* Check that we have assist pages on all online CPUs */
14480 for_each_online_cpu(cpu) {
14481 if (!hv_get_vp_assist_page(cpu)) {
14482 enlightened_vmcs = false;
14483 break;
14484 }
14485 }
14486
14487 if (enlightened_vmcs) {
14488 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14489 static_branch_enable(&enable_evmcs);
14490 }
14491 } else {
14492 enlightened_vmcs = false;
14493 }
14494#endif
14495
14496 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014497 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014498 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014499 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014500
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014501 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014502 * Must be called after kvm_init() so enable_ept is properly set
14503 * up. Hand the parameter mitigation value in which was stored in
14504 * the pre module init parser. If no parameter was given, it will
14505 * contain 'auto' which will be turned into the default 'cond'
14506 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014507 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014508 if (boot_cpu_has(X86_BUG_L1TF)) {
14509 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14510 if (r) {
14511 vmx_exit();
14512 return r;
14513 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014514 }
14515
Dave Young2965faa2015-09-09 15:38:55 -070014516#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014517 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14518 crash_vmclear_local_loaded_vmcss);
14519#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014520 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014521
He, Qingfdef3ad2007-04-30 09:45:24 +030014522 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014523}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014524module_init(vmx_init);