blob: 3831e0968d6a6b6a1d1cb1426ddb9b6b01fff61d [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050036#include <linux/frame.h>
Dan Williams085331d2018-01-31 17:47:03 -080037#include <linux/nospec.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030038#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030039#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040040
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +020041#include <asm/asm.h>
Feng Wu28b835d2015-09-18 22:29:54 +080042#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080043#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080044#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020045#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020046#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080047#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020048#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020049#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010050#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080051#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010052#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080053#include <asm/irq_remapping.h>
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070054#include <asm/mmu_context.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020055#include <asm/spec-ctrl.h>
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010056#include <asm/mshyperv.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080057
Marcelo Tosatti229456f2009-06-17 09:22:14 -030058#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020059#include "pmu.h"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010060#include "vmx_evmcs.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030061
Avi Kivity4ecac3f2008-05-13 13:23:38 +030062#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040063#define __ex_clear(x, reg) \
64 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030065
Avi Kivity6aa8b732006-12-10 02:21:36 -080066MODULE_AUTHOR("Qumranet");
67MODULE_LICENSE("GPL");
68
Josh Triplette9bda3b2012-03-20 23:33:51 -070069static const struct x86_cpu_id vmx_cpu_id[] = {
70 X86_FEATURE_MATCH(X86_FEATURE_VMX),
71 {}
72};
73MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
74
Rusty Russell476bc002012-01-13 09:32:18 +103075static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020076module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080077
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010078static bool __read_mostly enable_vnmi = 1;
79module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
80
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020082module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020085module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070088module_param_named(unrestricted_guest,
89 enable_unrestricted_guest, bool, S_IRUGO);
90
Xudong Hao83c3a332012-05-28 19:33:35 +080091static bool __read_mostly enable_ept_ad_bits = 1;
92module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
93
Avi Kivitya27685c2012-06-12 20:30:18 +030094static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020095module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030096
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030098module_param(fasteoi, bool, S_IRUGO);
99
Yang Zhang5a717852013-04-11 19:25:16 +0800100static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +0800101module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +0800102
Abel Gordonabc4fc52013-04-18 14:35:25 +0300103static bool __read_mostly enable_shadow_vmcs = 1;
104module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +0300105/*
106 * If nested=1, nested virtualization is supported, i.e., guests may use
107 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
108 * use VMX instructions.
109 */
Rusty Russell476bc002012-01-13 09:32:18 +1030110static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300111module_param(nested, bool, S_IRUGO);
112
Wanpeng Li20300092014-12-02 19:14:59 +0800113static u64 __read_mostly host_xss;
114
Kai Huang843e4332015-01-28 10:54:28 +0800115static bool __read_mostly enable_pml = 1;
116module_param_named(pml, enable_pml, bool, S_IRUGO);
117
Paolo Bonzini904e14f2018-01-16 16:51:18 +0100118#define MSR_TYPE_R 1
119#define MSR_TYPE_W 2
120#define MSR_TYPE_RW 3
121
122#define MSR_BITMAP_MODE_X2APIC 1
123#define MSR_BITMAP_MODE_X2APIC_APICV 2
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 Kiszkaf41245002014-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 Kiszkaf41245002014-03-07 20:03:13 +0100853
854 struct hrtimer preemption_timer;
855 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200856
857 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
858 u64 vmcs01_debugctl;
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 Kivity313dbd42008-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 Mattsonc7c2c7092017-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 Mattsonc7c2c7092017-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 Kiszkaf41245002014-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
2793 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002794
2795 /*
2796 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2797 * On CPUs that support "load IA32_EFER", always switch EFER
2798 * atomically, since it's faster than switching it manually.
2799 */
2800 if (cpu_has_load_ia32_efer ||
2801 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002802 if (!(guest_efer & EFER_LMA))
2803 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002804 if (guest_efer != host_efer)
2805 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002806 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002807 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002808 } else {
2809 guest_efer &= ~ignore_bits;
2810 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002811
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002812 vmx->guest_msrs[efer_offset].data = guest_efer;
2813 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2814
2815 return true;
2816 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002817}
2818
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002819#ifdef CONFIG_X86_32
2820/*
2821 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2822 * VMCS rather than the segment table. KVM uses this helper to figure
2823 * out the current bases to poke them into the VMCS before entry.
2824 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002825static unsigned long segment_base(u16 selector)
2826{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002827 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002828 unsigned long v;
2829
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002830 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002831 return 0;
2832
Thomas Garnier45fc8752017-03-14 10:05:08 -07002833 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002834
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002835 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836 u16 ldt_selector = kvm_read_ldt();
2837
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 return 0;
2840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002843 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002844 return v;
2845}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002846#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002848static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002849{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002850 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002851 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002852#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002853 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002854#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002855 unsigned long fs_base, gs_base;
2856 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002857 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002858
Sean Christophersond264ee02018-08-27 15:21:12 -07002859 vmx->req_immediate_exit = false;
2860
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002861 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002862 return;
2863
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002864 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002865 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002866
Avi Kivity33ed6322007-05-02 16:54:03 +03002867 /*
2868 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2869 * allow segment selectors with cpl > 0 or ti == 1.
2870 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002871 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002872
2873#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002874 savesegment(ds, host_state->ds_sel);
2875 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002876
2877 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002878 if (likely(is_64bit_mm(current->mm))) {
2879 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002880 fs_sel = current->thread.fsindex;
2881 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002882 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002883 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002884 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002885 savesegment(fs, fs_sel);
2886 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002887 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002888 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002889 }
2890
Paolo Bonzini4679b612018-09-24 17:23:01 +02002891 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002892#else
Sean Christophersone368b872018-07-23 12:32:41 -07002893 savesegment(fs, fs_sel);
2894 savesegment(gs, gs_sel);
2895 fs_base = segment_base(fs_sel);
2896 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002897#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002898
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002899 if (unlikely(fs_sel != host_state->fs_sel)) {
2900 if (!(fs_sel & 7))
2901 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2902 else
2903 vmcs_write16(HOST_FS_SELECTOR, 0);
2904 host_state->fs_sel = fs_sel;
2905 }
2906 if (unlikely(gs_sel != host_state->gs_sel)) {
2907 if (!(gs_sel & 7))
2908 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2909 else
2910 vmcs_write16(HOST_GS_SELECTOR, 0);
2911 host_state->gs_sel = gs_sel;
2912 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002913 if (unlikely(fs_base != host_state->fs_base)) {
2914 vmcs_writel(HOST_FS_BASE, fs_base);
2915 host_state->fs_base = fs_base;
2916 }
2917 if (unlikely(gs_base != host_state->gs_base)) {
2918 vmcs_writel(HOST_GS_BASE, gs_base);
2919 host_state->gs_base = gs_base;
2920 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002921
Avi Kivity26bb0982009-09-07 11:14:12 +03002922 for (i = 0; i < vmx->save_nmsrs; ++i)
2923 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002924 vmx->guest_msrs[i].data,
2925 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002926}
2927
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002928static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002929{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002930 struct vmcs_host_state *host_state;
2931
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002932 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002933 return;
2934
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002935 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002936 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002937
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002938 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002939 vmx->loaded_cpu_state = NULL;
2940
Avi Kivityc8770e72010-11-11 12:37:26 +02002941#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002942 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002943#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002944 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2945 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002946#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002947 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002948#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002949 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002950#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002951 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002952 if (host_state->fs_sel & 7)
2953 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002954#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2956 loadsegment(ds, host_state->ds_sel);
2957 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002958 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002959#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002960 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002961#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002962 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002963#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002964 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002965}
2966
Sean Christopherson678e3152018-07-23 12:32:43 -07002967#ifdef CONFIG_X86_64
2968static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002969{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002970 preempt_disable();
2971 if (vmx->loaded_cpu_state)
2972 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2973 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002974 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002975}
2976
Sean Christopherson678e3152018-07-23 12:32:43 -07002977static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2978{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002979 preempt_disable();
2980 if (vmx->loaded_cpu_state)
2981 wrmsrl(MSR_KERNEL_GS_BASE, data);
2982 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002983 vmx->msr_guest_kernel_gs_base = data;
2984}
2985#endif
2986
Feng Wu28b835d2015-09-18 22:29:54 +08002987static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2988{
2989 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2990 struct pi_desc old, new;
2991 unsigned int dest;
2992
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02002993 /*
2994 * In case of hot-plug or hot-unplug, we may have to undo
2995 * vmx_vcpu_pi_put even if there is no assigned device. And we
2996 * always keep PI.NDST up to date for simplicity: it makes the
2997 * code easier, and CPU migration is not a fast path.
2998 */
2999 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003000 return;
3001
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003002 /*
3003 * First handle the simple case where no cmpxchg is necessary; just
3004 * allow posting non-urgent interrupts.
3005 *
3006 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3007 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3008 * expects the VCPU to be on the blocked_vcpu_list that matches
3009 * PI.NDST.
3010 */
3011 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3012 vcpu->cpu == cpu) {
3013 pi_clear_sn(pi_desc);
3014 return;
3015 }
3016
3017 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003018 do {
3019 old.control = new.control = pi_desc->control;
3020
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003021 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003022
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003023 if (x2apic_enabled())
3024 new.ndst = dest;
3025 else
3026 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003027
Feng Wu28b835d2015-09-18 22:29:54 +08003028 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003029 } while (cmpxchg64(&pi_desc->control, old.control,
3030 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003031}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003032
Peter Feinerc95ba922016-08-17 09:36:47 -07003033static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3034{
3035 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3036 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3037}
3038
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039/*
3040 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3041 * vcpu mutex is already taken.
3042 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003043static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003045 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003046 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003048 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003049 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003050 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003051 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003052
3053 /*
3054 * Read loaded_vmcs->cpu should be before fetching
3055 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3056 * See the comments in __loaded_vmcs_clear().
3057 */
3058 smp_rmb();
3059
Nadav Har'Eld462b812011-05-24 15:26:10 +03003060 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3061 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003062 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003063 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003064 }
3065
3066 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3067 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3068 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003069 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003070 }
3071
3072 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003073 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003074 unsigned long sysenter_esp;
3075
3076 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003077
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 /*
3079 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003080 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003082 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003083 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003084 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003085
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003086 /*
3087 * VM exits change the host TR limit to 0x67 after a VM
3088 * exit. This is okay, since 0x67 covers everything except
3089 * the IO bitmap and have have code to handle the IO bitmap
3090 * being lost after a VM exit.
3091 */
3092 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3093
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3095 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003096
Nadav Har'Eld462b812011-05-24 15:26:10 +03003097 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 }
Feng Wu28b835d2015-09-18 22:29:54 +08003099
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003100 /* Setup TSC multiplier */
3101 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003102 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3103 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003104
Feng Wu28b835d2015-09-18 22:29:54 +08003105 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003106 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003107 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003108}
3109
3110static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3111{
3112 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3113
3114 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003115 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3116 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003117 return;
3118
3119 /* Set SN when the vCPU is preempted */
3120 if (vcpu->preempted)
3121 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003122}
3123
3124static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3125{
Feng Wu28b835d2015-09-18 22:29:54 +08003126 vmx_vcpu_pi_put(vcpu);
3127
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003128 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129}
3130
Wanpeng Lif244dee2017-07-20 01:11:54 -07003131static bool emulation_required(struct kvm_vcpu *vcpu)
3132{
3133 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3134}
3135
Avi Kivityedcafe32009-12-30 18:07:40 +02003136static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3137
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003138/*
3139 * Return the cr0 value that a nested guest would read. This is a combination
3140 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3141 * its hypervisor (cr0_read_shadow).
3142 */
3143static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3144{
3145 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3146 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3147}
3148static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3149{
3150 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3151 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3152}
3153
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3155{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003156 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003157
Avi Kivity6de12732011-03-07 12:51:22 +02003158 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3159 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3160 rflags = vmcs_readl(GUEST_RFLAGS);
3161 if (to_vmx(vcpu)->rmode.vm86_active) {
3162 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3163 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3164 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3165 }
3166 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003167 }
Avi Kivity6de12732011-03-07 12:51:22 +02003168 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003169}
3170
3171static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3172{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003173 unsigned long old_rflags = vmx_get_rflags(vcpu);
3174
Avi Kivity6de12732011-03-07 12:51:22 +02003175 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3176 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003177 if (to_vmx(vcpu)->rmode.vm86_active) {
3178 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003179 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003180 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003182
3183 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3184 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185}
3186
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003187static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003188{
3189 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3190 int ret = 0;
3191
3192 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003193 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003194 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003195 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003196
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003197 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003198}
3199
3200static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3201{
3202 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3203 u32 interruptibility = interruptibility_old;
3204
3205 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3206
Jan Kiszka48005f62010-02-19 19:38:07 +01003207 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003208 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003209 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003210 interruptibility |= GUEST_INTR_STATE_STI;
3211
3212 if ((interruptibility != interruptibility_old))
3213 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3214}
3215
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3217{
3218 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003220 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003221 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003222 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223
Glauber Costa2809f5d2009-05-12 16:21:05 -04003224 /* skipping an emulated instruction also counts */
3225 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003226}
3227
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003228static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3229 unsigned long exit_qual)
3230{
3231 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3232 unsigned int nr = vcpu->arch.exception.nr;
3233 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3234
3235 if (vcpu->arch.exception.has_error_code) {
3236 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3237 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3238 }
3239
3240 if (kvm_exception_is_soft(nr))
3241 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3242 else
3243 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3244
3245 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3246 vmx_get_nmi_mask(vcpu))
3247 intr_info |= INTR_INFO_UNBLOCK_NMI;
3248
3249 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3250}
3251
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003252/*
3253 * KVM wants to inject page-faults which it got to the guest. This function
3254 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003255 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003256static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003257{
3258 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003259 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003260
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003261 if (nr == PF_VECTOR) {
3262 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003263 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003264 return 1;
3265 }
3266 /*
3267 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3268 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3269 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3270 * can be written only when inject_pending_event runs. This should be
3271 * conditional on a new capability---if the capability is disabled,
3272 * kvm_multiple_exception would write the ancillary information to
3273 * CR2 or DR6, for backwards ABI-compatibility.
3274 */
3275 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3276 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003277 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003278 return 1;
3279 }
3280 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003281 if (vmcs12->exception_bitmap & (1u << nr)) {
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003282 if (nr == DB_VECTOR) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003283 *exit_qual = vcpu->arch.dr6;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003284 *exit_qual &= ~(DR6_FIXED_1 | DR6_BT);
3285 *exit_qual ^= DR6_RTM;
3286 } else {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003287 *exit_qual = 0;
Jim Mattsoncfb634f2018-09-21 10:36:17 -07003288 }
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003289 return 1;
3290 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003291 }
3292
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003293 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003294}
3295
Wanpeng Licaa057a2018-03-12 04:53:03 -07003296static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3297{
3298 /*
3299 * Ensure that we clear the HLT state in the VMCS. We don't need to
3300 * explicitly skip the instruction because if the HLT state is set,
3301 * then the instruction is already executing and RIP has already been
3302 * advanced.
3303 */
3304 if (kvm_hlt_in_guest(vcpu->kvm) &&
3305 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3306 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3307}
3308
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003309static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003310{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003312 unsigned nr = vcpu->arch.exception.nr;
3313 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003314 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003315 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003316
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003317 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003318 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003319 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3320 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003321
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003322 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003323 int inc_eip = 0;
3324 if (kvm_exception_is_soft(nr))
3325 inc_eip = vcpu->arch.event_exit_inst_len;
3326 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003327 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003328 return;
3329 }
3330
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003331 WARN_ON_ONCE(vmx->emulation_required);
3332
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003333 if (kvm_exception_is_soft(nr)) {
3334 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3335 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003336 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3337 } else
3338 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3339
3340 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003341
3342 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003343}
3344
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003345static bool vmx_rdtscp_supported(void)
3346{
3347 return cpu_has_vmx_rdtscp();
3348}
3349
Mao, Junjiead756a12012-07-02 01:18:48 +00003350static bool vmx_invpcid_supported(void)
3351{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003352 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003353}
3354
Avi Kivity6aa8b732006-12-10 02:21:36 -08003355/*
Eddie Donga75beee2007-05-17 18:55:15 +03003356 * Swap MSR entry in host/guest MSR entry array.
3357 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003358static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003359{
Avi Kivity26bb0982009-09-07 11:14:12 +03003360 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003361
3362 tmp = vmx->guest_msrs[to];
3363 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3364 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003365}
3366
3367/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003368 * Set up the vmcs to automatically save and restore system
3369 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3370 * mode, as fiddling with msrs is very expensive.
3371 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003372static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003373{
Avi Kivity26bb0982009-09-07 11:14:12 +03003374 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003375
Eddie Donga75beee2007-05-17 18:55:15 +03003376 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003377#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003378 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003379 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003380 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003381 move_msr_up(vmx, index, save_nmsrs++);
3382 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003383 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003384 move_msr_up(vmx, index, save_nmsrs++);
3385 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003386 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003388 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003389 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003390 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003391 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003392 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003393 * if efer.sce is enabled.
3394 */
Brian Gerst8c065852010-07-17 09:03:26 -04003395 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003396 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003397 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003398 }
Eddie Donga75beee2007-05-17 18:55:15 +03003399#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003400 index = __find_msr_index(vmx, MSR_EFER);
3401 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003402 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003403
Avi Kivity26bb0982009-09-07 11:14:12 +03003404 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003405
Yang Zhang8d146952013-01-25 10:18:50 +08003406 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003407 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003408}
3409
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003410static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003412 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003414 if (is_guest_mode(vcpu) &&
3415 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3416 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3417
3418 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419}
3420
3421/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003422 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003424static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003426 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003427 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003428 * We're here if L1 chose not to trap WRMSR to TSC. According
3429 * to the spec, this should set L1's TSC; The offset that L1
3430 * set for L2 remains unchanged, and still needs to be added
3431 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003432 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003433 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003434 /* recalculate vmcs02.TSC_OFFSET: */
3435 vmcs12 = get_vmcs12(vcpu);
3436 vmcs_write64(TSC_OFFSET, offset +
3437 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3438 vmcs12->tsc_offset : 0));
3439 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003440 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3441 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003442 vmcs_write64(TSC_OFFSET, offset);
3443 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444}
3445
Nadav Har'El801d3422011-05-25 23:02:23 +03003446/*
3447 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3448 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3449 * all guests if the "nested" module option is off, and can also be disabled
3450 * for a single guest by disabling its VMX cpuid bit.
3451 */
3452static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3453{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003454 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003455}
3456
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003458 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3459 * returned for the various VMX controls MSRs when nested VMX is enabled.
3460 * The same values should also be used to verify that vmcs12 control fields are
3461 * valid during nested entry from L1 to L2.
3462 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3463 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3464 * bit in the high half is on if the corresponding bit in the control field
3465 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003467static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003468{
Paolo Bonzini13893092018-02-26 13:40:09 +01003469 if (!nested) {
3470 memset(msrs, 0, sizeof(*msrs));
3471 return;
3472 }
3473
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474 /*
3475 * Note that as a general rule, the high half of the MSRs (bits in
3476 * the control fields which may be 1) should be initialized by the
3477 * intersection of the underlying hardware's MSR (i.e., features which
3478 * can be supported) and the list of features we want to expose -
3479 * because they are known to be properly supported in our code.
3480 * Also, usually, the low half of the MSRs (bits which must be 1) can
3481 * be set to 0, meaning that L1 may turn off any of these bits. The
3482 * reason is that if one of these bits is necessary, it will appear
3483 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3484 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003485 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003486 * These rules have exceptions below.
3487 */
3488
3489 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003490 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003491 msrs->pinbased_ctls_low,
3492 msrs->pinbased_ctls_high);
3493 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003494 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003495 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003496 PIN_BASED_EXT_INTR_MASK |
3497 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003498 PIN_BASED_VIRTUAL_NMIS |
3499 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003500 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003501 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003502 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003503
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003504 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003505 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003506 msrs->exit_ctls_low,
3507 msrs->exit_ctls_high);
3508 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003509 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003510
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003511 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003512#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003513 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003514#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003515 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003516 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003517 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003518 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003519 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3520
Jan Kiszka2996fca2014-06-16 13:59:43 +02003521 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003522 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003523
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003524 /* entry controls */
3525 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003526 msrs->entry_ctls_low,
3527 msrs->entry_ctls_high);
3528 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003529 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003531#ifdef CONFIG_X86_64
3532 VM_ENTRY_IA32E_MODE |
3533#endif
3534 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003535 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003536 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003537
Jan Kiszka2996fca2014-06-16 13:59:43 +02003538 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003539 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003540
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003541 /* cpu-based controls */
3542 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->procbased_ctls_low,
3544 msrs->procbased_ctls_high);
3545 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003546 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003547 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003548 CPU_BASED_VIRTUAL_INTR_PENDING |
3549 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003550 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3551 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3552 CPU_BASED_CR3_STORE_EXITING |
3553#ifdef CONFIG_X86_64
3554 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3555#endif
3556 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003557 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3558 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3559 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3560 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003561 /*
3562 * We can allow some features even when not supported by the
3563 * hardware. For example, L1 can specify an MSR bitmap - and we
3564 * can use it to avoid exits to L1 - even when L0 runs L2
3565 * without MSR bitmaps.
3566 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003567 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003568 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003569 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003570
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003571 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003572 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003573 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3574
Paolo Bonzini80154d72017-08-24 13:55:35 +02003575 /*
3576 * secondary cpu-based controls. Do not include those that
3577 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3578 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003579 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003580 msrs->secondary_ctls_low,
3581 msrs->secondary_ctls_high);
3582 msrs->secondary_ctls_low = 0;
3583 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003584 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003585 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003586 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003587 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003588 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003589
Liran Alon32c7acf2018-06-23 02:35:11 +03003590 /*
3591 * We can emulate "VMCS shadowing," even if the hardware
3592 * doesn't support it.
3593 */
3594 msrs->secondary_ctls_high |=
3595 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003596
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003597 if (enable_ept) {
3598 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003599 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003600 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003601 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003602 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003603 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003604 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003605 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003606 msrs->ept_caps &= vmx_capability.ept;
3607 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003608 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3609 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003610 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003611 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003612 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003613 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003614 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003615 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003616
Bandan Das27c42a12017-08-03 15:54:42 -04003617 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003618 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003619 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003620 /*
3621 * Advertise EPTP switching unconditionally
3622 * since we emulate it
3623 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003624 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003625 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003626 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003627 }
3628
Paolo Bonzinief697a72016-03-18 16:58:38 +01003629 /*
3630 * Old versions of KVM use the single-context version without
3631 * checking for support, so declare that it is supported even
3632 * though it is treated as global context. The alternative is
3633 * not failing the single-context invvpid, and it is worse.
3634 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003635 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003636 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003637 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003638 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003639 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003640 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003641
Radim Krčmář0790ec12015-03-17 14:02:32 +01003642 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003643 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003644 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3645
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003646 if (flexpriority_enabled)
3647 msrs->secondary_ctls_high |=
3648 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3649
Jan Kiszkac18911a2013-03-13 16:06:41 +01003650 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003651 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003652 msrs->misc_low,
3653 msrs->misc_high);
3654 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3655 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003656 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003657 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003658 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003659 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003660
3661 /*
3662 * This MSR reports some information about VMX support. We
3663 * should return information about the VMX we emulate for the
3664 * guest, and the VMCS structure we give it - not about the
3665 * VMX support of the underlying hardware.
3666 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003667 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003668 VMCS12_REVISION |
3669 VMX_BASIC_TRUE_CTLS |
3670 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3671 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3672
3673 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003674 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003675
3676 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003677 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003678 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3679 * We picked the standard core2 setting.
3680 */
3681#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3682#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003683 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3684 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003685
3686 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003687 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3688 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003689
3690 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003692}
3693
David Matlack38991522016-11-29 18:14:08 -08003694/*
3695 * if fixed0[i] == 1: val[i] must be 1
3696 * if fixed1[i] == 0: val[i] must be 0
3697 */
3698static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3699{
3700 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003701}
3702
3703static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3704{
David Matlack38991522016-11-29 18:14:08 -08003705 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003706}
3707
3708static inline u64 vmx_control_msr(u32 low, u32 high)
3709{
3710 return low | ((u64)high << 32);
3711}
3712
David Matlack62cc6b9d2016-11-29 18:14:07 -08003713static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3714{
3715 superset &= mask;
3716 subset &= mask;
3717
3718 return (superset | subset) == superset;
3719}
3720
3721static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3722{
3723 const u64 feature_and_reserved =
3724 /* feature (except bit 48; see below) */
3725 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3726 /* reserved */
3727 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003728 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003729
3730 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3731 return -EINVAL;
3732
3733 /*
3734 * KVM does not emulate a version of VMX that constrains physical
3735 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3736 */
3737 if (data & BIT_ULL(48))
3738 return -EINVAL;
3739
3740 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3741 vmx_basic_vmcs_revision_id(data))
3742 return -EINVAL;
3743
3744 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3745 return -EINVAL;
3746
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003747 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003748 return 0;
3749}
3750
3751static int
3752vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3753{
3754 u64 supported;
3755 u32 *lowp, *highp;
3756
3757 switch (msr_index) {
3758 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003759 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3760 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003761 break;
3762 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003763 lowp = &vmx->nested.msrs.procbased_ctls_low;
3764 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003765 break;
3766 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 lowp = &vmx->nested.msrs.exit_ctls_low;
3768 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003769 break;
3770 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 lowp = &vmx->nested.msrs.entry_ctls_low;
3772 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003773 break;
3774 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 lowp = &vmx->nested.msrs.secondary_ctls_low;
3776 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777 break;
3778 default:
3779 BUG();
3780 }
3781
3782 supported = vmx_control_msr(*lowp, *highp);
3783
3784 /* Check must-be-1 bits are still 1. */
3785 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3786 return -EINVAL;
3787
3788 /* Check must-be-0 bits are still 0. */
3789 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3790 return -EINVAL;
3791
3792 *lowp = data;
3793 *highp = data >> 32;
3794 return 0;
3795}
3796
3797static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3798{
3799 const u64 feature_and_reserved_bits =
3800 /* feature */
3801 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3802 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3803 /* reserved */
3804 GENMASK_ULL(13, 9) | BIT_ULL(31);
3805 u64 vmx_misc;
3806
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003807 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3808 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003809
3810 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3811 return -EINVAL;
3812
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003813 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003814 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3815 vmx_misc_preemption_timer_rate(data) !=
3816 vmx_misc_preemption_timer_rate(vmx_misc))
3817 return -EINVAL;
3818
3819 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3820 return -EINVAL;
3821
3822 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3823 return -EINVAL;
3824
3825 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3826 return -EINVAL;
3827
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003828 vmx->nested.msrs.misc_low = data;
3829 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003830
3831 /*
3832 * If L1 has read-only VM-exit information fields, use the
3833 * less permissive vmx_vmwrite_bitmap to specify write
3834 * permissions for the shadow VMCS.
3835 */
3836 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3837 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3838
David Matlack62cc6b9d2016-11-29 18:14:07 -08003839 return 0;
3840}
3841
3842static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3843{
3844 u64 vmx_ept_vpid_cap;
3845
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003846 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3847 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003848
3849 /* Every bit is either reserved or a feature bit. */
3850 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3851 return -EINVAL;
3852
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003853 vmx->nested.msrs.ept_caps = data;
3854 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003855 return 0;
3856}
3857
3858static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3859{
3860 u64 *msr;
3861
3862 switch (msr_index) {
3863 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003864 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003865 break;
3866 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003867 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003868 break;
3869 default:
3870 BUG();
3871 }
3872
3873 /*
3874 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3875 * must be 1 in the restored value.
3876 */
3877 if (!is_bitwise_subset(data, *msr, -1ULL))
3878 return -EINVAL;
3879
3880 *msr = data;
3881 return 0;
3882}
3883
3884/*
3885 * Called when userspace is restoring VMX MSRs.
3886 *
3887 * Returns 0 on success, non-0 otherwise.
3888 */
3889static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3890{
3891 struct vcpu_vmx *vmx = to_vmx(vcpu);
3892
Jim Mattsona943ac52018-05-29 09:11:32 -07003893 /*
3894 * Don't allow changes to the VMX capability MSRs while the vCPU
3895 * is in VMX operation.
3896 */
3897 if (vmx->nested.vmxon)
3898 return -EBUSY;
3899
David Matlack62cc6b9d2016-11-29 18:14:07 -08003900 switch (msr_index) {
3901 case MSR_IA32_VMX_BASIC:
3902 return vmx_restore_vmx_basic(vmx, data);
3903 case MSR_IA32_VMX_PINBASED_CTLS:
3904 case MSR_IA32_VMX_PROCBASED_CTLS:
3905 case MSR_IA32_VMX_EXIT_CTLS:
3906 case MSR_IA32_VMX_ENTRY_CTLS:
3907 /*
3908 * The "non-true" VMX capability MSRs are generated from the
3909 * "true" MSRs, so we do not support restoring them directly.
3910 *
3911 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3912 * should restore the "true" MSRs with the must-be-1 bits
3913 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3914 * DEFAULT SETTINGS".
3915 */
3916 return -EINVAL;
3917 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3918 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3919 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3920 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3921 case MSR_IA32_VMX_PROCBASED_CTLS2:
3922 return vmx_restore_control_msr(vmx, msr_index, data);
3923 case MSR_IA32_VMX_MISC:
3924 return vmx_restore_vmx_misc(vmx, data);
3925 case MSR_IA32_VMX_CR0_FIXED0:
3926 case MSR_IA32_VMX_CR4_FIXED0:
3927 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3928 case MSR_IA32_VMX_CR0_FIXED1:
3929 case MSR_IA32_VMX_CR4_FIXED1:
3930 /*
3931 * These MSRs are generated based on the vCPU's CPUID, so we
3932 * do not support restoring them directly.
3933 */
3934 return -EINVAL;
3935 case MSR_IA32_VMX_EPT_VPID_CAP:
3936 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3937 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003938 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003939 return 0;
3940 default:
3941 /*
3942 * The rest of the VMX capability MSRs do not support restore.
3943 */
3944 return -EINVAL;
3945 }
3946}
3947
Jan Kiszkacae50132014-01-04 18:47:22 +01003948/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003949static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003950{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003951 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003952 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003953 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003954 break;
3955 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3956 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003957 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003958 msrs->pinbased_ctls_low,
3959 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003960 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3961 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 break;
3963 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3964 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003965 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003966 msrs->procbased_ctls_low,
3967 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003968 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3969 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003970 break;
3971 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3972 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003973 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003974 msrs->exit_ctls_low,
3975 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003976 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3977 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003978 break;
3979 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3980 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003981 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003982 msrs->entry_ctls_low,
3983 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003984 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3985 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 break;
3987 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003988 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003989 msrs->misc_low,
3990 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003991 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003992 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003993 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
3995 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003996 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003997 break;
3998 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003999 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 break;
4001 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004002 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004003 break;
4004 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004005 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004006 break;
4007 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004008 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004009 msrs->secondary_ctls_low,
4010 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->ept_caps |
4014 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004015 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004016 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004017 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004018 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004020 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004021 }
4022
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004023 return 0;
4024}
4025
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004026static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4027 uint64_t val)
4028{
4029 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4030
4031 return !(val & ~valid_bits);
4032}
4033
Tom Lendacky801e4592018-02-21 13:39:51 -06004034static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4035{
Paolo Bonzini13893092018-02-26 13:40:09 +01004036 switch (msr->index) {
4037 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4038 if (!nested)
4039 return 1;
4040 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4041 default:
4042 return 1;
4043 }
4044
4045 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004046}
4047
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004048/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004049 * Reads an msr value (of 'msr_index') into 'pdata'.
4050 * Returns 0 on success, non-0 otherwise.
4051 * Assumes vcpu_load() was already called.
4052 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004053static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004054{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004055 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004056 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004058 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004059#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004061 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 break;
4063 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004064 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004066 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004067 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004068 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004069#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004071 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004072 case MSR_IA32_SPEC_CTRL:
4073 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004074 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4075 return 1;
4076
4077 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4078 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004079 case MSR_IA32_ARCH_CAPABILITIES:
4080 if (!msr_info->host_initiated &&
4081 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4082 return 1;
4083 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4084 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004086 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087 break;
4088 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004089 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090 break;
4091 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004092 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004094 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004095 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004096 (!msr_info->host_initiated &&
4097 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004098 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004099 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004100 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004101 case MSR_IA32_MCG_EXT_CTL:
4102 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004103 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004104 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004105 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004106 msr_info->data = vcpu->arch.mcg_ext_ctl;
4107 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004108 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004109 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004110 break;
4111 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4112 if (!nested_vmx_allowed(vcpu))
4113 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004114 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4115 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004116 case MSR_IA32_XSS:
4117 if (!vmx_xsaves_supported())
4118 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004119 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004120 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004121 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004122 if (!msr_info->host_initiated &&
4123 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004124 return 1;
4125 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004127 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004128 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004129 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004130 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004132 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 }
4134
Avi Kivity6aa8b732006-12-10 02:21:36 -08004135 return 0;
4136}
4137
Jan Kiszkacae50132014-01-04 18:47:22 +01004138static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4139
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140/*
4141 * Writes msr value into into the appropriate "register".
4142 * Returns 0 on success, non-0 otherwise.
4143 * Assumes vcpu_load() was already called.
4144 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004145static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004146{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004147 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004148 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004149 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004150 u32 msr_index = msr_info->index;
4151 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004152
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004154 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004155 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004156 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004157#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004159 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160 vmcs_writel(GUEST_FS_BASE, data);
4161 break;
4162 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004163 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004164 vmcs_writel(GUEST_GS_BASE, data);
4165 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004166 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004167 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004168 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169#endif
4170 case MSR_IA32_SYSENTER_CS:
4171 vmcs_write32(GUEST_SYSENTER_CS, data);
4172 break;
4173 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004174 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004175 break;
4176 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004177 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004179 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004180 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004181 (!msr_info->host_initiated &&
4182 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004183 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004184 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004185 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004187 vmcs_write64(GUEST_BNDCFGS, data);
4188 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004189 case MSR_IA32_SPEC_CTRL:
4190 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004191 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4192 return 1;
4193
4194 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004195 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004196 return 1;
4197
4198 vmx->spec_ctrl = data;
4199
4200 if (!data)
4201 break;
4202
4203 /*
4204 * For non-nested:
4205 * When it's written (to non-zero) for the first time, pass
4206 * it through.
4207 *
4208 * For nested:
4209 * The handling of the MSR bitmap for L2 guests is done in
4210 * nested_vmx_merge_msr_bitmap. We should not touch the
4211 * vmcs02.msr_bitmap here since it gets completely overwritten
4212 * in the merging. We update the vmcs01 here for L1 as well
4213 * since it will end up touching the MSR anyway now.
4214 */
4215 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4216 MSR_IA32_SPEC_CTRL,
4217 MSR_TYPE_RW);
4218 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004219 case MSR_IA32_PRED_CMD:
4220 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004221 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4222 return 1;
4223
4224 if (data & ~PRED_CMD_IBPB)
4225 return 1;
4226
4227 if (!data)
4228 break;
4229
4230 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4231
4232 /*
4233 * For non-nested:
4234 * When it's written (to non-zero) for the first time, pass
4235 * it through.
4236 *
4237 * For nested:
4238 * The handling of the MSR bitmap for L2 guests is done in
4239 * nested_vmx_merge_msr_bitmap. We should not touch the
4240 * vmcs02.msr_bitmap here since it gets completely overwritten
4241 * in the merging.
4242 */
4243 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4244 MSR_TYPE_W);
4245 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004246 case MSR_IA32_ARCH_CAPABILITIES:
4247 if (!msr_info->host_initiated)
4248 return 1;
4249 vmx->arch_capabilities = data;
4250 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004251 case MSR_IA32_CR_PAT:
4252 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004253 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4254 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004255 vmcs_write64(GUEST_IA32_PAT, data);
4256 vcpu->arch.pat = data;
4257 break;
4258 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004259 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004260 break;
Will Auldba904632012-11-29 12:42:50 -08004261 case MSR_IA32_TSC_ADJUST:
4262 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004263 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004264 case MSR_IA32_MCG_EXT_CTL:
4265 if ((!msr_info->host_initiated &&
4266 !(to_vmx(vcpu)->msr_ia32_feature_control &
4267 FEATURE_CONTROL_LMCE)) ||
4268 (data & ~MCG_EXT_CTL_LMCE_EN))
4269 return 1;
4270 vcpu->arch.mcg_ext_ctl = data;
4271 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004272 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004273 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004274 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004275 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4276 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004277 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004278 if (msr_info->host_initiated && data == 0)
4279 vmx_leave_nested(vcpu);
4280 break;
4281 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004282 if (!msr_info->host_initiated)
4283 return 1; /* they are read-only */
4284 if (!nested_vmx_allowed(vcpu))
4285 return 1;
4286 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004287 case MSR_IA32_XSS:
4288 if (!vmx_xsaves_supported())
4289 return 1;
4290 /*
4291 * The only supported bit as of Skylake is bit 8, but
4292 * it is not supported on KVM.
4293 */
4294 if (data != 0)
4295 return 1;
4296 vcpu->arch.ia32_xss = data;
4297 if (vcpu->arch.ia32_xss != host_xss)
4298 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004299 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004300 else
4301 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4302 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004303 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004304 if (!msr_info->host_initiated &&
4305 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004306 return 1;
4307 /* Check reserved bit, higher 32 bits should be zero */
4308 if ((data >> 32) != 0)
4309 return 1;
4310 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004311 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004312 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004313 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004314 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004315 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004316 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4317 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004318 ret = kvm_set_shared_msr(msr->index, msr->data,
4319 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004320 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004321 if (ret)
4322 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004323 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004324 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004326 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327 }
4328
Eddie Dong2cc51562007-05-21 07:28:09 +03004329 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330}
4331
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004332static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004334 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4335 switch (reg) {
4336 case VCPU_REGS_RSP:
4337 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4338 break;
4339 case VCPU_REGS_RIP:
4340 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4341 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004342 case VCPU_EXREG_PDPTR:
4343 if (enable_ept)
4344 ept_save_pdptrs(vcpu);
4345 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004346 default:
4347 break;
4348 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004349}
4350
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351static __init int cpu_has_kvm_support(void)
4352{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004353 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004354}
4355
4356static __init int vmx_disabled_by_bios(void)
4357{
4358 u64 msr;
4359
4360 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004361 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004362 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004363 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4364 && tboot_enabled())
4365 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004366 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004367 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004368 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004369 && !tboot_enabled()) {
4370 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004371 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004372 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004373 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004374 /* launched w/o TXT and VMX disabled */
4375 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4376 && !tboot_enabled())
4377 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004378 }
4379
4380 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381}
4382
Dongxiao Xu7725b892010-05-11 18:29:38 +08004383static void kvm_cpu_vmxon(u64 addr)
4384{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004385 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004386 intel_pt_handle_vmx(1);
4387
Dongxiao Xu7725b892010-05-11 18:29:38 +08004388 asm volatile (ASM_VMX_VMXON_RAX
4389 : : "a"(&addr), "m"(addr)
4390 : "memory", "cc");
4391}
4392
Radim Krčmář13a34e02014-08-28 15:13:03 +02004393static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394{
4395 int cpu = raw_smp_processor_id();
4396 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004397 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004399 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004400 return -EBUSY;
4401
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004402 /*
4403 * This can happen if we hot-added a CPU but failed to allocate
4404 * VP assist page for it.
4405 */
4406 if (static_branch_unlikely(&enable_evmcs) &&
4407 !hv_get_vp_assist_page(cpu))
4408 return -EFAULT;
4409
Nadav Har'Eld462b812011-05-24 15:26:10 +03004410 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004411 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4412 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004413
4414 /*
4415 * Now we can enable the vmclear operation in kdump
4416 * since the loaded_vmcss_on_cpu list on this cpu
4417 * has been initialized.
4418 *
4419 * Though the cpu is not in VMX operation now, there
4420 * is no problem to enable the vmclear operation
4421 * for the loaded_vmcss_on_cpu list is empty!
4422 */
4423 crash_enable_local_vmclear(cpu);
4424
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004426
4427 test_bits = FEATURE_CONTROL_LOCKED;
4428 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4429 if (tboot_enabled())
4430 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4431
4432 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004434 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4435 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004436 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004437 if (enable_ept)
4438 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004439
4440 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441}
4442
Nadav Har'Eld462b812011-05-24 15:26:10 +03004443static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004444{
4445 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004446 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004447
Nadav Har'Eld462b812011-05-24 15:26:10 +03004448 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4449 loaded_vmcss_on_cpu_link)
4450 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004451}
4452
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004453
4454/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4455 * tricks.
4456 */
4457static void kvm_cpu_vmxoff(void)
4458{
4459 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004460
4461 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004462 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004463}
4464
Radim Krčmář13a34e02014-08-28 15:13:03 +02004465static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004466{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004467 vmclear_local_loaded_vmcss();
4468 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469}
4470
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004471static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004472 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004473{
4474 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004475 u32 ctl = ctl_min | ctl_opt;
4476
4477 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4478
4479 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4480 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4481
4482 /* Ensure minimum (required) set of control bits are supported. */
4483 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004484 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004485
4486 *result = ctl;
4487 return 0;
4488}
4489
Avi Kivity110312c2010-12-21 12:54:20 +02004490static __init bool allow_1_setting(u32 msr, u32 ctl)
4491{
4492 u32 vmx_msr_low, vmx_msr_high;
4493
4494 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4495 return vmx_msr_high & ctl;
4496}
4497
Yang, Sheng002c7f72007-07-31 14:23:01 +03004498static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004499{
4500 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004501 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004502 u32 _pin_based_exec_control = 0;
4503 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004504 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004505 u32 _vmexit_control = 0;
4506 u32 _vmentry_control = 0;
4507
Paolo Bonzini13893092018-02-26 13:40:09 +01004508 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304509 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510#ifdef CONFIG_X86_64
4511 CPU_BASED_CR8_LOAD_EXITING |
4512 CPU_BASED_CR8_STORE_EXITING |
4513#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004514 CPU_BASED_CR3_LOAD_EXITING |
4515 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004516 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004517 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004518 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004519 CPU_BASED_MWAIT_EXITING |
4520 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004521 CPU_BASED_INVLPG_EXITING |
4522 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004523
Sheng Yangf78e0e22007-10-29 09:40:42 +08004524 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004525 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004526 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004527 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4528 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004529 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004530#ifdef CONFIG_X86_64
4531 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4532 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4533 ~CPU_BASED_CR8_STORE_EXITING;
4534#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004535 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004536 min2 = 0;
4537 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004538 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004539 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004540 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004541 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004542 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004543 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004544 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004545 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004546 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004547 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004548 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004549 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004550 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004551 SECONDARY_EXEC_RDSEED_EXITING |
4552 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004553 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004554 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004555 SECONDARY_EXEC_ENABLE_VMFUNC |
4556 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004557 if (adjust_vmx_controls(min2, opt2,
4558 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004559 &_cpu_based_2nd_exec_control) < 0)
4560 return -EIO;
4561 }
4562#ifndef CONFIG_X86_64
4563 if (!(_cpu_based_2nd_exec_control &
4564 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4565 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4566#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004567
4568 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4569 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004570 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004571 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4572 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004573
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004574 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4575 &vmx_capability.ept, &vmx_capability.vpid);
4576
Sheng Yangd56f5462008-04-25 10:13:16 +08004577 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004578 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4579 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004580 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4581 CPU_BASED_CR3_STORE_EXITING |
4582 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004583 } else if (vmx_capability.ept) {
4584 vmx_capability.ept = 0;
4585 pr_warn_once("EPT CAP should not exist if not support "
4586 "1-setting enable EPT VM-execution control\n");
4587 }
4588 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4589 vmx_capability.vpid) {
4590 vmx_capability.vpid = 0;
4591 pr_warn_once("VPID CAP should not exist if not support "
4592 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004593 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004594
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004595 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004596#ifdef CONFIG_X86_64
4597 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4598#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004599 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004600 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004601 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4602 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004603 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004604
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004605 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4606 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4607 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004608 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4609 &_pin_based_exec_control) < 0)
4610 return -EIO;
4611
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004612 if (cpu_has_broken_vmx_preemption_timer())
4613 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004614 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004615 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004616 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4617
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004618 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004619 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004620 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4621 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004622 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004624 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004625
4626 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4627 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004628 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004629
4630#ifdef CONFIG_X86_64
4631 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4632 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004633 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004634#endif
4635
4636 /* Require Write-Back (WB) memory type for VMCS accesses. */
4637 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004638 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004639
Yang, Sheng002c7f72007-07-31 14:23:01 +03004640 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004641 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004642 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004643
Liran Alon2307af12018-06-29 22:59:04 +03004644 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004645
Yang, Sheng002c7f72007-07-31 14:23:01 +03004646 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4647 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004648 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004649 vmcs_conf->vmexit_ctrl = _vmexit_control;
4650 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004651
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004652 if (static_branch_unlikely(&enable_evmcs))
4653 evmcs_sanitize_exec_ctrls(vmcs_conf);
4654
Avi Kivity110312c2010-12-21 12:54:20 +02004655 cpu_has_load_ia32_efer =
4656 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4657 VM_ENTRY_LOAD_IA32_EFER)
4658 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4659 VM_EXIT_LOAD_IA32_EFER);
4660
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004661 cpu_has_load_perf_global_ctrl =
4662 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4663 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4664 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4665 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4666
4667 /*
4668 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004669 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004670 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4671 *
4672 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4673 *
4674 * AAK155 (model 26)
4675 * AAP115 (model 30)
4676 * AAT100 (model 37)
4677 * BC86,AAY89,BD102 (model 44)
4678 * BA97 (model 46)
4679 *
4680 */
4681 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4682 switch (boot_cpu_data.x86_model) {
4683 case 26:
4684 case 30:
4685 case 37:
4686 case 44:
4687 case 46:
4688 cpu_has_load_perf_global_ctrl = false;
4689 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4690 "does not work properly. Using workaround\n");
4691 break;
4692 default:
4693 break;
4694 }
4695 }
4696
Borislav Petkov782511b2016-04-04 22:25:03 +02004697 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004698 rdmsrl(MSR_IA32_XSS, host_xss);
4699
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004700 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004701}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004702
Liran Alon491a6032018-06-23 02:35:12 +03004703static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004704{
4705 int node = cpu_to_node(cpu);
4706 struct page *pages;
4707 struct vmcs *vmcs;
4708
Vlastimil Babka96db8002015-09-08 15:03:50 -07004709 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710 if (!pages)
4711 return NULL;
4712 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004713 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004714
4715 /* KVM supports Enlightened VMCS v1 only */
4716 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004717 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004718 else
Liran Alon392b2f22018-06-23 02:35:01 +03004719 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004720
Liran Alon491a6032018-06-23 02:35:12 +03004721 if (shadow)
4722 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004723 return vmcs;
4724}
4725
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726static void free_vmcs(struct vmcs *vmcs)
4727{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004728 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004729}
4730
Nadav Har'Eld462b812011-05-24 15:26:10 +03004731/*
4732 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4733 */
4734static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4735{
4736 if (!loaded_vmcs->vmcs)
4737 return;
4738 loaded_vmcs_clear(loaded_vmcs);
4739 free_vmcs(loaded_vmcs->vmcs);
4740 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004741 if (loaded_vmcs->msr_bitmap)
4742 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004743 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004744}
4745
Liran Alon491a6032018-06-23 02:35:12 +03004746static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004747{
Liran Alon491a6032018-06-23 02:35:12 +03004748 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004749}
4750
4751static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4752{
Liran Alon491a6032018-06-23 02:35:12 +03004753 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004754 if (!loaded_vmcs->vmcs)
4755 return -ENOMEM;
4756
4757 loaded_vmcs->shadow_vmcs = NULL;
4758 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004759
4760 if (cpu_has_vmx_msr_bitmap()) {
4761 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4762 if (!loaded_vmcs->msr_bitmap)
4763 goto out_vmcs;
4764 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004765
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004766 if (IS_ENABLED(CONFIG_HYPERV) &&
4767 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004768 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4769 struct hv_enlightened_vmcs *evmcs =
4770 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4771
4772 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4773 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004774 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004775
4776 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4777
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004778 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004779
4780out_vmcs:
4781 free_loaded_vmcs(loaded_vmcs);
4782 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004783}
4784
Sam Ravnborg39959582007-06-01 00:47:13 -07004785static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004786{
4787 int cpu;
4788
Zachary Amsden3230bb42009-09-29 11:38:37 -10004789 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004791 per_cpu(vmxarea, cpu) = NULL;
4792 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004793}
4794
Jim Mattsond37f4262017-12-22 12:12:16 -08004795enum vmcs_field_width {
4796 VMCS_FIELD_WIDTH_U16 = 0,
4797 VMCS_FIELD_WIDTH_U64 = 1,
4798 VMCS_FIELD_WIDTH_U32 = 2,
4799 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004800};
4801
Jim Mattsond37f4262017-12-22 12:12:16 -08004802static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004803{
4804 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004805 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004806 return (field >> 13) & 0x3 ;
4807}
4808
4809static inline int vmcs_field_readonly(unsigned long field)
4810{
4811 return (((field >> 10) & 0x3) == 1);
4812}
4813
Bandan Dasfe2b2012014-04-21 15:20:14 -04004814static void init_vmcs_shadow_fields(void)
4815{
4816 int i, j;
4817
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004818 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4819 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004820 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004821 (i + 1 == max_shadow_read_only_fields ||
4822 shadow_read_only_fields[i + 1] != field + 1))
4823 pr_err("Missing field from shadow_read_only_field %x\n",
4824 field + 1);
4825
4826 clear_bit(field, vmx_vmread_bitmap);
4827#ifdef CONFIG_X86_64
4828 if (field & 1)
4829 continue;
4830#endif
4831 if (j < i)
4832 shadow_read_only_fields[j] = field;
4833 j++;
4834 }
4835 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004836
4837 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004838 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004839 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004840 (i + 1 == max_shadow_read_write_fields ||
4841 shadow_read_write_fields[i + 1] != field + 1))
4842 pr_err("Missing field from shadow_read_write_field %x\n",
4843 field + 1);
4844
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004845 /*
4846 * PML and the preemption timer can be emulated, but the
4847 * processor cannot vmwrite to fields that don't exist
4848 * on bare metal.
4849 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004850 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004851 case GUEST_PML_INDEX:
4852 if (!cpu_has_vmx_pml())
4853 continue;
4854 break;
4855 case VMX_PREEMPTION_TIMER_VALUE:
4856 if (!cpu_has_vmx_preemption_timer())
4857 continue;
4858 break;
4859 case GUEST_INTR_STATUS:
4860 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004861 continue;
4862 break;
4863 default:
4864 break;
4865 }
4866
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004867 clear_bit(field, vmx_vmwrite_bitmap);
4868 clear_bit(field, vmx_vmread_bitmap);
4869#ifdef CONFIG_X86_64
4870 if (field & 1)
4871 continue;
4872#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004873 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004874 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004875 j++;
4876 }
4877 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004878}
4879
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880static __init int alloc_kvm_area(void)
4881{
4882 int cpu;
4883
Zachary Amsden3230bb42009-09-29 11:38:37 -10004884 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885 struct vmcs *vmcs;
4886
Liran Alon491a6032018-06-23 02:35:12 +03004887 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888 if (!vmcs) {
4889 free_kvm_area();
4890 return -ENOMEM;
4891 }
4892
Liran Alon2307af12018-06-29 22:59:04 +03004893 /*
4894 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4895 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4896 * revision_id reported by MSR_IA32_VMX_BASIC.
4897 *
4898 * However, even though not explictly documented by
4899 * TLFS, VMXArea passed as VMXON argument should
4900 * still be marked with revision_id reported by
4901 * physical CPU.
4902 */
4903 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004904 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004905
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906 per_cpu(vmxarea, cpu) = vmcs;
4907 }
4908 return 0;
4909}
4910
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004911static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004912 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004913{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004914 if (!emulate_invalid_guest_state) {
4915 /*
4916 * CS and SS RPL should be equal during guest entry according
4917 * to VMX spec, but in reality it is not always so. Since vcpu
4918 * is in the middle of the transition from real mode to
4919 * protected mode it is safe to assume that RPL 0 is a good
4920 * default value.
4921 */
4922 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004923 save->selector &= ~SEGMENT_RPL_MASK;
4924 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004925 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004927 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004928}
4929
4930static void enter_pmode(struct kvm_vcpu *vcpu)
4931{
4932 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004933 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934
Gleb Natapovd99e4152012-12-20 16:57:45 +02004935 /*
4936 * Update real mode segment cache. It may be not up-to-date if sement
4937 * register was written while vcpu was in a guest mode.
4938 */
4939 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4940 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4945
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004946 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004947
Avi Kivity2fb92db2011-04-27 19:42:18 +03004948 vmx_segment_cache_clear(vmx);
4949
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004950 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951
4952 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004953 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4954 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955 vmcs_writel(GUEST_RFLAGS, flags);
4956
Rusty Russell66aee912007-07-17 23:34:16 +10004957 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4958 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959
4960 update_exception_bitmap(vcpu);
4961
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004962 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4963 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4964 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4965 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4966 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4967 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968}
4969
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004970static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004971{
Mathias Krause772e0312012-08-30 01:30:19 +02004972 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004973 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004974
Gleb Natapovd99e4152012-12-20 16:57:45 +02004975 var.dpl = 0x3;
4976 if (seg == VCPU_SREG_CS)
4977 var.type = 0x3;
4978
4979 if (!emulate_invalid_guest_state) {
4980 var.selector = var.base >> 4;
4981 var.base = var.base & 0xffff0;
4982 var.limit = 0xffff;
4983 var.g = 0;
4984 var.db = 0;
4985 var.present = 1;
4986 var.s = 1;
4987 var.l = 0;
4988 var.unusable = 0;
4989 var.type = 0x3;
4990 var.avl = 0;
4991 if (save->base & 0xf)
4992 printk_once(KERN_WARNING "kvm: segment base is not "
4993 "paragraph aligned when entering "
4994 "protected mode (seg=%d)", seg);
4995 }
4996
4997 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05004998 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004999 vmcs_write32(sf->limit, var.limit);
5000 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001}
5002
5003static void enter_rmode(struct kvm_vcpu *vcpu)
5004{
5005 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005006 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005007 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005008
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005009 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5010 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5011 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5012 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5013 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005014 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5015 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005016
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005017 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005018
Gleb Natapov776e58e2011-03-13 12:34:27 +02005019 /*
5020 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005021 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005022 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005023 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005024 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5025 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005026
Avi Kivity2fb92db2011-04-27 19:42:18 +03005027 vmx_segment_cache_clear(vmx);
5028
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005029 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005030 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005031 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5032
5033 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005034 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005036 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005037
5038 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005039 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005040 update_exception_bitmap(vcpu);
5041
Gleb Natapovd99e4152012-12-20 16:57:45 +02005042 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5043 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5044 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5045 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5046 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5047 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005048
Eddie Dong8668a3c2007-10-10 14:26:45 +08005049 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050}
5051
Amit Shah401d10d2009-02-20 22:53:37 +05305052static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5053{
5054 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005055 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5056
5057 if (!msr)
5058 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305059
Avi Kivityf6801df2010-01-21 15:31:50 +02005060 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305061 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005062 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305063 msr->data = efer;
5064 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005065 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305066
5067 msr->data = efer & ~EFER_LME;
5068 }
5069 setup_msrs(vmx);
5070}
5071
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005072#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073
5074static void enter_lmode(struct kvm_vcpu *vcpu)
5075{
5076 u32 guest_tr_ar;
5077
Avi Kivity2fb92db2011-04-27 19:42:18 +03005078 vmx_segment_cache_clear(to_vmx(vcpu));
5079
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005081 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005082 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5083 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005084 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005085 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5086 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005087 }
Avi Kivityda38f432010-07-06 11:30:49 +03005088 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005089}
5090
5091static void exit_lmode(struct kvm_vcpu *vcpu)
5092{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005093 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005094 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095}
5096
5097#endif
5098
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005099static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5100 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005101{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005102 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005103 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5104 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005105 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005106 } else {
5107 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005108 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005109}
5110
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005111static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005112{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005113 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005114}
5115
Junaid Shahidfaff8752018-06-29 13:10:05 -07005116static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5117{
5118 int vpid = to_vmx(vcpu)->vpid;
5119
5120 if (!vpid_sync_vcpu_addr(vpid, addr))
5121 vpid_sync_context(vpid);
5122
5123 /*
5124 * If VPIDs are not supported or enabled, then the above is a no-op.
5125 * But we don't really need a TLB flush in that case anyway, because
5126 * each VM entry/exit includes an implicit flush when VPID is 0.
5127 */
5128}
5129
Avi Kivitye8467fd2009-12-29 18:43:06 +02005130static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5131{
5132 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5133
5134 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5135 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5136}
5137
Avi Kivityaff48ba2010-12-05 18:56:11 +02005138static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5139{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005140 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005141 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5142 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5143}
5144
Anthony Liguori25c4c272007-04-27 09:29:21 +03005145static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005146{
Avi Kivityfc78f512009-12-07 12:16:48 +02005147 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5148
5149 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5150 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005151}
5152
Sheng Yang14394422008-04-28 12:24:45 +08005153static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5154{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005155 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5156
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005157 if (!test_bit(VCPU_EXREG_PDPTR,
5158 (unsigned long *)&vcpu->arch.regs_dirty))
5159 return;
5160
Sheng Yang14394422008-04-28 12:24:45 +08005161 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005162 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5163 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5164 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5165 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005166 }
5167}
5168
Avi Kivity8f5d5492009-05-31 18:41:29 +03005169static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5170{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005171 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5172
Avi Kivity8f5d5492009-05-31 18:41:29 +03005173 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005174 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5175 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5176 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5177 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005178 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005179
5180 __set_bit(VCPU_EXREG_PDPTR,
5181 (unsigned long *)&vcpu->arch.regs_avail);
5182 __set_bit(VCPU_EXREG_PDPTR,
5183 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005184}
5185
David Matlack38991522016-11-29 18:14:08 -08005186static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5187{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005188 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5189 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005190 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5191
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005192 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005193 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5194 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5195 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5196
5197 return fixed_bits_valid(val, fixed0, fixed1);
5198}
5199
5200static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5201{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005202 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5203 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005204
5205 return fixed_bits_valid(val, fixed0, fixed1);
5206}
5207
5208static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5209{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005210 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5211 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005212
5213 return fixed_bits_valid(val, fixed0, fixed1);
5214}
5215
5216/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5217#define nested_guest_cr4_valid nested_cr4_valid
5218#define nested_host_cr4_valid nested_cr4_valid
5219
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005220static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005221
5222static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5223 unsigned long cr0,
5224 struct kvm_vcpu *vcpu)
5225{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005226 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5227 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005228 if (!(cr0 & X86_CR0_PG)) {
5229 /* From paging/starting to nonpaging */
5230 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005231 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08005232 (CPU_BASED_CR3_LOAD_EXITING |
5233 CPU_BASED_CR3_STORE_EXITING));
5234 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005235 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005236 } else if (!is_paging(vcpu)) {
5237 /* From nonpaging to paging */
5238 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005239 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005240 ~(CPU_BASED_CR3_LOAD_EXITING |
5241 CPU_BASED_CR3_STORE_EXITING));
5242 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005243 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005244 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005245
5246 if (!(cr0 & X86_CR0_WP))
5247 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005248}
5249
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5251{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005252 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005253 unsigned long hw_cr0;
5254
Sean Christopherson3de63472018-07-13 08:42:30 -07005255 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005256 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005257 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005258 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005259 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005260
Gleb Natapov218e7632013-01-21 15:36:45 +02005261 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5262 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263
Gleb Natapov218e7632013-01-21 15:36:45 +02005264 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5265 enter_rmode(vcpu);
5266 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005267
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005268#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005269 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005270 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005271 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005272 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005273 exit_lmode(vcpu);
5274 }
5275#endif
5276
Sean Christophersonb4d18512018-03-05 12:04:40 -08005277 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005278 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5279
Avi Kivity6aa8b732006-12-10 02:21:36 -08005280 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005281 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005282 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005283
5284 /* depends on vcpu->arch.cr0 to be set to a new value */
5285 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005286}
5287
Yu Zhang855feb62017-08-24 20:27:55 +08005288static int get_ept_level(struct kvm_vcpu *vcpu)
5289{
5290 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5291 return 5;
5292 return 4;
5293}
5294
Peter Feiner995f00a2017-06-30 17:26:32 -07005295static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005296{
Yu Zhang855feb62017-08-24 20:27:55 +08005297 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005298
Yu Zhang855feb62017-08-24 20:27:55 +08005299 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005300
Peter Feiner995f00a2017-06-30 17:26:32 -07005301 if (enable_ept_ad_bits &&
5302 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005303 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005304 eptp |= (root_hpa & PAGE_MASK);
5305
5306 return eptp;
5307}
5308
Avi Kivity6aa8b732006-12-10 02:21:36 -08005309static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5310{
Tianyu Lan877ad952018-07-19 08:40:23 +00005311 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005312 unsigned long guest_cr3;
5313 u64 eptp;
5314
5315 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005316 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005317 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005318 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005319
5320 if (kvm_x86_ops->tlb_remote_flush) {
5321 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5322 to_vmx(vcpu)->ept_pointer = eptp;
5323 to_kvm_vmx(kvm)->ept_pointers_match
5324 = EPT_POINTERS_CHECK;
5325 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5326 }
5327
Sean Christophersone90008d2018-03-05 12:04:37 -08005328 if (enable_unrestricted_guest || is_paging(vcpu) ||
5329 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005330 guest_cr3 = kvm_read_cr3(vcpu);
5331 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005332 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005333 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005334 }
5335
Sheng Yang14394422008-04-28 12:24:45 +08005336 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005337}
5338
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005339static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005341 /*
5342 * Pass through host's Machine Check Enable value to hw_cr4, which
5343 * is in force while we are in guest mode. Do not let guests control
5344 * this bit, even if host CR4.MCE == 0.
5345 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005346 unsigned long hw_cr4;
5347
5348 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5349 if (enable_unrestricted_guest)
5350 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5351 else if (to_vmx(vcpu)->rmode.vm86_active)
5352 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5353 else
5354 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005355
Sean Christopherson64f7a112018-04-30 10:01:06 -07005356 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5357 if (cr4 & X86_CR4_UMIP) {
5358 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005359 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005360 hw_cr4 &= ~X86_CR4_UMIP;
5361 } else if (!is_guest_mode(vcpu) ||
5362 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5363 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5364 SECONDARY_EXEC_DESC);
5365 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005366
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005367 if (cr4 & X86_CR4_VMXE) {
5368 /*
5369 * To use VMXON (and later other VMX instructions), a guest
5370 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5371 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005372 * is here. We operate under the default treatment of SMM,
5373 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005374 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005375 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005376 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005377 }
David Matlack38991522016-11-29 18:14:08 -08005378
5379 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005380 return 1;
5381
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005382 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005383
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005384 if (!enable_unrestricted_guest) {
5385 if (enable_ept) {
5386 if (!is_paging(vcpu)) {
5387 hw_cr4 &= ~X86_CR4_PAE;
5388 hw_cr4 |= X86_CR4_PSE;
5389 } else if (!(cr4 & X86_CR4_PAE)) {
5390 hw_cr4 &= ~X86_CR4_PAE;
5391 }
5392 }
5393
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005394 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005395 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5396 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5397 * to be manually disabled when guest switches to non-paging
5398 * mode.
5399 *
5400 * If !enable_unrestricted_guest, the CPU is always running
5401 * with CR0.PG=1 and CR4 needs to be modified.
5402 * If enable_unrestricted_guest, the CPU automatically
5403 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005404 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005405 if (!is_paging(vcpu))
5406 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5407 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005408
Sheng Yang14394422008-04-28 12:24:45 +08005409 vmcs_writel(CR4_READ_SHADOW, cr4);
5410 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005411 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005412}
5413
Avi Kivity6aa8b732006-12-10 02:21:36 -08005414static void vmx_get_segment(struct kvm_vcpu *vcpu,
5415 struct kvm_segment *var, int seg)
5416{
Avi Kivitya9179492011-01-03 14:28:52 +02005417 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005418 u32 ar;
5419
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005420 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005421 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005422 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005423 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005424 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005425 var->base = vmx_read_guest_seg_base(vmx, seg);
5426 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5427 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005428 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005429 var->base = vmx_read_guest_seg_base(vmx, seg);
5430 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5431 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5432 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005433 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005434 var->type = ar & 15;
5435 var->s = (ar >> 4) & 1;
5436 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005437 /*
5438 * Some userspaces do not preserve unusable property. Since usable
5439 * segment has to be present according to VMX spec we can use present
5440 * property to amend userspace bug by making unusable segment always
5441 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5442 * segment as unusable.
5443 */
5444 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005445 var->avl = (ar >> 12) & 1;
5446 var->l = (ar >> 13) & 1;
5447 var->db = (ar >> 14) & 1;
5448 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005449}
5450
Avi Kivitya9179492011-01-03 14:28:52 +02005451static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5452{
Avi Kivitya9179492011-01-03 14:28:52 +02005453 struct kvm_segment s;
5454
5455 if (to_vmx(vcpu)->rmode.vm86_active) {
5456 vmx_get_segment(vcpu, &s, seg);
5457 return s.base;
5458 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005459 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005460}
5461
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005462static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005463{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005464 struct vcpu_vmx *vmx = to_vmx(vcpu);
5465
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005466 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005467 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005468 else {
5469 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005470 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005471 }
Avi Kivity69c73022011-03-07 15:26:44 +02005472}
5473
Avi Kivity653e3102007-05-07 10:55:37 +03005474static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005475{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005476 u32 ar;
5477
Avi Kivityf0495f92012-06-07 17:06:10 +03005478 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005479 ar = 1 << 16;
5480 else {
5481 ar = var->type & 15;
5482 ar |= (var->s & 1) << 4;
5483 ar |= (var->dpl & 3) << 5;
5484 ar |= (var->present & 1) << 7;
5485 ar |= (var->avl & 1) << 12;
5486 ar |= (var->l & 1) << 13;
5487 ar |= (var->db & 1) << 14;
5488 ar |= (var->g & 1) << 15;
5489 }
Avi Kivity653e3102007-05-07 10:55:37 +03005490
5491 return ar;
5492}
5493
5494static void vmx_set_segment(struct kvm_vcpu *vcpu,
5495 struct kvm_segment *var, int seg)
5496{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005497 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005498 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005499
Avi Kivity2fb92db2011-04-27 19:42:18 +03005500 vmx_segment_cache_clear(vmx);
5501
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005502 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5503 vmx->rmode.segs[seg] = *var;
5504 if (seg == VCPU_SREG_TR)
5505 vmcs_write16(sf->selector, var->selector);
5506 else if (var->s)
5507 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005508 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005509 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005510
Avi Kivity653e3102007-05-07 10:55:37 +03005511 vmcs_writel(sf->base, var->base);
5512 vmcs_write32(sf->limit, var->limit);
5513 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005514
5515 /*
5516 * Fix the "Accessed" bit in AR field of segment registers for older
5517 * qemu binaries.
5518 * IA32 arch specifies that at the time of processor reset the
5519 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005520 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005521 * state vmexit when "unrestricted guest" mode is turned on.
5522 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5523 * tree. Newer qemu binaries with that qemu fix would not need this
5524 * kvm hack.
5525 */
5526 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005527 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005528
Gleb Natapovf924d662012-12-12 19:10:55 +02005529 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005530
5531out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005532 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005533}
5534
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5536{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005537 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005538
5539 *db = (ar >> 14) & 1;
5540 *l = (ar >> 13) & 1;
5541}
5542
Gleb Natapov89a27f42010-02-16 10:51:48 +02005543static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005544{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005545 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5546 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005547}
5548
Gleb Natapov89a27f42010-02-16 10:51:48 +02005549static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005551 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5552 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005553}
5554
Gleb Natapov89a27f42010-02-16 10:51:48 +02005555static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005556{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005557 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5558 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005559}
5560
Gleb Natapov89a27f42010-02-16 10:51:48 +02005561static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005562{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005563 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5564 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005565}
5566
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005567static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5568{
5569 struct kvm_segment var;
5570 u32 ar;
5571
5572 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005573 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005574 if (seg == VCPU_SREG_CS)
5575 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005576 ar = vmx_segment_access_rights(&var);
5577
5578 if (var.base != (var.selector << 4))
5579 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005580 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005581 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005582 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005583 return false;
5584
5585 return true;
5586}
5587
5588static bool code_segment_valid(struct kvm_vcpu *vcpu)
5589{
5590 struct kvm_segment cs;
5591 unsigned int cs_rpl;
5592
5593 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005594 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005595
Avi Kivity1872a3f2009-01-04 23:26:52 +02005596 if (cs.unusable)
5597 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005598 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005599 return false;
5600 if (!cs.s)
5601 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005602 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005603 if (cs.dpl > cs_rpl)
5604 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005605 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005606 if (cs.dpl != cs_rpl)
5607 return false;
5608 }
5609 if (!cs.present)
5610 return false;
5611
5612 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5613 return true;
5614}
5615
5616static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5617{
5618 struct kvm_segment ss;
5619 unsigned int ss_rpl;
5620
5621 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005622 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005623
Avi Kivity1872a3f2009-01-04 23:26:52 +02005624 if (ss.unusable)
5625 return true;
5626 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005627 return false;
5628 if (!ss.s)
5629 return false;
5630 if (ss.dpl != ss_rpl) /* DPL != RPL */
5631 return false;
5632 if (!ss.present)
5633 return false;
5634
5635 return true;
5636}
5637
5638static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5639{
5640 struct kvm_segment var;
5641 unsigned int rpl;
5642
5643 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005644 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005645
Avi Kivity1872a3f2009-01-04 23:26:52 +02005646 if (var.unusable)
5647 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005648 if (!var.s)
5649 return false;
5650 if (!var.present)
5651 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005652 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005653 if (var.dpl < rpl) /* DPL < RPL */
5654 return false;
5655 }
5656
5657 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5658 * rights flags
5659 */
5660 return true;
5661}
5662
5663static bool tr_valid(struct kvm_vcpu *vcpu)
5664{
5665 struct kvm_segment tr;
5666
5667 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5668
Avi Kivity1872a3f2009-01-04 23:26:52 +02005669 if (tr.unusable)
5670 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005671 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005672 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005673 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005674 return false;
5675 if (!tr.present)
5676 return false;
5677
5678 return true;
5679}
5680
5681static bool ldtr_valid(struct kvm_vcpu *vcpu)
5682{
5683 struct kvm_segment ldtr;
5684
5685 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5686
Avi Kivity1872a3f2009-01-04 23:26:52 +02005687 if (ldtr.unusable)
5688 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005689 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005690 return false;
5691 if (ldtr.type != 2)
5692 return false;
5693 if (!ldtr.present)
5694 return false;
5695
5696 return true;
5697}
5698
5699static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5700{
5701 struct kvm_segment cs, ss;
5702
5703 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5704 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5705
Nadav Amitb32a9912015-03-29 16:33:04 +03005706 return ((cs.selector & SEGMENT_RPL_MASK) ==
5707 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005708}
5709
5710/*
5711 * Check if guest state is valid. Returns true if valid, false if
5712 * not.
5713 * We assume that registers are always usable
5714 */
5715static bool guest_state_valid(struct kvm_vcpu *vcpu)
5716{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005717 if (enable_unrestricted_guest)
5718 return true;
5719
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005720 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005721 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005722 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5723 return false;
5724 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5725 return false;
5726 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5727 return false;
5728 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5729 return false;
5730 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5731 return false;
5732 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5733 return false;
5734 } else {
5735 /* protected mode guest state checks */
5736 if (!cs_ss_rpl_check(vcpu))
5737 return false;
5738 if (!code_segment_valid(vcpu))
5739 return false;
5740 if (!stack_segment_valid(vcpu))
5741 return false;
5742 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5743 return false;
5744 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5745 return false;
5746 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5747 return false;
5748 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5749 return false;
5750 if (!tr_valid(vcpu))
5751 return false;
5752 if (!ldtr_valid(vcpu))
5753 return false;
5754 }
5755 /* TODO:
5756 * - Add checks on RIP
5757 * - Add checks on RFLAGS
5758 */
5759
5760 return true;
5761}
5762
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005763static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5764{
5765 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5766}
5767
Mike Dayd77c26f2007-10-08 09:02:08 -04005768static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005769{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005770 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005771 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005772 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005773
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005774 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005775 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02005776 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5777 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005778 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005779 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005780 r = kvm_write_guest_page(kvm, fn++, &data,
5781 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005782 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005783 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005784 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5785 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005786 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005787 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5788 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005789 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005790 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005791 r = kvm_write_guest_page(kvm, fn, &data,
5792 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5793 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005794out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005795 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005796 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005797}
5798
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005799static int init_rmode_identity_map(struct kvm *kvm)
5800{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005801 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005802 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005803 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005804 u32 tmp;
5805
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005806 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005807 mutex_lock(&kvm->slots_lock);
5808
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005809 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005810 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005811
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005812 if (!kvm_vmx->ept_identity_map_addr)
5813 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5814 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005815
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005816 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005817 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005818 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005819 goto out2;
5820
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005821 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005822 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5823 if (r < 0)
5824 goto out;
5825 /* Set up identity-mapping pagetable for EPT in real mode */
5826 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5827 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5828 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5829 r = kvm_write_guest_page(kvm, identity_map_pfn,
5830 &tmp, i * sizeof(tmp), sizeof(tmp));
5831 if (r < 0)
5832 goto out;
5833 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005834 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005835
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005836out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005837 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005838
5839out2:
5840 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005841 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005842}
5843
Avi Kivity6aa8b732006-12-10 02:21:36 -08005844static void seg_setup(int seg)
5845{
Mathias Krause772e0312012-08-30 01:30:19 +02005846 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005847 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005848
5849 vmcs_write16(sf->selector, 0);
5850 vmcs_writel(sf->base, 0);
5851 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005852 ar = 0x93;
5853 if (seg == VCPU_SREG_CS)
5854 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005855
5856 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005857}
5858
Sheng Yangf78e0e22007-10-29 09:40:42 +08005859static int alloc_apic_access_page(struct kvm *kvm)
5860{
Xiao Guangrong44841412012-09-07 14:14:20 +08005861 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005862 int r = 0;
5863
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005864 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005865 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005866 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005867 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5868 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005869 if (r)
5870 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005871
Tang Chen73a6d942014-09-11 13:38:00 +08005872 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005873 if (is_error_page(page)) {
5874 r = -EFAULT;
5875 goto out;
5876 }
5877
Tang Chenc24ae0d2014-09-24 15:57:58 +08005878 /*
5879 * Do not pin the page in memory, so that memory hot-unplug
5880 * is able to migrate it.
5881 */
5882 put_page(page);
5883 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005884out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005885 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005886 return r;
5887}
5888
Wanpeng Li991e7a02015-09-16 17:30:05 +08005889static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005890{
5891 int vpid;
5892
Avi Kivity919818a2009-03-23 18:01:29 +02005893 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005894 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005895 spin_lock(&vmx_vpid_lock);
5896 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005897 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005898 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005899 else
5900 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005901 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005902 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005903}
5904
Wanpeng Li991e7a02015-09-16 17:30:05 +08005905static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005906{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005907 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005908 return;
5909 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005910 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005911 spin_unlock(&vmx_vpid_lock);
5912}
5913
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005914static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5915 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005916{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005917 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005918
5919 if (!cpu_has_vmx_msr_bitmap())
5920 return;
5921
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005922 if (static_branch_unlikely(&enable_evmcs))
5923 evmcs_touch_msr_bitmap();
5924
Sheng Yang25c5f222008-03-28 13:18:56 +08005925 /*
5926 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5927 * have the write-low and read-high bitmap offsets the wrong way round.
5928 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5929 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005930 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005931 if (type & MSR_TYPE_R)
5932 /* read-low */
5933 __clear_bit(msr, msr_bitmap + 0x000 / f);
5934
5935 if (type & MSR_TYPE_W)
5936 /* write-low */
5937 __clear_bit(msr, msr_bitmap + 0x800 / f);
5938
Sheng Yang25c5f222008-03-28 13:18:56 +08005939 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5940 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005941 if (type & MSR_TYPE_R)
5942 /* read-high */
5943 __clear_bit(msr, msr_bitmap + 0x400 / f);
5944
5945 if (type & MSR_TYPE_W)
5946 /* write-high */
5947 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5948
5949 }
5950}
5951
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005952static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5953 u32 msr, int type)
5954{
5955 int f = sizeof(unsigned long);
5956
5957 if (!cpu_has_vmx_msr_bitmap())
5958 return;
5959
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005960 if (static_branch_unlikely(&enable_evmcs))
5961 evmcs_touch_msr_bitmap();
5962
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005963 /*
5964 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5965 * have the write-low and read-high bitmap offsets the wrong way round.
5966 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5967 */
5968 if (msr <= 0x1fff) {
5969 if (type & MSR_TYPE_R)
5970 /* read-low */
5971 __set_bit(msr, msr_bitmap + 0x000 / f);
5972
5973 if (type & MSR_TYPE_W)
5974 /* write-low */
5975 __set_bit(msr, msr_bitmap + 0x800 / f);
5976
5977 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5978 msr &= 0x1fff;
5979 if (type & MSR_TYPE_R)
5980 /* read-high */
5981 __set_bit(msr, msr_bitmap + 0x400 / f);
5982
5983 if (type & MSR_TYPE_W)
5984 /* write-high */
5985 __set_bit(msr, msr_bitmap + 0xc00 / f);
5986
5987 }
5988}
5989
5990static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5991 u32 msr, int type, bool value)
5992{
5993 if (value)
5994 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
5995 else
5996 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
5997}
5998
Wincy Vanf2b93282015-02-03 23:56:03 +08005999/*
6000 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6001 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6002 */
6003static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6004 unsigned long *msr_bitmap_nested,
6005 u32 msr, int type)
6006{
6007 int f = sizeof(unsigned long);
6008
Wincy Vanf2b93282015-02-03 23:56:03 +08006009 /*
6010 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6011 * have the write-low and read-high bitmap offsets the wrong way round.
6012 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6013 */
6014 if (msr <= 0x1fff) {
6015 if (type & MSR_TYPE_R &&
6016 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6017 /* read-low */
6018 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6019
6020 if (type & MSR_TYPE_W &&
6021 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6022 /* write-low */
6023 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6024
6025 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6026 msr &= 0x1fff;
6027 if (type & MSR_TYPE_R &&
6028 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6029 /* read-high */
6030 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6031
6032 if (type & MSR_TYPE_W &&
6033 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6034 /* write-high */
6035 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6036
6037 }
6038}
6039
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006040static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006041{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006042 u8 mode = 0;
6043
6044 if (cpu_has_secondary_exec_ctrls() &&
6045 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6046 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6047 mode |= MSR_BITMAP_MODE_X2APIC;
6048 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6049 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6050 }
6051
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006052 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006053}
6054
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006055#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6056
6057static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6058 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006059{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006060 int msr;
6061
6062 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6063 unsigned word = msr / BITS_PER_LONG;
6064 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6065 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006066 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006067
6068 if (mode & MSR_BITMAP_MODE_X2APIC) {
6069 /*
6070 * TPR reads and writes can be virtualized even if virtual interrupt
6071 * delivery is not in use.
6072 */
6073 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6074 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6075 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6076 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6077 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6078 }
6079 }
6080}
6081
6082static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6083{
6084 struct vcpu_vmx *vmx = to_vmx(vcpu);
6085 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6086 u8 mode = vmx_msr_bitmap_mode(vcpu);
6087 u8 changed = mode ^ vmx->msr_bitmap_mode;
6088
6089 if (!changed)
6090 return;
6091
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006092 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6093 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6094
6095 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006096}
6097
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006098static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006099{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006100 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006101}
6102
David Matlackc9f04402017-08-01 14:00:40 -07006103static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6104{
6105 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6106 gfn_t gfn;
6107
6108 /*
6109 * Don't need to mark the APIC access page dirty; it is never
6110 * written to by the CPU during APIC virtualization.
6111 */
6112
6113 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6114 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6115 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6116 }
6117
6118 if (nested_cpu_has_posted_intr(vmcs12)) {
6119 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6120 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6121 }
6122}
6123
6124
David Hildenbrand6342c502017-01-25 11:58:58 +01006125static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006126{
6127 struct vcpu_vmx *vmx = to_vmx(vcpu);
6128 int max_irr;
6129 void *vapic_page;
6130 u16 status;
6131
David Matlackc9f04402017-08-01 14:00:40 -07006132 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6133 return;
Wincy Van705699a2015-02-03 23:58:17 +08006134
David Matlackc9f04402017-08-01 14:00:40 -07006135 vmx->nested.pi_pending = false;
6136 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6137 return;
Wincy Van705699a2015-02-03 23:58:17 +08006138
David Matlackc9f04402017-08-01 14:00:40 -07006139 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6140 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006141 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006142 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6143 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006144 kunmap(vmx->nested.virtual_apic_page);
6145
6146 status = vmcs_read16(GUEST_INTR_STATUS);
6147 if ((u8)max_irr > ((u8)status & 0xff)) {
6148 status &= ~0xff;
6149 status |= (u8)max_irr;
6150 vmcs_write16(GUEST_INTR_STATUS, status);
6151 }
6152 }
David Matlackc9f04402017-08-01 14:00:40 -07006153
6154 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006155}
6156
Paolo Bonzini7e712682018-10-03 13:44:26 +02006157static u8 vmx_get_rvi(void)
6158{
6159 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6160}
6161
Liran Alone6c67d82018-09-04 10:56:52 +03006162static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6163{
6164 struct vcpu_vmx *vmx = to_vmx(vcpu);
6165 void *vapic_page;
6166 u32 vppr;
6167 int rvi;
6168
6169 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6170 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6171 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6172 return false;
6173
Paolo Bonzini7e712682018-10-03 13:44:26 +02006174 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006175
6176 vapic_page = kmap(vmx->nested.virtual_apic_page);
6177 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6178 kunmap(vmx->nested.virtual_apic_page);
6179
6180 return ((rvi & 0xf0) > (vppr & 0xf0));
6181}
6182
Wincy Van06a55242017-04-28 13:13:59 +08006183static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6184 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006185{
6186#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006187 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6188
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006189 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006190 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006191 * The vector of interrupt to be delivered to vcpu had
6192 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006193 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006194 * Following cases will be reached in this block, and
6195 * we always send a notification event in all cases as
6196 * explained below.
6197 *
6198 * Case 1: vcpu keeps in non-root mode. Sending a
6199 * notification event posts the interrupt to vcpu.
6200 *
6201 * Case 2: vcpu exits to root mode and is still
6202 * runnable. PIR will be synced to vIRR before the
6203 * next vcpu entry. Sending a notification event in
6204 * this case has no effect, as vcpu is not in root
6205 * mode.
6206 *
6207 * Case 3: vcpu exits to root mode and is blocked.
6208 * vcpu_block() has already synced PIR to vIRR and
6209 * never blocks vcpu if vIRR is not cleared. Therefore,
6210 * a blocked vcpu here does not wait for any requested
6211 * interrupts in PIR, and sending a notification event
6212 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006213 */
Feng Wu28b835d2015-09-18 22:29:54 +08006214
Wincy Van06a55242017-04-28 13:13:59 +08006215 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006216 return true;
6217 }
6218#endif
6219 return false;
6220}
6221
Wincy Van705699a2015-02-03 23:58:17 +08006222static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6223 int vector)
6224{
6225 struct vcpu_vmx *vmx = to_vmx(vcpu);
6226
6227 if (is_guest_mode(vcpu) &&
6228 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006229 /*
6230 * If a posted intr is not recognized by hardware,
6231 * we will accomplish it in the next vmentry.
6232 */
6233 vmx->nested.pi_pending = true;
6234 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006235 /* the PIR and ON have been set by L1. */
6236 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6237 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006238 return 0;
6239 }
6240 return -1;
6241}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006242/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006243 * Send interrupt to vcpu via posted interrupt way.
6244 * 1. If target vcpu is running(non-root mode), send posted interrupt
6245 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6246 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6247 * interrupt from PIR in next vmentry.
6248 */
6249static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6250{
6251 struct vcpu_vmx *vmx = to_vmx(vcpu);
6252 int r;
6253
Wincy Van705699a2015-02-03 23:58:17 +08006254 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6255 if (!r)
6256 return;
6257
Yang Zhanga20ed542013-04-11 19:25:15 +08006258 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6259 return;
6260
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006261 /* If a previous notification has sent the IPI, nothing to do. */
6262 if (pi_test_and_set_on(&vmx->pi_desc))
6263 return;
6264
Wincy Van06a55242017-04-28 13:13:59 +08006265 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006266 kvm_vcpu_kick(vcpu);
6267}
6268
Avi Kivity6aa8b732006-12-10 02:21:36 -08006269/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006270 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6271 * will not change in the lifetime of the guest.
6272 * Note that host-state that does change is set elsewhere. E.g., host-state
6273 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6274 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006275static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006276{
6277 u32 low32, high32;
6278 unsigned long tmpl;
6279 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006280 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006281
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006282 cr0 = read_cr0();
6283 WARN_ON(cr0 & X86_CR0_TS);
6284 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006285
6286 /*
6287 * Save the most likely value for this task's CR3 in the VMCS.
6288 * We can't use __get_current_cr3_fast() because we're not atomic.
6289 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006290 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006291 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006292 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006293
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006294 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006295 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006296 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006297 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006298
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006299 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006300#ifdef CONFIG_X86_64
6301 /*
6302 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006303 * vmx_prepare_switch_to_host(), in case userspace uses
6304 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006305 */
6306 vmcs_write16(HOST_DS_SELECTOR, 0);
6307 vmcs_write16(HOST_ES_SELECTOR, 0);
6308#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006309 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6310 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006311#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006312 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6313 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6314
Juergen Gross87930012017-09-04 12:25:27 +02006315 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006316 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006317 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006318
Avi Kivity83287ea422012-09-16 15:10:57 +03006319 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006320
6321 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6322 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6323 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6324 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6325
6326 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6327 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6328 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6329 }
6330}
6331
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006332static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6333{
6334 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6335 if (enable_ept)
6336 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006337 if (is_guest_mode(&vmx->vcpu))
6338 vmx->vcpu.arch.cr4_guest_owned_bits &=
6339 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006340 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6341}
6342
Yang Zhang01e439b2013-04-11 19:25:12 +08006343static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6344{
6345 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6346
Andrey Smetanind62caab2015-11-10 15:36:33 +03006347 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006348 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006349
6350 if (!enable_vnmi)
6351 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6352
Yunhong Jiang64672c92016-06-13 14:19:59 -07006353 /* Enable the preemption timer dynamically */
6354 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006355 return pin_based_exec_ctrl;
6356}
6357
Andrey Smetanind62caab2015-11-10 15:36:33 +03006358static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6359{
6360 struct vcpu_vmx *vmx = to_vmx(vcpu);
6361
6362 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006363 if (cpu_has_secondary_exec_ctrls()) {
6364 if (kvm_vcpu_apicv_active(vcpu))
6365 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6366 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6367 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6368 else
6369 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6370 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6371 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6372 }
6373
6374 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006375 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006376}
6377
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006378static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6379{
6380 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006381
6382 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6383 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6384
Paolo Bonzini35754c92015-07-29 12:05:37 +02006385 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006386 exec_control &= ~CPU_BASED_TPR_SHADOW;
6387#ifdef CONFIG_X86_64
6388 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6389 CPU_BASED_CR8_LOAD_EXITING;
6390#endif
6391 }
6392 if (!enable_ept)
6393 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6394 CPU_BASED_CR3_LOAD_EXITING |
6395 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006396 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6397 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6398 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006399 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6400 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006401 return exec_control;
6402}
6403
Jim Mattson45ec3682017-08-23 16:32:04 -07006404static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006405{
Jim Mattson45ec3682017-08-23 16:32:04 -07006406 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006407 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006408}
6409
Jim Mattson75f4fc82017-08-23 16:32:03 -07006410static bool vmx_rdseed_supported(void)
6411{
6412 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006413 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006414}
6415
Paolo Bonzini80154d72017-08-24 13:55:35 +02006416static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006417{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006418 struct kvm_vcpu *vcpu = &vmx->vcpu;
6419
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006420 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006421
Paolo Bonzini80154d72017-08-24 13:55:35 +02006422 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006423 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6424 if (vmx->vpid == 0)
6425 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6426 if (!enable_ept) {
6427 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6428 enable_unrestricted_guest = 0;
6429 }
6430 if (!enable_unrestricted_guest)
6431 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006432 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006433 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006434 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006435 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6436 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006437 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006438
6439 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6440 * in vmx_set_cr4. */
6441 exec_control &= ~SECONDARY_EXEC_DESC;
6442
Abel Gordonabc4fc52013-04-18 14:35:25 +03006443 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6444 (handle_vmptrld).
6445 We can NOT enable shadow_vmcs here because we don't have yet
6446 a current VMCS12
6447 */
6448 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006449
6450 if (!enable_pml)
6451 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006452
Paolo Bonzini3db13482017-08-24 14:48:03 +02006453 if (vmx_xsaves_supported()) {
6454 /* Exposing XSAVES only when XSAVE is exposed */
6455 bool xsaves_enabled =
6456 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6457 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6458
6459 if (!xsaves_enabled)
6460 exec_control &= ~SECONDARY_EXEC_XSAVES;
6461
6462 if (nested) {
6463 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006464 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006465 SECONDARY_EXEC_XSAVES;
6466 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006467 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006468 ~SECONDARY_EXEC_XSAVES;
6469 }
6470 }
6471
Paolo Bonzini80154d72017-08-24 13:55:35 +02006472 if (vmx_rdtscp_supported()) {
6473 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6474 if (!rdtscp_enabled)
6475 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6476
6477 if (nested) {
6478 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006479 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006480 SECONDARY_EXEC_RDTSCP;
6481 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006482 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006483 ~SECONDARY_EXEC_RDTSCP;
6484 }
6485 }
6486
6487 if (vmx_invpcid_supported()) {
6488 /* Exposing INVPCID only when PCID is exposed */
6489 bool invpcid_enabled =
6490 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6491 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6492
6493 if (!invpcid_enabled) {
6494 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6495 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6496 }
6497
6498 if (nested) {
6499 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006500 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006501 SECONDARY_EXEC_ENABLE_INVPCID;
6502 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006503 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006504 ~SECONDARY_EXEC_ENABLE_INVPCID;
6505 }
6506 }
6507
Jim Mattson45ec3682017-08-23 16:32:04 -07006508 if (vmx_rdrand_supported()) {
6509 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6510 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006511 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006512
6513 if (nested) {
6514 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006515 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006516 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006517 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006518 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006519 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006520 }
6521 }
6522
Jim Mattson75f4fc82017-08-23 16:32:03 -07006523 if (vmx_rdseed_supported()) {
6524 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6525 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006526 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006527
6528 if (nested) {
6529 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006530 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006531 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006532 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006533 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006534 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006535 }
6536 }
6537
Paolo Bonzini80154d72017-08-24 13:55:35 +02006538 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006539}
6540
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006541static void ept_set_mmio_spte_mask(void)
6542{
6543 /*
6544 * EPT Misconfigurations can be generated if the value of bits 2:0
6545 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006546 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006547 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6548 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006549}
6550
Wanpeng Lif53cd632014-12-02 19:14:58 +08006551#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006552/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006553 * Sets up the vmcs for emulated real mode.
6554 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006555static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006556{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006557 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006558
Abel Gordon4607c2d2013-04-18 14:35:55 +03006559 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006560 /*
6561 * At vCPU creation, "VMWRITE to any supported field
6562 * in the VMCS" is supported, so use the more
6563 * permissive vmx_vmread_bitmap to specify both read
6564 * and write permissions for the shadow VMCS.
6565 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006566 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006567 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006568 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006569 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006570 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006571
Avi Kivity6aa8b732006-12-10 02:21:36 -08006572 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6573
Avi Kivity6aa8b732006-12-10 02:21:36 -08006574 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006575 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006576 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006577
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006578 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006579
Dan Williamsdfa169b2016-06-02 11:17:24 -07006580 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006581 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006582 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006583 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006584 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006585
Andrey Smetanind62caab2015-11-10 15:36:33 +03006586 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006587 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6588 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6589 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6590 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6591
6592 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006593
Li RongQing0bcf2612015-12-03 13:29:34 +08006594 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006595 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006596 }
6597
Wanpeng Lib31c1142018-03-12 04:53:04 -07006598 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006599 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006600 vmx->ple_window = ple_window;
6601 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006602 }
6603
Xiao Guangrongc3707952011-07-12 03:28:04 +08006604 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6605 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006606 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6607
Avi Kivity9581d442010-10-19 16:46:55 +02006608 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6609 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006610 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006611 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6612 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006613
Bandan Das2a499e42017-08-03 15:54:41 -04006614 if (cpu_has_vmx_vmfunc())
6615 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6616
Eddie Dong2cc51562007-05-21 07:28:09 +03006617 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6618 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006619 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006620 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006621 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622
Radim Krčmář74545702015-04-27 15:11:25 +02006623 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6624 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006625
Paolo Bonzini03916db2014-07-24 14:21:57 +02006626 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006627 u32 index = vmx_msr_index[i];
6628 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006629 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006630
6631 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6632 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006633 if (wrmsr_safe(index, data_low, data_high) < 0)
6634 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006635 vmx->guest_msrs[j].index = i;
6636 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006637 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006638 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006640
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006641 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006642
6643 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006644
6645 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006646 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006647
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006648 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6649 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6650
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006651 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006652
Wanpeng Lif53cd632014-12-02 19:14:58 +08006653 if (vmx_xsaves_supported())
6654 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6655
Peter Feiner4e595162016-07-07 14:49:58 -07006656 if (enable_pml) {
6657 ASSERT(vmx->pml_pg);
6658 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6659 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6660 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006661
6662 if (cpu_has_vmx_encls_vmexit())
6663 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006664}
6665
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006666static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006667{
6668 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006669 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006670 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006671
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006672 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006673 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006674
Wanpeng Li518e7b92018-02-28 14:03:31 +08006675 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006676 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006677 kvm_set_cr8(vcpu, 0);
6678
6679 if (!init_event) {
6680 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6681 MSR_IA32_APICBASE_ENABLE;
6682 if (kvm_vcpu_is_reset_bsp(vcpu))
6683 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6684 apic_base_msr.host_initiated = true;
6685 kvm_set_apic_base(vcpu, &apic_base_msr);
6686 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006687
Avi Kivity2fb92db2011-04-27 19:42:18 +03006688 vmx_segment_cache_clear(vmx);
6689
Avi Kivity5706be02008-08-20 15:07:31 +03006690 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006691 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006692 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006693
6694 seg_setup(VCPU_SREG_DS);
6695 seg_setup(VCPU_SREG_ES);
6696 seg_setup(VCPU_SREG_FS);
6697 seg_setup(VCPU_SREG_GS);
6698 seg_setup(VCPU_SREG_SS);
6699
6700 vmcs_write16(GUEST_TR_SELECTOR, 0);
6701 vmcs_writel(GUEST_TR_BASE, 0);
6702 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6703 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6704
6705 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6706 vmcs_writel(GUEST_LDTR_BASE, 0);
6707 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6708 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6709
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006710 if (!init_event) {
6711 vmcs_write32(GUEST_SYSENTER_CS, 0);
6712 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6713 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6714 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6715 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006716
Wanpeng Lic37c2872017-11-20 14:52:21 -08006717 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006718 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006719
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006720 vmcs_writel(GUEST_GDTR_BASE, 0);
6721 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6722
6723 vmcs_writel(GUEST_IDTR_BASE, 0);
6724 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6725
Anthony Liguori443381a2010-12-06 10:53:38 -06006726 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006727 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006728 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006729 if (kvm_mpx_supported())
6730 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006731
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006732 setup_msrs(vmx);
6733
Avi Kivity6aa8b732006-12-10 02:21:36 -08006734 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6735
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006736 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006737 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006738 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006739 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006740 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006741 vmcs_write32(TPR_THRESHOLD, 0);
6742 }
6743
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006744 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006745
Sheng Yang2384d2b2008-01-17 15:14:33 +08006746 if (vmx->vpid != 0)
6747 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6748
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006749 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006750 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006751 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006752 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006753 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006754
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006755 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006756
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006757 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006758 if (init_event)
6759 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006760}
6761
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006762/*
6763 * In nested virtualization, check if L1 asked to exit on external interrupts.
6764 * For most existing hypervisors, this will always return true.
6765 */
6766static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6767{
6768 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6769 PIN_BASED_EXT_INTR_MASK;
6770}
6771
Bandan Das77b0f5d2014-04-19 18:17:45 -04006772/*
6773 * In nested virtualization, check if L1 has set
6774 * VM_EXIT_ACK_INTR_ON_EXIT
6775 */
6776static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6777{
6778 return get_vmcs12(vcpu)->vm_exit_controls &
6779 VM_EXIT_ACK_INTR_ON_EXIT;
6780}
6781
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006782static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6783{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006784 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006785}
6786
Jan Kiszkac9a79532014-03-07 20:03:15 +01006787static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006788{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006789 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6790 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006791}
6792
Jan Kiszkac9a79532014-03-07 20:03:15 +01006793static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006794{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006795 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006796 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006797 enable_irq_window(vcpu);
6798 return;
6799 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006800
Paolo Bonzini47c01522016-12-19 11:44:07 +01006801 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6802 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006803}
6804
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006805static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006806{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006807 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006808 uint32_t intr;
6809 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006810
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006811 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006812
Avi Kivityfa89a812008-09-01 15:57:51 +03006813 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006814 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006815 int inc_eip = 0;
6816 if (vcpu->arch.interrupt.soft)
6817 inc_eip = vcpu->arch.event_exit_inst_len;
6818 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006819 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006820 return;
6821 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006822 intr = irq | INTR_INFO_VALID_MASK;
6823 if (vcpu->arch.interrupt.soft) {
6824 intr |= INTR_TYPE_SOFT_INTR;
6825 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6826 vmx->vcpu.arch.event_exit_inst_len);
6827 } else
6828 intr |= INTR_TYPE_EXT_INTR;
6829 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006830
6831 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006832}
6833
Sheng Yangf08864b2008-05-15 18:23:25 +08006834static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6835{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006836 struct vcpu_vmx *vmx = to_vmx(vcpu);
6837
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006838 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006839 /*
6840 * Tracking the NMI-blocked state in software is built upon
6841 * finding the next open IRQ window. This, in turn, depends on
6842 * well-behaving guests: They have to keep IRQs disabled at
6843 * least as long as the NMI handler runs. Otherwise we may
6844 * cause NMI nesting, maybe breaking the guest. But as this is
6845 * highly unlikely, we can live with the residual risk.
6846 */
6847 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6848 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6849 }
6850
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006851 ++vcpu->stat.nmi_injections;
6852 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006853
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006854 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006855 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006856 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006857 return;
6858 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006859
Sheng Yangf08864b2008-05-15 18:23:25 +08006860 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6861 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006862
6863 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006864}
6865
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006866static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6867{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006868 struct vcpu_vmx *vmx = to_vmx(vcpu);
6869 bool masked;
6870
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006871 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006872 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006873 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006874 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006875 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6876 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6877 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006878}
6879
6880static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6881{
6882 struct vcpu_vmx *vmx = to_vmx(vcpu);
6883
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006884 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006885 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6886 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6887 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6888 }
6889 } else {
6890 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6891 if (masked)
6892 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6893 GUEST_INTR_STATE_NMI);
6894 else
6895 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6896 GUEST_INTR_STATE_NMI);
6897 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006898}
6899
Jan Kiszka2505dc92013-04-14 12:12:47 +02006900static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6901{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006902 if (to_vmx(vcpu)->nested.nested_run_pending)
6903 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006904
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006905 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006906 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6907 return 0;
6908
Jan Kiszka2505dc92013-04-14 12:12:47 +02006909 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6910 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6911 | GUEST_INTR_STATE_NMI));
6912}
6913
Gleb Natapov78646122009-03-23 12:12:11 +02006914static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6915{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006916 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6917 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006918 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6919 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006920}
6921
Izik Eiduscbc94022007-10-25 00:29:55 +02006922static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6923{
6924 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006925
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006926 if (enable_unrestricted_guest)
6927 return 0;
6928
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006929 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6930 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006931 if (ret)
6932 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006933 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006934 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006935}
6936
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006937static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6938{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006939 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006940 return 0;
6941}
6942
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006943static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006944{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006945 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006946 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006947 /*
6948 * Update instruction length as we may reinject the exception
6949 * from user space while in guest debugging mode.
6950 */
6951 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6952 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006953 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006954 return false;
6955 /* fall through */
6956 case DB_VECTOR:
6957 if (vcpu->guest_debug &
6958 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6959 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006960 /* fall through */
6961 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006962 case OF_VECTOR:
6963 case BR_VECTOR:
6964 case UD_VECTOR:
6965 case DF_VECTOR:
6966 case SS_VECTOR:
6967 case GP_VECTOR:
6968 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006969 return true;
6970 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006971 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006972 return false;
6973}
6974
6975static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6976 int vec, u32 err_code)
6977{
6978 /*
6979 * Instruction with address size override prefix opcode 0x67
6980 * Cause the #SS fault with 0 error code in VM86 mode.
6981 */
6982 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006983 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006984 if (vcpu->arch.halt_request) {
6985 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006986 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006987 }
6988 return 1;
6989 }
6990 return 0;
6991 }
6992
6993 /*
6994 * Forward all other exceptions that are valid in real mode.
6995 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
6996 * the required debugging infrastructure rework.
6997 */
6998 kvm_queue_exception(vcpu, vec);
6999 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007000}
7001
Andi Kleena0861c02009-06-08 17:37:09 +08007002/*
7003 * Trigger machine check on the host. We assume all the MSRs are already set up
7004 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7005 * We pass a fake environment to the machine check handler because we want
7006 * the guest to be always treated like user space, no matter what context
7007 * it used internally.
7008 */
7009static void kvm_machine_check(void)
7010{
7011#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7012 struct pt_regs regs = {
7013 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7014 .flags = X86_EFLAGS_IF,
7015 };
7016
7017 do_machine_check(&regs, 0);
7018#endif
7019}
7020
Avi Kivity851ba692009-08-24 11:10:17 +03007021static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007022{
7023 /* already handled by vcpu_run */
7024 return 1;
7025}
7026
Avi Kivity851ba692009-08-24 11:10:17 +03007027static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007028{
Avi Kivity1155f762007-11-22 11:30:47 +02007029 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007030 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007031 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007032 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007033 u32 vect_info;
7034 enum emulation_result er;
7035
Avi Kivity1155f762007-11-22 11:30:47 +02007036 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007037 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007038
Andi Kleena0861c02009-06-08 17:37:09 +08007039 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007040 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007041
Jim Mattsonef85b672016-12-12 11:01:37 -08007042 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007043 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007044
Wanpeng Li082d06e2018-04-03 16:28:48 -07007045 if (is_invalid_opcode(intr_info))
7046 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007047
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007049 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007050 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007051
Liran Alon9e869482018-03-12 13:12:51 +02007052 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7053 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007054 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007055 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7056 if (er == EMULATE_USER_EXIT)
7057 return 0;
7058 else if (er != EMULATE_DONE)
7059 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7060 return 1;
7061 }
7062
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007063 /*
7064 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7065 * MMIO, it is better to report an internal error.
7066 * See the comments in vmx_handle_exit.
7067 */
7068 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7069 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7070 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7071 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007072 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007073 vcpu->run->internal.data[0] = vect_info;
7074 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007075 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007076 return 0;
7077 }
7078
Avi Kivity6aa8b732006-12-10 02:21:36 -08007079 if (is_page_fault(intr_info)) {
7080 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007081 /* EPT won't cause page fault directly */
7082 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007083 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007084 }
7085
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007086 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007087
7088 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7089 return handle_rmode_exception(vcpu, ex_no, error_code);
7090
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007091 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007092 case AC_VECTOR:
7093 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7094 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007095 case DB_VECTOR:
7096 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7097 if (!(vcpu->guest_debug &
7098 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007099 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007100 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007101 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007102 skip_emulated_instruction(vcpu);
7103
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007104 kvm_queue_exception(vcpu, DB_VECTOR);
7105 return 1;
7106 }
7107 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7108 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7109 /* fall through */
7110 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01007111 /*
7112 * Update instruction length as we may reinject #BP from
7113 * user space while in guest debugging mode. Reading it for
7114 * #DB as well causes no harm, it is not used in that case.
7115 */
7116 vmx->vcpu.arch.event_exit_inst_len =
7117 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007118 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007119 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007120 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7121 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007122 break;
7123 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007124 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7125 kvm_run->ex.exception = ex_no;
7126 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007127 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007128 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007129 return 0;
7130}
7131
Avi Kivity851ba692009-08-24 11:10:17 +03007132static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007133{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007134 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007135 return 1;
7136}
7137
Avi Kivity851ba692009-08-24 11:10:17 +03007138static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007139{
Avi Kivity851ba692009-08-24 11:10:17 +03007140 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007141 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007142 return 0;
7143}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007144
Avi Kivity851ba692009-08-24 11:10:17 +03007145static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007146{
He, Qingbfdaab02007-09-12 14:18:28 +08007147 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007148 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007149 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007150
He, Qingbfdaab02007-09-12 14:18:28 +08007151 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007152 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007153
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007154 ++vcpu->stat.io_exits;
7155
Sean Christopherson432baf62018-03-08 08:57:26 -08007156 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007157 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007158
7159 port = exit_qualification >> 16;
7160 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007161 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007162
Sean Christophersondca7f122018-03-08 08:57:27 -08007163 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007164}
7165
Ingo Molnar102d8322007-02-19 14:37:47 +02007166static void
7167vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7168{
7169 /*
7170 * Patch in the VMCALL instruction:
7171 */
7172 hypercall[0] = 0x0f;
7173 hypercall[1] = 0x01;
7174 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007175}
7176
Guo Chao0fa06072012-06-28 15:16:19 +08007177/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007178static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7179{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007180 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007181 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7182 unsigned long orig_val = val;
7183
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007184 /*
7185 * We get here when L2 changed cr0 in a way that did not change
7186 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007187 * but did change L0 shadowed bits. So we first calculate the
7188 * effective cr0 value that L1 would like to write into the
7189 * hardware. It consists of the L2-owned bits from the new
7190 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007191 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007192 val = (val & ~vmcs12->cr0_guest_host_mask) |
7193 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7194
David Matlack38991522016-11-29 18:14:08 -08007195 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007196 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007197
7198 if (kvm_set_cr0(vcpu, val))
7199 return 1;
7200 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007201 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007202 } else {
7203 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007204 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007205 return 1;
David Matlack38991522016-11-29 18:14:08 -08007206
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007207 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007208 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007209}
7210
7211static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7212{
7213 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007214 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7215 unsigned long orig_val = val;
7216
7217 /* analogously to handle_set_cr0 */
7218 val = (val & ~vmcs12->cr4_guest_host_mask) |
7219 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7220 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007221 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007222 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007223 return 0;
7224 } else
7225 return kvm_set_cr4(vcpu, val);
7226}
7227
Paolo Bonzini0367f202016-07-12 10:44:55 +02007228static int handle_desc(struct kvm_vcpu *vcpu)
7229{
7230 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007231 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007232}
7233
Avi Kivity851ba692009-08-24 11:10:17 +03007234static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007235{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007236 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237 int cr;
7238 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007239 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007240 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007241
He, Qingbfdaab02007-09-12 14:18:28 +08007242 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007243 cr = exit_qualification & 15;
7244 reg = (exit_qualification >> 8) & 15;
7245 switch ((exit_qualification >> 4) & 3) {
7246 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007247 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007248 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007249 switch (cr) {
7250 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007251 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007252 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007253 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007254 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007255 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007256 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007257 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007258 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007259 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007260 case 8: {
7261 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007262 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007263 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007264 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007265 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007266 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007267 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007268 return ret;
7269 /*
7270 * TODO: we might be squashing a
7271 * KVM_GUESTDBG_SINGLESTEP-triggered
7272 * KVM_EXIT_DEBUG here.
7273 */
Avi Kivity851ba692009-08-24 11:10:17 +03007274 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007275 return 0;
7276 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007277 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007278 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007279 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007280 WARN_ONCE(1, "Guest should always own CR0.TS");
7281 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007282 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007283 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284 case 1: /*mov from cr*/
7285 switch (cr) {
7286 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007287 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007288 val = kvm_read_cr3(vcpu);
7289 kvm_register_write(vcpu, reg, val);
7290 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007291 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007293 val = kvm_get_cr8(vcpu);
7294 kvm_register_write(vcpu, reg, val);
7295 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007296 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007297 }
7298 break;
7299 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007300 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007301 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007302 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007303
Kyle Huey6affcbe2016-11-29 12:40:40 -08007304 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 default:
7306 break;
7307 }
Avi Kivity851ba692009-08-24 11:10:17 +03007308 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007309 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007310 (int)(exit_qualification >> 4) & 3, cr);
7311 return 0;
7312}
7313
Avi Kivity851ba692009-08-24 11:10:17 +03007314static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007315{
He, Qingbfdaab02007-09-12 14:18:28 +08007316 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007317 int dr, dr7, reg;
7318
7319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7320 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7321
7322 /* First, if DR does not exist, trigger UD */
7323 if (!kvm_require_dr(vcpu, dr))
7324 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007325
Jan Kiszkaf2483412010-01-20 18:20:20 +01007326 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007327 if (!kvm_require_cpl(vcpu, 0))
7328 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007329 dr7 = vmcs_readl(GUEST_DR7);
7330 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007331 /*
7332 * As the vm-exit takes precedence over the debug trap, we
7333 * need to emulate the latter, either for the host or the
7334 * guest debugging itself.
7335 */
7336 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007337 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007338 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007339 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007340 vcpu->run->debug.arch.exception = DB_VECTOR;
7341 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007342 return 0;
7343 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007344 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007345 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007346 kvm_queue_exception(vcpu, DB_VECTOR);
7347 return 1;
7348 }
7349 }
7350
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007351 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007352 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7353 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007354
7355 /*
7356 * No more DR vmexits; force a reload of the debug registers
7357 * and reenter on this instruction. The next vmexit will
7358 * retrieve the full state of the debug registers.
7359 */
7360 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7361 return 1;
7362 }
7363
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007364 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7365 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007366 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007367
7368 if (kvm_get_dr(vcpu, dr, &val))
7369 return 1;
7370 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007371 } else
Nadav Amit57773922014-06-18 17:19:23 +03007372 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007373 return 1;
7374
Kyle Huey6affcbe2016-11-29 12:40:40 -08007375 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007376}
7377
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007378static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7379{
7380 return vcpu->arch.dr6;
7381}
7382
7383static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7384{
7385}
7386
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007387static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7388{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007389 get_debugreg(vcpu->arch.db[0], 0);
7390 get_debugreg(vcpu->arch.db[1], 1);
7391 get_debugreg(vcpu->arch.db[2], 2);
7392 get_debugreg(vcpu->arch.db[3], 3);
7393 get_debugreg(vcpu->arch.dr6, 6);
7394 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7395
7396 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007397 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007398}
7399
Gleb Natapov020df072010-04-13 10:05:23 +03007400static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7401{
7402 vmcs_writel(GUEST_DR7, val);
7403}
7404
Avi Kivity851ba692009-08-24 11:10:17 +03007405static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007406{
Kyle Huey6a908b62016-11-29 12:40:37 -08007407 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007408}
7409
Avi Kivity851ba692009-08-24 11:10:17 +03007410static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007411{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007412 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007413 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007414
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007415 msr_info.index = ecx;
7416 msr_info.host_initiated = false;
7417 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007418 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007419 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007420 return 1;
7421 }
7422
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007423 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007424
Avi Kivity6aa8b732006-12-10 02:21:36 -08007425 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007426 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7427 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007428 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007429}
7430
Avi Kivity851ba692009-08-24 11:10:17 +03007431static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007432{
Will Auld8fe8ab42012-11-29 12:42:12 -08007433 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007434 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7435 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7436 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007437
Will Auld8fe8ab42012-11-29 12:42:12 -08007438 msr.data = data;
7439 msr.index = ecx;
7440 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007441 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007442 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007443 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007444 return 1;
7445 }
7446
Avi Kivity59200272010-01-25 19:47:02 +02007447 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007448 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007449}
7450
Avi Kivity851ba692009-08-24 11:10:17 +03007451static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007452{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007453 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007454 return 1;
7455}
7456
Avi Kivity851ba692009-08-24 11:10:17 +03007457static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007458{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007459 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7460 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007461
Avi Kivity3842d132010-07-27 12:30:24 +03007462 kvm_make_request(KVM_REQ_EVENT, vcpu);
7463
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007464 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007465 return 1;
7466}
7467
Avi Kivity851ba692009-08-24 11:10:17 +03007468static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007469{
Avi Kivityd3bef152007-06-05 15:53:05 +03007470 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007471}
7472
Avi Kivity851ba692009-08-24 11:10:17 +03007473static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007474{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007475 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007476}
7477
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007478static int handle_invd(struct kvm_vcpu *vcpu)
7479{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007480 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007481}
7482
Avi Kivity851ba692009-08-24 11:10:17 +03007483static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007484{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007485 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007486
7487 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007488 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007489}
7490
Avi Kivityfee84b02011-11-10 14:57:25 +02007491static int handle_rdpmc(struct kvm_vcpu *vcpu)
7492{
7493 int err;
7494
7495 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007496 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007497}
7498
Avi Kivity851ba692009-08-24 11:10:17 +03007499static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007500{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007501 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007502}
7503
Dexuan Cui2acf9232010-06-10 11:27:12 +08007504static int handle_xsetbv(struct kvm_vcpu *vcpu)
7505{
7506 u64 new_bv = kvm_read_edx_eax(vcpu);
7507 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7508
7509 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007510 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007511 return 1;
7512}
7513
Wanpeng Lif53cd632014-12-02 19:14:58 +08007514static int handle_xsaves(struct kvm_vcpu *vcpu)
7515{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007516 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007517 WARN(1, "this should never happen\n");
7518 return 1;
7519}
7520
7521static int handle_xrstors(struct kvm_vcpu *vcpu)
7522{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007523 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007524 WARN(1, "this should never happen\n");
7525 return 1;
7526}
7527
Avi Kivity851ba692009-08-24 11:10:17 +03007528static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007529{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007530 if (likely(fasteoi)) {
7531 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7532 int access_type, offset;
7533
7534 access_type = exit_qualification & APIC_ACCESS_TYPE;
7535 offset = exit_qualification & APIC_ACCESS_OFFSET;
7536 /*
7537 * Sane guest uses MOV to write EOI, with written value
7538 * not cared. So make a short-circuit here by avoiding
7539 * heavy instruction emulation.
7540 */
7541 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7542 (offset == APIC_EOI)) {
7543 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007544 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007545 }
7546 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007547 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007548}
7549
Yang Zhangc7c9c562013-01-25 10:18:51 +08007550static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7551{
7552 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7553 int vector = exit_qualification & 0xff;
7554
7555 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7556 kvm_apic_set_eoi_accelerated(vcpu, vector);
7557 return 1;
7558}
7559
Yang Zhang83d4c282013-01-25 10:18:49 +08007560static int handle_apic_write(struct kvm_vcpu *vcpu)
7561{
7562 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7563 u32 offset = exit_qualification & 0xfff;
7564
7565 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7566 kvm_apic_write_nodecode(vcpu, offset);
7567 return 1;
7568}
7569
Avi Kivity851ba692009-08-24 11:10:17 +03007570static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007571{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007572 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007573 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007574 bool has_error_code = false;
7575 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007576 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007577 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007578
7579 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007580 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007581 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007582
7583 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7584
7585 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007586 if (reason == TASK_SWITCH_GATE && idt_v) {
7587 switch (type) {
7588 case INTR_TYPE_NMI_INTR:
7589 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007590 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007591 break;
7592 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007593 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007594 kvm_clear_interrupt_queue(vcpu);
7595 break;
7596 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007597 if (vmx->idt_vectoring_info &
7598 VECTORING_INFO_DELIVER_CODE_MASK) {
7599 has_error_code = true;
7600 error_code =
7601 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7602 }
7603 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007604 case INTR_TYPE_SOFT_EXCEPTION:
7605 kvm_clear_exception_queue(vcpu);
7606 break;
7607 default:
7608 break;
7609 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007610 }
Izik Eidus37817f22008-03-24 23:14:53 +02007611 tss_selector = exit_qualification;
7612
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007613 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7614 type != INTR_TYPE_EXT_INTR &&
7615 type != INTR_TYPE_NMI_INTR))
7616 skip_emulated_instruction(vcpu);
7617
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007618 if (kvm_task_switch(vcpu, tss_selector,
7619 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7620 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007621 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7622 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7623 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007624 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007625 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007626
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007627 /*
7628 * TODO: What about debug traps on tss switch?
7629 * Are we supposed to inject them and update dr6?
7630 */
7631
7632 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007633}
7634
Avi Kivity851ba692009-08-24 11:10:17 +03007635static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007636{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007637 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007638 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007639 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007640
Sheng Yangf9c617f2009-03-25 10:08:52 +08007641 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007642
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007643 /*
7644 * EPT violation happened while executing iret from NMI,
7645 * "blocked by NMI" bit has to be set before next VM entry.
7646 * There are errata that may cause this bit to not be set:
7647 * AAK134, BY25.
7648 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007649 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007650 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007651 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007652 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7653
Sheng Yang14394422008-04-28 12:24:45 +08007654 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007655 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007656
Junaid Shahid27959a42016-12-06 16:46:10 -08007657 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007658 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007659 ? PFERR_USER_MASK : 0;
7660 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007661 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007662 ? PFERR_WRITE_MASK : 0;
7663 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007664 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007665 ? PFERR_FETCH_MASK : 0;
7666 /* ept page table entry is present? */
7667 error_code |= (exit_qualification &
7668 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7669 EPT_VIOLATION_EXECUTABLE))
7670 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007671
Paolo Bonzinieebed242016-11-28 14:39:58 +01007672 error_code |= (exit_qualification & 0x100) != 0 ?
7673 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007674
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007675 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007676 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007677}
7678
Avi Kivity851ba692009-08-24 11:10:17 +03007679static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007680{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007681 gpa_t gpa;
7682
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007683 /*
7684 * A nested guest cannot optimize MMIO vmexits, because we have an
7685 * nGPA here instead of the required GPA.
7686 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007687 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007688 if (!is_guest_mode(vcpu) &&
7689 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007690 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007691 /*
7692 * Doing kvm_skip_emulated_instruction() depends on undefined
7693 * behavior: Intel's manual doesn't mandate
7694 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7695 * occurs and while on real hardware it was observed to be set,
7696 * other hypervisors (namely Hyper-V) don't set it, we end up
7697 * advancing IP with some random value. Disable fast mmio when
7698 * running nested and keep it for real hardware in hope that
7699 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7700 */
7701 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7702 return kvm_skip_emulated_instruction(vcpu);
7703 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007704 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007705 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007706 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007707
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007708 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007709}
7710
Avi Kivity851ba692009-08-24 11:10:17 +03007711static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007712{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007713 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007714 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7715 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007716 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007717 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007718
7719 return 1;
7720}
7721
Mohammed Gamal80ced182009-09-01 12:48:18 +02007722static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007723{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007724 struct vcpu_vmx *vmx = to_vmx(vcpu);
7725 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007726 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007727 u32 cpu_exec_ctrl;
7728 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007729 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007730
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007731 /*
7732 * We should never reach the point where we are emulating L2
7733 * due to invalid guest state as that means we incorrectly
7734 * allowed a nested VMEntry with an invalid vmcs12.
7735 */
7736 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7737
Avi Kivity49e9d552010-09-19 14:34:08 +02007738 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7739 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007740
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007741 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007742 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007743 return handle_interrupt_window(&vmx->vcpu);
7744
Radim Krčmář72875d82017-04-26 22:32:19 +02007745 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007746 return 1;
7747
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007748 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007749
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007750 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007751 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007752 ret = 0;
7753 goto out;
7754 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007755
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007756 if (err != EMULATE_DONE)
7757 goto emulation_error;
7758
7759 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7760 vcpu->arch.exception.pending)
7761 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007762
Gleb Natapov8d76c492013-05-08 18:38:44 +03007763 if (vcpu->arch.halt_request) {
7764 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007765 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007766 goto out;
7767 }
7768
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007769 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007770 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007771 if (need_resched())
7772 schedule();
7773 }
7774
Mohammed Gamal80ced182009-09-01 12:48:18 +02007775out:
7776 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007777
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007778emulation_error:
7779 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7780 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7781 vcpu->run->internal.ndata = 0;
7782 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007783}
7784
7785static void grow_ple_window(struct kvm_vcpu *vcpu)
7786{
7787 struct vcpu_vmx *vmx = to_vmx(vcpu);
7788 int old = vmx->ple_window;
7789
Babu Mogerc8e88712018-03-16 16:37:24 -04007790 vmx->ple_window = __grow_ple_window(old, ple_window,
7791 ple_window_grow,
7792 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007793
7794 if (vmx->ple_window != old)
7795 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007796
7797 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007798}
7799
7800static void shrink_ple_window(struct kvm_vcpu *vcpu)
7801{
7802 struct vcpu_vmx *vmx = to_vmx(vcpu);
7803 int old = vmx->ple_window;
7804
Babu Mogerc8e88712018-03-16 16:37:24 -04007805 vmx->ple_window = __shrink_ple_window(old, ple_window,
7806 ple_window_shrink,
7807 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007808
7809 if (vmx->ple_window != old)
7810 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007811
7812 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007813}
7814
7815/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007816 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7817 */
7818static void wakeup_handler(void)
7819{
7820 struct kvm_vcpu *vcpu;
7821 int cpu = smp_processor_id();
7822
7823 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7824 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7825 blocked_vcpu_list) {
7826 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7827
7828 if (pi_test_on(pi_desc) == 1)
7829 kvm_vcpu_kick(vcpu);
7830 }
7831 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7832}
7833
Peng Haoe01bca22018-04-07 05:47:32 +08007834static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007835{
7836 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7837 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7838 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7839 0ull, VMX_EPT_EXECUTABLE_MASK,
7840 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007841 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007842
7843 ept_set_mmio_spte_mask();
7844 kvm_enable_tdp();
7845}
7846
Tiejun Chenf2c76482014-10-28 10:14:47 +08007847static __init int hardware_setup(void)
7848{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007849 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007850 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007851
7852 rdmsrl_safe(MSR_EFER, &host_efer);
7853
7854 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7855 kvm_define_shared_msr(i, vmx_msr_index[i]);
7856
Radim Krčmář23611332016-09-29 22:41:33 +02007857 for (i = 0; i < VMX_BITMAP_NR; i++) {
7858 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7859 if (!vmx_bitmap[i])
7860 goto out;
7861 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007862
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007863 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7864 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7865
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007866 if (setup_vmcs_config(&vmcs_config) < 0) {
7867 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007868 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007869 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007870
7871 if (boot_cpu_has(X86_FEATURE_NX))
7872 kvm_enable_efer_bits(EFER_NX);
7873
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007874 if (boot_cpu_has(X86_FEATURE_MPX)) {
7875 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7876 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7877 }
7878
Wanpeng Li08d839c2017-03-23 05:30:08 -07007879 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7880 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007881 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007882
Tiejun Chenf2c76482014-10-28 10:14:47 +08007883 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007884 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007885 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007886 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007887 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007888
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007889 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007890 enable_ept_ad_bits = 0;
7891
Wanpeng Li8ad81822017-10-09 15:51:53 -07007892 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007893 enable_unrestricted_guest = 0;
7894
Paolo Bonziniad15a292015-01-30 16:18:49 +01007895 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007896 flexpriority_enabled = 0;
7897
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007898 if (!cpu_has_virtual_nmis())
7899 enable_vnmi = 0;
7900
Paolo Bonziniad15a292015-01-30 16:18:49 +01007901 /*
7902 * set_apic_access_page_addr() is used to reload apic access
7903 * page upon invalidation. No need to do anything if not
7904 * using the APIC_ACCESS_ADDR VMCS field.
7905 */
7906 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007907 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007908
7909 if (!cpu_has_vmx_tpr_shadow())
7910 kvm_x86_ops->update_cr8_intercept = NULL;
7911
7912 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7913 kvm_disable_largepages();
7914
Tianyu Lan877ad952018-07-19 08:40:23 +00007915#if IS_ENABLED(CONFIG_HYPERV)
7916 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7917 && enable_ept)
7918 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7919#endif
7920
Wanpeng Li0f107682017-09-28 18:06:24 -07007921 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007922 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007923 ple_window = 0;
7924 ple_window_grow = 0;
7925 ple_window_max = 0;
7926 ple_window_shrink = 0;
7927 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007928
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007929 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007930 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007931 kvm_x86_ops->sync_pir_to_irr = NULL;
7932 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007933
Haozhong Zhang64903d62015-10-20 15:39:09 +08007934 if (cpu_has_vmx_tsc_scaling()) {
7935 kvm_has_tsc_control = true;
7936 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7937 kvm_tsc_scaling_ratio_frac_bits = 48;
7938 }
7939
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007940 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7941
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007942 if (enable_ept)
7943 vmx_enable_tdp();
7944 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007945 kvm_disable_tdp();
7946
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007947 if (!nested) {
7948 kvm_x86_ops->get_nested_state = NULL;
7949 kvm_x86_ops->set_nested_state = NULL;
7950 }
7951
Kai Huang843e4332015-01-28 10:54:28 +08007952 /*
7953 * Only enable PML when hardware supports PML feature, and both EPT
7954 * and EPT A/D bit features are enabled -- PML depends on them to work.
7955 */
7956 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7957 enable_pml = 0;
7958
7959 if (!enable_pml) {
7960 kvm_x86_ops->slot_enable_log_dirty = NULL;
7961 kvm_x86_ops->slot_disable_log_dirty = NULL;
7962 kvm_x86_ops->flush_log_dirty = NULL;
7963 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7964 }
7965
Sean Christophersond264ee02018-08-27 15:21:12 -07007966 if (!cpu_has_vmx_preemption_timer())
7967 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7968
Yunhong Jiang64672c92016-06-13 14:19:59 -07007969 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7970 u64 vmx_msr;
7971
7972 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7973 cpu_preemption_timer_multi =
7974 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7975 } else {
7976 kvm_x86_ops->set_hv_timer = NULL;
7977 kvm_x86_ops->cancel_hv_timer = NULL;
7978 }
7979
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007980 if (!cpu_has_vmx_shadow_vmcs())
7981 enable_shadow_vmcs = 0;
7982 if (enable_shadow_vmcs)
7983 init_vmcs_shadow_fields();
7984
Feng Wubf9f6ac2015-09-18 22:29:55 +08007985 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007986 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007987
Ashok Rajc45dcc72016-06-22 14:59:56 +08007988 kvm_mce_cap_supported |= MCG_LMCE_P;
7989
Tiejun Chenf2c76482014-10-28 10:14:47 +08007990 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007991
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007992out:
Radim Krčmář23611332016-09-29 22:41:33 +02007993 for (i = 0; i < VMX_BITMAP_NR; i++)
7994 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007995
7996 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007997}
7998
7999static __exit void hardware_unsetup(void)
8000{
Radim Krčmář23611332016-09-29 22:41:33 +02008001 int i;
8002
8003 for (i = 0; i < VMX_BITMAP_NR; i++)
8004 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008005
Tiejun Chenf2c76482014-10-28 10:14:47 +08008006 free_kvm_area();
8007}
8008
Avi Kivity6aa8b732006-12-10 02:21:36 -08008009/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008010 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8011 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8012 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008013static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008014{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008015 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008016 grow_ple_window(vcpu);
8017
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008018 /*
8019 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8020 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8021 * never set PAUSE_EXITING and just set PLE if supported,
8022 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8023 */
8024 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008025 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008026}
8027
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008028static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008029{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008030 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008031}
8032
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008033static int handle_mwait(struct kvm_vcpu *vcpu)
8034{
8035 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8036 return handle_nop(vcpu);
8037}
8038
Jim Mattson45ec3682017-08-23 16:32:04 -07008039static int handle_invalid_op(struct kvm_vcpu *vcpu)
8040{
8041 kvm_queue_exception(vcpu, UD_VECTOR);
8042 return 1;
8043}
8044
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008045static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8046{
8047 return 1;
8048}
8049
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008050static int handle_monitor(struct kvm_vcpu *vcpu)
8051{
8052 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8053 return handle_nop(vcpu);
8054}
8055
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008056/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008057 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8058 * set the success or error code of an emulated VMX instruction, as specified
8059 * by Vol 2B, VMX Instruction Reference, "Conventions".
8060 */
8061static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8062{
8063 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8064 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8065 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8066}
8067
8068static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8069{
8070 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8071 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8072 X86_EFLAGS_SF | X86_EFLAGS_OF))
8073 | X86_EFLAGS_CF);
8074}
8075
Abel Gordon145c28d2013-04-18 14:36:55 +03008076static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008077 u32 vm_instruction_error)
8078{
8079 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8080 /*
8081 * failValid writes the error number to the current VMCS, which
8082 * can't be done there isn't a current VMCS.
8083 */
8084 nested_vmx_failInvalid(vcpu);
8085 return;
8086 }
8087 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8088 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8089 X86_EFLAGS_SF | X86_EFLAGS_OF))
8090 | X86_EFLAGS_ZF);
8091 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8092 /*
8093 * We don't need to force a shadow sync because
8094 * VM_INSTRUCTION_ERROR is not shadowed
8095 */
8096}
Abel Gordon145c28d2013-04-18 14:36:55 +03008097
Wincy Vanff651cb2014-12-11 08:52:58 +03008098static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8099{
8100 /* TODO: not to reset guest simply here. */
8101 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008102 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008103}
8104
Jan Kiszkaf41245002014-03-07 20:03:13 +01008105static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8106{
8107 struct vcpu_vmx *vmx =
8108 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8109
8110 vmx->nested.preemption_timer_expired = true;
8111 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8112 kvm_vcpu_kick(&vmx->vcpu);
8113
8114 return HRTIMER_NORESTART;
8115}
8116
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008117/*
Bandan Das19677e32014-05-06 02:19:15 -04008118 * Decode the memory-address operand of a vmx instruction, as recorded on an
8119 * exit caused by such an instruction (run by a guest hypervisor).
8120 * On success, returns 0. When the operand is invalid, returns 1 and throws
8121 * #UD or #GP.
8122 */
8123static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8124 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008125 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008126{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008127 gva_t off;
8128 bool exn;
8129 struct kvm_segment s;
8130
Bandan Das19677e32014-05-06 02:19:15 -04008131 /*
8132 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8133 * Execution", on an exit, vmx_instruction_info holds most of the
8134 * addressing components of the operand. Only the displacement part
8135 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8136 * For how an actual address is calculated from all these components,
8137 * refer to Vol. 1, "Operand Addressing".
8138 */
8139 int scaling = vmx_instruction_info & 3;
8140 int addr_size = (vmx_instruction_info >> 7) & 7;
8141 bool is_reg = vmx_instruction_info & (1u << 10);
8142 int seg_reg = (vmx_instruction_info >> 15) & 7;
8143 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8144 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8145 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8146 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8147
8148 if (is_reg) {
8149 kvm_queue_exception(vcpu, UD_VECTOR);
8150 return 1;
8151 }
8152
8153 /* Addr = segment_base + offset */
8154 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008155 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008156 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008157 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008158 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008159 off += kvm_register_read(vcpu, index_reg)<<scaling;
8160 vmx_get_segment(vcpu, &s, seg_reg);
8161 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008162
8163 if (addr_size == 1) /* 32 bit */
8164 *ret &= 0xffffffff;
8165
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008166 /* Checks for #GP/#SS exceptions. */
8167 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008168 if (is_long_mode(vcpu)) {
8169 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8170 * non-canonical form. This is the only check on the memory
8171 * destination for long mode!
8172 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008173 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008174 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008175 /* Protected mode: apply checks for segment validity in the
8176 * following order:
8177 * - segment type check (#GP(0) may be thrown)
8178 * - usability check (#GP(0)/#SS(0))
8179 * - limit check (#GP(0)/#SS(0))
8180 */
8181 if (wr)
8182 /* #GP(0) if the destination operand is located in a
8183 * read-only data segment or any code segment.
8184 */
8185 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8186 else
8187 /* #GP(0) if the source operand is located in an
8188 * execute-only code segment
8189 */
8190 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008191 if (exn) {
8192 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8193 return 1;
8194 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008195 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8196 */
8197 exn = (s.unusable != 0);
8198 /* Protected mode: #GP(0)/#SS(0) if the memory
8199 * operand is outside the segment limit.
8200 */
8201 exn = exn || (off + sizeof(u64) > s.limit);
8202 }
8203 if (exn) {
8204 kvm_queue_exception_e(vcpu,
8205 seg_reg == VCPU_SREG_SS ?
8206 SS_VECTOR : GP_VECTOR,
8207 0);
8208 return 1;
8209 }
8210
Bandan Das19677e32014-05-06 02:19:15 -04008211 return 0;
8212}
8213
Radim Krčmářcbf71272017-05-19 15:48:51 +02008214static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008215{
8216 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008217 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008218
8219 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008220 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008221 return 1;
8222
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008223 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008224 kvm_inject_page_fault(vcpu, &e);
8225 return 1;
8226 }
8227
Bandan Das3573e222014-05-06 02:19:16 -04008228 return 0;
8229}
8230
Liran Alonabfc52c2018-06-23 02:35:13 +03008231/*
8232 * Allocate a shadow VMCS and associate it with the currently loaded
8233 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8234 * VMCS is also VMCLEARed, so that it is ready for use.
8235 */
8236static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8237{
8238 struct vcpu_vmx *vmx = to_vmx(vcpu);
8239 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8240
8241 /*
8242 * We should allocate a shadow vmcs for vmcs01 only when L1
8243 * executes VMXON and free it when L1 executes VMXOFF.
8244 * As it is invalid to execute VMXON twice, we shouldn't reach
8245 * here when vmcs01 already have an allocated shadow vmcs.
8246 */
8247 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8248
8249 if (!loaded_vmcs->shadow_vmcs) {
8250 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8251 if (loaded_vmcs->shadow_vmcs)
8252 vmcs_clear(loaded_vmcs->shadow_vmcs);
8253 }
8254 return loaded_vmcs->shadow_vmcs;
8255}
8256
Jim Mattsone29acc52016-11-30 12:03:43 -08008257static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8258{
8259 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008260 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008261
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008262 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8263 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008264 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008265
8266 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8267 if (!vmx->nested.cached_vmcs12)
8268 goto out_cached_vmcs12;
8269
Liran Alon61ada742018-06-23 02:35:08 +03008270 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8271 if (!vmx->nested.cached_shadow_vmcs12)
8272 goto out_cached_shadow_vmcs12;
8273
Liran Alonabfc52c2018-06-23 02:35:13 +03008274 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8275 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008276
Jim Mattsone29acc52016-11-30 12:03:43 -08008277 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8278 HRTIMER_MODE_REL_PINNED);
8279 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8280
Roman Kagan63aff652018-07-19 21:59:07 +03008281 vmx->nested.vpid02 = allocate_vpid();
8282
Jim Mattsone29acc52016-11-30 12:03:43 -08008283 vmx->nested.vmxon = true;
8284 return 0;
8285
8286out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008287 kfree(vmx->nested.cached_shadow_vmcs12);
8288
8289out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008290 kfree(vmx->nested.cached_vmcs12);
8291
8292out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008293 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008294
Jim Mattsonde3a0022017-11-27 17:22:25 -06008295out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008296 return -ENOMEM;
8297}
8298
Bandan Das3573e222014-05-06 02:19:16 -04008299/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008300 * Emulate the VMXON instruction.
8301 * Currently, we just remember that VMX is active, and do not save or even
8302 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8303 * do not currently need to store anything in that guest-allocated memory
8304 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8305 * argument is different from the VMXON pointer (which the spec says they do).
8306 */
8307static int handle_vmon(struct kvm_vcpu *vcpu)
8308{
Jim Mattsone29acc52016-11-30 12:03:43 -08008309 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008310 gpa_t vmptr;
8311 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008312 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008313 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8314 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008315
Jim Mattson70f3aac2017-04-26 08:53:46 -07008316 /*
8317 * The Intel VMX Instruction Reference lists a bunch of bits that are
8318 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8319 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8320 * Otherwise, we should fail with #UD. But most faulting conditions
8321 * have already been checked by hardware, prior to the VM-exit for
8322 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8323 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008324 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008325 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008326 kvm_queue_exception(vcpu, UD_VECTOR);
8327 return 1;
8328 }
8329
Felix Wilhelm727ba742018-06-11 09:43:44 +02008330 /* CPL=0 must be checked manually. */
8331 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008332 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008333 return 1;
8334 }
8335
Abel Gordon145c28d2013-04-18 14:36:55 +03008336 if (vmx->nested.vmxon) {
8337 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008338 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008339 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008340
Haozhong Zhang3b840802016-06-22 14:59:54 +08008341 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008342 != VMXON_NEEDED_FEATURES) {
8343 kvm_inject_gp(vcpu, 0);
8344 return 1;
8345 }
8346
Radim Krčmářcbf71272017-05-19 15:48:51 +02008347 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008348 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008349
8350 /*
8351 * SDM 3: 24.11.5
8352 * The first 4 bytes of VMXON region contain the supported
8353 * VMCS revision identifier
8354 *
8355 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8356 * which replaces physical address width with 32
8357 */
8358 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8359 nested_vmx_failInvalid(vcpu);
8360 return kvm_skip_emulated_instruction(vcpu);
8361 }
8362
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008363 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8364 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008365 nested_vmx_failInvalid(vcpu);
8366 return kvm_skip_emulated_instruction(vcpu);
8367 }
8368 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8369 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008370 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008371 nested_vmx_failInvalid(vcpu);
8372 return kvm_skip_emulated_instruction(vcpu);
8373 }
8374 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008375 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008376
8377 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008378 ret = enter_vmx_operation(vcpu);
8379 if (ret)
8380 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008381
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008382 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008383 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008384}
8385
8386/*
8387 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8388 * for running VMX instructions (except VMXON, whose prerequisites are
8389 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008390 * Note that many of these exceptions have priority over VM exits, so they
8391 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008392 */
8393static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8394{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008395 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008396 kvm_queue_exception(vcpu, UD_VECTOR);
8397 return 0;
8398 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008399
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008400 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008401 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008402 return 0;
8403 }
8404
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008405 return 1;
8406}
8407
David Matlack8ca44e82017-08-01 14:00:39 -07008408static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8409{
8410 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8411 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8412}
8413
Abel Gordone7953d72013-04-18 14:37:55 +03008414static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8415{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008416 if (vmx->nested.current_vmptr == -1ull)
8417 return;
8418
Abel Gordon012f83c2013-04-18 14:39:25 +03008419 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008420 /* copy to memory all shadowed fields in case
8421 they were modified */
8422 copy_shadow_to_vmcs12(vmx);
8423 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008424 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008425 }
Wincy Van705699a2015-02-03 23:58:17 +08008426 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008427
8428 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008429 kvm_vcpu_write_guest_page(&vmx->vcpu,
8430 vmx->nested.current_vmptr >> PAGE_SHIFT,
8431 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008432
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008433 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008434}
8435
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008436/*
8437 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8438 * just stops using VMX.
8439 */
8440static void free_nested(struct vcpu_vmx *vmx)
8441{
Wanpeng Lib7455822017-11-22 14:04:00 -08008442 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008443 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008444
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008445 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008446 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008447 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008448 vmx->nested.posted_intr_nv = -1;
8449 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008450 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008451 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008452 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8453 free_vmcs(vmx->vmcs01.shadow_vmcs);
8454 vmx->vmcs01.shadow_vmcs = NULL;
8455 }
David Matlack4f2777b2016-07-13 17:16:37 -07008456 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008457 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008458 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008459 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008460 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008461 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008462 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008463 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008464 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008465 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008466 }
Wincy Van705699a2015-02-03 23:58:17 +08008467 if (vmx->nested.pi_desc_page) {
8468 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008469 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008470 vmx->nested.pi_desc_page = NULL;
8471 vmx->nested.pi_desc = NULL;
8472 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008473
Jim Mattsonde3a0022017-11-27 17:22:25 -06008474 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008475}
8476
8477/* Emulate the VMXOFF instruction */
8478static int handle_vmoff(struct kvm_vcpu *vcpu)
8479{
8480 if (!nested_vmx_check_permission(vcpu))
8481 return 1;
8482 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008483 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008484 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008485}
8486
Nadav Har'El27d6c862011-05-25 23:06:59 +03008487/* Emulate the VMCLEAR instruction */
8488static int handle_vmclear(struct kvm_vcpu *vcpu)
8489{
8490 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008491 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008492 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008493
8494 if (!nested_vmx_check_permission(vcpu))
8495 return 1;
8496
Radim Krčmářcbf71272017-05-19 15:48:51 +02008497 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008498 return 1;
8499
Radim Krčmářcbf71272017-05-19 15:48:51 +02008500 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8501 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8502 return kvm_skip_emulated_instruction(vcpu);
8503 }
8504
8505 if (vmptr == vmx->nested.vmxon_ptr) {
8506 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8507 return kvm_skip_emulated_instruction(vcpu);
8508 }
8509
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008510 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008511 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008512
Jim Mattson587d7e722017-03-02 12:41:48 -08008513 kvm_vcpu_write_guest(vcpu,
8514 vmptr + offsetof(struct vmcs12, launch_state),
8515 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008516
Nadav Har'El27d6c862011-05-25 23:06:59 +03008517 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008518 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008519}
8520
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008521static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8522
8523/* Emulate the VMLAUNCH instruction */
8524static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8525{
8526 return nested_vmx_run(vcpu, true);
8527}
8528
8529/* Emulate the VMRESUME instruction */
8530static int handle_vmresume(struct kvm_vcpu *vcpu)
8531{
8532
8533 return nested_vmx_run(vcpu, false);
8534}
8535
Nadav Har'El49f705c2011-05-25 23:08:30 +03008536/*
8537 * Read a vmcs12 field. Since these can have varying lengths and we return
8538 * one type, we chose the biggest type (u64) and zero-extend the return value
8539 * to that size. Note that the caller, handle_vmread, might need to use only
8540 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8541 * 64-bit fields are to be returned).
8542 */
Liran Alone2536742018-06-23 02:35:02 +03008543static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008544 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008545{
8546 short offset = vmcs_field_to_offset(field);
8547 char *p;
8548
8549 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008550 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008551
Liran Alone2536742018-06-23 02:35:02 +03008552 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553
Jim Mattsond37f4262017-12-22 12:12:16 -08008554 switch (vmcs_field_width(field)) {
8555 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008556 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008557 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008558 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008560 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008561 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008562 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008563 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008564 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008565 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008566 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 WARN_ON(1);
8569 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 }
8571}
8572
Abel Gordon20b97fe2013-04-18 14:36:25 +03008573
Liran Alone2536742018-06-23 02:35:02 +03008574static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008575 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008576 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008577 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008578 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008579 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008580
Jim Mattsond37f4262017-12-22 12:12:16 -08008581 switch (vmcs_field_width(field)) {
8582 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008583 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008584 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008585 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008588 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008589 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008590 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008591 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008592 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008593 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 WARN_ON(1);
8596 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 }
8598
8599}
8600
Jim Mattsonf4160e42018-05-29 09:11:33 -07008601/*
8602 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8603 * they have been modified by the L1 guest. Note that the "read-only"
8604 * VM-exit information fields are actually writable if the vCPU is
8605 * configured to support "VMWRITE to any supported field in the VMCS."
8606 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008607static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8608{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008609 const u16 *fields[] = {
8610 shadow_read_write_fields,
8611 shadow_read_only_fields
8612 };
8613 const int max_fields[] = {
8614 max_shadow_read_write_fields,
8615 max_shadow_read_only_fields
8616 };
8617 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008618 unsigned long field;
8619 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008620 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008621
Jan Kiszka282da872014-10-08 18:05:39 +02008622 preempt_disable();
8623
Abel Gordon16f5b902013-04-18 14:38:25 +03008624 vmcs_load(shadow_vmcs);
8625
Jim Mattsonf4160e42018-05-29 09:11:33 -07008626 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8627 for (i = 0; i < max_fields[q]; i++) {
8628 field = fields[q][i];
8629 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008630 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008631 }
8632 /*
8633 * Skip the VM-exit information fields if they are read-only.
8634 */
8635 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8636 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008637 }
8638
8639 vmcs_clear(shadow_vmcs);
8640 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008641
8642 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008643}
8644
Abel Gordonc3114422013-04-18 14:38:55 +03008645static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8646{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008647 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008648 shadow_read_write_fields,
8649 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008650 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008651 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008652 max_shadow_read_write_fields,
8653 max_shadow_read_only_fields
8654 };
8655 int i, q;
8656 unsigned long field;
8657 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008658 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008659
8660 vmcs_load(shadow_vmcs);
8661
Mathias Krausec2bae892013-06-26 20:36:21 +02008662 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008663 for (i = 0; i < max_fields[q]; i++) {
8664 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008665 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008666 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008667 }
8668 }
8669
8670 vmcs_clear(shadow_vmcs);
8671 vmcs_load(vmx->loaded_vmcs->vmcs);
8672}
8673
Nadav Har'El49f705c2011-05-25 23:08:30 +03008674/*
8675 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8676 * used before) all generate the same failure when it is missing.
8677 */
8678static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8679{
8680 struct vcpu_vmx *vmx = to_vmx(vcpu);
8681 if (vmx->nested.current_vmptr == -1ull) {
8682 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008683 return 0;
8684 }
8685 return 1;
8686}
8687
8688static int handle_vmread(struct kvm_vcpu *vcpu)
8689{
8690 unsigned long field;
8691 u64 field_value;
8692 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8693 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8694 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008695 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008696
Kyle Hueyeb277562016-11-29 12:40:39 -08008697 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008698 return 1;
8699
Kyle Huey6affcbe2016-11-29 12:40:40 -08008700 if (!nested_vmx_check_vmcs12(vcpu))
8701 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008702
Liran Alon6d894f42018-06-23 02:35:09 +03008703 if (!is_guest_mode(vcpu))
8704 vmcs12 = get_vmcs12(vcpu);
8705 else {
8706 /*
8707 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8708 * to shadowed-field sets the ALU flags for VMfailInvalid.
8709 */
8710 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8711 nested_vmx_failInvalid(vcpu);
8712 return kvm_skip_emulated_instruction(vcpu);
8713 }
8714 vmcs12 = get_shadow_vmcs12(vcpu);
8715 }
8716
Nadav Har'El49f705c2011-05-25 23:08:30 +03008717 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008718 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008719 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008720 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008721 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008722 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008723 }
8724 /*
8725 * Now copy part of this value to register or memory, as requested.
8726 * Note that the number of bits actually copied is 32 or 64 depending
8727 * on the guest's mode (32 or 64 bit), not on the given field's length.
8728 */
8729 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008730 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008731 field_value);
8732 } else {
8733 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008734 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008735 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008736 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008737 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8738 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008739 }
8740
8741 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008742 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008743}
8744
8745
8746static int handle_vmwrite(struct kvm_vcpu *vcpu)
8747{
8748 unsigned long field;
8749 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008750 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008751 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8752 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008753
Nadav Har'El49f705c2011-05-25 23:08:30 +03008754 /* The value to write might be 32 or 64 bits, depending on L1's long
8755 * mode, and eventually we need to write that into a field of several
8756 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008757 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008758 * bits into the vmcs12 field.
8759 */
8760 u64 field_value = 0;
8761 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008762 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008763
Kyle Hueyeb277562016-11-29 12:40:39 -08008764 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008765 return 1;
8766
Kyle Huey6affcbe2016-11-29 12:40:40 -08008767 if (!nested_vmx_check_vmcs12(vcpu))
8768 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008769
Nadav Har'El49f705c2011-05-25 23:08:30 +03008770 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008771 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008772 (((vmx_instruction_info) >> 3) & 0xf));
8773 else {
8774 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008775 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008776 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008777 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8778 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008779 kvm_inject_page_fault(vcpu, &e);
8780 return 1;
8781 }
8782 }
8783
8784
Nadav Amit27e6fb52014-06-18 17:19:26 +03008785 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008786 /*
8787 * If the vCPU supports "VMWRITE to any supported field in the
8788 * VMCS," then the "read-only" fields are actually read/write.
8789 */
8790 if (vmcs_field_readonly(field) &&
8791 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008792 nested_vmx_failValid(vcpu,
8793 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008794 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008795 }
8796
Liran Alon6d894f42018-06-23 02:35:09 +03008797 if (!is_guest_mode(vcpu))
8798 vmcs12 = get_vmcs12(vcpu);
8799 else {
8800 /*
8801 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8802 * to shadowed-field sets the ALU flags for VMfailInvalid.
8803 */
8804 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8805 nested_vmx_failInvalid(vcpu);
8806 return kvm_skip_emulated_instruction(vcpu);
8807 }
8808 vmcs12 = get_shadow_vmcs12(vcpu);
8809
8810 }
8811
8812 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008813 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008814 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008815 }
8816
Liran Alon6d894f42018-06-23 02:35:09 +03008817 /*
8818 * Do not track vmcs12 dirty-state if in guest-mode
8819 * as we actually dirty shadow vmcs12 instead of vmcs12.
8820 */
8821 if (!is_guest_mode(vcpu)) {
8822 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008823#define SHADOW_FIELD_RW(x) case x:
8824#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008825 /*
8826 * The fields that can be updated by L1 without a vmexit are
8827 * always updated in the vmcs02, the others go down the slow
8828 * path of prepare_vmcs02.
8829 */
8830 break;
8831 default:
8832 vmx->nested.dirty_vmcs12 = true;
8833 break;
8834 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008835 }
8836
Nadav Har'El49f705c2011-05-25 23:08:30 +03008837 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008838 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008839}
8840
Jim Mattsona8bc2842016-11-30 12:03:44 -08008841static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8842{
8843 vmx->nested.current_vmptr = vmptr;
8844 if (enable_shadow_vmcs) {
8845 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8846 SECONDARY_EXEC_SHADOW_VMCS);
8847 vmcs_write64(VMCS_LINK_POINTER,
8848 __pa(vmx->vmcs01.shadow_vmcs));
8849 vmx->nested.sync_shadow_vmcs = true;
8850 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008851 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008852}
8853
Nadav Har'El63846662011-05-25 23:07:29 +03008854/* Emulate the VMPTRLD instruction */
8855static int handle_vmptrld(struct kvm_vcpu *vcpu)
8856{
8857 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008858 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008859
8860 if (!nested_vmx_check_permission(vcpu))
8861 return 1;
8862
Radim Krčmářcbf71272017-05-19 15:48:51 +02008863 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008864 return 1;
8865
Radim Krčmářcbf71272017-05-19 15:48:51 +02008866 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8867 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8868 return kvm_skip_emulated_instruction(vcpu);
8869 }
8870
8871 if (vmptr == vmx->nested.vmxon_ptr) {
8872 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8873 return kvm_skip_emulated_instruction(vcpu);
8874 }
8875
Nadav Har'El63846662011-05-25 23:07:29 +03008876 if (vmx->nested.current_vmptr != vmptr) {
8877 struct vmcs12 *new_vmcs12;
8878 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008879 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8880 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008881 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008882 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008883 }
8884 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008885 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008886 (new_vmcs12->hdr.shadow_vmcs &&
8887 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008888 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008889 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008890 nested_vmx_failValid(vcpu,
8891 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008892 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008893 }
Nadav Har'El63846662011-05-25 23:07:29 +03008894
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008895 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008896 /*
8897 * Load VMCS12 from guest memory since it is not already
8898 * cached.
8899 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008900 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8901 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008902 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008903
Jim Mattsona8bc2842016-11-30 12:03:44 -08008904 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008905 }
8906
8907 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008908 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008909}
8910
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008911/* Emulate the VMPTRST instruction */
8912static int handle_vmptrst(struct kvm_vcpu *vcpu)
8913{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008914 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8915 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8916 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008917 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008918 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008919
8920 if (!nested_vmx_check_permission(vcpu))
8921 return 1;
8922
Sean Christopherson0a06d422018-07-19 10:31:00 -07008923 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008924 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008925 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008926 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8927 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008928 kvm_inject_page_fault(vcpu, &e);
8929 return 1;
8930 }
8931 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008932 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008933}
8934
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008935/* Emulate the INVEPT instruction */
8936static int handle_invept(struct kvm_vcpu *vcpu)
8937{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008938 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008939 u32 vmx_instruction_info, types;
8940 unsigned long type;
8941 gva_t gva;
8942 struct x86_exception e;
8943 struct {
8944 u64 eptp, gpa;
8945 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008946
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008947 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008948 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008949 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008950 kvm_queue_exception(vcpu, UD_VECTOR);
8951 return 1;
8952 }
8953
8954 if (!nested_vmx_check_permission(vcpu))
8955 return 1;
8956
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008957 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008958 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008959
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008960 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008961
Jim Mattson85c856b2016-10-26 08:38:38 -07008962 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008963 nested_vmx_failValid(vcpu,
8964 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008965 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008966 }
8967
8968 /* According to the Intel VMX instruction reference, the memory
8969 * operand is read even if it isn't needed (e.g., for type==global)
8970 */
8971 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008972 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008973 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008974 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008975 kvm_inject_page_fault(vcpu, &e);
8976 return 1;
8977 }
8978
8979 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008980 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008981 /*
8982 * TODO: track mappings and invalidate
8983 * single context requests appropriately
8984 */
8985 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008986 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008987 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008988 nested_vmx_succeed(vcpu);
8989 break;
8990 default:
8991 BUG_ON(1);
8992 break;
8993 }
8994
Kyle Huey6affcbe2016-11-29 12:40:40 -08008995 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008996}
8997
Liran Alon3d5bdae2018-10-08 23:42:18 +03008998static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
8999{
9000 struct vcpu_vmx *vmx = to_vmx(vcpu);
9001
9002 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9003}
9004
Petr Matouseka642fc32014-09-23 20:22:30 +02009005static int handle_invvpid(struct kvm_vcpu *vcpu)
9006{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009007 struct vcpu_vmx *vmx = to_vmx(vcpu);
9008 u32 vmx_instruction_info;
9009 unsigned long type, types;
9010 gva_t gva;
9011 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009012 struct {
9013 u64 vpid;
9014 u64 gla;
9015 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009016 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009017
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009018 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009019 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009020 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009021 kvm_queue_exception(vcpu, UD_VECTOR);
9022 return 1;
9023 }
9024
9025 if (!nested_vmx_check_permission(vcpu))
9026 return 1;
9027
9028 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9029 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9030
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009031 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009032 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009033
Jim Mattson85c856b2016-10-26 08:38:38 -07009034 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009035 nested_vmx_failValid(vcpu,
9036 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009037 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009038 }
9039
9040 /* according to the intel vmx instruction reference, the memory
9041 * operand is read even if it isn't needed (e.g., for type==global)
9042 */
9043 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9044 vmx_instruction_info, false, &gva))
9045 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009046 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009047 kvm_inject_page_fault(vcpu, &e);
9048 return 1;
9049 }
Jim Mattson40352602017-06-28 09:37:37 -07009050 if (operand.vpid >> 16) {
9051 nested_vmx_failValid(vcpu,
9052 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9053 return kvm_skip_emulated_instruction(vcpu);
9054 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009055
Liran Alon3d5bdae2018-10-08 23:42:18 +03009056 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009057 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009058 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009059 if (!operand.vpid ||
9060 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009061 nested_vmx_failValid(vcpu,
9062 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9063 return kvm_skip_emulated_instruction(vcpu);
9064 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009065 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009066 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009067 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009068 } else
Liran Alon327c0722018-10-08 23:42:19 +03009069 __vmx_flush_tlb(vcpu, vpid02, false);
Liran Aloncd9a4912018-05-22 17:16:15 +03009070 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009071 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009072 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009073 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009074 nested_vmx_failValid(vcpu,
9075 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009076 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009077 }
Liran Alon327c0722018-10-08 23:42:19 +03009078 __vmx_flush_tlb(vcpu, vpid02, false);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009079 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009080 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon327c0722018-10-08 23:42:19 +03009081 __vmx_flush_tlb(vcpu, vpid02, false);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009082 break;
9083 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009084 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009085 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009086 }
9087
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009088 nested_vmx_succeed(vcpu);
9089
Kyle Huey6affcbe2016-11-29 12:40:40 -08009090 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009091}
9092
Junaid Shahideb4b2482018-06-27 14:59:14 -07009093static int handle_invpcid(struct kvm_vcpu *vcpu)
9094{
9095 u32 vmx_instruction_info;
9096 unsigned long type;
9097 bool pcid_enabled;
9098 gva_t gva;
9099 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009100 unsigned i;
9101 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009102 struct {
9103 u64 pcid;
9104 u64 gla;
9105 } operand;
9106
9107 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9108 kvm_queue_exception(vcpu, UD_VECTOR);
9109 return 1;
9110 }
9111
9112 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9113 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9114
9115 if (type > 3) {
9116 kvm_inject_gp(vcpu, 0);
9117 return 1;
9118 }
9119
9120 /* According to the Intel instruction reference, the memory operand
9121 * is read even if it isn't needed (e.g., for type==all)
9122 */
9123 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9124 vmx_instruction_info, false, &gva))
9125 return 1;
9126
9127 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9128 kvm_inject_page_fault(vcpu, &e);
9129 return 1;
9130 }
9131
9132 if (operand.pcid >> 12 != 0) {
9133 kvm_inject_gp(vcpu, 0);
9134 return 1;
9135 }
9136
9137 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9138
9139 switch (type) {
9140 case INVPCID_TYPE_INDIV_ADDR:
9141 if ((!pcid_enabled && (operand.pcid != 0)) ||
9142 is_noncanonical_address(operand.gla, vcpu)) {
9143 kvm_inject_gp(vcpu, 0);
9144 return 1;
9145 }
9146 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9147 return kvm_skip_emulated_instruction(vcpu);
9148
9149 case INVPCID_TYPE_SINGLE_CTXT:
9150 if (!pcid_enabled && (operand.pcid != 0)) {
9151 kvm_inject_gp(vcpu, 0);
9152 return 1;
9153 }
9154
9155 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9156 kvm_mmu_sync_roots(vcpu);
9157 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9158 }
9159
Junaid Shahidb94742c2018-06-27 14:59:20 -07009160 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9161 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9162 == operand.pcid)
9163 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009164
Junaid Shahidb94742c2018-06-27 14:59:20 -07009165 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009166 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009167 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009168 * given PCID, then nothing needs to be done here because a
9169 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009170 */
9171
9172 return kvm_skip_emulated_instruction(vcpu);
9173
9174 case INVPCID_TYPE_ALL_NON_GLOBAL:
9175 /*
9176 * Currently, KVM doesn't mark global entries in the shadow
9177 * page tables, so a non-global flush just degenerates to a
9178 * global flush. If needed, we could optimize this later by
9179 * keeping track of global entries in shadow page tables.
9180 */
9181
9182 /* fall-through */
9183 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9184 kvm_mmu_unload(vcpu);
9185 return kvm_skip_emulated_instruction(vcpu);
9186
9187 default:
9188 BUG(); /* We have already checked above that type <= 3 */
9189 }
9190}
9191
Kai Huang843e4332015-01-28 10:54:28 +08009192static int handle_pml_full(struct kvm_vcpu *vcpu)
9193{
9194 unsigned long exit_qualification;
9195
9196 trace_kvm_pml_full(vcpu->vcpu_id);
9197
9198 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9199
9200 /*
9201 * PML buffer FULL happened while executing iret from NMI,
9202 * "blocked by NMI" bit has to be set before next VM entry.
9203 */
9204 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009205 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009206 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9207 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9208 GUEST_INTR_STATE_NMI);
9209
9210 /*
9211 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9212 * here.., and there's no userspace involvement needed for PML.
9213 */
9214 return 1;
9215}
9216
Yunhong Jiang64672c92016-06-13 14:19:59 -07009217static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9218{
Sean Christophersond264ee02018-08-27 15:21:12 -07009219 if (!to_vmx(vcpu)->req_immediate_exit)
9220 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009221 return 1;
9222}
9223
Bandan Das41ab9372017-08-03 15:54:43 -04009224static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9225{
9226 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009227 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9228
9229 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009230 switch (address & VMX_EPTP_MT_MASK) {
9231 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009232 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009233 return false;
9234 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009235 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009236 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009237 return false;
9238 break;
9239 default:
9240 return false;
9241 }
9242
David Hildenbrandbb97a012017-08-10 23:15:28 +02009243 /* only 4 levels page-walk length are valid */
9244 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009245 return false;
9246
9247 /* Reserved bits should not be set */
9248 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9249 return false;
9250
9251 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009252 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009253 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009254 return false;
9255 }
9256
9257 return true;
9258}
9259
9260static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9261 struct vmcs12 *vmcs12)
9262{
9263 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9264 u64 address;
9265 bool accessed_dirty;
9266 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9267
9268 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9269 !nested_cpu_has_ept(vmcs12))
9270 return 1;
9271
9272 if (index >= VMFUNC_EPTP_ENTRIES)
9273 return 1;
9274
9275
9276 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9277 &address, index * 8, 8))
9278 return 1;
9279
David Hildenbrandbb97a012017-08-10 23:15:28 +02009280 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009281
9282 /*
9283 * If the (L2) guest does a vmfunc to the currently
9284 * active ept pointer, we don't have to do anything else
9285 */
9286 if (vmcs12->ept_pointer != address) {
9287 if (!valid_ept_address(vcpu, address))
9288 return 1;
9289
9290 kvm_mmu_unload(vcpu);
9291 mmu->ept_ad = accessed_dirty;
9292 mmu->base_role.ad_disabled = !accessed_dirty;
9293 vmcs12->ept_pointer = address;
9294 /*
9295 * TODO: Check what's the correct approach in case
9296 * mmu reload fails. Currently, we just let the next
9297 * reload potentially fail
9298 */
9299 kvm_mmu_reload(vcpu);
9300 }
9301
9302 return 0;
9303}
9304
Bandan Das2a499e42017-08-03 15:54:41 -04009305static int handle_vmfunc(struct kvm_vcpu *vcpu)
9306{
Bandan Das27c42a12017-08-03 15:54:42 -04009307 struct vcpu_vmx *vmx = to_vmx(vcpu);
9308 struct vmcs12 *vmcs12;
9309 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9310
9311 /*
9312 * VMFUNC is only supported for nested guests, but we always enable the
9313 * secondary control for simplicity; for non-nested mode, fake that we
9314 * didn't by injecting #UD.
9315 */
9316 if (!is_guest_mode(vcpu)) {
9317 kvm_queue_exception(vcpu, UD_VECTOR);
9318 return 1;
9319 }
9320
9321 vmcs12 = get_vmcs12(vcpu);
9322 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9323 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009324
9325 switch (function) {
9326 case 0:
9327 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9328 goto fail;
9329 break;
9330 default:
9331 goto fail;
9332 }
9333 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009334
9335fail:
9336 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9337 vmcs_read32(VM_EXIT_INTR_INFO),
9338 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009339 return 1;
9340}
9341
Sean Christopherson0b665d32018-08-14 09:33:34 -07009342static int handle_encls(struct kvm_vcpu *vcpu)
9343{
9344 /*
9345 * SGX virtualization is not yet supported. There is no software
9346 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9347 * to prevent the guest from executing ENCLS.
9348 */
9349 kvm_queue_exception(vcpu, UD_VECTOR);
9350 return 1;
9351}
9352
Nadav Har'El0140cae2011-05-25 23:06:28 +03009353/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009354 * The exit handlers return 1 if the exit was handled fully and guest execution
9355 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9356 * to be done to userspace and return 0.
9357 */
Mathias Krause772e0312012-08-30 01:30:19 +02009358static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009359 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9360 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009361 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009362 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009363 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009364 [EXIT_REASON_CR_ACCESS] = handle_cr,
9365 [EXIT_REASON_DR_ACCESS] = handle_dr,
9366 [EXIT_REASON_CPUID] = handle_cpuid,
9367 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9368 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9369 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9370 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009371 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009372 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009373 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009374 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009375 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009376 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009377 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009378 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009379 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009380 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009381 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009382 [EXIT_REASON_VMOFF] = handle_vmoff,
9383 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009384 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9385 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009386 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009387 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009388 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009389 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009390 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009391 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009392 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9393 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009394 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9395 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009396 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009397 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009398 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009399 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009400 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009401 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009402 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009403 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009404 [EXIT_REASON_XSAVES] = handle_xsaves,
9405 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009406 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009407 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009408 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009409 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009410 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009411};
9412
9413static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009414 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009415
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009416static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9417 struct vmcs12 *vmcs12)
9418{
9419 unsigned long exit_qualification;
9420 gpa_t bitmap, last_bitmap;
9421 unsigned int port;
9422 int size;
9423 u8 b;
9424
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009425 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009426 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009427
9428 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9429
9430 port = exit_qualification >> 16;
9431 size = (exit_qualification & 7) + 1;
9432
9433 last_bitmap = (gpa_t)-1;
9434 b = -1;
9435
9436 while (size > 0) {
9437 if (port < 0x8000)
9438 bitmap = vmcs12->io_bitmap_a;
9439 else if (port < 0x10000)
9440 bitmap = vmcs12->io_bitmap_b;
9441 else
Joe Perches1d804d02015-03-30 16:46:09 -07009442 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009443 bitmap += (port & 0x7fff) / 8;
9444
9445 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009446 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009447 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009448 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009449 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009450
9451 port++;
9452 size--;
9453 last_bitmap = bitmap;
9454 }
9455
Joe Perches1d804d02015-03-30 16:46:09 -07009456 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009457}
9458
Nadav Har'El644d7112011-05-25 23:12:35 +03009459/*
9460 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9461 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9462 * disinterest in the current event (read or write a specific MSR) by using an
9463 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9464 */
9465static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9466 struct vmcs12 *vmcs12, u32 exit_reason)
9467{
9468 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9469 gpa_t bitmap;
9470
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009471 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009472 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009473
9474 /*
9475 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9476 * for the four combinations of read/write and low/high MSR numbers.
9477 * First we need to figure out which of the four to use:
9478 */
9479 bitmap = vmcs12->msr_bitmap;
9480 if (exit_reason == EXIT_REASON_MSR_WRITE)
9481 bitmap += 2048;
9482 if (msr_index >= 0xc0000000) {
9483 msr_index -= 0xc0000000;
9484 bitmap += 1024;
9485 }
9486
9487 /* Then read the msr_index'th bit from this bitmap: */
9488 if (msr_index < 1024*8) {
9489 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009490 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009491 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009492 return 1 & (b >> (msr_index & 7));
9493 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009494 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009495}
9496
9497/*
9498 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9499 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9500 * intercept (via guest_host_mask etc.) the current event.
9501 */
9502static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9503 struct vmcs12 *vmcs12)
9504{
9505 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9506 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009507 int reg;
9508 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009509
9510 switch ((exit_qualification >> 4) & 3) {
9511 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009512 reg = (exit_qualification >> 8) & 15;
9513 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009514 switch (cr) {
9515 case 0:
9516 if (vmcs12->cr0_guest_host_mask &
9517 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009518 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009519 break;
9520 case 3:
9521 if ((vmcs12->cr3_target_count >= 1 &&
9522 vmcs12->cr3_target_value0 == val) ||
9523 (vmcs12->cr3_target_count >= 2 &&
9524 vmcs12->cr3_target_value1 == val) ||
9525 (vmcs12->cr3_target_count >= 3 &&
9526 vmcs12->cr3_target_value2 == val) ||
9527 (vmcs12->cr3_target_count >= 4 &&
9528 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009529 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009530 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009531 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009532 break;
9533 case 4:
9534 if (vmcs12->cr4_guest_host_mask &
9535 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009536 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009537 break;
9538 case 8:
9539 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009540 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009541 break;
9542 }
9543 break;
9544 case 2: /* clts */
9545 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9546 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009547 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009548 break;
9549 case 1: /* mov from cr */
9550 switch (cr) {
9551 case 3:
9552 if (vmcs12->cpu_based_vm_exec_control &
9553 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009554 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009555 break;
9556 case 8:
9557 if (vmcs12->cpu_based_vm_exec_control &
9558 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009559 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009560 break;
9561 }
9562 break;
9563 case 3: /* lmsw */
9564 /*
9565 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9566 * cr0. Other attempted changes are ignored, with no exit.
9567 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009568 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009569 if (vmcs12->cr0_guest_host_mask & 0xe &
9570 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009571 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009572 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9573 !(vmcs12->cr0_read_shadow & 0x1) &&
9574 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009575 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009576 break;
9577 }
Joe Perches1d804d02015-03-30 16:46:09 -07009578 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009579}
9580
Liran Alona7cde482018-06-23 02:35:10 +03009581static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9582 struct vmcs12 *vmcs12, gpa_t bitmap)
9583{
9584 u32 vmx_instruction_info;
9585 unsigned long field;
9586 u8 b;
9587
9588 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9589 return true;
9590
9591 /* Decode instruction info and find the field to access */
9592 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9593 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9594
9595 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9596 if (field >> 15)
9597 return true;
9598
9599 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9600 return true;
9601
9602 return 1 & (b >> (field & 7));
9603}
9604
Nadav Har'El644d7112011-05-25 23:12:35 +03009605/*
9606 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9607 * should handle it ourselves in L0 (and then continue L2). Only call this
9608 * when in is_guest_mode (L2).
9609 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009610static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009611{
Nadav Har'El644d7112011-05-25 23:12:35 +03009612 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9613 struct vcpu_vmx *vmx = to_vmx(vcpu);
9614 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9615
Jim Mattson4f350c62017-09-14 16:31:44 -07009616 if (vmx->nested.nested_run_pending)
9617 return false;
9618
9619 if (unlikely(vmx->fail)) {
9620 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9621 vmcs_read32(VM_INSTRUCTION_ERROR));
9622 return true;
9623 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009624
David Matlackc9f04402017-08-01 14:00:40 -07009625 /*
9626 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009627 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9628 * Page). The CPU may write to these pages via their host
9629 * physical address while L2 is running, bypassing any
9630 * address-translation-based dirty tracking (e.g. EPT write
9631 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009632 *
9633 * Mark them dirty on every exit from L2 to prevent them from
9634 * getting out of sync with dirty tracking.
9635 */
9636 nested_mark_vmcs12_pages_dirty(vcpu);
9637
Jim Mattson4f350c62017-09-14 16:31:44 -07009638 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9639 vmcs_readl(EXIT_QUALIFICATION),
9640 vmx->idt_vectoring_info,
9641 intr_info,
9642 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9643 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009644
9645 switch (exit_reason) {
9646 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009647 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009648 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009649 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009650 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Jan Kiszka6f054852016-02-09 20:15:18 +01009651 else if (is_debug(intr_info) &&
9652 vcpu->guest_debug &
9653 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9654 return false;
9655 else if (is_breakpoint(intr_info) &&
9656 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9657 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009658 return vmcs12->exception_bitmap &
9659 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9660 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009661 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009662 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009663 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009664 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009665 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009666 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009667 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009668 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009669 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009670 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009671 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009672 case EXIT_REASON_HLT:
9673 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9674 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009675 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009676 case EXIT_REASON_INVLPG:
9677 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9678 case EXIT_REASON_RDPMC:
9679 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009680 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009681 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009682 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009683 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009684 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009685 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009686 case EXIT_REASON_VMREAD:
9687 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9688 vmcs12->vmread_bitmap);
9689 case EXIT_REASON_VMWRITE:
9690 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9691 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009692 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9693 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009694 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009695 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009696 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009697 /*
9698 * VMX instructions trap unconditionally. This allows L1 to
9699 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9700 */
Joe Perches1d804d02015-03-30 16:46:09 -07009701 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009702 case EXIT_REASON_CR_ACCESS:
9703 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9704 case EXIT_REASON_DR_ACCESS:
9705 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9706 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009707 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009708 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9709 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009710 case EXIT_REASON_MSR_READ:
9711 case EXIT_REASON_MSR_WRITE:
9712 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9713 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009714 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009715 case EXIT_REASON_MWAIT_INSTRUCTION:
9716 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009717 case EXIT_REASON_MONITOR_TRAP_FLAG:
9718 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009719 case EXIT_REASON_MONITOR_INSTRUCTION:
9720 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9721 case EXIT_REASON_PAUSE_INSTRUCTION:
9722 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9723 nested_cpu_has2(vmcs12,
9724 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9725 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009726 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009727 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009728 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009729 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009730 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009731 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009732 /*
9733 * The controls for "virtualize APIC accesses," "APIC-
9734 * register virtualization," and "virtual-interrupt
9735 * delivery" only come from vmcs12.
9736 */
Joe Perches1d804d02015-03-30 16:46:09 -07009737 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009738 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009739 /*
9740 * L0 always deals with the EPT violation. If nested EPT is
9741 * used, and the nested mmu code discovers that the address is
9742 * missing in the guest EPT table (EPT12), the EPT violation
9743 * will be injected with nested_ept_inject_page_fault()
9744 */
Joe Perches1d804d02015-03-30 16:46:09 -07009745 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009746 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009747 /*
9748 * L2 never uses directly L1's EPT, but rather L0's own EPT
9749 * table (shadow on EPT) or a merged EPT table that L0 built
9750 * (EPT on EPT). So any problems with the structure of the
9751 * table is L0's fault.
9752 */
Joe Perches1d804d02015-03-30 16:46:09 -07009753 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009754 case EXIT_REASON_INVPCID:
9755 return
9756 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9757 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009758 case EXIT_REASON_WBINVD:
9759 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9760 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009761 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009762 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9763 /*
9764 * This should never happen, since it is not possible to
9765 * set XSS to a non-zero value---neither in L1 nor in L2.
9766 * If if it were, XSS would have to be checked against
9767 * the XSS exit bitmap in vmcs12.
9768 */
9769 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009770 case EXIT_REASON_PREEMPTION_TIMER:
9771 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009772 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009773 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009774 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009775 case EXIT_REASON_VMFUNC:
9776 /* VM functions are emulated through L2->L0 vmexits. */
9777 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009778 case EXIT_REASON_ENCLS:
9779 /* SGX is never exposed to L1 */
9780 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009781 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009782 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009783 }
9784}
9785
Paolo Bonzini7313c692017-07-27 10:31:25 +02009786static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9787{
9788 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9789
9790 /*
9791 * At this point, the exit interruption info in exit_intr_info
9792 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9793 * we need to query the in-kernel LAPIC.
9794 */
9795 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9796 if ((exit_intr_info &
9797 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9798 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9799 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9800 vmcs12->vm_exit_intr_error_code =
9801 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9802 }
9803
9804 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9805 vmcs_readl(EXIT_QUALIFICATION));
9806 return 1;
9807}
9808
Avi Kivity586f9602010-11-18 13:09:54 +02009809static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9810{
9811 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9812 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9813}
9814
Kai Huanga3eaa862015-11-04 13:46:05 +08009815static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009816{
Kai Huanga3eaa862015-11-04 13:46:05 +08009817 if (vmx->pml_pg) {
9818 __free_page(vmx->pml_pg);
9819 vmx->pml_pg = NULL;
9820 }
Kai Huang843e4332015-01-28 10:54:28 +08009821}
9822
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009823static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009824{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009825 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009826 u64 *pml_buf;
9827 u16 pml_idx;
9828
9829 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9830
9831 /* Do nothing if PML buffer is empty */
9832 if (pml_idx == (PML_ENTITY_NUM - 1))
9833 return;
9834
9835 /* PML index always points to next available PML buffer entity */
9836 if (pml_idx >= PML_ENTITY_NUM)
9837 pml_idx = 0;
9838 else
9839 pml_idx++;
9840
9841 pml_buf = page_address(vmx->pml_pg);
9842 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9843 u64 gpa;
9844
9845 gpa = pml_buf[pml_idx];
9846 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009847 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009848 }
9849
9850 /* reset PML index */
9851 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9852}
9853
9854/*
9855 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9856 * Called before reporting dirty_bitmap to userspace.
9857 */
9858static void kvm_flush_pml_buffers(struct kvm *kvm)
9859{
9860 int i;
9861 struct kvm_vcpu *vcpu;
9862 /*
9863 * We only need to kick vcpu out of guest mode here, as PML buffer
9864 * is flushed at beginning of all VMEXITs, and it's obvious that only
9865 * vcpus running in guest are possible to have unflushed GPAs in PML
9866 * buffer.
9867 */
9868 kvm_for_each_vcpu(i, vcpu, kvm)
9869 kvm_vcpu_kick(vcpu);
9870}
9871
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009872static void vmx_dump_sel(char *name, uint32_t sel)
9873{
9874 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009875 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009876 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9877 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9878 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9879}
9880
9881static void vmx_dump_dtsel(char *name, uint32_t limit)
9882{
9883 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9884 name, vmcs_read32(limit),
9885 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9886}
9887
9888static void dump_vmcs(void)
9889{
9890 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9891 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9892 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9893 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9894 u32 secondary_exec_control = 0;
9895 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009896 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009897 int i, n;
9898
9899 if (cpu_has_secondary_exec_ctrls())
9900 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9901
9902 pr_err("*** Guest State ***\n");
9903 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9904 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9905 vmcs_readl(CR0_GUEST_HOST_MASK));
9906 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9907 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9908 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9909 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9910 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9911 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009912 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9913 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9914 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9915 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009916 }
9917 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9918 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9919 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9920 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9921 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9922 vmcs_readl(GUEST_SYSENTER_ESP),
9923 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9924 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9925 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9926 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9927 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9928 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9929 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9930 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9931 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9932 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9933 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9934 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9935 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009936 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9937 efer, vmcs_read64(GUEST_IA32_PAT));
9938 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9939 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009940 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009941 if (cpu_has_load_perf_global_ctrl &&
9942 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009943 pr_err("PerfGlobCtl = 0x%016llx\n",
9944 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009945 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009946 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009947 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9948 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9949 vmcs_read32(GUEST_ACTIVITY_STATE));
9950 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9951 pr_err("InterruptStatus = %04x\n",
9952 vmcs_read16(GUEST_INTR_STATUS));
9953
9954 pr_err("*** Host State ***\n");
9955 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9956 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9957 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9958 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9959 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9960 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9961 vmcs_read16(HOST_TR_SELECTOR));
9962 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9963 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9964 vmcs_readl(HOST_TR_BASE));
9965 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9966 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9967 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9968 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9969 vmcs_readl(HOST_CR4));
9970 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9971 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9972 vmcs_read32(HOST_IA32_SYSENTER_CS),
9973 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9974 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009975 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9976 vmcs_read64(HOST_IA32_EFER),
9977 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009978 if (cpu_has_load_perf_global_ctrl &&
9979 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009980 pr_err("PerfGlobCtl = 0x%016llx\n",
9981 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009982
9983 pr_err("*** Control State ***\n");
9984 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9985 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9986 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9987 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9988 vmcs_read32(EXCEPTION_BITMAP),
9989 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
9990 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
9991 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
9992 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
9993 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
9994 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
9995 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
9996 vmcs_read32(VM_EXIT_INTR_INFO),
9997 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9998 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
9999 pr_err(" reason=%08x qualification=%016lx\n",
10000 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10001 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10002 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10003 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010004 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010005 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010006 pr_err("TSC Multiplier = 0x%016llx\n",
10007 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010008 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10009 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10010 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10011 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10012 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010013 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010014 n = vmcs_read32(CR3_TARGET_COUNT);
10015 for (i = 0; i + 1 < n; i += 4)
10016 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10017 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10018 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10019 if (i < n)
10020 pr_err("CR3 target%u=%016lx\n",
10021 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10022 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10023 pr_err("PLE Gap=%08x Window=%08x\n",
10024 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10025 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10026 pr_err("Virtual processor ID = 0x%04x\n",
10027 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10028}
10029
Avi Kivity6aa8b732006-12-10 02:21:36 -080010030/*
10031 * The guest has exited. See if we can fix it or if we need userspace
10032 * assistance.
10033 */
Avi Kivity851ba692009-08-24 11:10:17 +030010034static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010035{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010036 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010037 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010038 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010039
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010040 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10041
Kai Huang843e4332015-01-28 10:54:28 +080010042 /*
10043 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10044 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10045 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10046 * mode as if vcpus is in root mode, the PML buffer must has been
10047 * flushed already.
10048 */
10049 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010050 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010051
Mohammed Gamal80ced182009-09-01 12:48:18 +020010052 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010053 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010054 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010055
Paolo Bonzini7313c692017-07-27 10:31:25 +020010056 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10057 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010058
Mohammed Gamal51207022010-05-31 22:40:54 +030010059 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010060 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010061 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10062 vcpu->run->fail_entry.hardware_entry_failure_reason
10063 = exit_reason;
10064 return 0;
10065 }
10066
Avi Kivity29bd8a72007-09-10 17:27:03 +030010067 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010068 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10069 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010070 = vmcs_read32(VM_INSTRUCTION_ERROR);
10071 return 0;
10072 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010073
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010074 /*
10075 * Note:
10076 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10077 * delivery event since it indicates guest is accessing MMIO.
10078 * The vm-exit can be triggered again after return to guest that
10079 * will cause infinite loop.
10080 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010081 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010082 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010083 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010084 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010085 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10086 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10087 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010088 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010089 vcpu->run->internal.data[0] = vectoring_info;
10090 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010091 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10092 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10093 vcpu->run->internal.ndata++;
10094 vcpu->run->internal.data[3] =
10095 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10096 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010097 return 0;
10098 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010099
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010100 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010101 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10102 if (vmx_interrupt_allowed(vcpu)) {
10103 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10104 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10105 vcpu->arch.nmi_pending) {
10106 /*
10107 * This CPU don't support us in finding the end of an
10108 * NMI-blocked window if the guest runs with IRQs
10109 * disabled. So we pull the trigger after 1 s of
10110 * futile waiting, but inform the user about this.
10111 */
10112 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10113 "state on VCPU %d after 1 s timeout\n",
10114 __func__, vcpu->vcpu_id);
10115 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10116 }
10117 }
10118
Avi Kivity6aa8b732006-12-10 02:21:36 -080010119 if (exit_reason < kvm_vmx_max_exit_handlers
10120 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010121 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010122 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010123 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10124 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010125 kvm_queue_exception(vcpu, UD_VECTOR);
10126 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010127 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010128}
10129
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010130/*
10131 * Software based L1D cache flush which is used when microcode providing
10132 * the cache control MSR is not loaded.
10133 *
10134 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10135 * flush it is required to read in 64 KiB because the replacement algorithm
10136 * is not exactly LRU. This could be sized at runtime via topology
10137 * information but as all relevant affected CPUs have 32KiB L1D cache size
10138 * there is no point in doing so.
10139 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010140static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010141{
10142 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010143
10144 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010145 * This code is only executed when the the flush mode is 'cond' or
10146 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010147 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010148 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010149 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010150
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010151 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010152 * Clear the per-vcpu flush bit, it gets set again
10153 * either from vcpu_run() or from one of the unsafe
10154 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010155 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010156 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010157 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010158
10159 /*
10160 * Clear the per-cpu flush bit, it gets set again from
10161 * the interrupt handlers.
10162 */
10163 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10164 kvm_clear_cpu_l1tf_flush_l1d();
10165
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010166 if (!flush_l1d)
10167 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010168 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010169
10170 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010171
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010172 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10173 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10174 return;
10175 }
10176
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010177 asm volatile(
10178 /* First ensure the pages are in the TLB */
10179 "xorl %%eax, %%eax\n"
10180 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010181 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010182 "addl $4096, %%eax\n\t"
10183 "cmpl %%eax, %[size]\n\t"
10184 "jne .Lpopulate_tlb\n\t"
10185 "xorl %%eax, %%eax\n\t"
10186 "cpuid\n\t"
10187 /* Now fill the cache */
10188 "xorl %%eax, %%eax\n"
10189 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010190 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010191 "addl $64, %%eax\n\t"
10192 "cmpl %%eax, %[size]\n\t"
10193 "jne .Lfill_cache\n\t"
10194 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010195 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010196 [size] "r" (size)
10197 : "eax", "ebx", "ecx", "edx");
10198}
10199
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010200static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010201{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010202 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10203
10204 if (is_guest_mode(vcpu) &&
10205 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10206 return;
10207
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010208 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010209 vmcs_write32(TPR_THRESHOLD, 0);
10210 return;
10211 }
10212
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010213 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010214}
10215
Jim Mattson8d860bb2018-05-09 16:56:05 -040010216static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010217{
10218 u32 sec_exec_control;
10219
Jim Mattson8d860bb2018-05-09 16:56:05 -040010220 if (!lapic_in_kernel(vcpu))
10221 return;
10222
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010223 if (!flexpriority_enabled &&
10224 !cpu_has_vmx_virtualize_x2apic_mode())
10225 return;
10226
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010227 /* Postpone execution until vmcs01 is the current VMCS. */
10228 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010229 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010230 return;
10231 }
10232
Yang Zhang8d146952013-01-25 10:18:50 +080010233 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010234 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10235 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010236
Jim Mattson8d860bb2018-05-09 16:56:05 -040010237 switch (kvm_get_apic_mode(vcpu)) {
10238 case LAPIC_MODE_INVALID:
10239 WARN_ONCE(true, "Invalid local APIC state");
10240 case LAPIC_MODE_DISABLED:
10241 break;
10242 case LAPIC_MODE_XAPIC:
10243 if (flexpriority_enabled) {
10244 sec_exec_control |=
10245 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10246 vmx_flush_tlb(vcpu, true);
10247 }
10248 break;
10249 case LAPIC_MODE_X2APIC:
10250 if (cpu_has_vmx_virtualize_x2apic_mode())
10251 sec_exec_control |=
10252 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10253 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010254 }
10255 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10256
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010257 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010258}
10259
Tang Chen38b99172014-09-24 15:57:54 +080010260static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10261{
Jim Mattsonab5df312018-05-09 17:02:03 -040010262 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010263 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010264 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010265 }
Tang Chen38b99172014-09-24 15:57:54 +080010266}
10267
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010268static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010269{
10270 u16 status;
10271 u8 old;
10272
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010273 if (max_isr == -1)
10274 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010275
10276 status = vmcs_read16(GUEST_INTR_STATUS);
10277 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010278 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010279 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010280 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010281 vmcs_write16(GUEST_INTR_STATUS, status);
10282 }
10283}
10284
10285static void vmx_set_rvi(int vector)
10286{
10287 u16 status;
10288 u8 old;
10289
Wei Wang4114c272014-11-05 10:53:43 +080010290 if (vector == -1)
10291 vector = 0;
10292
Yang Zhangc7c9c562013-01-25 10:18:51 +080010293 status = vmcs_read16(GUEST_INTR_STATUS);
10294 old = (u8)status & 0xff;
10295 if ((u8)vector != old) {
10296 status &= ~0xff;
10297 status |= (u8)vector;
10298 vmcs_write16(GUEST_INTR_STATUS, status);
10299 }
10300}
10301
10302static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10303{
Liran Alon851c1a182017-12-24 18:12:56 +020010304 /*
10305 * When running L2, updating RVI is only relevant when
10306 * vmcs12 virtual-interrupt-delivery enabled.
10307 * However, it can be enabled only when L1 also
10308 * intercepts external-interrupts and in that case
10309 * we should not update vmcs02 RVI but instead intercept
10310 * interrupt. Therefore, do nothing when running L2.
10311 */
10312 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010313 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010314}
10315
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010316static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010317{
10318 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010319 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010320 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010321
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010322 WARN_ON(!vcpu->arch.apicv_active);
10323 if (pi_test_on(&vmx->pi_desc)) {
10324 pi_clear_on(&vmx->pi_desc);
10325 /*
10326 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10327 * But on x86 this is just a compiler barrier anyway.
10328 */
10329 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010330 max_irr_updated =
10331 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10332
10333 /*
10334 * If we are running L2 and L1 has a new pending interrupt
10335 * which can be injected, we should re-evaluate
10336 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010337 * If L1 intercepts external-interrupts, we should
10338 * exit from L2 to L1. Otherwise, interrupt should be
10339 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010340 */
Liran Alon851c1a182017-12-24 18:12:56 +020010341 if (is_guest_mode(vcpu) && max_irr_updated) {
10342 if (nested_exit_on_intr(vcpu))
10343 kvm_vcpu_exiting_guest_mode(vcpu);
10344 else
10345 kvm_make_request(KVM_REQ_EVENT, vcpu);
10346 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010347 } else {
10348 max_irr = kvm_lapic_find_highest_irr(vcpu);
10349 }
10350 vmx_hwapic_irr_update(vcpu, max_irr);
10351 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010352}
10353
Paolo Bonzini7e712682018-10-03 13:44:26 +020010354static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10355{
10356 u8 rvi = vmx_get_rvi();
10357 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10358
10359 return ((rvi & 0xf0) > (vppr & 0xf0));
10360}
10361
Andrey Smetanin63086302015-11-10 15:36:32 +030010362static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010363{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010364 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010365 return;
10366
Yang Zhangc7c9c562013-01-25 10:18:51 +080010367 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10368 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10369 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10370 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10371}
10372
Paolo Bonzini967235d2016-12-19 14:03:45 +010010373static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10374{
10375 struct vcpu_vmx *vmx = to_vmx(vcpu);
10376
10377 pi_clear_on(&vmx->pi_desc);
10378 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10379}
10380
Avi Kivity51aa01d2010-07-20 14:31:20 +030010381static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010382{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010383 u32 exit_intr_info = 0;
10384 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010385
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010386 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10387 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010388 return;
10389
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010390 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10391 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10392 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010393
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010394 /* if exit due to PF check for async PF */
10395 if (is_page_fault(exit_intr_info))
10396 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10397
Andi Kleena0861c02009-06-08 17:37:09 +080010398 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010399 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10400 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010401 kvm_machine_check();
10402
Gleb Natapov20f65982009-05-11 13:35:55 +030010403 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010404 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010405 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010406 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010407 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010408 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010409}
Gleb Natapov20f65982009-05-11 13:35:55 +030010410
Yang Zhanga547c6d2013-04-11 19:25:10 +080010411static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10412{
10413 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10414
Yang Zhanga547c6d2013-04-11 19:25:10 +080010415 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10416 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10417 unsigned int vector;
10418 unsigned long entry;
10419 gate_desc *desc;
10420 struct vcpu_vmx *vmx = to_vmx(vcpu);
10421#ifdef CONFIG_X86_64
10422 unsigned long tmp;
10423#endif
10424
10425 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10426 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010427 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010428 asm volatile(
10429#ifdef CONFIG_X86_64
10430 "mov %%" _ASM_SP ", %[sp]\n\t"
10431 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10432 "push $%c[ss]\n\t"
10433 "push %[sp]\n\t"
10434#endif
10435 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010436 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010437 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010438 :
10439#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010440 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010441#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010442 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010443 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010444 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010445 [ss]"i"(__KERNEL_DS),
10446 [cs]"i"(__KERNEL_CS)
10447 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010448 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010449}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010450STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010451
Tom Lendackybc226f02018-05-10 22:06:39 +020010452static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010453{
Tom Lendackybc226f02018-05-10 22:06:39 +020010454 switch (index) {
10455 case MSR_IA32_SMBASE:
10456 /*
10457 * We cannot do SMM unless we can run the guest in big
10458 * real mode.
10459 */
10460 return enable_unrestricted_guest || emulate_invalid_guest_state;
10461 case MSR_AMD64_VIRT_SPEC_CTRL:
10462 /* This is AMD only. */
10463 return false;
10464 default:
10465 return true;
10466 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010467}
10468
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010469static bool vmx_mpx_supported(void)
10470{
10471 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10472 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10473}
10474
Wanpeng Li55412b22014-12-02 19:21:30 +080010475static bool vmx_xsaves_supported(void)
10476{
10477 return vmcs_config.cpu_based_2nd_exec_ctrl &
10478 SECONDARY_EXEC_XSAVES;
10479}
10480
Avi Kivity51aa01d2010-07-20 14:31:20 +030010481static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10482{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010483 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010484 bool unblock_nmi;
10485 u8 vector;
10486 bool idtv_info_valid;
10487
10488 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010489
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010490 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010491 if (vmx->loaded_vmcs->nmi_known_unmasked)
10492 return;
10493 /*
10494 * Can't use vmx->exit_intr_info since we're not sure what
10495 * the exit reason is.
10496 */
10497 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10498 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10499 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10500 /*
10501 * SDM 3: 27.7.1.2 (September 2008)
10502 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10503 * a guest IRET fault.
10504 * SDM 3: 23.2.2 (September 2008)
10505 * Bit 12 is undefined in any of the following cases:
10506 * If the VM exit sets the valid bit in the IDT-vectoring
10507 * information field.
10508 * If the VM exit is due to a double fault.
10509 */
10510 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10511 vector != DF_VECTOR && !idtv_info_valid)
10512 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10513 GUEST_INTR_STATE_NMI);
10514 else
10515 vmx->loaded_vmcs->nmi_known_unmasked =
10516 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10517 & GUEST_INTR_STATE_NMI);
10518 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10519 vmx->loaded_vmcs->vnmi_blocked_time +=
10520 ktime_to_ns(ktime_sub(ktime_get(),
10521 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010522}
10523
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010524static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010525 u32 idt_vectoring_info,
10526 int instr_len_field,
10527 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010528{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010529 u8 vector;
10530 int type;
10531 bool idtv_info_valid;
10532
10533 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010534
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010535 vcpu->arch.nmi_injected = false;
10536 kvm_clear_exception_queue(vcpu);
10537 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010538
10539 if (!idtv_info_valid)
10540 return;
10541
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010542 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010543
Avi Kivity668f6122008-07-02 09:28:55 +030010544 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10545 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010546
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010547 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010548 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010549 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010550 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010551 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010552 * Clear bit "block by NMI" before VM entry if a NMI
10553 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010554 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010555 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010556 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010557 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010558 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010559 /* fall through */
10560 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010561 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010562 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010563 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010564 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010565 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010566 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010567 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010568 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010569 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010570 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010571 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010572 break;
10573 default:
10574 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010575 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010576}
10577
Avi Kivity83422e12010-07-20 14:43:23 +030010578static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10579{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010580 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010581 VM_EXIT_INSTRUCTION_LEN,
10582 IDT_VECTORING_ERROR_CODE);
10583}
10584
Avi Kivityb463a6f2010-07-20 15:06:17 +030010585static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10586{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010587 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010588 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10589 VM_ENTRY_INSTRUCTION_LEN,
10590 VM_ENTRY_EXCEPTION_ERROR_CODE);
10591
10592 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10593}
10594
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010595static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10596{
10597 int i, nr_msrs;
10598 struct perf_guest_switch_msr *msrs;
10599
10600 msrs = perf_guest_get_msrs(&nr_msrs);
10601
10602 if (!msrs)
10603 return;
10604
10605 for (i = 0; i < nr_msrs; i++)
10606 if (msrs[i].host == msrs[i].guest)
10607 clear_atomic_switch_msr(vmx, msrs[i].msr);
10608 else
10609 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010610 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010611}
10612
Sean Christophersonf459a702018-08-27 15:21:11 -070010613static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10614{
10615 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10616 if (!vmx->loaded_vmcs->hv_timer_armed)
10617 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10618 PIN_BASED_VMX_PREEMPTION_TIMER);
10619 vmx->loaded_vmcs->hv_timer_armed = true;
10620}
10621
10622static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010623{
10624 struct vcpu_vmx *vmx = to_vmx(vcpu);
10625 u64 tscl;
10626 u32 delta_tsc;
10627
Sean Christophersond264ee02018-08-27 15:21:12 -070010628 if (vmx->req_immediate_exit) {
10629 vmx_arm_hv_timer(vmx, 0);
10630 return;
10631 }
10632
Sean Christophersonf459a702018-08-27 15:21:11 -070010633 if (vmx->hv_deadline_tsc != -1) {
10634 tscl = rdtsc();
10635 if (vmx->hv_deadline_tsc > tscl)
10636 /* set_hv_timer ensures the delta fits in 32-bits */
10637 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10638 cpu_preemption_timer_multi);
10639 else
10640 delta_tsc = 0;
10641
10642 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010643 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010644 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010645
Sean Christophersonf459a702018-08-27 15:21:11 -070010646 if (vmx->loaded_vmcs->hv_timer_armed)
10647 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10648 PIN_BASED_VMX_PREEMPTION_TIMER);
10649 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010650}
10651
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010652static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010653{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010654 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010655 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010656
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010657 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010658 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010659 vmx->loaded_vmcs->soft_vnmi_blocked))
10660 vmx->loaded_vmcs->entry_time = ktime_get();
10661
Avi Kivity104f2262010-11-18 13:12:52 +020010662 /* Don't enter VMX if guest state is invalid, let the exit handler
10663 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010664 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010665 return;
10666
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010667 if (vmx->ple_window_dirty) {
10668 vmx->ple_window_dirty = false;
10669 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10670 }
10671
Abel Gordon012f83c2013-04-18 14:39:25 +030010672 if (vmx->nested.sync_shadow_vmcs) {
10673 copy_vmcs12_to_shadow(vmx);
10674 vmx->nested.sync_shadow_vmcs = false;
10675 }
10676
Avi Kivity104f2262010-11-18 13:12:52 +020010677 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10678 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10679 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10680 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10681
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010682 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010683 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010684 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010685 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010686 }
10687
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010688 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010689 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010690 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010691 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010692 }
10693
Avi Kivity104f2262010-11-18 13:12:52 +020010694 /* When single-stepping over STI and MOV SS, we must clear the
10695 * corresponding interruptibility bits in the guest state. Otherwise
10696 * vmentry fails as it then expects bit 14 (BS) in pending debug
10697 * exceptions being set, but that's not correct for the guest debugging
10698 * case. */
10699 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10700 vmx_set_interrupt_shadow(vcpu, 0);
10701
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010702 if (static_cpu_has(X86_FEATURE_PKU) &&
10703 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10704 vcpu->arch.pkru != vmx->host_pkru)
10705 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010706
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010707 atomic_switch_perf_msrs(vmx);
10708
Sean Christophersonf459a702018-08-27 15:21:11 -070010709 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010710
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010711 /*
10712 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10713 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10714 * is no need to worry about the conditional branch over the wrmsr
10715 * being speculatively taken.
10716 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010717 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010718
Nadav Har'Eld462b812011-05-24 15:26:10 +030010719 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010720
10721 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10722 (unsigned long)&current_evmcs->host_rsp : 0;
10723
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010724 if (static_branch_unlikely(&vmx_l1d_should_flush))
10725 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010726
Avi Kivity104f2262010-11-18 13:12:52 +020010727 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010728 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010729 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10730 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10731 "push %%" _ASM_CX " \n\t"
10732 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010733 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010734 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010735 /* Avoid VMWRITE when Enlightened VMCS is in use */
10736 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10737 "jz 2f \n\t"
10738 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10739 "jmp 1f \n\t"
10740 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010741 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010742 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010743 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010744 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10745 "mov %%cr2, %%" _ASM_DX " \n\t"
10746 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010747 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010748 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010749 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010750 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010751 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010752 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010753 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10754 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10755 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10756 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10757 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10758 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010759#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010760 "mov %c[r8](%0), %%r8 \n\t"
10761 "mov %c[r9](%0), %%r9 \n\t"
10762 "mov %c[r10](%0), %%r10 \n\t"
10763 "mov %c[r11](%0), %%r11 \n\t"
10764 "mov %c[r12](%0), %%r12 \n\t"
10765 "mov %c[r13](%0), %%r13 \n\t"
10766 "mov %c[r14](%0), %%r14 \n\t"
10767 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010768#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010769 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010770
Avi Kivity6aa8b732006-12-10 02:21:36 -080010771 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010772 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010773 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010774 "jmp 2f \n\t"
10775 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10776 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010777 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010778 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010779 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010780 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010781 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10782 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10783 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10784 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10785 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10786 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10787 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010788#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010789 "mov %%r8, %c[r8](%0) \n\t"
10790 "mov %%r9, %c[r9](%0) \n\t"
10791 "mov %%r10, %c[r10](%0) \n\t"
10792 "mov %%r11, %c[r11](%0) \n\t"
10793 "mov %%r12, %c[r12](%0) \n\t"
10794 "mov %%r13, %c[r13](%0) \n\t"
10795 "mov %%r14, %c[r14](%0) \n\t"
10796 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010797 "xor %%r8d, %%r8d \n\t"
10798 "xor %%r9d, %%r9d \n\t"
10799 "xor %%r10d, %%r10d \n\t"
10800 "xor %%r11d, %%r11d \n\t"
10801 "xor %%r12d, %%r12d \n\t"
10802 "xor %%r13d, %%r13d \n\t"
10803 "xor %%r14d, %%r14d \n\t"
10804 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010805#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010806 "mov %%cr2, %%" _ASM_AX " \n\t"
10807 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010808
Jim Mattson0cb5b302018-01-03 14:31:38 -080010809 "xor %%eax, %%eax \n\t"
10810 "xor %%ebx, %%ebx \n\t"
10811 "xor %%esi, %%esi \n\t"
10812 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010813 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010814 ".pushsection .rodata \n\t"
10815 ".global vmx_return \n\t"
10816 "vmx_return: " _ASM_PTR " 2b \n\t"
10817 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010818 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010819 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010820 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010821 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010822 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10823 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10824 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10825 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10826 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10827 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10828 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010829#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010830 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10831 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10832 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10833 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10834 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10835 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10836 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10837 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010838#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010839 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10840 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010841 : "cc", "memory"
10842#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010843 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010844 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010845#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010846 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010847#endif
10848 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010849
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010850 /*
10851 * We do not use IBRS in the kernel. If this vCPU has used the
10852 * SPEC_CTRL MSR it may have left it on; save the value and
10853 * turn it off. This is much more efficient than blindly adding
10854 * it to the atomic save/restore list. Especially as the former
10855 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10856 *
10857 * For non-nested case:
10858 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10859 * save it.
10860 *
10861 * For nested case:
10862 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10863 * save it.
10864 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010865 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010866 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010867
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010868 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010869
David Woodhouse117cc7a2018-01-12 11:11:27 +000010870 /* Eliminate branch target predictions from guest mode */
10871 vmexit_fill_RSB();
10872
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010873 /* All fields are clean at this point */
10874 if (static_branch_unlikely(&enable_evmcs))
10875 current_evmcs->hv_clean_fields |=
10876 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10877
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010878 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010879 if (vmx->host_debugctlmsr)
10880 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010881
Avi Kivityaa67f602012-08-01 16:48:03 +030010882#ifndef CONFIG_X86_64
10883 /*
10884 * The sysexit path does not restore ds/es, so we must set them to
10885 * a reasonable value ourselves.
10886 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010887 * We can't defer this to vmx_prepare_switch_to_host() since that
10888 * function may be executed in interrupt context, which saves and
10889 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010890 */
10891 loadsegment(ds, __USER_DS);
10892 loadsegment(es, __USER_DS);
10893#endif
10894
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010895 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010896 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010897 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010898 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010899 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010900 vcpu->arch.regs_dirty = 0;
10901
Gleb Natapove0b890d2013-09-25 12:51:33 +030010902 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010903 * eager fpu is enabled if PKEY is supported and CR4 is switched
10904 * back on host, so it is safe to read guest PKRU from current
10905 * XSAVE.
10906 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010907 if (static_cpu_has(X86_FEATURE_PKU) &&
10908 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10909 vcpu->arch.pkru = __read_pkru();
10910 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010911 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010912 }
10913
Gleb Natapove0b890d2013-09-25 12:51:33 +030010914 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010915 vmx->idt_vectoring_info = 0;
10916
10917 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10918 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10919 return;
10920
10921 vmx->loaded_vmcs->launched = 1;
10922 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010923
Avi Kivity51aa01d2010-07-20 14:31:20 +030010924 vmx_complete_atomic_exit(vmx);
10925 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010926 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010927}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010928STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010929
Sean Christopherson434a1e92018-03-20 12:17:18 -070010930static struct kvm *vmx_vm_alloc(void)
10931{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010932 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010933 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010934}
10935
10936static void vmx_vm_free(struct kvm *kvm)
10937{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010938 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010939}
10940
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010941static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010942{
10943 struct vcpu_vmx *vmx = to_vmx(vcpu);
10944 int cpu;
10945
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010946 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010947 return;
10948
10949 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010950 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010951 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010952 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010953 put_cpu();
10954}
10955
Jim Mattson2f1fe812016-07-08 15:36:06 -070010956/*
10957 * Ensure that the current vmcs of the logical processor is the
10958 * vmcs01 of the vcpu before calling free_nested().
10959 */
10960static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10961{
10962 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010963
Christoffer Dallec7660c2017-12-04 21:35:23 +010010964 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010965 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010966 free_nested(vmx);
10967 vcpu_put(vcpu);
10968}
10969
Avi Kivity6aa8b732006-12-10 02:21:36 -080010970static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10971{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010972 struct vcpu_vmx *vmx = to_vmx(vcpu);
10973
Kai Huang843e4332015-01-28 10:54:28 +080010974 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010975 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010976 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010977 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010978 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010979 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010980 kfree(vmx->guest_msrs);
10981 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010982 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010983}
10984
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010985static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010986{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010987 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010988 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010989 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030010990 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010991
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010992 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010993 return ERR_PTR(-ENOMEM);
10994
Wanpeng Li991e7a02015-09-16 17:30:05 +080010995 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080010996
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010997 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
10998 if (err)
10999 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011000
Peter Feiner4e595162016-07-07 14:49:58 -070011001 err = -ENOMEM;
11002
11003 /*
11004 * If PML is turned on, failure on enabling PML just results in failure
11005 * of creating the vcpu, therefore we can simplify PML logic (by
11006 * avoiding dealing with cases, such as enabling PML partially on vcpus
11007 * for the guest, etc.
11008 */
11009 if (enable_pml) {
11010 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11011 if (!vmx->pml_pg)
11012 goto uninit_vcpu;
11013 }
11014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011015 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011016 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11017 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011018
Peter Feiner4e595162016-07-07 14:49:58 -070011019 if (!vmx->guest_msrs)
11020 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011021
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011022 err = alloc_loaded_vmcs(&vmx->vmcs01);
11023 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011024 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011025
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011026 msr_bitmap = vmx->vmcs01.msr_bitmap;
11027 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11028 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11029 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11030 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11031 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11032 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11033 vmx->msr_bitmap_mode = 0;
11034
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011035 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011036 cpu = get_cpu();
11037 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011038 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011039 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011040 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011041 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011042 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011043 err = alloc_apic_access_page(kvm);
11044 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011045 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011046 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011047
Sean Christophersone90008d2018-03-05 12:04:37 -080011048 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011049 err = init_rmode_identity_map(kvm);
11050 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011051 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011052 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011053
Roman Kagan63aff652018-07-19 21:59:07 +030011054 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011055 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11056 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011057
Wincy Van705699a2015-02-03 23:58:17 +080011058 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011059 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011060
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011061 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11062
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011063 /*
11064 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11065 * or POSTED_INTR_WAKEUP_VECTOR.
11066 */
11067 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11068 vmx->pi_desc.sn = 1;
11069
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011070 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011071
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011072free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011073 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011074free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011075 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011076free_pml:
11077 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011078uninit_vcpu:
11079 kvm_vcpu_uninit(&vmx->vcpu);
11080free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011081 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011082 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011083 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011084}
11085
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011086#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"
11087#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 -040011088
Wanpeng Lib31c1142018-03-12 04:53:04 -070011089static int vmx_vm_init(struct kvm *kvm)
11090{
Tianyu Lan877ad952018-07-19 08:40:23 +000011091 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11092
Wanpeng Lib31c1142018-03-12 04:53:04 -070011093 if (!ple_gap)
11094 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011095
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011096 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11097 switch (l1tf_mitigation) {
11098 case L1TF_MITIGATION_OFF:
11099 case L1TF_MITIGATION_FLUSH_NOWARN:
11100 /* 'I explicitly don't care' is set */
11101 break;
11102 case L1TF_MITIGATION_FLUSH:
11103 case L1TF_MITIGATION_FLUSH_NOSMT:
11104 case L1TF_MITIGATION_FULL:
11105 /*
11106 * Warn upon starting the first VM in a potentially
11107 * insecure environment.
11108 */
11109 if (cpu_smt_control == CPU_SMT_ENABLED)
11110 pr_warn_once(L1TF_MSG_SMT);
11111 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11112 pr_warn_once(L1TF_MSG_L1D);
11113 break;
11114 case L1TF_MITIGATION_FULL_FORCE:
11115 /* Flush is enforced */
11116 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011117 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011118 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011119 return 0;
11120}
11121
Yang, Sheng002c7f72007-07-31 14:23:01 +030011122static void __init vmx_check_processor_compat(void *rtn)
11123{
11124 struct vmcs_config vmcs_conf;
11125
11126 *(int *)rtn = 0;
11127 if (setup_vmcs_config(&vmcs_conf) < 0)
11128 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011129 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011130 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11131 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11132 smp_processor_id());
11133 *(int *)rtn = -EIO;
11134 }
11135}
11136
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011137static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011138{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011139 u8 cache;
11140 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011141
Sheng Yang522c68c2009-04-27 20:35:43 +080011142 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011143 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011144 * 2. EPT with VT-d:
11145 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011146 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011147 * b. VT-d with snooping control feature: snooping control feature of
11148 * VT-d engine can guarantee the cache correctness. Just set it
11149 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011150 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011151 * consistent with host MTRR
11152 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011153 if (is_mmio) {
11154 cache = MTRR_TYPE_UNCACHABLE;
11155 goto exit;
11156 }
11157
11158 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011159 ipat = VMX_EPT_IPAT_BIT;
11160 cache = MTRR_TYPE_WRBACK;
11161 goto exit;
11162 }
11163
11164 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11165 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011166 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011167 cache = MTRR_TYPE_WRBACK;
11168 else
11169 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011170 goto exit;
11171 }
11172
Xiao Guangrongff536042015-06-15 16:55:22 +080011173 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011174
11175exit:
11176 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011177}
11178
Sheng Yang17cc3932010-01-05 19:02:27 +080011179static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011180{
Sheng Yang878403b2010-01-05 19:02:29 +080011181 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11182 return PT_DIRECTORY_LEVEL;
11183 else
11184 /* For shadow and EPT supported 1GB page */
11185 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011186}
11187
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011188static void vmcs_set_secondary_exec_control(u32 new_ctl)
11189{
11190 /*
11191 * These bits in the secondary execution controls field
11192 * are dynamic, the others are mostly based on the hypervisor
11193 * architecture and the guest's CPUID. Do not touch the
11194 * dynamic bits.
11195 */
11196 u32 mask =
11197 SECONDARY_EXEC_SHADOW_VMCS |
11198 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011199 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11200 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011201
11202 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11203
11204 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11205 (new_ctl & ~mask) | (cur_ctl & mask));
11206}
11207
David Matlack8322ebb2016-11-29 18:14:09 -080011208/*
11209 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11210 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11211 */
11212static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11213{
11214 struct vcpu_vmx *vmx = to_vmx(vcpu);
11215 struct kvm_cpuid_entry2 *entry;
11216
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011217 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11218 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011219
11220#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11221 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011222 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011223} while (0)
11224
11225 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11226 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11227 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11228 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11229 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11230 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11231 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11232 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11233 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11234 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11235 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11236 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11237 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11238 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11239 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11240
11241 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11242 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11243 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11244 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11245 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011246 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011247
11248#undef cr4_fixed1_update
11249}
11250
Liran Alon5f76f6f2018-09-14 03:25:52 +030011251static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11252{
11253 struct vcpu_vmx *vmx = to_vmx(vcpu);
11254
11255 if (kvm_mpx_supported()) {
11256 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11257
11258 if (mpx_enabled) {
11259 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11260 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11261 } else {
11262 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11263 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11264 }
11265 }
11266}
11267
Sheng Yang0e851882009-12-18 16:48:46 +080011268static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11269{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011270 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011271
Paolo Bonzini80154d72017-08-24 13:55:35 +020011272 if (cpu_has_secondary_exec_ctrls()) {
11273 vmx_compute_secondary_exec_control(vmx);
11274 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011275 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011276
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011277 if (nested_vmx_allowed(vcpu))
11278 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11279 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11280 else
11281 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11282 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011283
Liran Alon5f76f6f2018-09-14 03:25:52 +030011284 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011285 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011286 nested_vmx_entry_exit_ctls_update(vcpu);
11287 }
Sheng Yang0e851882009-12-18 16:48:46 +080011288}
11289
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011290static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11291{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011292 if (func == 1 && nested)
11293 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011294}
11295
Yang Zhang25d92082013-08-06 12:00:32 +030011296static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11297 struct x86_exception *fault)
11298{
Jan Kiszka533558b2014-01-04 18:47:20 +010011299 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011300 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011301 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011302 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011303
Bandan Dasc5f983f2017-05-05 15:25:14 -040011304 if (vmx->nested.pml_full) {
11305 exit_reason = EXIT_REASON_PML_FULL;
11306 vmx->nested.pml_full = false;
11307 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11308 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011309 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011310 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011311 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011312
11313 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011314 vmcs12->guest_physical_address = fault->address;
11315}
11316
Peter Feiner995f00a2017-06-30 17:26:32 -070011317static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11318{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011319 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011320}
11321
Nadav Har'El155a97a2013-08-05 11:07:16 +030011322/* Callbacks for nested_ept_init_mmu_context: */
11323
11324static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11325{
11326 /* return the page table to be shadowed - in our case, EPT12 */
11327 return get_vmcs12(vcpu)->ept_pointer;
11328}
11329
Sean Christopherson5b8ba412018-09-26 09:23:40 -070011330static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011331{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011332 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011333
Paolo Bonziniad896af2013-10-02 16:56:14 +020011334 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011335 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011336 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011337 nested_ept_ad_enabled(vcpu),
11338 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011339 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11340 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11341 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11342
11343 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011344}
11345
11346static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11347{
11348 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11349}
11350
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011351static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11352 u16 error_code)
11353{
11354 bool inequality, bit;
11355
11356 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11357 inequality =
11358 (error_code & vmcs12->page_fault_error_code_mask) !=
11359 vmcs12->page_fault_error_code_match;
11360 return inequality ^ bit;
11361}
11362
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011363static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11364 struct x86_exception *fault)
11365{
11366 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11367
11368 WARN_ON(!is_guest_mode(vcpu));
11369
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011370 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11371 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011372 vmcs12->vm_exit_intr_error_code = fault->error_code;
11373 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11374 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11375 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11376 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011377 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011378 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011379 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011380}
11381
Paolo Bonzinic9923842017-12-13 14:16:30 +010011382static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11383 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011384
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011385static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011386{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011387 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011388 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011389 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011390 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011391
11392 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011393 /*
11394 * Translate L1 physical address to host physical
11395 * address for vmcs02. Keep the page pinned, so this
11396 * physical address remains valid. We keep a reference
11397 * to it so we can release it later.
11398 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011399 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011400 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011401 vmx->nested.apic_access_page = NULL;
11402 }
11403 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011404 /*
11405 * If translation failed, no matter: This feature asks
11406 * to exit when accessing the given address, and if it
11407 * can never be accessed, this feature won't do
11408 * anything anyway.
11409 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011410 if (!is_error_page(page)) {
11411 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011412 hpa = page_to_phys(vmx->nested.apic_access_page);
11413 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11414 } else {
11415 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11416 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11417 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011418 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011419
11420 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011421 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011422 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011423 vmx->nested.virtual_apic_page = NULL;
11424 }
11425 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011426
11427 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011428 * If translation failed, VM entry will fail because
11429 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11430 * Failing the vm entry is _not_ what the processor
11431 * does but it's basically the only possibility we
11432 * have. We could still enter the guest if CR8 load
11433 * exits are enabled, CR8 store exits are enabled, and
11434 * virtualize APIC access is disabled; in this case
11435 * the processor would never use the TPR shadow and we
11436 * could simply clear the bit from the execution
11437 * control. But such a configuration is useless, so
11438 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011439 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011440 if (!is_error_page(page)) {
11441 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011442 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11443 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11444 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011445 }
11446
Wincy Van705699a2015-02-03 23:58:17 +080011447 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011448 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11449 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011450 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011451 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011452 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011453 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11454 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011455 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011456 vmx->nested.pi_desc_page = page;
11457 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011458 vmx->nested.pi_desc =
11459 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11460 (unsigned long)(vmcs12->posted_intr_desc_addr &
11461 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011462 vmcs_write64(POSTED_INTR_DESC_ADDR,
11463 page_to_phys(vmx->nested.pi_desc_page) +
11464 (unsigned long)(vmcs12->posted_intr_desc_addr &
11465 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011466 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011467 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011468 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11469 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011470 else
11471 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11472 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011473}
11474
Jan Kiszkaf41245002014-03-07 20:03:13 +010011475static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11476{
11477 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11478 struct vcpu_vmx *vmx = to_vmx(vcpu);
11479
Sean Christopherson4c008122018-08-27 15:21:10 -070011480 /*
11481 * A timer value of zero is architecturally guaranteed to cause
11482 * a VMExit prior to executing any instructions in the guest.
11483 */
11484 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010011485 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11486 return;
11487 }
11488
Sean Christopherson4c008122018-08-27 15:21:10 -070011489 if (vcpu->arch.virtual_tsc_khz == 0)
11490 return;
11491
Jan Kiszkaf41245002014-03-07 20:03:13 +010011492 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11493 preemption_timeout *= 1000000;
11494 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11495 hrtimer_start(&vmx->nested.preemption_timer,
11496 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11497}
11498
Jim Mattson56a20512017-07-06 16:33:06 -070011499static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11500 struct vmcs12 *vmcs12)
11501{
11502 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11503 return 0;
11504
11505 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11506 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11507 return -EINVAL;
11508
11509 return 0;
11510}
11511
Wincy Van3af18d92015-02-03 23:49:31 +080011512static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11513 struct vmcs12 *vmcs12)
11514{
Wincy Van3af18d92015-02-03 23:49:31 +080011515 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11516 return 0;
11517
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011518 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011519 return -EINVAL;
11520
11521 return 0;
11522}
11523
Jim Mattson712b12d2017-08-24 13:24:47 -070011524static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11525 struct vmcs12 *vmcs12)
11526{
11527 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11528 return 0;
11529
11530 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11531 return -EINVAL;
11532
11533 return 0;
11534}
11535
Wincy Van3af18d92015-02-03 23:49:31 +080011536/*
11537 * Merge L0's and L1's MSR bitmap, return false to indicate that
11538 * we do not use the hardware.
11539 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011540static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11541 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011542{
Wincy Van82f0dd42015-02-03 23:57:18 +080011543 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011544 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011545 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011546 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011547 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011548 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011549 *
11550 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11551 * ensures that we do not accidentally generate an L02 MSR bitmap
11552 * from the L12 MSR bitmap that is too permissive.
11553 * 2. That L1 or L2s have actually used the MSR. This avoids
11554 * unnecessarily merging of the bitmap if the MSR is unused. This
11555 * works properly because we only update the L01 MSR bitmap lazily.
11556 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11557 * updated to reflect this when L1 (or its L2s) actually write to
11558 * the MSR.
11559 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011560 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11561 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011562
Paolo Bonzinic9923842017-12-13 14:16:30 +010011563 /* Nothing to do if the MSR bitmap is not in use. */
11564 if (!cpu_has_vmx_msr_bitmap() ||
11565 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11566 return false;
11567
Ashok Raj15d45072018-02-01 22:59:43 +010011568 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011569 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011570 return false;
11571
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011572 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11573 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011574 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011575
Radim Krčmářd048c092016-08-08 20:16:22 +020011576 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011577 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11578 /*
11579 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11580 * just lets the processor take the value from the virtual-APIC page;
11581 * take those 256 bits directly from the L1 bitmap.
11582 */
11583 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11584 unsigned word = msr / BITS_PER_LONG;
11585 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11586 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011587 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011588 } else {
11589 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11590 unsigned word = msr / BITS_PER_LONG;
11591 msr_bitmap_l0[word] = ~0;
11592 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11593 }
11594 }
11595
11596 nested_vmx_disable_intercept_for_msr(
11597 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011598 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011599 MSR_TYPE_W);
11600
11601 if (nested_cpu_has_vid(vmcs12)) {
11602 nested_vmx_disable_intercept_for_msr(
11603 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011604 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011605 MSR_TYPE_W);
11606 nested_vmx_disable_intercept_for_msr(
11607 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011608 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011609 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011610 }
Ashok Raj15d45072018-02-01 22:59:43 +010011611
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011612 if (spec_ctrl)
11613 nested_vmx_disable_intercept_for_msr(
11614 msr_bitmap_l1, msr_bitmap_l0,
11615 MSR_IA32_SPEC_CTRL,
11616 MSR_TYPE_R | MSR_TYPE_W);
11617
Ashok Raj15d45072018-02-01 22:59:43 +010011618 if (pred_cmd)
11619 nested_vmx_disable_intercept_for_msr(
11620 msr_bitmap_l1, msr_bitmap_l0,
11621 MSR_IA32_PRED_CMD,
11622 MSR_TYPE_W);
11623
Wincy Vanf2b93282015-02-03 23:56:03 +080011624 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011625 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011626
11627 return true;
11628}
11629
Liran Alon61ada742018-06-23 02:35:08 +030011630static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11631 struct vmcs12 *vmcs12)
11632{
11633 struct vmcs12 *shadow;
11634 struct page *page;
11635
11636 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11637 vmcs12->vmcs_link_pointer == -1ull)
11638 return;
11639
11640 shadow = get_shadow_vmcs12(vcpu);
11641 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11642
11643 memcpy(shadow, kmap(page), VMCS12_SIZE);
11644
11645 kunmap(page);
11646 kvm_release_page_clean(page);
11647}
11648
11649static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11650 struct vmcs12 *vmcs12)
11651{
11652 struct vcpu_vmx *vmx = to_vmx(vcpu);
11653
11654 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11655 vmcs12->vmcs_link_pointer == -1ull)
11656 return;
11657
11658 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11659 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11660}
11661
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011662static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11663 struct vmcs12 *vmcs12)
11664{
11665 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11666 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11667 return -EINVAL;
11668 else
11669 return 0;
11670}
11671
Wincy Vanf2b93282015-02-03 23:56:03 +080011672static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11673 struct vmcs12 *vmcs12)
11674{
Wincy Van82f0dd42015-02-03 23:57:18 +080011675 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011676 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011677 !nested_cpu_has_vid(vmcs12) &&
11678 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011679 return 0;
11680
11681 /*
11682 * If virtualize x2apic mode is enabled,
11683 * virtualize apic access must be disabled.
11684 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011685 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11686 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011687 return -EINVAL;
11688
Wincy Van608406e2015-02-03 23:57:51 +080011689 /*
11690 * If virtual interrupt delivery is enabled,
11691 * we must exit on external interrupts.
11692 */
11693 if (nested_cpu_has_vid(vmcs12) &&
11694 !nested_exit_on_intr(vcpu))
11695 return -EINVAL;
11696
Wincy Van705699a2015-02-03 23:58:17 +080011697 /*
11698 * bits 15:8 should be zero in posted_intr_nv,
11699 * the descriptor address has been already checked
11700 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011701 *
11702 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011703 */
11704 if (nested_cpu_has_posted_intr(vmcs12) &&
11705 (!nested_cpu_has_vid(vmcs12) ||
11706 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011707 (vmcs12->posted_intr_nv & 0xff00) ||
11708 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11709 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011710 return -EINVAL;
11711
Wincy Vanf2b93282015-02-03 23:56:03 +080011712 /* tpr shadow is needed by all apicv features. */
11713 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11714 return -EINVAL;
11715
11716 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011717}
11718
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011719static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11720 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011721 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011722{
Liran Alone2536742018-06-23 02:35:02 +030011723 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011724 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011725 u64 count, addr;
11726
Liran Alone2536742018-06-23 02:35:02 +030011727 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11728 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011729 WARN_ON(1);
11730 return -EINVAL;
11731 }
11732 if (count == 0)
11733 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011734 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011735 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11736 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011737 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011738 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11739 addr_field, maxphyaddr, count, addr);
11740 return -EINVAL;
11741 }
11742 return 0;
11743}
11744
11745static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11746 struct vmcs12 *vmcs12)
11747{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011748 if (vmcs12->vm_exit_msr_load_count == 0 &&
11749 vmcs12->vm_exit_msr_store_count == 0 &&
11750 vmcs12->vm_entry_msr_load_count == 0)
11751 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011752 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011753 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011754 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011755 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011756 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011757 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011758 return -EINVAL;
11759 return 0;
11760}
11761
Bandan Dasc5f983f2017-05-05 15:25:14 -040011762static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11763 struct vmcs12 *vmcs12)
11764{
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011765 if (!nested_cpu_has_pml(vmcs12))
11766 return 0;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011767
Krish Sadhukhan55c1dcd2018-09-27 14:33:27 -040011768 if (!nested_cpu_has_ept(vmcs12) ||
11769 !page_address_valid(vcpu, vmcs12->pml_address))
11770 return -EINVAL;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011771
11772 return 0;
11773}
11774
Liran Alona8a7c022018-06-23 02:35:06 +030011775static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11776 struct vmcs12 *vmcs12)
11777{
11778 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11779 return 0;
11780
11781 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11782 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11783 return -EINVAL;
11784
11785 return 0;
11786}
11787
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011788static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11789 struct vmx_msr_entry *e)
11790{
11791 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011792 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011793 return -EINVAL;
11794 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11795 e->index == MSR_IA32_UCODE_REV)
11796 return -EINVAL;
11797 if (e->reserved != 0)
11798 return -EINVAL;
11799 return 0;
11800}
11801
11802static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11803 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011804{
11805 if (e->index == MSR_FS_BASE ||
11806 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011807 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11808 nested_vmx_msr_check_common(vcpu, e))
11809 return -EINVAL;
11810 return 0;
11811}
11812
11813static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11814 struct vmx_msr_entry *e)
11815{
11816 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11817 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011818 return -EINVAL;
11819 return 0;
11820}
11821
11822/*
11823 * Load guest's/host's msr at nested entry/exit.
11824 * return 0 for success, entry index for failure.
11825 */
11826static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11827{
11828 u32 i;
11829 struct vmx_msr_entry e;
11830 struct msr_data msr;
11831
11832 msr.host_initiated = false;
11833 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011834 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11835 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011836 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011837 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11838 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011839 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011840 }
11841 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011842 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011843 "%s check failed (%u, 0x%x, 0x%x)\n",
11844 __func__, i, e.index, e.reserved);
11845 goto fail;
11846 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011847 msr.index = e.index;
11848 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011849 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011850 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011851 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11852 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011853 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011854 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011855 }
11856 return 0;
11857fail:
11858 return i + 1;
11859}
11860
11861static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11862{
11863 u32 i;
11864 struct vmx_msr_entry e;
11865
11866 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011867 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011868 if (kvm_vcpu_read_guest(vcpu,
11869 gpa + i * sizeof(e),
11870 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011871 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011872 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11873 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011874 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011875 }
11876 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011877 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011878 "%s check failed (%u, 0x%x, 0x%x)\n",
11879 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011880 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011881 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011882 msr_info.host_initiated = false;
11883 msr_info.index = e.index;
11884 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011885 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011886 "%s cannot read MSR (%u, 0x%x)\n",
11887 __func__, i, e.index);
11888 return -EINVAL;
11889 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011890 if (kvm_vcpu_write_guest(vcpu,
11891 gpa + i * sizeof(e) +
11892 offsetof(struct vmx_msr_entry, value),
11893 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011894 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011895 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011896 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011897 return -EINVAL;
11898 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011899 }
11900 return 0;
11901}
11902
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011903static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11904{
11905 unsigned long invalid_mask;
11906
11907 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11908 return (val & invalid_mask) == 0;
11909}
11910
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011911/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011912 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11913 * emulating VM entry into a guest with EPT enabled.
11914 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11915 * is assigned to entry_failure_code on failure.
11916 */
11917static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011918 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011919{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011920 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011921 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011922 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11923 return 1;
11924 }
11925
11926 /*
11927 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11928 * must not be dereferenced.
11929 */
11930 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11931 !nested_ept) {
11932 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11933 *entry_failure_code = ENTRY_FAIL_PDPTE;
11934 return 1;
11935 }
11936 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011937 }
11938
Junaid Shahid50c28f22018-06-27 14:59:11 -070011939 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011940 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011941
11942 vcpu->arch.cr3 = cr3;
11943 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11944
11945 kvm_init_mmu(vcpu, false);
11946
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011947 return 0;
11948}
11949
Liran Alonefebf0a2018-10-08 23:42:20 +030011950/*
11951 * Returns if KVM is able to config CPU to tag TLB entries
11952 * populated by L2 differently than TLB entries populated
11953 * by L1.
11954 *
11955 * If L1 uses EPT, then TLB entries are tagged with different EPTP.
11956 *
11957 * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged
11958 * with different VPID (L1 entries are tagged with vmx->vpid
11959 * while L2 entries are tagged with vmx->nested.vpid02).
11960 */
11961static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu)
11962{
11963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11964
11965 return nested_cpu_has_ept(vmcs12) ||
11966 (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02);
11967}
11968
Jim Mattson6514dc32018-04-26 16:09:12 -070011969static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011970{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011971 struct vcpu_vmx *vmx = to_vmx(vcpu);
11972
11973 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11974 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11975 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11976 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11977 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11978 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11979 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11980 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11981 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11982 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11983 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11984 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11985 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11986 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11987 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11988 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11989 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11990 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11991 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11992 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11993 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11994 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11995 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11996 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11997 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11998 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11999 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
12000 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
12001 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12002 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12003 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012004
12005 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12006 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12007 vmcs12->guest_pending_dbg_exceptions);
12008 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12009 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12010
12011 if (nested_cpu_has_xsaves(vmcs12))
12012 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12013 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12014
12015 if (cpu_has_vmx_posted_intr())
12016 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12017
12018 /*
12019 * Whether page-faults are trapped is determined by a combination of
12020 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12021 * If enable_ept, L0 doesn't care about page faults and we should
12022 * set all of these to L1's desires. However, if !enable_ept, L0 does
12023 * care about (at least some) page faults, and because it is not easy
12024 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12025 * to exit on each and every L2 page fault. This is done by setting
12026 * MASK=MATCH=0 and (see below) EB.PF=1.
12027 * Note that below we don't need special code to set EB.PF beyond the
12028 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12029 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12030 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12031 */
12032 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12033 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12034 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12035 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12036
12037 /* All VMFUNCs are currently emulated through L0 vmexits. */
12038 if (cpu_has_vmx_vmfunc())
12039 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12040
12041 if (cpu_has_vmx_apicv()) {
12042 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12043 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12044 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12045 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12046 }
12047
12048 /*
12049 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12050 * Some constant fields are set here by vmx_set_constant_host_state().
12051 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012052 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12053 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012054 */
12055 vmx_set_constant_host_state(vmx);
12056
12057 /*
12058 * Set the MSR load/store lists to match L0's settings.
12059 */
12060 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012061 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12062 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12063 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12064 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012065
12066 set_cr4_guest_host_mask(vmx);
12067
Liran Alon62cf9bd812018-09-14 03:25:54 +030012068 if (kvm_mpx_supported()) {
12069 if (vmx->nested.nested_run_pending &&
12070 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12071 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12072 else
12073 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12074 }
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012075
12076 if (enable_vpid) {
12077 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12078 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12079 else
12080 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12081 }
12082
12083 /*
12084 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12085 */
12086 if (enable_ept) {
12087 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12088 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12089 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12090 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12091 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012092
12093 if (cpu_has_vmx_msr_bitmap())
12094 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012095}
12096
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012097/*
12098 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12099 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012100 * 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 +030012101 * guest in a way that will both be appropriate to L1's requests, and our
12102 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12103 * function also has additional necessary side-effects, like setting various
12104 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012105 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12106 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012107 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012108static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012109 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012110{
12111 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012112 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012113
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012114 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012115 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012116 vmx->nested.dirty_vmcs12 = false;
12117 }
12118
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012119 /*
12120 * First, the fields that are shadowed. This must be kept in sync
12121 * with vmx_shadow_fields.h.
12122 */
12123
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012124 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012125 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012126 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012127 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12128 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012129
Jim Mattson6514dc32018-04-26 16:09:12 -070012130 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012131 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012132 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12133 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12134 } else {
12135 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12136 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12137 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012138 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012139 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12140 vmcs12->vm_entry_intr_info_field);
12141 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12142 vmcs12->vm_entry_exception_error_code);
12143 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12144 vmcs12->vm_entry_instruction_len);
12145 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12146 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012147 vmx->loaded_vmcs->nmi_known_unmasked =
12148 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012149 } else {
12150 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12151 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012152 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012153
Jan Kiszkaf41245002014-03-07 20:03:13 +010012154 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012155
Sean Christophersonf459a702018-08-27 15:21:11 -070012156 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012157 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012158 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12159 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012160
12161 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012162 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012163 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12164 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012165 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012166 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012167 }
Wincy Van705699a2015-02-03 23:58:17 +080012168
Jan Kiszkaf41245002014-03-07 20:03:13 +010012169 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012170
Jan Kiszkaf41245002014-03-07 20:03:13 +010012171 vmx->nested.preemption_timer_expired = false;
12172 if (nested_cpu_has_preemption_timer(vmcs12))
12173 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012174
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012175 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012176 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012177
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012178 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012179 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012180 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012181 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012182 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012183 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012184 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12185 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012186 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012187 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12188 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12189 ~SECONDARY_EXEC_ENABLE_PML;
12190 exec_control |= vmcs12_exec_ctrl;
12191 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012192
Liran Alon32c7acf2018-06-23 02:35:11 +030012193 /* VMCS shadowing for L2 is emulated for now */
12194 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12195
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012196 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012197 vmcs_write16(GUEST_INTR_STATUS,
12198 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012199
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012200 /*
12201 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12202 * nested_get_vmcs12_pages will either fix it up or
12203 * remove the VM execution control.
12204 */
12205 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12206 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12207
Sean Christopherson0b665d32018-08-14 09:33:34 -070012208 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12209 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12210
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012211 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12212 }
12213
Jim Mattson83bafef2016-10-04 10:48:38 -070012214 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012215 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12216 * entry, but only if the current (host) sp changed from the value
12217 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12218 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12219 * here we just force the write to happen on entry.
12220 */
12221 vmx->host_rsp = 0;
12222
12223 exec_control = vmx_exec_control(vmx); /* L0's desires */
12224 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12225 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12226 exec_control &= ~CPU_BASED_TPR_SHADOW;
12227 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012228
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012229 /*
12230 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12231 * nested_get_vmcs12_pages can't fix it up, the illegal value
12232 * will result in a VM entry failure.
12233 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012234 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012235 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012236 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012237 } else {
12238#ifdef CONFIG_X86_64
12239 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12240 CPU_BASED_CR8_STORE_EXITING;
12241#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012242 }
12243
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012244 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080012245 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12246 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012247 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012248 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12249 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12250
12251 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12252
12253 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12254 * bitwise-or of what L1 wants to trap for L2, and what we want to
12255 * trap. Note that CR0.TS also needs updating - we do this later.
12256 */
12257 update_exception_bitmap(vcpu);
12258 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12259 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12260
Nadav Har'El8049d652013-08-05 11:07:06 +030012261 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12262 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12263 * bits are further modified by vmx_set_efer() below.
12264 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012265 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012266
12267 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12268 * emulated by vmx_set_efer(), below.
12269 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012270 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012271 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12272 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012273 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12274
Jim Mattson6514dc32018-04-26 16:09:12 -070012275 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012276 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012277 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012278 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012279 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012280 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012281 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012282
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012283 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12284
Peter Feinerc95ba922016-08-17 09:36:47 -070012285 if (kvm_has_tsc_control)
12286 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012287
12288 if (enable_vpid) {
12289 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012290 * There is no direct mapping between vpid02 and vpid12, the
12291 * vpid02 is per-vCPU for L0 and reused while the value of
12292 * vpid12 is changed w/ one invvpid during nested vmentry.
12293 * The vpid12 is allocated by L1 for L2, so it will not
12294 * influence global bitmap(for vpid01 and vpid02 allocation)
12295 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012296 */
Liran Alonefebf0a2018-10-08 23:42:20 +030012297 if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012298 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12299 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alonefebf0a2018-10-08 23:42:20 +030012300 __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012301 }
12302 } else {
Liran Alon14389212018-10-08 23:42:17 +030012303 /*
12304 * If L1 use EPT, then L0 needs to execute INVEPT on
12305 * EPTP02 instead of EPTP01. Therefore, delay TLB
12306 * flush until vmcs02->eptp is fully updated by
12307 * KVM_REQ_LOAD_CR3. Note that this assumes
12308 * KVM_REQ_TLB_FLUSH is evaluated after
12309 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12310 */
12311 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012312 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012313 }
12314
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012315 if (enable_pml) {
12316 /*
12317 * Conceptually we want to copy the PML address and index from
12318 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12319 * since we always flush the log on each vmexit, this happens
12320 * to be equivalent to simply resetting the fields in vmcs02.
12321 */
12322 ASSERT(vmx->pml_pg);
12323 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12324 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12325 }
12326
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012327 if (nested_cpu_has_ept(vmcs12))
12328 nested_ept_init_mmu_context(vcpu);
12329 else if (nested_cpu_has2(vmcs12,
12330 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Junaid Shahida468f2d2018-04-26 13:09:50 -070012331 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012332
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012333 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012334 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12335 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012336 * The CR0_READ_SHADOW is what L2 should have expected to read given
12337 * the specifications by L1; It's not enough to take
12338 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12339 * have more bits than L1 expected.
12340 */
12341 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12342 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12343
12344 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12345 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12346
Jim Mattson6514dc32018-04-26 16:09:12 -070012347 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012348 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012349 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12350 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12351 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12352 else
12353 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12354 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12355 vmx_set_efer(vcpu, vcpu->arch.efer);
12356
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012357 /*
12358 * Guest state is invalid and unrestricted guest is disabled,
12359 * which means L1 attempted VMEntry to L2 with invalid state.
12360 * Fail the VMEntry.
12361 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012362 if (vmx->emulation_required) {
12363 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012364 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012365 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012366
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012367 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012368 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012369 entry_failure_code))
12370 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012371
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012372 if (!enable_ept)
12373 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12374
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012375 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12376 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012377 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012378}
12379
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012380static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12381{
12382 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12383 nested_cpu_has_virtual_nmis(vmcs12))
12384 return -EINVAL;
12385
12386 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12387 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12388 return -EINVAL;
12389
12390 return 0;
12391}
12392
Jim Mattsonca0bde22016-11-30 12:03:46 -080012393static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12394{
12395 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson64a919f2018-09-26 09:23:39 -070012396 bool ia32e;
Jim Mattsonca0bde22016-11-30 12:03:46 -080012397
12398 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12399 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12400 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12401
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012402 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12403 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12404
Jim Mattson56a20512017-07-06 16:33:06 -070012405 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12406 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12407
Jim Mattsonca0bde22016-11-30 12:03:46 -080012408 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12409 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12410
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012411 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12412 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12413
Jim Mattson712b12d2017-08-24 13:24:47 -070012414 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12415 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12416
Jim Mattsonca0bde22016-11-30 12:03:46 -080012417 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12418 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12419
12420 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12421 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12422
Bandan Dasc5f983f2017-05-05 15:25:14 -040012423 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12424 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12425
Liran Alona8a7c022018-06-23 02:35:06 +030012426 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12427 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12428
Jim Mattsonca0bde22016-11-30 12:03:46 -080012429 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012430 vmx->nested.msrs.procbased_ctls_low,
12431 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012432 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12433 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012434 vmx->nested.msrs.secondary_ctls_low,
12435 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012436 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012437 vmx->nested.msrs.pinbased_ctls_low,
12438 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012439 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012440 vmx->nested.msrs.exit_ctls_low,
12441 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012442 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012443 vmx->nested.msrs.entry_ctls_low,
12444 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012445 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12446
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012447 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012448 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12449
Bandan Das41ab9372017-08-03 15:54:43 -040012450 if (nested_cpu_has_vmfunc(vmcs12)) {
12451 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012452 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012453 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12454
12455 if (nested_cpu_has_eptp_switching(vmcs12)) {
12456 if (!nested_cpu_has_ept(vmcs12) ||
12457 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12458 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12459 }
12460 }
Bandan Das27c42a12017-08-03 15:54:42 -040012461
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012462 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12463 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12464
Jim Mattsonca0bde22016-11-30 12:03:46 -080012465 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12466 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12467 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12468 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12469
Marc Orr04473782018-06-20 17:21:29 -070012470 /*
Sean Christopherson64a919f2018-09-26 09:23:39 -070012471 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12472 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12473 * the values of the LMA and LME bits in the field must each be that of
12474 * the host address-space size VM-exit control.
12475 */
12476 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12477 ia32e = (vmcs12->vm_exit_controls &
12478 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12479 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12480 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12481 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12482 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12483 }
12484
12485 /*
Marc Orr04473782018-06-20 17:21:29 -070012486 * From the Intel SDM, volume 3:
12487 * Fields relevant to VM-entry event injection must be set properly.
12488 * These fields are the VM-entry interruption-information field, the
12489 * VM-entry exception error code, and the VM-entry instruction length.
12490 */
12491 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12492 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12493 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12494 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12495 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12496 bool should_have_error_code;
12497 bool urg = nested_cpu_has2(vmcs12,
12498 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12499 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12500
12501 /* VM-entry interruption-info field: interruption type */
12502 if (intr_type == INTR_TYPE_RESERVED ||
12503 (intr_type == INTR_TYPE_OTHER_EVENT &&
12504 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12505 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12506
12507 /* VM-entry interruption-info field: vector */
12508 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12509 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12510 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12511 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12512
12513 /* VM-entry interruption-info field: deliver error code */
12514 should_have_error_code =
12515 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12516 x86_exception_has_error_code(vector);
12517 if (has_error_code != should_have_error_code)
12518 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12519
12520 /* VM-entry exception error code */
12521 if (has_error_code &&
12522 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12523 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12524
12525 /* VM-entry interruption-info field: reserved bits */
12526 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12527 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12528
12529 /* VM-entry instruction length */
12530 switch (intr_type) {
12531 case INTR_TYPE_SOFT_EXCEPTION:
12532 case INTR_TYPE_SOFT_INTR:
12533 case INTR_TYPE_PRIV_SW_EXCEPTION:
12534 if ((vmcs12->vm_entry_instruction_len > 15) ||
12535 (vmcs12->vm_entry_instruction_len == 0 &&
12536 !nested_cpu_has_zero_length_injection(vcpu)))
12537 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12538 }
12539 }
12540
Sean Christopherson5b8ba412018-09-26 09:23:40 -070012541 if (nested_cpu_has_ept(vmcs12) &&
12542 !valid_ept_address(vcpu, vmcs12->ept_pointer))
12543 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12544
Jim Mattsonca0bde22016-11-30 12:03:46 -080012545 return 0;
12546}
12547
Liran Alonf145d902018-06-23 02:35:07 +030012548static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12549 struct vmcs12 *vmcs12)
12550{
12551 int r;
12552 struct page *page;
12553 struct vmcs12 *shadow;
12554
12555 if (vmcs12->vmcs_link_pointer == -1ull)
12556 return 0;
12557
12558 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12559 return -EINVAL;
12560
12561 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12562 if (is_error_page(page))
12563 return -EINVAL;
12564
12565 r = 0;
12566 shadow = kmap(page);
12567 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12568 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12569 r = -EINVAL;
12570 kunmap(page);
12571 kvm_release_page_clean(page);
12572 return r;
12573}
12574
Jim Mattsonca0bde22016-11-30 12:03:46 -080012575static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12576 u32 *exit_qual)
12577{
12578 bool ia32e;
12579
12580 *exit_qual = ENTRY_FAIL_DEFAULT;
12581
12582 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12583 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12584 return 1;
12585
Liran Alonf145d902018-06-23 02:35:07 +030012586 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012587 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12588 return 1;
12589 }
12590
12591 /*
12592 * If the load IA32_EFER VM-entry control is 1, the following checks
12593 * are performed on the field for the IA32_EFER MSR:
12594 * - Bits reserved in the IA32_EFER MSR must be 0.
12595 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12596 * the IA-32e mode guest VM-exit control. It must also be identical
12597 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12598 * CR0.PG) is 1.
12599 */
12600 if (to_vmx(vcpu)->nested.nested_run_pending &&
12601 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12602 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12603 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12604 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12605 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12606 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12607 return 1;
12608 }
12609
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012610 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12611 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12612 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12613 return 1;
12614
Jim Mattsonca0bde22016-11-30 12:03:46 -080012615 return 0;
12616}
12617
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012618/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012619 * If exit_qual is NULL, this is being called from state restore (either RSM
12620 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012621 */
12622static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012623{
12624 struct vcpu_vmx *vmx = to_vmx(vcpu);
12625 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012626 bool from_vmentry = !!exit_qual;
12627 u32 dummy_exit_qual;
Paolo Bonzini7e712682018-10-03 13:44:26 +020012628 bool evaluate_pending_interrupts;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012629 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012630
Paolo Bonzini7e712682018-10-03 13:44:26 +020012631 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12632 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12633 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12634 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012635
Jim Mattson858e25c2016-11-30 12:03:47 -080012636 enter_guest_mode(vcpu);
12637
12638 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12639 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012640 if (kvm_mpx_supported() &&
12641 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12642 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012643
Jim Mattsonde3a0022017-11-27 17:22:25 -060012644 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012645 vmx_segment_cache_clear(vmx);
12646
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012647 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12648 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12649
12650 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012651 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012652 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012653
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012654 if (from_vmentry) {
12655 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012656
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012657 r = EXIT_REASON_MSR_LOAD_FAIL;
12658 *exit_qual = nested_vmx_load_msr(vcpu,
12659 vmcs12->vm_entry_msr_load_addr,
12660 vmcs12->vm_entry_msr_load_count);
12661 if (*exit_qual)
12662 goto fail;
12663 } else {
12664 /*
12665 * The MMU is not initialized to point at the right entities yet and
12666 * "get pages" would need to read data from the guest (i.e. we will
12667 * need to perform gpa to hpa translation). Request a call
12668 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12669 * have already been set at vmentry time and should not be reset.
12670 */
12671 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12672 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012673
Jim Mattson858e25c2016-11-30 12:03:47 -080012674 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012675 * If L1 had a pending IRQ/NMI until it executed
12676 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12677 * disallowed (e.g. interrupts disabled), L0 needs to
12678 * evaluate if this pending event should cause an exit from L2
12679 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12680 * intercept EXTERNAL_INTERRUPT).
12681 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012682 * Usually this would be handled by the processor noticing an
12683 * IRQ/NMI window request, or checking RVI during evaluation of
12684 * pending virtual interrupts. However, this setting was done
12685 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12686 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012687 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012688 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012689 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012690
12691 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012692 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12693 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12694 * returned as far as L1 is concerned. It will only return (and set
12695 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12696 */
12697 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012698
12699fail:
12700 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12701 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12702 leave_guest_mode(vcpu);
12703 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012704 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012705}
12706
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012707/*
12708 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12709 * for running an L2 nested guest.
12710 */
12711static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12712{
12713 struct vmcs12 *vmcs12;
12714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012715 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012716 u32 exit_qual;
12717 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012718
Kyle Hueyeb277562016-11-29 12:40:39 -080012719 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012720 return 1;
12721
Kyle Hueyeb277562016-11-29 12:40:39 -080012722 if (!nested_vmx_check_vmcs12(vcpu))
12723 goto out;
12724
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012725 vmcs12 = get_vmcs12(vcpu);
12726
Liran Alona6192d42018-06-23 02:35:04 +030012727 /*
12728 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12729 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12730 * rather than RFLAGS.ZF, and no error number is stored to the
12731 * VM-instruction error field.
12732 */
12733 if (vmcs12->hdr.shadow_vmcs) {
12734 nested_vmx_failInvalid(vcpu);
12735 goto out;
12736 }
12737
Abel Gordon012f83c2013-04-18 14:39:25 +030012738 if (enable_shadow_vmcs)
12739 copy_shadow_to_vmcs12(vmx);
12740
Nadav Har'El7c177932011-05-25 23:12:04 +030012741 /*
12742 * The nested entry process starts with enforcing various prerequisites
12743 * on vmcs12 as required by the Intel SDM, and act appropriately when
12744 * they fail: As the SDM explains, some conditions should cause the
12745 * instruction to fail, while others will cause the instruction to seem
12746 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12747 * To speed up the normal (success) code path, we should avoid checking
12748 * for misconfigurations which will anyway be caught by the processor
12749 * when using the merged vmcs02.
12750 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012751 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12752 nested_vmx_failValid(vcpu,
12753 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12754 goto out;
12755 }
12756
Nadav Har'El7c177932011-05-25 23:12:04 +030012757 if (vmcs12->launch_state == launch) {
12758 nested_vmx_failValid(vcpu,
12759 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12760 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012761 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012762 }
12763
Jim Mattsonca0bde22016-11-30 12:03:46 -080012764 ret = check_vmentry_prereqs(vcpu, vmcs12);
12765 if (ret) {
12766 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012767 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012768 }
12769
Nadav Har'El7c177932011-05-25 23:12:04 +030012770 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012771 * After this point, the trap flag no longer triggers a singlestep trap
12772 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12773 * This is not 100% correct; for performance reasons, we delegate most
12774 * of the checks on host state to the processor. If those fail,
12775 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012776 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012777 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012778
Jim Mattsonca0bde22016-11-30 12:03:46 -080012779 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12780 if (ret) {
12781 nested_vmx_entry_failure(vcpu, vmcs12,
12782 EXIT_REASON_INVALID_STATE, exit_qual);
12783 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012784 }
12785
12786 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012787 * We're finally done with prerequisite checking, and can start with
12788 * the nested entry.
12789 */
12790
Jim Mattson6514dc32018-04-26 16:09:12 -070012791 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012792 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012793 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012794 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012795 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012796 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012797 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012798
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012799 /* Hide L1D cache contents from the nested guest. */
12800 vmx->vcpu.arch.l1tf_flush_l1d = true;
12801
Chao Gao135a06c2018-02-11 10:06:30 +080012802 /*
Liran Alon61ada742018-06-23 02:35:08 +030012803 * Must happen outside of enter_vmx_non_root_mode() as it will
12804 * also be used as part of restoring nVMX state for
12805 * snapshot restore (migration).
12806 *
12807 * In this flow, it is assumed that vmcs12 cache was
12808 * trasferred as part of captured nVMX state and should
12809 * therefore not be read from guest memory (which may not
12810 * exist on destination host yet).
12811 */
12812 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12813
12814 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012815 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12816 * by event injection, halt vcpu.
12817 */
12818 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012819 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12820 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012821 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012822 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012823 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012824
12825out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012826 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012827}
12828
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012829/*
12830 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12831 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12832 * This function returns the new value we should put in vmcs12.guest_cr0.
12833 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12834 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12835 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12836 * didn't trap the bit, because if L1 did, so would L0).
12837 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12838 * been modified by L2, and L1 knows it. So just leave the old value of
12839 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12840 * isn't relevant, because if L0 traps this bit it can set it to anything.
12841 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12842 * changed these bits, and therefore they need to be updated, but L0
12843 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12844 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12845 */
12846static inline unsigned long
12847vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12848{
12849 return
12850 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12851 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12852 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12853 vcpu->arch.cr0_guest_owned_bits));
12854}
12855
12856static inline unsigned long
12857vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12858{
12859 return
12860 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12861 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12862 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12863 vcpu->arch.cr4_guest_owned_bits));
12864}
12865
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012866static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12867 struct vmcs12 *vmcs12)
12868{
12869 u32 idt_vectoring;
12870 unsigned int nr;
12871
Wanpeng Li664f8e22017-08-24 03:35:09 -070012872 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012873 nr = vcpu->arch.exception.nr;
12874 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12875
12876 if (kvm_exception_is_soft(nr)) {
12877 vmcs12->vm_exit_instruction_len =
12878 vcpu->arch.event_exit_inst_len;
12879 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12880 } else
12881 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12882
12883 if (vcpu->arch.exception.has_error_code) {
12884 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12885 vmcs12->idt_vectoring_error_code =
12886 vcpu->arch.exception.error_code;
12887 }
12888
12889 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012890 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012891 vmcs12->idt_vectoring_info_field =
12892 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012893 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012894 nr = vcpu->arch.interrupt.nr;
12895 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12896
12897 if (vcpu->arch.interrupt.soft) {
12898 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12899 vmcs12->vm_entry_instruction_len =
12900 vcpu->arch.event_exit_inst_len;
12901 } else
12902 idt_vectoring |= INTR_TYPE_EXT_INTR;
12903
12904 vmcs12->idt_vectoring_info_field = idt_vectoring;
12905 }
12906}
12907
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012908static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12909{
12910 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012911 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012912 bool block_nested_events =
12913 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012914
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012915 if (vcpu->arch.exception.pending &&
12916 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012917 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012918 return -EBUSY;
12919 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012920 return 0;
12921 }
12922
Jan Kiszkaf41245002014-03-07 20:03:13 +010012923 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12924 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012925 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012926 return -EBUSY;
12927 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12928 return 0;
12929 }
12930
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012931 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012932 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012933 return -EBUSY;
12934 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12935 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12936 INTR_INFO_VALID_MASK, 0);
12937 /*
12938 * The NMI-triggered VM exit counts as injection:
12939 * clear this one and block further NMIs.
12940 */
12941 vcpu->arch.nmi_pending = 0;
12942 vmx_set_nmi_mask(vcpu, true);
12943 return 0;
12944 }
12945
12946 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12947 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012948 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012949 return -EBUSY;
12950 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012951 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012952 }
12953
David Hildenbrand6342c502017-01-25 11:58:58 +010012954 vmx_complete_nested_posted_interrupt(vcpu);
12955 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012956}
12957
Sean Christophersond264ee02018-08-27 15:21:12 -070012958static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12959{
12960 to_vmx(vcpu)->req_immediate_exit = true;
12961}
12962
Jan Kiszkaf41245002014-03-07 20:03:13 +010012963static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12964{
12965 ktime_t remaining =
12966 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12967 u64 value;
12968
12969 if (ktime_to_ns(remaining) <= 0)
12970 return 0;
12971
12972 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12973 do_div(value, 1000000);
12974 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12975}
12976
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012977/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012978 * Update the guest state fields of vmcs12 to reflect changes that
12979 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12980 * VM-entry controls is also updated, since this is really a guest
12981 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012982 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012983static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012984{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012985 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12986 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12987
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012988 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12989 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12990 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12991
12992 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12993 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12994 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12995 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12996 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12997 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12998 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12999 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
13000 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
13001 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
13002 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13003 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13004 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13005 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13006 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13007 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13008 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13009 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13010 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13011 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13012 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13013 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13014 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13015 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13016 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13017 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13018 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13019 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13020 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13021 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13022 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13023 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13024 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13025 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13026 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13027 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13028
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013029 vmcs12->guest_interruptibility_info =
13030 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13031 vmcs12->guest_pending_dbg_exceptions =
13032 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013033 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13034 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13035 else
13036 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013037
Jan Kiszkaf41245002014-03-07 20:03:13 +010013038 if (nested_cpu_has_preemption_timer(vmcs12)) {
13039 if (vmcs12->vm_exit_controls &
13040 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13041 vmcs12->vmx_preemption_timer_value =
13042 vmx_get_preemption_timer_value(vcpu);
13043 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13044 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013045
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013046 /*
13047 * In some cases (usually, nested EPT), L2 is allowed to change its
13048 * own CR3 without exiting. If it has changed it, we must keep it.
13049 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13050 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13051 *
13052 * Additionally, restore L2's PDPTR to vmcs12.
13053 */
13054 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013055 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013056 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13057 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13058 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13059 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13060 }
13061
Jim Mattsond281e132017-06-01 12:44:46 -070013062 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013063
Wincy Van608406e2015-02-03 23:57:51 +080013064 if (nested_cpu_has_vid(vmcs12))
13065 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13066
Jan Kiszkac18911a2013-03-13 16:06:41 +010013067 vmcs12->vm_entry_controls =
13068 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013069 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013070
Jan Kiszka2996fca2014-06-16 13:59:43 +020013071 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13072 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13073 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13074 }
13075
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013076 /* TODO: These cannot have changed unless we have MSR bitmaps and
13077 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013078 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013079 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013080 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13081 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013082 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13083 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13084 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013085 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013086 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013087}
13088
13089/*
13090 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13091 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13092 * and this function updates it to reflect the changes to the guest state while
13093 * L2 was running (and perhaps made some exits which were handled directly by L0
13094 * without going back to L1), and to reflect the exit reason.
13095 * Note that we do not have to copy here all VMCS fields, just those that
13096 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13097 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13098 * which already writes to vmcs12 directly.
13099 */
13100static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13101 u32 exit_reason, u32 exit_intr_info,
13102 unsigned long exit_qualification)
13103{
13104 /* update guest state fields: */
13105 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013106
13107 /* update exit information fields: */
13108
Jan Kiszka533558b2014-01-04 18:47:20 +010013109 vmcs12->vm_exit_reason = exit_reason;
13110 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013111 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013112
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013113 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013114 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13115 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13116
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013117 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013118 vmcs12->launch_state = 1;
13119
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013120 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13121 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013122 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013123
13124 /*
13125 * Transfer the event that L0 or L1 may wanted to inject into
13126 * L2 to IDT_VECTORING_INFO_FIELD.
13127 */
13128 vmcs12_save_pending_event(vcpu, vmcs12);
13129 }
13130
13131 /*
13132 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13133 * preserved above and would only end up incorrectly in L1.
13134 */
13135 vcpu->arch.nmi_injected = false;
13136 kvm_clear_exception_queue(vcpu);
13137 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013138}
13139
13140/*
13141 * A part of what we need to when the nested L2 guest exits and we want to
13142 * run its L1 parent, is to reset L1's guest state to the host state specified
13143 * in vmcs12.
13144 * This function is to be called not only on normal nested exit, but also on
13145 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13146 * Failures During or After Loading Guest State").
13147 * This function should be called when the active VMCS is L1's (vmcs01).
13148 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013149static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13150 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013151{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013152 struct kvm_segment seg;
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013153 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013154
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013155 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13156 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013157 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013158 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13159 else
13160 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13161 vmx_set_efer(vcpu, vcpu->arch.efer);
13162
13163 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13164 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013165 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013166 /*
13167 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013168 * actually changed, because vmx_set_cr0 refers to efer set above.
13169 *
13170 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13171 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013172 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013173 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020013174 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013175
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013176 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013177 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013178 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013179
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013180 nested_ept_uninit_mmu_context(vcpu);
13181
13182 /*
13183 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13184 * couldn't have changed.
13185 */
13186 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13187 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13188
13189 if (!enable_ept)
13190 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013191
Liran Alon6f1e03b2018-05-22 17:16:14 +030013192 /*
Liran Alonefebf0a2018-10-08 23:42:20 +030013193 * If vmcs01 doesn't use VPID, CPU flushes TLB on every
Liran Alon6f1e03b2018-05-22 17:16:14 +030013194 * VMEntry/VMExit. Thus, no need to flush TLB.
13195 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013196 * If vmcs12 doesn't use VPID, L1 expects TLB to be
13197 * flushed on every VMEntry/VMExit.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013198 *
Liran Alonefebf0a2018-10-08 23:42:20 +030013199 * Otherwise, we can preserve TLB entries as long as we are
13200 * able to tag L1 TLB entries differently than L2 TLB entries.
Liran Alon14389212018-10-08 23:42:17 +030013201 *
13202 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13203 * and therefore we request the TLB flush to happen only after VMCS EPTP
13204 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013205 */
13206 if (enable_vpid &&
Liran Alonefebf0a2018-10-08 23:42:20 +030013207 (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) {
Liran Alon14389212018-10-08 23:42:17 +030013208 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013209 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013210
13211 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13212 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13213 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13214 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13215 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013216 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13217 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013218
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013219 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13220 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13221 vmcs_write64(GUEST_BNDCFGS, 0);
13222
Jan Kiszka44811c02013-08-04 17:17:27 +020013223 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013224 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013225 vcpu->arch.pat = vmcs12->host_ia32_pat;
13226 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013227 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13228 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13229 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013230
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013231 /* Set L1 segment info according to Intel SDM
13232 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13233 seg = (struct kvm_segment) {
13234 .base = 0,
13235 .limit = 0xFFFFFFFF,
13236 .selector = vmcs12->host_cs_selector,
13237 .type = 11,
13238 .present = 1,
13239 .s = 1,
13240 .g = 1
13241 };
13242 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13243 seg.l = 1;
13244 else
13245 seg.db = 1;
13246 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13247 seg = (struct kvm_segment) {
13248 .base = 0,
13249 .limit = 0xFFFFFFFF,
13250 .type = 3,
13251 .present = 1,
13252 .s = 1,
13253 .db = 1,
13254 .g = 1
13255 };
13256 seg.selector = vmcs12->host_ds_selector;
13257 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13258 seg.selector = vmcs12->host_es_selector;
13259 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13260 seg.selector = vmcs12->host_ss_selector;
13261 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13262 seg.selector = vmcs12->host_fs_selector;
13263 seg.base = vmcs12->host_fs_base;
13264 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13265 seg.selector = vmcs12->host_gs_selector;
13266 seg.base = vmcs12->host_gs_base;
13267 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13268 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013269 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013270 .limit = 0x67,
13271 .selector = vmcs12->host_tr_selector,
13272 .type = 11,
13273 .present = 1
13274 };
13275 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13276
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013277 kvm_set_dr(vcpu, 7, 0x400);
13278 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013279
Wincy Van3af18d92015-02-03 23:49:31 +080013280 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013281 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013282
Wincy Vanff651cb2014-12-11 08:52:58 +030013283 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13284 vmcs12->vm_exit_msr_load_count))
13285 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013286}
13287
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013288static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx)
13289{
13290 struct shared_msr_entry *efer_msr;
13291 unsigned int i;
13292
13293 if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER)
13294 return vmcs_read64(GUEST_IA32_EFER);
13295
13296 if (cpu_has_load_ia32_efer)
13297 return host_efer;
13298
13299 for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) {
13300 if (vmx->msr_autoload.guest.val[i].index == MSR_EFER)
13301 return vmx->msr_autoload.guest.val[i].value;
13302 }
13303
13304 efer_msr = find_msr_entry(vmx, MSR_EFER);
13305 if (efer_msr)
13306 return efer_msr->data;
13307
13308 return host_efer;
13309}
13310
13311static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
13312{
13313 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13314 struct vcpu_vmx *vmx = to_vmx(vcpu);
13315 struct vmx_msr_entry g, h;
13316 struct msr_data msr;
13317 gpa_t gpa;
13318 u32 i, j;
13319
13320 vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT);
13321
13322 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
13323 /*
13324 * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set
13325 * as vmcs01.GUEST_DR7 contains a userspace defined value
13326 * and vcpu->arch.dr7 is not squirreled away before the
13327 * nested VMENTER (not worth adding a variable in nested_vmx).
13328 */
13329 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
13330 kvm_set_dr(vcpu, 7, DR7_FIXED_1);
13331 else
13332 WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7)));
13333 }
13334
13335 /*
13336 * Note that calling vmx_set_{efer,cr0,cr4} is important as they
13337 * handle a variety of side effects to KVM's software model.
13338 */
13339 vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
13340
13341 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
13342 vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
13343
13344 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
13345 vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW));
13346
13347 nested_ept_uninit_mmu_context(vcpu);
13348 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
13349 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
13350
13351 /*
13352 * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs
13353 * from vmcs01 (if necessary). The PDPTRs are not loaded on
13354 * VMFail, like everything else we just need to ensure our
13355 * software model is up-to-date.
13356 */
13357 ept_save_pdptrs(vcpu);
13358
13359 kvm_mmu_reset_context(vcpu);
13360
13361 if (cpu_has_vmx_msr_bitmap())
13362 vmx_update_msr_bitmap(vcpu);
13363
13364 /*
13365 * This nasty bit of open coding is a compromise between blindly
13366 * loading L1's MSRs using the exit load lists (incorrect emulation
13367 * of VMFail), leaving the nested VM's MSRs in the software model
13368 * (incorrect behavior) and snapshotting the modified MSRs (too
13369 * expensive since the lists are unbound by hardware). For each
13370 * MSR that was (prematurely) loaded from the nested VMEntry load
13371 * list, reload it from the exit load list if it exists and differs
13372 * from the guest value. The intent is to stuff host state as
13373 * silently as possible, not to fully process the exit load list.
13374 */
13375 msr.host_initiated = false;
13376 for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) {
13377 gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g));
13378 if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) {
13379 pr_debug_ratelimited(
13380 "%s read MSR index failed (%u, 0x%08llx)\n",
13381 __func__, i, gpa);
13382 goto vmabort;
13383 }
13384
13385 for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) {
13386 gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h));
13387 if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) {
13388 pr_debug_ratelimited(
13389 "%s read MSR failed (%u, 0x%08llx)\n",
13390 __func__, j, gpa);
13391 goto vmabort;
13392 }
13393 if (h.index != g.index)
13394 continue;
13395 if (h.value == g.value)
13396 break;
13397
13398 if (nested_vmx_load_msr_check(vcpu, &h)) {
13399 pr_debug_ratelimited(
13400 "%s check failed (%u, 0x%x, 0x%x)\n",
13401 __func__, j, h.index, h.reserved);
13402 goto vmabort;
13403 }
13404
13405 msr.index = h.index;
13406 msr.data = h.value;
13407 if (kvm_set_msr(vcpu, &msr)) {
13408 pr_debug_ratelimited(
13409 "%s WRMSR failed (%u, 0x%x, 0x%llx)\n",
13410 __func__, j, h.index, h.value);
13411 goto vmabort;
13412 }
13413 }
13414 }
13415
13416 return;
13417
13418vmabort:
13419 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
13420}
13421
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013422/*
13423 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13424 * and modify vmcs12 to make it see what it would expect to see there if
13425 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13426 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013427static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13428 u32 exit_intr_info,
13429 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013430{
13431 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013432 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13433
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013434 /* trying to cancel vmlaunch/vmresume is a bug */
13435 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13436
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013437 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013438 * The only expected VM-instruction error is "VM entry with
13439 * invalid control field(s)." Anything else indicates a
13440 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013441 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013442 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13443 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13444
13445 leave_guest_mode(vcpu);
13446
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013447 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13448 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13449
Jim Mattson4f350c62017-09-14 16:31:44 -070013450 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013451 if (exit_reason == -1)
13452 sync_vmcs12(vcpu, vmcs12);
13453 else
13454 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13455 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013456
Liran Alon61ada742018-06-23 02:35:08 +030013457 /*
13458 * Must happen outside of sync_vmcs12() as it will
13459 * also be used to capture vmcs12 cache as part of
13460 * capturing nVMX state for snapshot (migration).
13461 *
13462 * Otherwise, this flush will dirty guest memory at a
13463 * point it is already assumed by user-space to be
13464 * immutable.
13465 */
13466 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13467
Jim Mattson4f350c62017-09-14 16:31:44 -070013468 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13469 vmcs12->vm_exit_msr_store_count))
13470 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013471 }
13472
Jim Mattson4f350c62017-09-14 16:31:44 -070013473 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013474 vm_entry_controls_reset_shadow(vmx);
13475 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013476 vmx_segment_cache_clear(vmx);
13477
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013478 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013479 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13480 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013481 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013482
Peter Feinerc95ba922016-08-17 09:36:47 -070013483 if (kvm_has_tsc_control)
13484 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013485
Jim Mattson8d860bb2018-05-09 16:56:05 -040013486 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13487 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13488 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013489 } else if (!nested_cpu_has_ept(vmcs12) &&
13490 nested_cpu_has2(vmcs12,
13491 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013492 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013493 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013494
13495 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13496 vmx->host_rsp = 0;
13497
13498 /* Unpin physical memory we referred to in vmcs02 */
13499 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013500 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013501 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013502 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013503 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013504 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013505 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013506 }
Wincy Van705699a2015-02-03 23:58:17 +080013507 if (vmx->nested.pi_desc_page) {
13508 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013509 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013510 vmx->nested.pi_desc_page = NULL;
13511 vmx->nested.pi_desc = NULL;
13512 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013513
13514 /*
Tang Chen38b99172014-09-24 15:57:54 +080013515 * We are now running in L2, mmu_notifier will force to reload the
13516 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13517 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013518 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013519
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013520 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013521 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013522
13523 /* in case we halted in L2 */
13524 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013525
13526 if (likely(!vmx->fail)) {
13527 /*
13528 * TODO: SDM says that with acknowledge interrupt on
13529 * exit, bit 31 of the VM-exit interrupt information
13530 * (valid interrupt) is always set to 1 on
13531 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13532 * need kvm_cpu_has_interrupt(). See the commit
13533 * message for details.
13534 */
13535 if (nested_exit_intr_ack_set(vcpu) &&
13536 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13537 kvm_cpu_has_interrupt(vcpu)) {
13538 int irq = kvm_cpu_get_interrupt(vcpu);
13539 WARN_ON(irq < 0);
13540 vmcs12->vm_exit_intr_info = irq |
13541 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13542 }
13543
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013544 if (exit_reason != -1)
13545 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13546 vmcs12->exit_qualification,
13547 vmcs12->idt_vectoring_info_field,
13548 vmcs12->vm_exit_intr_info,
13549 vmcs12->vm_exit_intr_error_code,
13550 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013551
13552 load_vmcs12_host_state(vcpu, vmcs12);
13553
13554 return;
13555 }
13556
13557 /*
13558 * After an early L2 VM-entry failure, we're now back
13559 * in L1 which thinks it just finished a VMLAUNCH or
13560 * VMRESUME instruction, so we need to set the failure
13561 * flag and the VM-instruction error field of the VMCS
13562 * accordingly.
13563 */
13564 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013565
Sean Christophersonbd18bff2018-08-22 14:57:07 -070013566 /*
13567 * Restore L1's host state to KVM's software model. We're here
13568 * because a consistency check was caught by hardware, which
13569 * means some amount of guest state has been propagated to KVM's
13570 * model and needs to be unwound to the host's state.
13571 */
13572 nested_vmx_restore_host_state(vcpu);
Wanpeng Li5af41572017-11-05 16:54:49 -080013573
Jim Mattson4f350c62017-09-14 16:31:44 -070013574 /*
13575 * The emulated instruction was already skipped in
13576 * nested_vmx_run, but the updated RIP was never
13577 * written back to the vmcs01.
13578 */
13579 skip_emulated_instruction(vcpu);
13580 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013581}
13582
Nadav Har'El7c177932011-05-25 23:12:04 +030013583/*
Jan Kiszka42124922014-01-04 18:47:19 +010013584 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13585 */
13586static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13587{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013588 if (is_guest_mode(vcpu)) {
13589 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013590 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013591 }
Jan Kiszka42124922014-01-04 18:47:19 +010013592 free_nested(to_vmx(vcpu));
13593}
13594
13595/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013596 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13597 * 23.7 "VM-entry failures during or after loading guest state" (this also
13598 * lists the acceptable exit-reason and exit-qualification parameters).
13599 * It should only be called before L2 actually succeeded to run, and when
13600 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13601 */
13602static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13603 struct vmcs12 *vmcs12,
13604 u32 reason, unsigned long qualification)
13605{
13606 load_vmcs12_host_state(vcpu, vmcs12);
13607 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13608 vmcs12->exit_qualification = qualification;
13609 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013610 if (enable_shadow_vmcs)
13611 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013612}
13613
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013614static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13615 struct x86_instruction_info *info,
13616 enum x86_intercept_stage stage)
13617{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013618 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13619 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13620
13621 /*
13622 * RDPID causes #UD if disabled through secondary execution controls.
13623 * Because it is marked as EmulateOnUD, we need to intercept it here.
13624 */
13625 if (info->intercept == x86_intercept_rdtscp &&
13626 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13627 ctxt->exception.vector = UD_VECTOR;
13628 ctxt->exception.error_code_valid = false;
13629 return X86EMUL_PROPAGATE_FAULT;
13630 }
13631
13632 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013633 return X86EMUL_CONTINUE;
13634}
13635
Yunhong Jiang64672c92016-06-13 14:19:59 -070013636#ifdef CONFIG_X86_64
13637/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13638static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13639 u64 divisor, u64 *result)
13640{
13641 u64 low = a << shift, high = a >> (64 - shift);
13642
13643 /* To avoid the overflow on divq */
13644 if (high >= divisor)
13645 return 1;
13646
13647 /* Low hold the result, high hold rem which is discarded */
13648 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13649 "rm" (divisor), "0" (low), "1" (high));
13650 *result = low;
13651
13652 return 0;
13653}
13654
13655static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13656{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013657 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013658 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013659
13660 if (kvm_mwait_in_guest(vcpu->kvm))
13661 return -EOPNOTSUPP;
13662
13663 vmx = to_vmx(vcpu);
13664 tscl = rdtsc();
13665 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13666 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013667 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13668
13669 if (delta_tsc > lapic_timer_advance_cycles)
13670 delta_tsc -= lapic_timer_advance_cycles;
13671 else
13672 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013673
13674 /* Convert to host delta tsc if tsc scaling is enabled */
13675 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13676 u64_shl_div_u64(delta_tsc,
13677 kvm_tsc_scaling_ratio_frac_bits,
13678 vcpu->arch.tsc_scaling_ratio,
13679 &delta_tsc))
13680 return -ERANGE;
13681
13682 /*
13683 * If the delta tsc can't fit in the 32 bit after the multi shift,
13684 * we can't use the preemption timer.
13685 * It's possible that it fits on later vmentries, but checking
13686 * on every vmentry is costly so we just use an hrtimer.
13687 */
13688 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13689 return -ERANGE;
13690
13691 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013692 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013693}
13694
13695static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13696{
Sean Christophersonf459a702018-08-27 15:21:11 -070013697 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013698}
13699#endif
13700
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013701static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013702{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013703 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013704 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013705}
13706
Kai Huang843e4332015-01-28 10:54:28 +080013707static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13708 struct kvm_memory_slot *slot)
13709{
13710 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13711 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13712}
13713
13714static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13715 struct kvm_memory_slot *slot)
13716{
13717 kvm_mmu_slot_set_dirty(kvm, slot);
13718}
13719
13720static void vmx_flush_log_dirty(struct kvm *kvm)
13721{
13722 kvm_flush_pml_buffers(kvm);
13723}
13724
Bandan Dasc5f983f2017-05-05 15:25:14 -040013725static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13726{
13727 struct vmcs12 *vmcs12;
13728 struct vcpu_vmx *vmx = to_vmx(vcpu);
13729 gpa_t gpa;
13730 struct page *page = NULL;
13731 u64 *pml_address;
13732
13733 if (is_guest_mode(vcpu)) {
13734 WARN_ON_ONCE(vmx->nested.pml_full);
13735
13736 /*
13737 * Check if PML is enabled for the nested guest.
13738 * Whether eptp bit 6 is set is already checked
13739 * as part of A/D emulation.
13740 */
13741 vmcs12 = get_vmcs12(vcpu);
13742 if (!nested_cpu_has_pml(vmcs12))
13743 return 0;
13744
Dan Carpenter47698862017-05-10 22:43:17 +030013745 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013746 vmx->nested.pml_full = true;
13747 return 1;
13748 }
13749
13750 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13751
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013752 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13753 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013754 return 0;
13755
13756 pml_address = kmap(page);
13757 pml_address[vmcs12->guest_pml_index--] = gpa;
13758 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013759 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013760 }
13761
13762 return 0;
13763}
13764
Kai Huang843e4332015-01-28 10:54:28 +080013765static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13766 struct kvm_memory_slot *memslot,
13767 gfn_t offset, unsigned long mask)
13768{
13769 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13770}
13771
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013772static void __pi_post_block(struct kvm_vcpu *vcpu)
13773{
13774 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13775 struct pi_desc old, new;
13776 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013777
13778 do {
13779 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013780 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13781 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013782
13783 dest = cpu_physical_id(vcpu->cpu);
13784
13785 if (x2apic_enabled())
13786 new.ndst = dest;
13787 else
13788 new.ndst = (dest << 8) & 0xFF00;
13789
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013790 /* set 'NV' to 'notification vector' */
13791 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013792 } while (cmpxchg64(&pi_desc->control, old.control,
13793 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013794
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013795 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13796 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013797 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013798 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013799 vcpu->pre_pcpu = -1;
13800 }
13801}
13802
Feng Wuefc64402015-09-18 22:29:51 +080013803/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013804 * This routine does the following things for vCPU which is going
13805 * to be blocked if VT-d PI is enabled.
13806 * - Store the vCPU to the wakeup list, so when interrupts happen
13807 * we can find the right vCPU to wake up.
13808 * - Change the Posted-interrupt descriptor as below:
13809 * 'NDST' <-- vcpu->pre_pcpu
13810 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13811 * - If 'ON' is set during this process, which means at least one
13812 * interrupt is posted for this vCPU, we cannot block it, in
13813 * this case, return 1, otherwise, return 0.
13814 *
13815 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013816static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013817{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013818 unsigned int dest;
13819 struct pi_desc old, new;
13820 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13821
13822 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013823 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13824 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013825 return 0;
13826
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013827 WARN_ON(irqs_disabled());
13828 local_irq_disable();
13829 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13830 vcpu->pre_pcpu = vcpu->cpu;
13831 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13832 list_add_tail(&vcpu->blocked_vcpu_list,
13833 &per_cpu(blocked_vcpu_on_cpu,
13834 vcpu->pre_pcpu));
13835 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13836 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013837
13838 do {
13839 old.control = new.control = pi_desc->control;
13840
Feng Wubf9f6ac2015-09-18 22:29:55 +080013841 WARN((pi_desc->sn == 1),
13842 "Warning: SN field of posted-interrupts "
13843 "is set before blocking\n");
13844
13845 /*
13846 * Since vCPU can be preempted during this process,
13847 * vcpu->cpu could be different with pre_pcpu, we
13848 * need to set pre_pcpu as the destination of wakeup
13849 * notification event, then we can find the right vCPU
13850 * to wakeup in wakeup handler if interrupts happen
13851 * when the vCPU is in blocked state.
13852 */
13853 dest = cpu_physical_id(vcpu->pre_pcpu);
13854
13855 if (x2apic_enabled())
13856 new.ndst = dest;
13857 else
13858 new.ndst = (dest << 8) & 0xFF00;
13859
13860 /* set 'NV' to 'wakeup vector' */
13861 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013862 } while (cmpxchg64(&pi_desc->control, old.control,
13863 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013864
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013865 /* We should not block the vCPU if an interrupt is posted for it. */
13866 if (pi_test_on(pi_desc) == 1)
13867 __pi_post_block(vcpu);
13868
13869 local_irq_enable();
13870 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013871}
13872
Yunhong Jiangbc225122016-06-13 14:19:58 -070013873static int vmx_pre_block(struct kvm_vcpu *vcpu)
13874{
13875 if (pi_pre_block(vcpu))
13876 return 1;
13877
Yunhong Jiang64672c92016-06-13 14:19:59 -070013878 if (kvm_lapic_hv_timer_in_use(vcpu))
13879 kvm_lapic_switch_to_sw_timer(vcpu);
13880
Yunhong Jiangbc225122016-06-13 14:19:58 -070013881 return 0;
13882}
13883
13884static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013885{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013886 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013887 return;
13888
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013889 WARN_ON(irqs_disabled());
13890 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013891 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013892 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013893}
13894
Yunhong Jiangbc225122016-06-13 14:19:58 -070013895static void vmx_post_block(struct kvm_vcpu *vcpu)
13896{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013897 if (kvm_x86_ops->set_hv_timer)
13898 kvm_lapic_switch_to_hv_timer(vcpu);
13899
Yunhong Jiangbc225122016-06-13 14:19:58 -070013900 pi_post_block(vcpu);
13901}
13902
Feng Wubf9f6ac2015-09-18 22:29:55 +080013903/*
Feng Wuefc64402015-09-18 22:29:51 +080013904 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13905 *
13906 * @kvm: kvm
13907 * @host_irq: host irq of the interrupt
13908 * @guest_irq: gsi of the interrupt
13909 * @set: set or unset PI
13910 * returns 0 on success, < 0 on failure
13911 */
13912static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13913 uint32_t guest_irq, bool set)
13914{
13915 struct kvm_kernel_irq_routing_entry *e;
13916 struct kvm_irq_routing_table *irq_rt;
13917 struct kvm_lapic_irq irq;
13918 struct kvm_vcpu *vcpu;
13919 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013920 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013921
13922 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013923 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13924 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013925 return 0;
13926
13927 idx = srcu_read_lock(&kvm->irq_srcu);
13928 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013929 if (guest_irq >= irq_rt->nr_rt_entries ||
13930 hlist_empty(&irq_rt->map[guest_irq])) {
13931 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13932 guest_irq, irq_rt->nr_rt_entries);
13933 goto out;
13934 }
Feng Wuefc64402015-09-18 22:29:51 +080013935
13936 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13937 if (e->type != KVM_IRQ_ROUTING_MSI)
13938 continue;
13939 /*
13940 * VT-d PI cannot support posting multicast/broadcast
13941 * interrupts to a vCPU, we still use interrupt remapping
13942 * for these kind of interrupts.
13943 *
13944 * For lowest-priority interrupts, we only support
13945 * those with single CPU as the destination, e.g. user
13946 * configures the interrupts via /proc/irq or uses
13947 * irqbalance to make the interrupts single-CPU.
13948 *
13949 * We will support full lowest-priority interrupt later.
13950 */
13951
Radim Krčmář371313132016-07-12 22:09:27 +020013952 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013953 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13954 /*
13955 * Make sure the IRTE is in remapped mode if
13956 * we don't handle it in posted mode.
13957 */
13958 ret = irq_set_vcpu_affinity(host_irq, NULL);
13959 if (ret < 0) {
13960 printk(KERN_INFO
13961 "failed to back to remapped mode, irq: %u\n",
13962 host_irq);
13963 goto out;
13964 }
13965
Feng Wuefc64402015-09-18 22:29:51 +080013966 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013967 }
Feng Wuefc64402015-09-18 22:29:51 +080013968
13969 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13970 vcpu_info.vector = irq.vector;
13971
hu huajun2698d822018-04-11 15:16:40 +080013972 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013973 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13974
13975 if (set)
13976 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013977 else
Feng Wuefc64402015-09-18 22:29:51 +080013978 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013979
13980 if (ret < 0) {
13981 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13982 __func__);
13983 goto out;
13984 }
13985 }
13986
13987 ret = 0;
13988out:
13989 srcu_read_unlock(&kvm->irq_srcu, idx);
13990 return ret;
13991}
13992
Ashok Rajc45dcc72016-06-22 14:59:56 +080013993static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13994{
13995 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13996 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13997 FEATURE_CONTROL_LMCE;
13998 else
13999 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
14000 ~FEATURE_CONTROL_LMCE;
14001}
14002
Ladi Prosek72d7b372017-10-11 16:54:41 +020014003static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
14004{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014005 /* we need a nested vmexit to enter SMM, postpone if run is pending */
14006 if (to_vmx(vcpu)->nested.nested_run_pending)
14007 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020014008 return 1;
14009}
14010
Ladi Prosek0234bf82017-10-11 16:54:40 +020014011static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
14012{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014013 struct vcpu_vmx *vmx = to_vmx(vcpu);
14014
14015 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
14016 if (vmx->nested.smm.guest_mode)
14017 nested_vmx_vmexit(vcpu, -1, 0, 0);
14018
14019 vmx->nested.smm.vmxon = vmx->nested.vmxon;
14020 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070014021 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020014022 return 0;
14023}
14024
14025static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
14026{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014027 struct vcpu_vmx *vmx = to_vmx(vcpu);
14028 int ret;
14029
14030 if (vmx->nested.smm.vmxon) {
14031 vmx->nested.vmxon = true;
14032 vmx->nested.smm.vmxon = false;
14033 }
14034
14035 if (vmx->nested.smm.guest_mode) {
14036 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014037 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020014038 vcpu->arch.hflags |= HF_SMM_MASK;
14039 if (ret)
14040 return ret;
14041
14042 vmx->nested.smm.guest_mode = false;
14043 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020014044 return 0;
14045}
14046
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014047static int enable_smi_window(struct kvm_vcpu *vcpu)
14048{
14049 return 0;
14050}
14051
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014052static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
14053 struct kvm_nested_state __user *user_kvm_nested_state,
14054 u32 user_data_size)
14055{
14056 struct vcpu_vmx *vmx;
14057 struct vmcs12 *vmcs12;
14058 struct kvm_nested_state kvm_state = {
14059 .flags = 0,
14060 .format = 0,
14061 .size = sizeof(kvm_state),
14062 .vmx.vmxon_pa = -1ull,
14063 .vmx.vmcs_pa = -1ull,
14064 };
14065
14066 if (!vcpu)
14067 return kvm_state.size + 2 * VMCS12_SIZE;
14068
14069 vmx = to_vmx(vcpu);
14070 vmcs12 = get_vmcs12(vcpu);
14071 if (nested_vmx_allowed(vcpu) &&
14072 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
14073 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
14074 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
14075
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014076 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014077 kvm_state.size += VMCS12_SIZE;
14078
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014079 if (is_guest_mode(vcpu) &&
14080 nested_cpu_has_shadow_vmcs(vmcs12) &&
14081 vmcs12->vmcs_link_pointer != -1ull)
14082 kvm_state.size += VMCS12_SIZE;
14083 }
14084
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014085 if (vmx->nested.smm.vmxon)
14086 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
14087
14088 if (vmx->nested.smm.guest_mode)
14089 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
14090
14091 if (is_guest_mode(vcpu)) {
14092 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
14093
14094 if (vmx->nested.nested_run_pending)
14095 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
14096 }
14097 }
14098
14099 if (user_data_size < kvm_state.size)
14100 goto out;
14101
14102 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
14103 return -EFAULT;
14104
14105 if (vmx->nested.current_vmptr == -1ull)
14106 goto out;
14107
14108 /*
14109 * When running L2, the authoritative vmcs12 state is in the
14110 * vmcs02. When running L1, the authoritative vmcs12 state is
14111 * in the shadow vmcs linked to vmcs01, unless
14112 * sync_shadow_vmcs is set, in which case, the authoritative
14113 * vmcs12 state is in the vmcs12 already.
14114 */
14115 if (is_guest_mode(vcpu))
14116 sync_vmcs12(vcpu, vmcs12);
14117 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
14118 copy_shadow_to_vmcs12(vmx);
14119
14120 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
14121 return -EFAULT;
14122
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014123 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14124 vmcs12->vmcs_link_pointer != -1ull) {
14125 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
14126 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
14127 return -EFAULT;
14128 }
14129
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014130out:
14131 return kvm_state.size;
14132}
14133
14134static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14135 struct kvm_nested_state __user *user_kvm_nested_state,
14136 struct kvm_nested_state *kvm_state)
14137{
14138 struct vcpu_vmx *vmx = to_vmx(vcpu);
14139 struct vmcs12 *vmcs12;
14140 u32 exit_qual;
14141 int ret;
14142
14143 if (kvm_state->format != 0)
14144 return -EINVAL;
14145
14146 if (!nested_vmx_allowed(vcpu))
14147 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14148
14149 if (kvm_state->vmx.vmxon_pa == -1ull) {
14150 if (kvm_state->vmx.smm.flags)
14151 return -EINVAL;
14152
14153 if (kvm_state->vmx.vmcs_pa != -1ull)
14154 return -EINVAL;
14155
14156 vmx_leave_nested(vcpu);
14157 return 0;
14158 }
14159
14160 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14161 return -EINVAL;
14162
14163 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14164 return -EINVAL;
14165
14166 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14167 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14168 return -EINVAL;
14169
14170 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14171 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14172 return -EINVAL;
14173
14174 if (kvm_state->vmx.smm.flags &
14175 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14176 return -EINVAL;
14177
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014178 /*
14179 * SMM temporarily disables VMX, so we cannot be in guest mode,
14180 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14181 * must be zero.
14182 */
14183 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14184 return -EINVAL;
14185
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014186 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14187 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14188 return -EINVAL;
14189
14190 vmx_leave_nested(vcpu);
14191 if (kvm_state->vmx.vmxon_pa == -1ull)
14192 return 0;
14193
14194 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14195 ret = enter_vmx_operation(vcpu);
14196 if (ret)
14197 return ret;
14198
14199 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14200
14201 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14202 vmx->nested.smm.vmxon = true;
14203 vmx->nested.vmxon = false;
14204
14205 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14206 vmx->nested.smm.guest_mode = true;
14207 }
14208
14209 vmcs12 = get_vmcs12(vcpu);
14210 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14211 return -EFAULT;
14212
Liran Alon392b2f22018-06-23 02:35:01 +030014213 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014214 return -EINVAL;
14215
14216 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14217 return 0;
14218
14219 vmx->nested.nested_run_pending =
14220 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14221
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014222 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14223 vmcs12->vmcs_link_pointer != -1ull) {
14224 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14225 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14226 return -EINVAL;
14227
14228 if (copy_from_user(shadow_vmcs12,
14229 user_kvm_nested_state->data + VMCS12_SIZE,
14230 sizeof(*vmcs12)))
14231 return -EFAULT;
14232
14233 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14234 !shadow_vmcs12->hdr.shadow_vmcs)
14235 return -EINVAL;
14236 }
14237
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014238 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14239 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14240 return -EINVAL;
14241
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014242 vmx->nested.dirty_vmcs12 = true;
14243 ret = enter_vmx_non_root_mode(vcpu, NULL);
14244 if (ret)
14245 return -EINVAL;
14246
14247 return 0;
14248}
14249
Kees Cook404f6aa2016-08-08 16:29:06 -070014250static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014251 .cpu_has_kvm_support = cpu_has_kvm_support,
14252 .disabled_by_bios = vmx_disabled_by_bios,
14253 .hardware_setup = hardware_setup,
14254 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014255 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014256 .hardware_enable = hardware_enable,
14257 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014258 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014259 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014260
Wanpeng Lib31c1142018-03-12 04:53:04 -070014261 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014262 .vm_alloc = vmx_vm_alloc,
14263 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014264
Avi Kivity6aa8b732006-12-10 02:21:36 -080014265 .vcpu_create = vmx_create_vcpu,
14266 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014267 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014268
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014269 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014270 .vcpu_load = vmx_vcpu_load,
14271 .vcpu_put = vmx_vcpu_put,
14272
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014273 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014274 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014275 .get_msr = vmx_get_msr,
14276 .set_msr = vmx_set_msr,
14277 .get_segment_base = vmx_get_segment_base,
14278 .get_segment = vmx_get_segment,
14279 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014280 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014281 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014282 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014283 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014284 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014285 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014286 .set_cr3 = vmx_set_cr3,
14287 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014288 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014289 .get_idt = vmx_get_idt,
14290 .set_idt = vmx_set_idt,
14291 .get_gdt = vmx_get_gdt,
14292 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014293 .get_dr6 = vmx_get_dr6,
14294 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014295 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014296 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014297 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014298 .get_rflags = vmx_get_rflags,
14299 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014300
Avi Kivity6aa8b732006-12-10 02:21:36 -080014301 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014302 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014303
Avi Kivity6aa8b732006-12-10 02:21:36 -080014304 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014305 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014306 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014307 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14308 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014309 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014310 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014311 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014312 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014313 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014314 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014315 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014316 .get_nmi_mask = vmx_get_nmi_mask,
14317 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014318 .enable_nmi_window = enable_nmi_window,
14319 .enable_irq_window = enable_irq_window,
14320 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014321 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014322 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014323 .get_enable_apicv = vmx_get_enable_apicv,
14324 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014325 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014326 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014327 .hwapic_irr_update = vmx_hwapic_irr_update,
14328 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014329 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014330 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14331 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014332
Izik Eiduscbc94022007-10-25 00:29:55 +020014333 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014334 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014335 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014336 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014337
Avi Kivity586f9602010-11-18 13:09:54 +020014338 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014339
Sheng Yang17cc3932010-01-05 19:02:27 +080014340 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014341
14342 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014343
14344 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014345 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014346
14347 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014348
14349 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014350
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014351 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014352 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014353
14354 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014355
14356 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014357 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014358 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014359 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014360 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014361
14362 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014363 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014364
14365 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014366
14367 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14368 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14369 .flush_log_dirty = vmx_flush_log_dirty,
14370 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014371 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014372
Feng Wubf9f6ac2015-09-18 22:29:55 +080014373 .pre_block = vmx_pre_block,
14374 .post_block = vmx_post_block,
14375
Wei Huang25462f72015-06-19 15:45:05 +020014376 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014377
14378 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014379
14380#ifdef CONFIG_X86_64
14381 .set_hv_timer = vmx_set_hv_timer,
14382 .cancel_hv_timer = vmx_cancel_hv_timer,
14383#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014384
14385 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014386
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014387 .get_nested_state = vmx_get_nested_state,
14388 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014389 .get_vmcs12_pages = nested_get_vmcs12_pages,
14390
Ladi Prosek72d7b372017-10-11 16:54:41 +020014391 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014392 .pre_enter_smm = vmx_pre_enter_smm,
14393 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014394 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014395};
14396
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014397static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014398{
14399 if (vmx_l1d_flush_pages) {
14400 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14401 vmx_l1d_flush_pages = NULL;
14402 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014403 /* Restore state so sysfs ignores VMX */
14404 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014405}
14406
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014407static void vmx_exit(void)
14408{
14409#ifdef CONFIG_KEXEC_CORE
14410 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14411 synchronize_rcu();
14412#endif
14413
14414 kvm_exit();
14415
14416#if IS_ENABLED(CONFIG_HYPERV)
14417 if (static_branch_unlikely(&enable_evmcs)) {
14418 int cpu;
14419 struct hv_vp_assist_page *vp_ap;
14420 /*
14421 * Reset everything to support using non-enlightened VMCS
14422 * access later (e.g. when we reload the module with
14423 * enlightened_vmcs=0)
14424 */
14425 for_each_online_cpu(cpu) {
14426 vp_ap = hv_get_vp_assist_page(cpu);
14427
14428 if (!vp_ap)
14429 continue;
14430
14431 vp_ap->current_nested_vmcs = 0;
14432 vp_ap->enlighten_vmentry = 0;
14433 }
14434
14435 static_branch_disable(&enable_evmcs);
14436 }
14437#endif
14438 vmx_cleanup_l1d_flush();
14439}
14440module_exit(vmx_exit);
14441
Avi Kivity6aa8b732006-12-10 02:21:36 -080014442static int __init vmx_init(void)
14443{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014444 int r;
14445
14446#if IS_ENABLED(CONFIG_HYPERV)
14447 /*
14448 * Enlightened VMCS usage should be recommended and the host needs
14449 * to support eVMCS v1 or above. We can also disable eVMCS support
14450 * with module parameter.
14451 */
14452 if (enlightened_vmcs &&
14453 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14454 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14455 KVM_EVMCS_VERSION) {
14456 int cpu;
14457
14458 /* Check that we have assist pages on all online CPUs */
14459 for_each_online_cpu(cpu) {
14460 if (!hv_get_vp_assist_page(cpu)) {
14461 enlightened_vmcs = false;
14462 break;
14463 }
14464 }
14465
14466 if (enlightened_vmcs) {
14467 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14468 static_branch_enable(&enable_evmcs);
14469 }
14470 } else {
14471 enlightened_vmcs = false;
14472 }
14473#endif
14474
14475 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014476 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014477 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014478 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014479
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014480 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014481 * Must be called after kvm_init() so enable_ept is properly set
14482 * up. Hand the parameter mitigation value in which was stored in
14483 * the pre module init parser. If no parameter was given, it will
14484 * contain 'auto' which will be turned into the default 'cond'
14485 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014486 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014487 if (boot_cpu_has(X86_BUG_L1TF)) {
14488 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14489 if (r) {
14490 vmx_exit();
14491 return r;
14492 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014493 }
14494
Dave Young2965faa2015-09-09 15:38:55 -070014495#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014496 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14497 crash_vmclear_local_loaded_vmcss);
14498#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014499 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014500
He, Qingfdef3ad2007-04-30 09:45:24 +030014501 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014502}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014503module_init(vmx_init);