blob: 12d39e2561bef09a33b1cd7c48a76985d25d42fa [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_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001617{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001618 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001619}
1620
Gui Jianfeng31299942010-03-15 17:29:09 +08001621static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001622{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001623 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001624}
1625
Liran Alon9e869482018-03-12 13:12:51 +02001626static inline bool is_gp_fault(u32 intr_info)
1627{
1628 return is_exception_n(intr_info, GP_VECTOR);
1629}
1630
Gui Jianfeng31299942010-03-15 17:29:09 +08001631static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632{
1633 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1634 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1635}
1636
Gui Jianfeng31299942010-03-15 17:29:09 +08001637static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001638{
1639 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1640 INTR_INFO_VALID_MASK)) ==
1641 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1642}
1643
Linus Torvalds32d43cd2018-03-20 12:16:59 -07001644/* Undocumented: icebp/int1 */
1645static inline bool is_icebp(u32 intr_info)
1646{
1647 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1648 == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK);
1649}
1650
Gui Jianfeng31299942010-03-15 17:29:09 +08001651static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001652{
Sheng Yang04547152009-04-01 15:52:31 +08001653 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001654}
1655
Gui Jianfeng31299942010-03-15 17:29:09 +08001656static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001657{
Sheng Yang04547152009-04-01 15:52:31 +08001658 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001659}
1660
Paolo Bonzini35754c92015-07-29 12:05:37 +02001661static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001662{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001663 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001664}
1665
Gui Jianfeng31299942010-03-15 17:29:09 +08001666static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001667{
Sheng Yang04547152009-04-01 15:52:31 +08001668 return vmcs_config.cpu_based_exec_ctrl &
1669 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001670}
1671
Avi Kivity774ead32007-12-26 13:57:04 +02001672static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001673{
Sheng Yang04547152009-04-01 15:52:31 +08001674 return vmcs_config.cpu_based_2nd_exec_ctrl &
1675 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1676}
1677
Yang Zhang8d146952013-01-25 10:18:50 +08001678static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1679{
1680 return vmcs_config.cpu_based_2nd_exec_ctrl &
1681 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1682}
1683
Yang Zhang83d4c282013-01-25 10:18:49 +08001684static inline bool cpu_has_vmx_apic_register_virt(void)
1685{
1686 return vmcs_config.cpu_based_2nd_exec_ctrl &
1687 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1688}
1689
Yang Zhangc7c9c562013-01-25 10:18:51 +08001690static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1691{
1692 return vmcs_config.cpu_based_2nd_exec_ctrl &
1693 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1694}
1695
Sean Christopherson0b665d32018-08-14 09:33:34 -07001696static inline bool cpu_has_vmx_encls_vmexit(void)
1697{
1698 return vmcs_config.cpu_based_2nd_exec_ctrl &
1699 SECONDARY_EXEC_ENCLS_EXITING;
1700}
1701
Yunhong Jiang64672c92016-06-13 14:19:59 -07001702/*
1703 * Comment's format: document - errata name - stepping - processor name.
1704 * Refer from
1705 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1706 */
1707static u32 vmx_preemption_cpu_tfms[] = {
1708/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
17090x000206E6,
1710/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1711/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1712/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
17130x00020652,
1714/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
17150x00020655,
1716/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1717/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1718/*
1719 * 320767.pdf - AAP86 - B1 -
1720 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1721 */
17220x000106E5,
1723/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
17240x000106A0,
1725/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
17260x000106A1,
1727/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
17280x000106A4,
1729 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1730 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1731 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
17320x000106A5,
1733};
1734
1735static inline bool cpu_has_broken_vmx_preemption_timer(void)
1736{
1737 u32 eax = cpuid_eax(0x00000001), i;
1738
1739 /* Clear the reserved bits */
1740 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001741 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001742 if (eax == vmx_preemption_cpu_tfms[i])
1743 return true;
1744
1745 return false;
1746}
1747
1748static inline bool cpu_has_vmx_preemption_timer(void)
1749{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001750 return vmcs_config.pin_based_exec_ctrl &
1751 PIN_BASED_VMX_PREEMPTION_TIMER;
1752}
1753
Yang Zhang01e439b2013-04-11 19:25:12 +08001754static inline bool cpu_has_vmx_posted_intr(void)
1755{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001756 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1757 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001758}
1759
1760static inline bool cpu_has_vmx_apicv(void)
1761{
1762 return cpu_has_vmx_apic_register_virt() &&
1763 cpu_has_vmx_virtual_intr_delivery() &&
1764 cpu_has_vmx_posted_intr();
1765}
1766
Sheng Yang04547152009-04-01 15:52:31 +08001767static inline bool cpu_has_vmx_flexpriority(void)
1768{
1769 return cpu_has_vmx_tpr_shadow() &&
1770 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001771}
1772
Marcelo Tosattie7997942009-06-11 12:07:40 -03001773static inline bool cpu_has_vmx_ept_execute_only(void)
1774{
Gui Jianfeng31299942010-03-15 17:29:09 +08001775 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001776}
1777
Marcelo Tosattie7997942009-06-11 12:07:40 -03001778static inline bool cpu_has_vmx_ept_2m_page(void)
1779{
Gui Jianfeng31299942010-03-15 17:29:09 +08001780 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001781}
1782
Sheng Yang878403b2010-01-05 19:02:29 +08001783static inline bool cpu_has_vmx_ept_1g_page(void)
1784{
Gui Jianfeng31299942010-03-15 17:29:09 +08001785 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001786}
1787
Sheng Yang4bc9b982010-06-02 14:05:24 +08001788static inline bool cpu_has_vmx_ept_4levels(void)
1789{
1790 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1791}
1792
David Hildenbrand42aa53b2017-08-10 23:15:29 +02001793static inline bool cpu_has_vmx_ept_mt_wb(void)
1794{
1795 return vmx_capability.ept & VMX_EPTP_WB_BIT;
1796}
1797
Yu Zhang855feb62017-08-24 20:27:55 +08001798static inline bool cpu_has_vmx_ept_5levels(void)
1799{
1800 return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT;
1801}
1802
Xudong Hao83c3a332012-05-28 19:33:35 +08001803static inline bool cpu_has_vmx_ept_ad_bits(void)
1804{
1805 return vmx_capability.ept & VMX_EPT_AD_BIT;
1806}
1807
Gui Jianfeng31299942010-03-15 17:29:09 +08001808static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001809{
Gui Jianfeng31299942010-03-15 17:29:09 +08001810 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001811}
1812
Gui Jianfeng31299942010-03-15 17:29:09 +08001813static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001814{
Gui Jianfeng31299942010-03-15 17:29:09 +08001815 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001816}
1817
Liran Aloncd9a4912018-05-22 17:16:15 +03001818static inline bool cpu_has_vmx_invvpid_individual_addr(void)
1819{
1820 return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT;
1821}
1822
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001823static inline bool cpu_has_vmx_invvpid_single(void)
1824{
1825 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1826}
1827
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001828static inline bool cpu_has_vmx_invvpid_global(void)
1829{
1830 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1831}
1832
Wanpeng Li08d839c2017-03-23 05:30:08 -07001833static inline bool cpu_has_vmx_invvpid(void)
1834{
1835 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1836}
1837
Gui Jianfeng31299942010-03-15 17:29:09 +08001838static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001839{
Sheng Yang04547152009-04-01 15:52:31 +08001840 return vmcs_config.cpu_based_2nd_exec_ctrl &
1841 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001842}
1843
Gui Jianfeng31299942010-03-15 17:29:09 +08001844static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001845{
1846 return vmcs_config.cpu_based_2nd_exec_ctrl &
1847 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1848}
1849
Gui Jianfeng31299942010-03-15 17:29:09 +08001850static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001851{
1852 return vmcs_config.cpu_based_2nd_exec_ctrl &
1853 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1854}
1855
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001856static inline bool cpu_has_vmx_basic_inout(void)
1857{
1858 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1859}
1860
Paolo Bonzini35754c92015-07-29 12:05:37 +02001861static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001862{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001863 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001864}
1865
Gui Jianfeng31299942010-03-15 17:29:09 +08001866static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001867{
Sheng Yang04547152009-04-01 15:52:31 +08001868 return vmcs_config.cpu_based_2nd_exec_ctrl &
1869 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001870}
1871
Gui Jianfeng31299942010-03-15 17:29:09 +08001872static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001873{
1874 return vmcs_config.cpu_based_2nd_exec_ctrl &
1875 SECONDARY_EXEC_RDTSCP;
1876}
1877
Mao, Junjiead756a12012-07-02 01:18:48 +00001878static inline bool cpu_has_vmx_invpcid(void)
1879{
1880 return vmcs_config.cpu_based_2nd_exec_ctrl &
1881 SECONDARY_EXEC_ENABLE_INVPCID;
1882}
1883
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01001884static inline bool cpu_has_virtual_nmis(void)
1885{
1886 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1887}
1888
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001889static inline bool cpu_has_vmx_wbinvd_exit(void)
1890{
1891 return vmcs_config.cpu_based_2nd_exec_ctrl &
1892 SECONDARY_EXEC_WBINVD_EXITING;
1893}
1894
Abel Gordonabc4fc52013-04-18 14:35:25 +03001895static inline bool cpu_has_vmx_shadow_vmcs(void)
1896{
1897 u64 vmx_msr;
1898 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1899 /* check if the cpu supports writing r/o exit information fields */
1900 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1901 return false;
1902
1903 return vmcs_config.cpu_based_2nd_exec_ctrl &
1904 SECONDARY_EXEC_SHADOW_VMCS;
1905}
1906
Kai Huang843e4332015-01-28 10:54:28 +08001907static inline bool cpu_has_vmx_pml(void)
1908{
1909 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1910}
1911
Haozhong Zhang64903d62015-10-20 15:39:09 +08001912static inline bool cpu_has_vmx_tsc_scaling(void)
1913{
1914 return vmcs_config.cpu_based_2nd_exec_ctrl &
1915 SECONDARY_EXEC_TSC_SCALING;
1916}
1917
Bandan Das2a499e42017-08-03 15:54:41 -04001918static inline bool cpu_has_vmx_vmfunc(void)
1919{
1920 return vmcs_config.cpu_based_2nd_exec_ctrl &
1921 SECONDARY_EXEC_ENABLE_VMFUNC;
1922}
1923
Sean Christopherson64f7a112018-04-30 10:01:06 -07001924static bool vmx_umip_emulated(void)
1925{
1926 return vmcs_config.cpu_based_2nd_exec_ctrl &
1927 SECONDARY_EXEC_DESC;
1928}
1929
Sheng Yang04547152009-04-01 15:52:31 +08001930static inline bool report_flexpriority(void)
1931{
1932 return flexpriority_enabled;
1933}
1934
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001935static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu)
1936{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01001937 return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low);
Jim Mattsonc7c2c7092017-05-05 11:28:09 -07001938}
1939
Jim Mattsonf4160e42018-05-29 09:11:33 -07001940/*
1941 * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE
1942 * to modify any valid field of the VMCS, or are the VM-exit
1943 * information fields read-only?
1944 */
1945static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu)
1946{
1947 return to_vmx(vcpu)->nested.msrs.misc_low &
1948 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS;
1949}
1950
Marc Orr04473782018-06-20 17:21:29 -07001951static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu)
1952{
1953 return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS;
1954}
1955
1956static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu)
1957{
1958 return to_vmx(vcpu)->nested.msrs.procbased_ctls_high &
1959 CPU_BASED_MONITOR_TRAP_FLAG;
1960}
1961
Liran Alonfa97d7d2018-07-18 14:07:59 +02001962static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu)
1963{
1964 return to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
1965 SECONDARY_EXEC_SHADOW_VMCS;
1966}
1967
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001968static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1969{
1970 return vmcs12->cpu_based_vm_exec_control & bit;
1971}
1972
1973static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1974{
1975 return (vmcs12->cpu_based_vm_exec_control &
1976 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1977 (vmcs12->secondary_vm_exec_control & bit);
1978}
1979
Jan Kiszkaf41245002014-03-07 20:03:13 +01001980static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1981{
1982 return vmcs12->pin_based_vm_exec_control &
1983 PIN_BASED_VMX_PREEMPTION_TIMER;
1984}
1985
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05001986static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12)
1987{
1988 return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING;
1989}
1990
1991static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
1992{
1993 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1994}
1995
Nadav Har'El155a97a2013-08-05 11:07:16 +03001996static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1997{
1998 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1999}
2000
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002001static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
2002{
Paolo Bonzini3db13482017-08-24 14:48:03 +02002003 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002004}
2005
Bandan Dasc5f983f2017-05-05 15:25:14 -04002006static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12)
2007{
2008 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML);
2009}
2010
Wincy Vanf2b93282015-02-03 23:56:03 +08002011static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
2012{
2013 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
2014}
2015
Wanpeng Li5c614b32015-10-13 09:18:36 -07002016static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
2017{
2018 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
2019}
2020
Wincy Van82f0dd42015-02-03 23:57:18 +08002021static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
2022{
2023 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
2024}
2025
Wincy Van608406e2015-02-03 23:57:51 +08002026static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
2027{
2028 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
2029}
2030
Wincy Van705699a2015-02-03 23:58:17 +08002031static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
2032{
2033 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
2034}
2035
Bandan Das27c42a12017-08-03 15:54:42 -04002036static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12)
2037{
2038 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC);
2039}
2040
Bandan Das41ab9372017-08-03 15:54:43 -04002041static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12)
2042{
2043 return nested_cpu_has_vmfunc(vmcs12) &&
2044 (vmcs12->vm_function_control &
2045 VMX_VMFUNC_EPTP_SWITCHING);
2046}
2047
Liran Alonf792d272018-06-23 02:35:05 +03002048static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12)
2049{
2050 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS);
2051}
2052
Jim Mattsonef85b672016-12-12 11:01:37 -08002053static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03002054{
2055 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08002056 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03002057}
2058
Jan Kiszka533558b2014-01-04 18:47:20 +01002059static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
2060 u32 exit_intr_info,
2061 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03002062static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
2063 struct vmcs12 *vmcs12,
2064 u32 reason, unsigned long qualification);
2065
Rusty Russell8b9cf982007-07-30 16:31:43 +10002066static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08002067{
2068 int i;
2069
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002070 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03002071 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002072 return i;
2073 return -1;
2074}
2075
Sheng Yang2384d2b2008-01-17 15:14:33 +08002076static inline void __invvpid(int ext, u16 vpid, gva_t gva)
2077{
2078 struct {
2079 u64 vpid : 16;
2080 u64 rsvd : 48;
2081 u64 gva;
2082 } operand = { vpid, 0, gva };
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002083 bool error;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002084
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002085 asm volatile (__ex(ASM_VMX_INVVPID) CC_SET(na)
2086 : CC_OUT(na) (error) : "a"(&operand), "c"(ext)
2087 : "memory");
2088 BUG_ON(error);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002089}
2090
Sheng Yang14394422008-04-28 12:24:45 +08002091static inline void __invept(int ext, u64 eptp, gpa_t gpa)
2092{
2093 struct {
2094 u64 eptp, gpa;
2095 } operand = {eptp, gpa};
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002096 bool error;
Sheng Yang14394422008-04-28 12:24:45 +08002097
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002098 asm volatile (__ex(ASM_VMX_INVEPT) CC_SET(na)
2099 : CC_OUT(na) (error) : "a" (&operand), "c" (ext)
2100 : "memory");
2101 BUG_ON(error);
Sheng Yang14394422008-04-28 12:24:45 +08002102}
2103
Avi Kivity26bb0982009-09-07 11:14:12 +03002104static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03002105{
2106 int i;
2107
Rusty Russell8b9cf982007-07-30 16:31:43 +10002108 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03002109 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002110 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00002111 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08002112}
2113
Avi Kivity6aa8b732006-12-10 02:21:36 -08002114static void vmcs_clear(struct vmcs *vmcs)
2115{
2116 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002117 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002118
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002119 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) CC_SET(na)
2120 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2121 : "memory");
2122 if (unlikely(error))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
2124 vmcs, phys_addr);
2125}
2126
Nadav Har'Eld462b812011-05-24 15:26:10 +03002127static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
2128{
2129 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07002130 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
2131 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002132 loaded_vmcs->cpu = -1;
2133 loaded_vmcs->launched = 0;
2134}
2135
Dongxiao Xu7725b892010-05-11 18:29:38 +08002136static void vmcs_load(struct vmcs *vmcs)
2137{
2138 u64 phys_addr = __pa(vmcs);
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002139 bool error;
Dongxiao Xu7725b892010-05-11 18:29:38 +08002140
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002141 if (static_branch_unlikely(&enable_evmcs))
2142 return evmcs_load(phys_addr);
2143
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002144 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) CC_SET(na)
2145 : CC_OUT(na) (error) : "a"(&phys_addr), "m"(phys_addr)
2146 : "memory");
2147 if (unlikely(error))
Nadav Har'El2844d842011-05-25 23:16:40 +03002148 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08002149 vmcs, phys_addr);
2150}
2151
Dave Young2965faa2015-09-09 15:38:55 -07002152#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002153/*
2154 * This bitmap is used to indicate whether the vmclear
2155 * operation is enabled on all cpus. All disabled by
2156 * default.
2157 */
2158static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
2159
2160static inline void crash_enable_local_vmclear(int cpu)
2161{
2162 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
2163}
2164
2165static inline void crash_disable_local_vmclear(int cpu)
2166{
2167 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
2168}
2169
2170static inline int crash_local_vmclear_enabled(int cpu)
2171{
2172 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
2173}
2174
2175static void crash_vmclear_local_loaded_vmcss(void)
2176{
2177 int cpu = raw_smp_processor_id();
2178 struct loaded_vmcs *v;
2179
2180 if (!crash_local_vmclear_enabled(cpu))
2181 return;
2182
2183 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
2184 loaded_vmcss_on_cpu_link)
2185 vmcs_clear(v->vmcs);
2186}
2187#else
2188static inline void crash_enable_local_vmclear(int cpu) { }
2189static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07002190#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002191
Nadav Har'Eld462b812011-05-24 15:26:10 +03002192static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193{
Nadav Har'Eld462b812011-05-24 15:26:10 +03002194 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08002195 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002196
Nadav Har'Eld462b812011-05-24 15:26:10 +03002197 if (loaded_vmcs->cpu != cpu)
2198 return; /* vcpu migration can race with cpu offline */
2199 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002201 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002202 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002203
2204 /*
2205 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
2206 * is before setting loaded_vmcs->vcpu to -1 which is done in
2207 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
2208 * then adds the vmcs into percpu list before it is deleted.
2209 */
2210 smp_wmb();
2211
Nadav Har'Eld462b812011-05-24 15:26:10 +03002212 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002213 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214}
2215
Nadav Har'Eld462b812011-05-24 15:26:10 +03002216static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002217{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08002218 int cpu = loaded_vmcs->cpu;
2219
2220 if (cpu != -1)
2221 smp_call_function_single(cpu,
2222 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08002223}
2224
Junaid Shahidfaff8752018-06-29 13:10:05 -07002225static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr)
2226{
2227 if (vpid == 0)
2228 return true;
2229
2230 if (cpu_has_vmx_invvpid_individual_addr()) {
2231 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr);
2232 return true;
2233 }
2234
2235 return false;
2236}
2237
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002238static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002239{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002240 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002241 return;
2242
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08002243 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002244 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002245}
2246
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002247static inline void vpid_sync_vcpu_global(void)
2248{
2249 if (cpu_has_vmx_invvpid_global())
2250 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
2251}
2252
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002253static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002254{
2255 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08002256 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002257 else
2258 vpid_sync_vcpu_global();
2259}
2260
Sheng Yang14394422008-04-28 12:24:45 +08002261static inline void ept_sync_global(void)
2262{
David Hildenbrandf5f51582017-08-24 20:51:30 +02002263 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
Sheng Yang14394422008-04-28 12:24:45 +08002264}
2265
2266static inline void ept_sync_context(u64 eptp)
2267{
David Hildenbrand0e1252d2017-08-24 20:51:28 +02002268 if (cpu_has_vmx_invept_context())
2269 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
2270 else
2271 ept_sync_global();
Sheng Yang14394422008-04-28 12:24:45 +08002272}
2273
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002274static __always_inline void vmcs_check16(unsigned long field)
2275{
2276 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2277 "16-bit accessor invalid for 64-bit field");
2278 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2279 "16-bit accessor invalid for 64-bit high field");
2280 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2281 "16-bit accessor invalid for 32-bit high field");
2282 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2283 "16-bit accessor invalid for natural width field");
2284}
2285
2286static __always_inline void vmcs_check32(unsigned long field)
2287{
2288 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2289 "32-bit accessor invalid for 16-bit field");
2290 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2291 "32-bit accessor invalid for natural width field");
2292}
2293
2294static __always_inline void vmcs_check64(unsigned long field)
2295{
2296 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2297 "64-bit accessor invalid for 16-bit field");
2298 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2299 "64-bit accessor invalid for 64-bit high field");
2300 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2301 "64-bit accessor invalid for 32-bit field");
2302 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
2303 "64-bit accessor invalid for natural width field");
2304}
2305
2306static __always_inline void vmcs_checkl(unsigned long field)
2307{
2308 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
2309 "Natural width accessor invalid for 16-bit field");
2310 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
2311 "Natural width accessor invalid for 64-bit field");
2312 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
2313 "Natural width accessor invalid for 64-bit high field");
2314 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
2315 "Natural width accessor invalid for 32-bit field");
2316}
2317
2318static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319{
Avi Kivity5e520e62011-05-15 10:13:12 -04002320 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Avi Kivity5e520e62011-05-15 10:13:12 -04002322 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
2323 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 return value;
2325}
2326
Avi Kivity96304212011-05-15 10:13:13 -04002327static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002328{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002329 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002330 if (static_branch_unlikely(&enable_evmcs))
2331 return evmcs_read16(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002332 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
Avi Kivity96304212011-05-15 10:13:13 -04002335static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002337 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002338 if (static_branch_unlikely(&enable_evmcs))
2339 return evmcs_read32(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002340 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341}
2342
Avi Kivity96304212011-05-15 10:13:13 -04002343static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002345 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002346 if (static_branch_unlikely(&enable_evmcs))
2347 return evmcs_read64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002348#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002349 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002350#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002351 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002352#endif
2353}
2354
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002355static __always_inline unsigned long vmcs_readl(unsigned long field)
2356{
2357 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002358 if (static_branch_unlikely(&enable_evmcs))
2359 return evmcs_read64(field);
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002360 return __vmcs_readl(field);
2361}
2362
Avi Kivitye52de1b2007-01-05 16:36:56 -08002363static noinline void vmwrite_error(unsigned long field, unsigned long value)
2364{
2365 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
2366 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
2367 dump_stack();
2368}
2369
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002370static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371{
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002372 bool error;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373
Uros Bizjakfd8ca6d2018-08-06 16:42:49 +02002374 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) CC_SET(na)
2375 : CC_OUT(na) (error) : "a"(value), "d"(field));
Avi Kivitye52de1b2007-01-05 16:36:56 -08002376 if (unlikely(error))
2377 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378}
2379
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002380static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002382 vmcs_check16(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002383 if (static_branch_unlikely(&enable_evmcs))
2384 return evmcs_write16(field, value);
2385
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002386 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387}
2388
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002389static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002391 vmcs_check32(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002392 if (static_branch_unlikely(&enable_evmcs))
2393 return evmcs_write32(field, value);
2394
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002395 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396}
2397
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002398static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002400 vmcs_check64(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002401 if (static_branch_unlikely(&enable_evmcs))
2402 return evmcs_write64(field, value);
2403
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002404 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03002405#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002407 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002408#endif
2409}
2410
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002411static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002412{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002413 vmcs_checkl(field);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002414 if (static_branch_unlikely(&enable_evmcs))
2415 return evmcs_write64(field, value);
2416
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002417 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002418}
2419
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002420static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002421{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002422 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2423 "vmcs_clear_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002424 if (static_branch_unlikely(&enable_evmcs))
2425 return evmcs_write32(field, evmcs_read32(field) & ~mask);
2426
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002427 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
2428}
2429
2430static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
2431{
2432 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
2433 "vmcs_set_bits does not support 64-bit fields");
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01002434 if (static_branch_unlikely(&enable_evmcs))
2435 return evmcs_write32(field, evmcs_read32(field) | mask);
2436
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01002437 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002438}
2439
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002440static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
2441{
2442 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
2443}
2444
Gleb Natapov2961e8762013-11-25 15:37:13 +02002445static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
2446{
2447 vmcs_write32(VM_ENTRY_CONTROLS, val);
2448 vmx->vm_entry_controls_shadow = val;
2449}
2450
2451static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
2452{
2453 if (vmx->vm_entry_controls_shadow != val)
2454 vm_entry_controls_init(vmx, val);
2455}
2456
2457static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
2458{
2459 return vmx->vm_entry_controls_shadow;
2460}
2461
2462
2463static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2464{
2465 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
2466}
2467
2468static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2469{
2470 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
2471}
2472
Paolo Bonzini8391ce42016-07-07 14:58:33 +02002473static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
2474{
2475 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
2476}
2477
Gleb Natapov2961e8762013-11-25 15:37:13 +02002478static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
2479{
2480 vmcs_write32(VM_EXIT_CONTROLS, val);
2481 vmx->vm_exit_controls_shadow = val;
2482}
2483
2484static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
2485{
2486 if (vmx->vm_exit_controls_shadow != val)
2487 vm_exit_controls_init(vmx, val);
2488}
2489
2490static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
2491{
2492 return vmx->vm_exit_controls_shadow;
2493}
2494
2495
2496static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
2497{
2498 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
2499}
2500
2501static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
2502{
2503 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
2504}
2505
Avi Kivity2fb92db2011-04-27 19:42:18 +03002506static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
2507{
2508 vmx->segment_cache.bitmask = 0;
2509}
2510
2511static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
2512 unsigned field)
2513{
2514 bool ret;
2515 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
2516
2517 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
2518 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
2519 vmx->segment_cache.bitmask = 0;
2520 }
2521 ret = vmx->segment_cache.bitmask & mask;
2522 vmx->segment_cache.bitmask |= mask;
2523 return ret;
2524}
2525
2526static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
2527{
2528 u16 *p = &vmx->segment_cache.seg[seg].selector;
2529
2530 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
2531 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
2532 return *p;
2533}
2534
2535static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
2536{
2537 ulong *p = &vmx->segment_cache.seg[seg].base;
2538
2539 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
2540 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
2541 return *p;
2542}
2543
2544static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
2545{
2546 u32 *p = &vmx->segment_cache.seg[seg].limit;
2547
2548 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
2549 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
2550 return *p;
2551}
2552
2553static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
2554{
2555 u32 *p = &vmx->segment_cache.seg[seg].ar;
2556
2557 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
2558 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
2559 return *p;
2560}
2561
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002562static void update_exception_bitmap(struct kvm_vcpu *vcpu)
2563{
2564 u32 eb;
2565
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002566 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08002567 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Liran Alon9e869482018-03-12 13:12:51 +02002568 /*
2569 * Guest access to VMware backdoor ports could legitimately
2570 * trigger #GP because of TSS I/O permission bitmap.
2571 * We intercept those #GP and allow access to them anyway
2572 * as VMware does.
2573 */
2574 if (enable_vmware_backdoor)
2575 eb |= (1u << GP_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01002576 if ((vcpu->guest_debug &
2577 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
2578 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
2579 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002580 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002581 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02002582 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002583 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002584
2585 /* When we are running a nested L2 guest and L1 specified for it a
2586 * certain exception bitmap, we must trap the same exceptions and pass
2587 * them to L1. When running L2, we will only handle the exceptions
2588 * specified above if L1 did not want them.
2589 */
2590 if (is_guest_mode(vcpu))
2591 eb |= get_vmcs12(vcpu)->exception_bitmap;
2592
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002593 vmcs_write32(EXCEPTION_BITMAP, eb);
2594}
2595
Ashok Raj15d45072018-02-01 22:59:43 +01002596/*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01002597 * Check if MSR is intercepted for currently loaded MSR bitmap.
2598 */
2599static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
2600{
2601 unsigned long *msr_bitmap;
2602 int f = sizeof(unsigned long);
2603
2604 if (!cpu_has_vmx_msr_bitmap())
2605 return true;
2606
2607 msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
2608
2609 if (msr <= 0x1fff) {
2610 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2611 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2612 msr &= 0x1fff;
2613 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2614 }
2615
2616 return true;
2617}
2618
2619/*
Ashok Raj15d45072018-02-01 22:59:43 +01002620 * Check if MSR is intercepted for L01 MSR bitmap.
2621 */
2622static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr)
2623{
2624 unsigned long *msr_bitmap;
2625 int f = sizeof(unsigned long);
2626
2627 if (!cpu_has_vmx_msr_bitmap())
2628 return true;
2629
2630 msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
2631
2632 if (msr <= 0x1fff) {
2633 return !!test_bit(msr, msr_bitmap + 0x800 / f);
2634 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2635 msr &= 0x1fff;
2636 return !!test_bit(msr, msr_bitmap + 0xc00 / f);
2637 }
2638
2639 return true;
2640}
2641
Gleb Natapov2961e8762013-11-25 15:37:13 +02002642static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2643 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002644{
Gleb Natapov2961e8762013-11-25 15:37:13 +02002645 vm_entry_controls_clearbit(vmx, entry);
2646 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002647}
2648
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002649static int find_msr(struct vmx_msrs *m, unsigned int msr)
2650{
2651 unsigned int i;
2652
2653 for (i = 0; i < m->nr; ++i) {
2654 if (m->val[i].index == msr)
2655 return i;
2656 }
2657 return -ENOENT;
2658}
2659
Avi Kivity61d2ef22010-04-28 16:40:38 +03002660static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
2661{
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002662 int i;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002663 struct msr_autoload *m = &vmx->msr_autoload;
2664
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002665 switch (msr) {
2666 case MSR_EFER:
2667 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002668 clear_atomic_switch_msr_special(vmx,
2669 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002670 VM_EXIT_LOAD_IA32_EFER);
2671 return;
2672 }
2673 break;
2674 case MSR_CORE_PERF_GLOBAL_CTRL:
2675 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002676 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002677 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2678 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2679 return;
2680 }
2681 break;
Avi Kivity110312c2010-12-21 12:54:20 +02002682 }
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002683 i = find_msr(&m->guest, msr);
2684 if (i < 0)
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002685 goto skip_guest;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002686 --m->guest.nr;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002687 m->guest.val[i] = m->guest.val[m->guest.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002688 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Avi Kivity110312c2010-12-21 12:54:20 +02002689
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002690skip_guest:
2691 i = find_msr(&m->host, msr);
2692 if (i < 0)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002693 return;
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002694
2695 --m->host.nr;
2696 m->host.val[i] = m->host.val[m->host.nr];
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002697 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002698}
2699
Gleb Natapov2961e8762013-11-25 15:37:13 +02002700static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
2701 unsigned long entry, unsigned long exit,
2702 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
2703 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002704{
2705 vmcs_write64(guest_val_vmcs, guest_val);
2706 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02002707 vm_entry_controls_setbit(vmx, entry);
2708 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002709}
2710
Avi Kivity61d2ef22010-04-28 16:40:38 +03002711static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002712 u64 guest_val, u64 host_val, bool entry_only)
Avi Kivity61d2ef22010-04-28 16:40:38 +03002713{
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002714 int i, j = 0;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002715 struct msr_autoload *m = &vmx->msr_autoload;
2716
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002717 switch (msr) {
2718 case MSR_EFER:
2719 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002720 add_atomic_switch_msr_special(vmx,
2721 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002722 VM_EXIT_LOAD_IA32_EFER,
2723 GUEST_IA32_EFER,
2724 HOST_IA32_EFER,
2725 guest_val, host_val);
2726 return;
2727 }
2728 break;
2729 case MSR_CORE_PERF_GLOBAL_CTRL:
2730 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02002731 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002732 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
2733 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
2734 GUEST_IA32_PERF_GLOBAL_CTRL,
2735 HOST_IA32_PERF_GLOBAL_CTRL,
2736 guest_val, host_val);
2737 return;
2738 }
2739 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01002740 case MSR_IA32_PEBS_ENABLE:
2741 /* PEBS needs a quiescent period after being disabled (to write
2742 * a record). Disabling PEBS through VMX MSR swapping doesn't
2743 * provide that period, so a CPU could write host's record into
2744 * guest's memory.
2745 */
2746 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02002747 }
2748
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002749 i = find_msr(&m->guest, msr);
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002750 if (!entry_only)
2751 j = find_msr(&m->host, msr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002752
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002753 if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02002754 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02002755 "Can't add msr %x\n", msr);
2756 return;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002757 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002758 if (i < 0) {
Konrad Rzeszutek Wilkca83b4a2018-06-20 20:11:39 -04002759 i = m->guest.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002760 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002761 }
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002762 m->guest.val[i].index = msr;
2763 m->guest.val[i].value = guest_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002764
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002765 if (entry_only)
2766 return;
2767
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002768 if (j < 0) {
2769 j = m->host.nr++;
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04002770 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002771 }
Konrad Rzeszutek Wilk31907092018-06-20 22:00:47 -04002772 m->host.val[j].index = msr;
2773 m->host.val[j].value = host_val;
Avi Kivity61d2ef22010-04-28 16:40:38 +03002774}
2775
Avi Kivity92c0d902009-10-29 11:00:16 +02002776static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03002777{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002778 u64 guest_efer = vmx->vcpu.arch.efer;
2779 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03002780
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002781 if (!enable_ept) {
2782 /*
2783 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
2784 * host CPUID is more efficient than testing guest CPUID
2785 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
2786 */
2787 if (boot_cpu_has(X86_FEATURE_SMEP))
2788 guest_efer |= EFER_NX;
2789 else if (!(guest_efer & EFER_NX))
2790 ignore_bits |= EFER_NX;
2791 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002792
Avi Kivity51c6cf62007-08-29 03:48:05 +03002793 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002794 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002795 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002796 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002797#ifdef CONFIG_X86_64
2798 ignore_bits |= EFER_LMA | EFER_LME;
2799 /* SCE is meaningful only in long mode on Intel */
2800 if (guest_efer & EFER_LMA)
2801 ignore_bits &= ~(u64)EFER_SCE;
2802#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002803
2804 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002805
2806 /*
2807 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2808 * On CPUs that support "load IA32_EFER", always switch EFER
2809 * atomically, since it's faster than switching it manually.
2810 */
2811 if (cpu_has_load_ia32_efer ||
2812 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002813 if (!(guest_efer & EFER_LMA))
2814 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002815 if (guest_efer != host_efer)
2816 add_atomic_switch_msr(vmx, MSR_EFER,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04002817 guest_efer, host_efer, false);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002818 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002819 } else {
2820 guest_efer &= ~ignore_bits;
2821 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002822
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002823 vmx->guest_msrs[efer_offset].data = guest_efer;
2824 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2825
2826 return true;
2827 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002828}
2829
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002830#ifdef CONFIG_X86_32
2831/*
2832 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2833 * VMCS rather than the segment table. KVM uses this helper to figure
2834 * out the current bases to poke them into the VMCS before entry.
2835 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002836static unsigned long segment_base(u16 selector)
2837{
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002838 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002839 unsigned long v;
2840
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002841 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002842 return 0;
2843
Thomas Garnier45fc8752017-03-14 10:05:08 -07002844 table = get_current_gdt_ro();
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002845
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002846 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002847 u16 ldt_selector = kvm_read_ldt();
2848
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002849 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002850 return 0;
2851
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002852 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002853 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002854 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002855 return v;
2856}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002857#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002858
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002859static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002860{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002862 struct vmcs_host_state *host_state;
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002863#ifdef CONFIG_X86_64
Vitaly Kuznetsov35060ed2018-03-13 18:48:05 +01002864 int cpu = raw_smp_processor_id();
Arnd Bergmann51e8a8c2018-04-04 12:44:14 +02002865#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002866 unsigned long fs_base, gs_base;
2867 u16 fs_sel, gs_sel;
Avi Kivity26bb0982009-09-07 11:14:12 +03002868 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002869
Sean Christophersond264ee02018-08-27 15:21:12 -07002870 vmx->req_immediate_exit = false;
2871
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002872 if (vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002873 return;
2874
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002875 vmx->loaded_cpu_state = vmx->loaded_vmcs;
Sean Christophersond7ee0392018-07-23 12:32:47 -07002876 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002877
Avi Kivity33ed6322007-05-02 16:54:03 +03002878 /*
2879 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2880 * allow segment selectors with cpl > 0 or ti == 1.
2881 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07002882 host_state->ldt_sel = kvm_read_ldt();
Vitaly Kuznetsov42b933b2018-03-13 18:48:04 +01002883
2884#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002885 savesegment(ds, host_state->ds_sel);
2886 savesegment(es, host_state->es_sel);
Sean Christophersone368b872018-07-23 12:32:41 -07002887
2888 gs_base = cpu_kernelmode_gs_base(cpu);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002889 if (likely(is_64bit_mm(current->mm))) {
2890 save_fsgs_for_kvm();
Sean Christophersone368b872018-07-23 12:32:41 -07002891 fs_sel = current->thread.fsindex;
2892 gs_sel = current->thread.gsindex;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002893 fs_base = current->thread.fsbase;
Sean Christophersone368b872018-07-23 12:32:41 -07002894 vmx->msr_host_kernel_gs_base = current->thread.gsbase;
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002895 } else {
Sean Christophersone368b872018-07-23 12:32:41 -07002896 savesegment(fs, fs_sel);
2897 savesegment(gs, gs_sel);
Vitaly Kuznetsovb062b792018-07-11 19:37:18 +02002898 fs_base = read_msr(MSR_FS_BASE);
Sean Christophersone368b872018-07-23 12:32:41 -07002899 vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
Avi Kivity33ed6322007-05-02 16:54:03 +03002900 }
2901
Paolo Bonzini4679b612018-09-24 17:23:01 +02002902 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity33ed6322007-05-02 16:54:03 +03002903#else
Sean Christophersone368b872018-07-23 12:32:41 -07002904 savesegment(fs, fs_sel);
2905 savesegment(gs, gs_sel);
2906 fs_base = segment_base(fs_sel);
2907 gs_base = segment_base(gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002908#endif
Sean Christophersone368b872018-07-23 12:32:41 -07002909
Sean Christopherson8f21a0b2018-07-23 12:32:49 -07002910 if (unlikely(fs_sel != host_state->fs_sel)) {
2911 if (!(fs_sel & 7))
2912 vmcs_write16(HOST_FS_SELECTOR, fs_sel);
2913 else
2914 vmcs_write16(HOST_FS_SELECTOR, 0);
2915 host_state->fs_sel = fs_sel;
2916 }
2917 if (unlikely(gs_sel != host_state->gs_sel)) {
2918 if (!(gs_sel & 7))
2919 vmcs_write16(HOST_GS_SELECTOR, gs_sel);
2920 else
2921 vmcs_write16(HOST_GS_SELECTOR, 0);
2922 host_state->gs_sel = gs_sel;
2923 }
Sean Christopherson5e079c72018-07-23 12:32:50 -07002924 if (unlikely(fs_base != host_state->fs_base)) {
2925 vmcs_writel(HOST_FS_BASE, fs_base);
2926 host_state->fs_base = fs_base;
2927 }
2928 if (unlikely(gs_base != host_state->gs_base)) {
2929 vmcs_writel(HOST_GS_BASE, gs_base);
2930 host_state->gs_base = gs_base;
2931 }
Avi Kivity33ed6322007-05-02 16:54:03 +03002932
Avi Kivity26bb0982009-09-07 11:14:12 +03002933 for (i = 0; i < vmx->save_nmsrs; ++i)
2934 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002935 vmx->guest_msrs[i].data,
2936 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002937}
2938
Sean Christopherson6d6095b2018-07-23 12:32:44 -07002939static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002940{
Sean Christophersond7ee0392018-07-23 12:32:47 -07002941 struct vmcs_host_state *host_state;
2942
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002943 if (!vmx->loaded_cpu_state)
Avi Kivity33ed6322007-05-02 16:54:03 +03002944 return;
2945
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002946 WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs);
Sean Christophersond7ee0392018-07-23 12:32:47 -07002947 host_state = &vmx->loaded_cpu_state->host_state;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002948
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002949 ++vmx->vcpu.stat.host_state_reload;
Sean Christophersonbd9966d2018-07-23 12:32:42 -07002950 vmx->loaded_cpu_state = NULL;
2951
Avi Kivityc8770e72010-11-11 12:37:26 +02002952#ifdef CONFIG_X86_64
Paolo Bonzini4679b612018-09-24 17:23:01 +02002953 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivityc8770e72010-11-11 12:37:26 +02002954#endif
Sean Christophersond7ee0392018-07-23 12:32:47 -07002955 if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2956 kvm_load_ldt(host_state->ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002957#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002958 load_gs_index(host_state->gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002959#else
Sean Christophersond7ee0392018-07-23 12:32:47 -07002960 loadsegment(gs, host_state->gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002961#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002962 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07002963 if (host_state->fs_sel & 7)
2964 loadsegment(fs, host_state->fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002965#ifdef CONFIG_X86_64
Sean Christophersond7ee0392018-07-23 12:32:47 -07002966 if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2967 loadsegment(ds, host_state->ds_sel);
2968 loadsegment(es, host_state->es_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002969 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002970#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002971 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002972#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002973 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002974#endif
Thomas Garnier45fc8752017-03-14 10:05:08 -07002975 load_fixmap_gdt(raw_smp_processor_id());
Avi Kivity33ed6322007-05-02 16:54:03 +03002976}
2977
Sean Christopherson678e3152018-07-23 12:32:43 -07002978#ifdef CONFIG_X86_64
2979static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
Avi Kivitya9b21b62008-06-24 11:48:49 +03002980{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002981 preempt_disable();
2982 if (vmx->loaded_cpu_state)
2983 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2984 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002985 return vmx->msr_guest_kernel_gs_base;
Avi Kivitya9b21b62008-06-24 11:48:49 +03002986}
2987
Sean Christopherson678e3152018-07-23 12:32:43 -07002988static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2989{
Paolo Bonzini4679b612018-09-24 17:23:01 +02002990 preempt_disable();
2991 if (vmx->loaded_cpu_state)
2992 wrmsrl(MSR_KERNEL_GS_BASE, data);
2993 preempt_enable();
Sean Christopherson678e3152018-07-23 12:32:43 -07002994 vmx->msr_guest_kernel_gs_base = data;
2995}
2996#endif
2997
Feng Wu28b835d2015-09-18 22:29:54 +08002998static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2999{
3000 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3001 struct pi_desc old, new;
3002 unsigned int dest;
3003
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003004 /*
3005 * In case of hot-plug or hot-unplug, we may have to undo
3006 * vmx_vcpu_pi_put even if there is no assigned device. And we
3007 * always keep PI.NDST up to date for simplicity: it makes the
3008 * code easier, and CPU migration is not a fast path.
3009 */
3010 if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
Feng Wu28b835d2015-09-18 22:29:54 +08003011 return;
3012
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003013 /*
3014 * First handle the simple case where no cmpxchg is necessary; just
3015 * allow posting non-urgent interrupts.
3016 *
3017 * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
3018 * PI.NDST: pi_post_block will do it for us and the wakeup_handler
3019 * expects the VCPU to be on the blocked_vcpu_list that matches
3020 * PI.NDST.
3021 */
3022 if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR ||
3023 vcpu->cpu == cpu) {
3024 pi_clear_sn(pi_desc);
3025 return;
3026 }
3027
3028 /* The full case. */
Feng Wu28b835d2015-09-18 22:29:54 +08003029 do {
3030 old.control = new.control = pi_desc->control;
3031
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003032 dest = cpu_physical_id(cpu);
Feng Wu28b835d2015-09-18 22:29:54 +08003033
Paolo Bonzini31afb2e2017-06-06 12:57:06 +02003034 if (x2apic_enabled())
3035 new.ndst = dest;
3036 else
3037 new.ndst = (dest << 8) & 0xFF00;
Feng Wu28b835d2015-09-18 22:29:54 +08003038
Feng Wu28b835d2015-09-18 22:29:54 +08003039 new.sn = 0;
Paolo Bonzinic0a16662017-09-28 17:58:41 +02003040 } while (cmpxchg64(&pi_desc->control, old.control,
3041 new.control) != old.control);
Feng Wu28b835d2015-09-18 22:29:54 +08003042}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003043
Peter Feinerc95ba922016-08-17 09:36:47 -07003044static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
3045{
3046 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
3047 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
3048}
3049
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050/*
3051 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
3052 * vcpu mutex is already taken.
3053 */
Avi Kivity15ad7142007-07-11 18:17:21 +03003054static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003057 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003059 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003060 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003061 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003062 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08003063
3064 /*
3065 * Read loaded_vmcs->cpu should be before fetching
3066 * loaded_vmcs->loaded_vmcss_on_cpu_link.
3067 * See the comments in __loaded_vmcs_clear().
3068 */
3069 smp_rmb();
3070
Nadav Har'Eld462b812011-05-24 15:26:10 +03003071 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
3072 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003073 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003074 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003075 }
3076
3077 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
3078 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
3079 vmcs_load(vmx->loaded_vmcs->vmcs);
Ashok Raj15d45072018-02-01 22:59:43 +01003080 indirect_branch_prediction_barrier();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003081 }
3082
3083 if (!already_loaded) {
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003084 void *gdt = get_current_gdt_ro();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07003085 unsigned long sysenter_esp;
3086
3087 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08003088
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 /*
3090 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003091 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08003093 vmcs_writel(HOST_TR_BASE,
Andy Lutomirski72f5e082017-12-04 15:07:20 +01003094 (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
Andy Lutomirski59c58ceb2017-03-22 14:32:33 -07003095 vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08003097 /*
3098 * VM exits change the host TR limit to 0x67 after a VM
3099 * exit. This is okay, since 0x67 covers everything except
3100 * the IO bitmap and have have code to handle the IO bitmap
3101 * being lost after a VM exit.
3102 */
3103 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
3104
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
3106 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08003107
Nadav Har'Eld462b812011-05-24 15:26:10 +03003108 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109 }
Feng Wu28b835d2015-09-18 22:29:54 +08003110
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003111 /* Setup TSC multiplier */
3112 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07003113 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
3114 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08003115
Feng Wu28b835d2015-09-18 22:29:54 +08003116 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08003117 vmx->host_pkru = read_pkru();
Wanpeng Li74c55932017-11-29 01:31:20 -08003118 vmx->host_debugctlmsr = get_debugctlmsr();
Feng Wu28b835d2015-09-18 22:29:54 +08003119}
3120
3121static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
3122{
3123 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
3124
3125 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08003126 !irq_remapping_cap(IRQ_POSTING_CAP) ||
3127 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08003128 return;
3129
3130 /* Set SN when the vCPU is preempted */
3131 if (vcpu->preempted)
3132 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003133}
3134
3135static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
3136{
Feng Wu28b835d2015-09-18 22:29:54 +08003137 vmx_vcpu_pi_put(vcpu);
3138
Sean Christopherson6d6095b2018-07-23 12:32:44 -07003139 vmx_prepare_switch_to_host(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140}
3141
Wanpeng Lif244dee2017-07-20 01:11:54 -07003142static bool emulation_required(struct kvm_vcpu *vcpu)
3143{
3144 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3145}
3146
Avi Kivityedcafe32009-12-30 18:07:40 +02003147static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
3148
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003149/*
3150 * Return the cr0 value that a nested guest would read. This is a combination
3151 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
3152 * its hypervisor (cr0_read_shadow).
3153 */
3154static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
3155{
3156 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
3157 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
3158}
3159static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
3160{
3161 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
3162 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
3163}
3164
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
3166{
Avi Kivity78ac8b42010-04-08 18:19:35 +03003167 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03003168
Avi Kivity6de12732011-03-07 12:51:22 +02003169 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
3170 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3171 rflags = vmcs_readl(GUEST_RFLAGS);
3172 if (to_vmx(vcpu)->rmode.vm86_active) {
3173 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3174 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
3175 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3176 }
3177 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003178 }
Avi Kivity6de12732011-03-07 12:51:22 +02003179 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180}
3181
3182static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
3183{
Wanpeng Lif244dee2017-07-20 01:11:54 -07003184 unsigned long old_rflags = vmx_get_rflags(vcpu);
3185
Avi Kivity6de12732011-03-07 12:51:22 +02003186 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
3187 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003188 if (to_vmx(vcpu)->rmode.vm86_active) {
3189 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003190 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03003191 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 vmcs_writel(GUEST_RFLAGS, rflags);
Wanpeng Lif244dee2017-07-20 01:11:54 -07003193
3194 if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM)
3195 to_vmx(vcpu)->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003196}
3197
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003198static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003199{
3200 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3201 int ret = 0;
3202
3203 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01003204 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003205 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01003206 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003207
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02003208 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04003209}
3210
3211static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
3212{
3213 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
3214 u32 interruptibility = interruptibility_old;
3215
3216 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
3217
Jan Kiszka48005f62010-02-19 19:38:07 +01003218 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003219 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01003220 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04003221 interruptibility |= GUEST_INTR_STATE_STI;
3222
3223 if ((interruptibility != interruptibility_old))
3224 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
3225}
3226
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
3228{
3229 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003231 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003233 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234
Glauber Costa2809f5d2009-05-12 16:21:05 -04003235 /* skipping an emulated instruction also counts */
3236 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237}
3238
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003239static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu,
3240 unsigned long exit_qual)
3241{
3242 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
3243 unsigned int nr = vcpu->arch.exception.nr;
3244 u32 intr_info = nr | INTR_INFO_VALID_MASK;
3245
3246 if (vcpu->arch.exception.has_error_code) {
3247 vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code;
3248 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3249 }
3250
3251 if (kvm_exception_is_soft(nr))
3252 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3253 else
3254 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3255
3256 if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) &&
3257 vmx_get_nmi_mask(vcpu))
3258 intr_info |= INTR_INFO_UNBLOCK_NMI;
3259
3260 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual);
3261}
3262
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003263/*
3264 * KVM wants to inject page-faults which it got to the guest. This function
3265 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003266 */
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003267static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003268{
3269 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003270 unsigned int nr = vcpu->arch.exception.nr;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003271
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003272 if (nr == PF_VECTOR) {
3273 if (vcpu->arch.exception.nested_apf) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003274 *exit_qual = vcpu->arch.apf.nested_apf_token;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003275 return 1;
3276 }
3277 /*
3278 * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception.
3279 * The fix is to add the ancillary datum (CR2 or DR6) to structs
3280 * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6
3281 * can be written only when inject_pending_event runs. This should be
3282 * conditional on a new capability---if the capability is disabled,
3283 * kvm_multiple_exception would write the ancillary information to
3284 * CR2 or DR6, for backwards ABI-compatibility.
3285 */
3286 if (nested_vmx_is_page_fault_vmexit(vmcs12,
3287 vcpu->arch.exception.error_code)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003288 *exit_qual = vcpu->arch.cr2;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003289 return 1;
3290 }
3291 } else {
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003292 if (vmcs12->exception_bitmap & (1u << nr)) {
Wanpeng Libfcf83b2017-08-24 03:35:11 -07003293 if (nr == DB_VECTOR)
3294 *exit_qual = vcpu->arch.dr6;
3295 else
3296 *exit_qual = 0;
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003297 return 1;
3298 }
Wanpeng Liadfe20f2017-07-13 18:30:41 -07003299 }
3300
Paolo Bonzinib96fb432017-07-27 12:29:32 +02003301 return 0;
Nadav Har'El0b6ac342011-05-25 23:13:36 +03003302}
3303
Wanpeng Licaa057a2018-03-12 04:53:03 -07003304static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
3305{
3306 /*
3307 * Ensure that we clear the HLT state in the VMCS. We don't need to
3308 * explicitly skip the instruction because if the HLT state is set,
3309 * then the instruction is already executing and RIP has already been
3310 * advanced.
3311 */
3312 if (kvm_hlt_in_guest(vcpu->kvm) &&
3313 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
3314 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
3315}
3316
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003317static void vmx_queue_exception(struct kvm_vcpu *vcpu)
Avi Kivity298101d2007-11-25 13:41:11 +02003318{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003319 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003320 unsigned nr = vcpu->arch.exception.nr;
3321 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -07003322 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003323 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003324
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003325 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003326 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003327 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
3328 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003329
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003330 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003331 int inc_eip = 0;
3332 if (kvm_exception_is_soft(nr))
3333 inc_eip = vcpu->arch.event_exit_inst_len;
3334 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003335 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003336 return;
3337 }
3338
Sean Christophersonadd5ff72018-03-23 09:34:00 -07003339 WARN_ON_ONCE(vmx->emulation_required);
3340
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003341 if (kvm_exception_is_soft(nr)) {
3342 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3343 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01003344 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
3345 } else
3346 intr_info |= INTR_TYPE_HARD_EXCEPTION;
3347
3348 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Wanpeng Licaa057a2018-03-12 04:53:03 -07003349
3350 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02003351}
3352
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003353static bool vmx_rdtscp_supported(void)
3354{
3355 return cpu_has_vmx_rdtscp();
3356}
3357
Mao, Junjiead756a12012-07-02 01:18:48 +00003358static bool vmx_invpcid_supported(void)
3359{
Junaid Shahideb4b2482018-06-27 14:59:14 -07003360 return cpu_has_vmx_invpcid();
Mao, Junjiead756a12012-07-02 01:18:48 +00003361}
3362
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363/*
Eddie Donga75beee2007-05-17 18:55:15 +03003364 * Swap MSR entry in host/guest MSR entry array.
3365 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003366static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03003367{
Avi Kivity26bb0982009-09-07 11:14:12 +03003368 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003369
3370 tmp = vmx->guest_msrs[to];
3371 vmx->guest_msrs[to] = vmx->guest_msrs[from];
3372 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03003373}
3374
3375/*
Avi Kivitye38aea32007-04-19 13:22:48 +03003376 * Set up the vmcs to automatically save and restore system
3377 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
3378 * mode, as fiddling with msrs is very expensive.
3379 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003380static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03003381{
Avi Kivity26bb0982009-09-07 11:14:12 +03003382 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03003383
Eddie Donga75beee2007-05-17 18:55:15 +03003384 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003385#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10003386 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10003387 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03003388 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003389 move_msr_up(vmx, index, save_nmsrs++);
3390 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003391 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003392 move_msr_up(vmx, index, save_nmsrs++);
3393 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03003394 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10003395 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003396 index = __find_msr_index(vmx, MSR_TSC_AUX);
Radim Krčmářd6321d42017-08-05 00:12:49 +02003397 if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003398 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03003399 /*
Brian Gerst8c065852010-07-17 09:03:26 -04003400 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03003401 * if efer.sce is enabled.
3402 */
Brian Gerst8c065852010-07-17 09:03:26 -04003403 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02003404 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10003405 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003406 }
Eddie Donga75beee2007-05-17 18:55:15 +03003407#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02003408 index = __find_msr_index(vmx, MSR_EFER);
3409 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03003410 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03003411
Avi Kivity26bb0982009-09-07 11:14:12 +03003412 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02003413
Yang Zhang8d146952013-01-25 10:18:50 +08003414 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01003415 vmx_update_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03003416}
3417
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003418static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003419{
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003420 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02003422 if (is_guest_mode(vcpu) &&
3423 (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING))
3424 return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
3425
3426 return vcpu->arch.tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003427}
3428
3429/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10003430 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10003432static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003434 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03003435 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003436 * We're here if L1 chose not to trap WRMSR to TSC. According
3437 * to the spec, this should set L1's TSC; The offset that L1
3438 * set for L2 remains unchanged, and still needs to be added
3439 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03003440 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003441 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003442 /* recalculate vmcs02.TSC_OFFSET: */
3443 vmcs12 = get_vmcs12(vcpu);
3444 vmcs_write64(TSC_OFFSET, offset +
3445 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
3446 vmcs12->tsc_offset : 0));
3447 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09003448 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
3449 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03003450 vmcs_write64(TSC_OFFSET, offset);
3451 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452}
3453
Nadav Har'El801d3422011-05-25 23:02:23 +03003454/*
3455 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
3456 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
3457 * all guests if the "nested" module option is off, and can also be disabled
3458 * for a single guest by disabling its VMX cpuid bit.
3459 */
3460static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
3461{
Radim Krčmářd6321d42017-08-05 00:12:49 +02003462 return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
Nadav Har'El801d3422011-05-25 23:02:23 +03003463}
3464
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003466 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
3467 * returned for the various VMX controls MSRs when nested VMX is enabled.
3468 * The same values should also be used to verify that vmcs12 control fields are
3469 * valid during nested entry from L1 to L2.
3470 * Each of these control msrs has a low and high 32-bit half: A low bit is on
3471 * if the corresponding bit in the (32-bit) control field *must* be on, and a
3472 * bit in the high half is on if the corresponding bit in the control field
3473 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003474 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003475static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003476{
Paolo Bonzini13893092018-02-26 13:40:09 +01003477 if (!nested) {
3478 memset(msrs, 0, sizeof(*msrs));
3479 return;
3480 }
3481
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003482 /*
3483 * Note that as a general rule, the high half of the MSRs (bits in
3484 * the control fields which may be 1) should be initialized by the
3485 * intersection of the underlying hardware's MSR (i.e., features which
3486 * can be supported) and the list of features we want to expose -
3487 * because they are known to be properly supported in our code.
3488 * Also, usually, the low half of the MSRs (bits which must be 1) can
3489 * be set to 0, meaning that L1 may turn off any of these bits. The
3490 * reason is that if one of these bits is necessary, it will appear
3491 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
3492 * fields of vmcs01 and vmcs02, will turn these bits off - and
Paolo Bonzini7313c692017-07-27 10:31:25 +02003493 * nested_vmx_exit_reflected() will not pass related exits to L1.
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003494 * These rules have exceptions below.
3495 */
3496
3497 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01003498 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003499 msrs->pinbased_ctls_low,
3500 msrs->pinbased_ctls_high);
3501 msrs->pinbased_ctls_low |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003502 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003503 msrs->pinbased_ctls_high &=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003504 PIN_BASED_EXT_INTR_MASK |
3505 PIN_BASED_NMI_EXITING |
Paolo Bonzini13893092018-02-26 13:40:09 +01003506 PIN_BASED_VIRTUAL_NMIS |
3507 (apicv ? PIN_BASED_POSTED_INTR : 0);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003508 msrs->pinbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003509 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01003510 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003511
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02003512 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003513 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003514 msrs->exit_ctls_low,
3515 msrs->exit_ctls_high);
3516 msrs->exit_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003517 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04003518
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003519 msrs->exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003520#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08003521 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003522#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01003523 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003524 msrs->exit_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003525 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003526 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04003527 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
3528
Jan Kiszka2996fca2014-06-16 13:59:43 +02003529 /* We support free control of debug control saving. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003530 msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003531
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003532 /* entry controls */
3533 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003534 msrs->entry_ctls_low,
3535 msrs->entry_ctls_high);
3536 msrs->entry_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003537 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003538 msrs->entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02003539#ifdef CONFIG_X86_64
3540 VM_ENTRY_IA32E_MODE |
3541#endif
3542 VM_ENTRY_LOAD_IA32_PAT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003543 msrs->entry_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003544 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02003545
Jan Kiszka2996fca2014-06-16 13:59:43 +02003546 /* We support free control of debug control loading. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003547 msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02003548
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003549 /* cpu-based controls */
3550 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003551 msrs->procbased_ctls_low,
3552 msrs->procbased_ctls_high);
3553 msrs->procbased_ctls_low =
Wincy Vanb9c237b2015-02-03 23:56:30 +08003554 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003555 msrs->procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01003556 CPU_BASED_VIRTUAL_INTR_PENDING |
3557 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003558 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
3559 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
3560 CPU_BASED_CR3_STORE_EXITING |
3561#ifdef CONFIG_X86_64
3562 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
3563#endif
3564 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03003565 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
3566 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
3567 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
3568 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003569 /*
3570 * We can allow some features even when not supported by the
3571 * hardware. For example, L1 can specify an MSR bitmap - and we
3572 * can use it to avoid exits to L1 - even when L0 runs L2
3573 * without MSR bitmaps.
3574 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003575 msrs->procbased_ctls_high |=
Wincy Vanb9c237b2015-02-03 23:56:30 +08003576 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02003577 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003578
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003579 /* We support free control of CR3 access interception. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003580 msrs->procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02003581 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
3582
Paolo Bonzini80154d72017-08-24 13:55:35 +02003583 /*
3584 * secondary cpu-based controls. Do not include those that
3585 * depend on CPUID bits, they are added later by vmx_cpuid_update.
3586 */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003587 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003588 msrs->secondary_ctls_low,
3589 msrs->secondary_ctls_high);
3590 msrs->secondary_ctls_low = 0;
3591 msrs->secondary_ctls_high &=
Paolo Bonzini1b073042016-10-25 16:06:30 +02003592 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08003593 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08003594 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08003595 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Paolo Bonzini3db13482017-08-24 14:48:03 +02003596 SECONDARY_EXEC_WBINVD_EXITING;
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003597
Liran Alon32c7acf2018-06-23 02:35:11 +03003598 /*
3599 * We can emulate "VMCS shadowing," even if the hardware
3600 * doesn't support it.
3601 */
3602 msrs->secondary_ctls_high |=
3603 SECONDARY_EXEC_SHADOW_VMCS;
Jan Kiszkac18911a2013-03-13 16:06:41 +01003604
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003605 if (enable_ept) {
3606 /* nested EPT: emulate EPT also to L1 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003607 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003608 SECONDARY_EXEC_ENABLE_EPT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003609 msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003610 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04003611 if (cpu_has_vmx_ept_execute_only())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003612 msrs->ept_caps |=
Bandan Das02120c42016-07-12 18:18:52 -04003613 VMX_EPT_EXECUTE_ONLY_BIT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003614 msrs->ept_caps &= vmx_capability.ept;
3615 msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01003616 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
3617 VMX_EPT_1GB_PAGE_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003618 if (enable_ept_ad_bits) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003619 msrs->secondary_ctls_high |=
Bandan Das03efce62017-05-05 15:25:15 -04003620 SECONDARY_EXEC_ENABLE_PML;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003621 msrs->ept_caps |= VMX_EPT_AD_BIT;
Bandan Das03efce62017-05-05 15:25:15 -04003622 }
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003623 }
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02003624
Bandan Das27c42a12017-08-03 15:54:42 -04003625 if (cpu_has_vmx_vmfunc()) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003626 msrs->secondary_ctls_high |=
Bandan Das27c42a12017-08-03 15:54:42 -04003627 SECONDARY_EXEC_ENABLE_VMFUNC;
Bandan Das41ab9372017-08-03 15:54:43 -04003628 /*
3629 * Advertise EPTP switching unconditionally
3630 * since we emulate it
3631 */
Wanpeng Li575b3a22017-10-19 07:00:34 +08003632 if (enable_ept)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003633 msrs->vmfunc_controls =
Wanpeng Li575b3a22017-10-19 07:00:34 +08003634 VMX_VMFUNC_EPTP_SWITCHING;
Bandan Das27c42a12017-08-03 15:54:42 -04003635 }
3636
Paolo Bonzinief697a72016-03-18 16:58:38 +01003637 /*
3638 * Old versions of KVM use the single-context version without
3639 * checking for support, so declare that it is supported even
3640 * though it is treated as global context. The alternative is
3641 * not failing the single-context invvpid, and it is worse.
3642 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003643 if (enable_vpid) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003644 msrs->secondary_ctls_high |=
Wanpeng Li63cb6d52017-03-20 21:18:53 -07003645 SECONDARY_EXEC_ENABLE_VPID;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003646 msrs->vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03003647 VMX_VPID_EXTENT_SUPPORTED_MASK;
David Hildenbrand1c13bff2017-08-24 20:51:33 +02003648 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07003649
Radim Krčmář0790ec12015-03-17 14:02:32 +01003650 if (enable_unrestricted_guest)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003651 msrs->secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01003652 SECONDARY_EXEC_UNRESTRICTED_GUEST;
3653
Paolo Bonzini2cf7ea92018-10-03 10:34:00 +02003654 if (flexpriority_enabled)
3655 msrs->secondary_ctls_high |=
3656 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3657
Jan Kiszkac18911a2013-03-13 16:06:41 +01003658 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08003659 rdmsr(MSR_IA32_VMX_MISC,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003660 msrs->misc_low,
3661 msrs->misc_high);
3662 msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA;
3663 msrs->misc_low |=
Jim Mattsonf4160e42018-05-29 09:11:33 -07003664 MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS |
Wincy Vanb9c237b2015-02-03 23:56:30 +08003665 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01003666 VMX_MISC_ACTIVITY_HLT;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003667 msrs->misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003668
3669 /*
3670 * This MSR reports some information about VMX support. We
3671 * should return information about the VMX we emulate for the
3672 * guest, and the VMCS structure we give it - not about the
3673 * VMX support of the underlying hardware.
3674 */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003675 msrs->basic =
David Matlack62cc6b9d2016-11-29 18:14:07 -08003676 VMCS12_REVISION |
3677 VMX_BASIC_TRUE_CTLS |
3678 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
3679 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
3680
3681 if (cpu_has_vmx_basic_inout())
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003682 msrs->basic |= VMX_BASIC_INOUT;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003683
3684 /*
David Matlack8322ebb2016-11-29 18:14:09 -08003685 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08003686 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
3687 * We picked the standard core2 setting.
3688 */
3689#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
3690#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003691 msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON;
3692 msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08003693
3694 /* These MSRs specify bits which the guest must keep fixed off. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003695 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1);
3696 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003697
3698 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003699 msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003700}
3701
David Matlack38991522016-11-29 18:14:08 -08003702/*
3703 * if fixed0[i] == 1: val[i] must be 1
3704 * if fixed1[i] == 0: val[i] must be 0
3705 */
3706static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
3707{
3708 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003709}
3710
3711static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
3712{
David Matlack38991522016-11-29 18:14:08 -08003713 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003714}
3715
3716static inline u64 vmx_control_msr(u32 low, u32 high)
3717{
3718 return low | ((u64)high << 32);
3719}
3720
David Matlack62cc6b9d2016-11-29 18:14:07 -08003721static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
3722{
3723 superset &= mask;
3724 subset &= mask;
3725
3726 return (superset | subset) == superset;
3727}
3728
3729static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
3730{
3731 const u64 feature_and_reserved =
3732 /* feature (except bit 48; see below) */
3733 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
3734 /* reserved */
3735 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003736 u64 vmx_basic = vmx->nested.msrs.basic;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003737
3738 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
3739 return -EINVAL;
3740
3741 /*
3742 * KVM does not emulate a version of VMX that constrains physical
3743 * addresses of VMX structures (e.g. VMCS) to 32-bits.
3744 */
3745 if (data & BIT_ULL(48))
3746 return -EINVAL;
3747
3748 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
3749 vmx_basic_vmcs_revision_id(data))
3750 return -EINVAL;
3751
3752 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
3753 return -EINVAL;
3754
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003755 vmx->nested.msrs.basic = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003756 return 0;
3757}
3758
3759static int
3760vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3761{
3762 u64 supported;
3763 u32 *lowp, *highp;
3764
3765 switch (msr_index) {
3766 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003767 lowp = &vmx->nested.msrs.pinbased_ctls_low;
3768 highp = &vmx->nested.msrs.pinbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003769 break;
3770 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003771 lowp = &vmx->nested.msrs.procbased_ctls_low;
3772 highp = &vmx->nested.msrs.procbased_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003773 break;
3774 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003775 lowp = &vmx->nested.msrs.exit_ctls_low;
3776 highp = &vmx->nested.msrs.exit_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003777 break;
3778 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003779 lowp = &vmx->nested.msrs.entry_ctls_low;
3780 highp = &vmx->nested.msrs.entry_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003781 break;
3782 case MSR_IA32_VMX_PROCBASED_CTLS2:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003783 lowp = &vmx->nested.msrs.secondary_ctls_low;
3784 highp = &vmx->nested.msrs.secondary_ctls_high;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003785 break;
3786 default:
3787 BUG();
3788 }
3789
3790 supported = vmx_control_msr(*lowp, *highp);
3791
3792 /* Check must-be-1 bits are still 1. */
3793 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
3794 return -EINVAL;
3795
3796 /* Check must-be-0 bits are still 0. */
3797 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
3798 return -EINVAL;
3799
3800 *lowp = data;
3801 *highp = data >> 32;
3802 return 0;
3803}
3804
3805static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
3806{
3807 const u64 feature_and_reserved_bits =
3808 /* feature */
3809 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
3810 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
3811 /* reserved */
3812 GENMASK_ULL(13, 9) | BIT_ULL(31);
3813 u64 vmx_misc;
3814
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003815 vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low,
3816 vmx->nested.msrs.misc_high);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003817
3818 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
3819 return -EINVAL;
3820
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003821 if ((vmx->nested.msrs.pinbased_ctls_high &
David Matlack62cc6b9d2016-11-29 18:14:07 -08003822 PIN_BASED_VMX_PREEMPTION_TIMER) &&
3823 vmx_misc_preemption_timer_rate(data) !=
3824 vmx_misc_preemption_timer_rate(vmx_misc))
3825 return -EINVAL;
3826
3827 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
3828 return -EINVAL;
3829
3830 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
3831 return -EINVAL;
3832
3833 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
3834 return -EINVAL;
3835
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003836 vmx->nested.msrs.misc_low = data;
3837 vmx->nested.msrs.misc_high = data >> 32;
Jim Mattsonf4160e42018-05-29 09:11:33 -07003838
3839 /*
3840 * If L1 has read-only VM-exit information fields, use the
3841 * less permissive vmx_vmwrite_bitmap to specify write
3842 * permissions for the shadow VMCS.
3843 */
3844 if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
3845 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
3846
David Matlack62cc6b9d2016-11-29 18:14:07 -08003847 return 0;
3848}
3849
3850static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
3851{
3852 u64 vmx_ept_vpid_cap;
3853
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003854 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps,
3855 vmx->nested.msrs.vpid_caps);
David Matlack62cc6b9d2016-11-29 18:14:07 -08003856
3857 /* Every bit is either reserved or a feature bit. */
3858 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
3859 return -EINVAL;
3860
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003861 vmx->nested.msrs.ept_caps = data;
3862 vmx->nested.msrs.vpid_caps = data >> 32;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003863 return 0;
3864}
3865
3866static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
3867{
3868 u64 *msr;
3869
3870 switch (msr_index) {
3871 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003872 msr = &vmx->nested.msrs.cr0_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003873 break;
3874 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003875 msr = &vmx->nested.msrs.cr4_fixed0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003876 break;
3877 default:
3878 BUG();
3879 }
3880
3881 /*
3882 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
3883 * must be 1 in the restored value.
3884 */
3885 if (!is_bitwise_subset(data, *msr, -1ULL))
3886 return -EINVAL;
3887
3888 *msr = data;
3889 return 0;
3890}
3891
3892/*
3893 * Called when userspace is restoring VMX MSRs.
3894 *
3895 * Returns 0 on success, non-0 otherwise.
3896 */
3897static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3898{
3899 struct vcpu_vmx *vmx = to_vmx(vcpu);
3900
Jim Mattsona943ac52018-05-29 09:11:32 -07003901 /*
3902 * Don't allow changes to the VMX capability MSRs while the vCPU
3903 * is in VMX operation.
3904 */
3905 if (vmx->nested.vmxon)
3906 return -EBUSY;
3907
David Matlack62cc6b9d2016-11-29 18:14:07 -08003908 switch (msr_index) {
3909 case MSR_IA32_VMX_BASIC:
3910 return vmx_restore_vmx_basic(vmx, data);
3911 case MSR_IA32_VMX_PINBASED_CTLS:
3912 case MSR_IA32_VMX_PROCBASED_CTLS:
3913 case MSR_IA32_VMX_EXIT_CTLS:
3914 case MSR_IA32_VMX_ENTRY_CTLS:
3915 /*
3916 * The "non-true" VMX capability MSRs are generated from the
3917 * "true" MSRs, so we do not support restoring them directly.
3918 *
3919 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3920 * should restore the "true" MSRs with the must-be-1 bits
3921 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3922 * DEFAULT SETTINGS".
3923 */
3924 return -EINVAL;
3925 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3926 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3927 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3928 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3929 case MSR_IA32_VMX_PROCBASED_CTLS2:
3930 return vmx_restore_control_msr(vmx, msr_index, data);
3931 case MSR_IA32_VMX_MISC:
3932 return vmx_restore_vmx_misc(vmx, data);
3933 case MSR_IA32_VMX_CR0_FIXED0:
3934 case MSR_IA32_VMX_CR4_FIXED0:
3935 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3936 case MSR_IA32_VMX_CR0_FIXED1:
3937 case MSR_IA32_VMX_CR4_FIXED1:
3938 /*
3939 * These MSRs are generated based on the vCPU's CPUID, so we
3940 * do not support restoring them directly.
3941 */
3942 return -EINVAL;
3943 case MSR_IA32_VMX_EPT_VPID_CAP:
3944 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3945 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003946 vmx->nested.msrs.vmcs_enum = data;
David Matlack62cc6b9d2016-11-29 18:14:07 -08003947 return 0;
3948 default:
3949 /*
3950 * The rest of the VMX capability MSRs do not support restore.
3951 */
3952 return -EINVAL;
3953 }
3954}
3955
Jan Kiszkacae50132014-01-04 18:47:22 +01003956/* Returns 0 on success, non-0 otherwise. */
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003957static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003958{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003959 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003960 case MSR_IA32_VMX_BASIC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003961 *pdata = msrs->basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003962 break;
3963 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3964 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003965 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003966 msrs->pinbased_ctls_low,
3967 msrs->pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003968 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3969 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003970 break;
3971 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3972 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003973 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003974 msrs->procbased_ctls_low,
3975 msrs->procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003976 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3977 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003978 break;
3979 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3980 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003981 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003982 msrs->exit_ctls_low,
3983 msrs->exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003984 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3985 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003986 break;
3987 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3988 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003989 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003990 msrs->entry_ctls_low,
3991 msrs->entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003992 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3993 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003994 break;
3995 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003996 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01003997 msrs->misc_low,
3998 msrs->misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003999 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004000 case MSR_IA32_VMX_CR0_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004001 *pdata = msrs->cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004002 break;
4003 case MSR_IA32_VMX_CR0_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004004 *pdata = msrs->cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004005 break;
4006 case MSR_IA32_VMX_CR4_FIXED0:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004007 *pdata = msrs->cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004008 break;
4009 case MSR_IA32_VMX_CR4_FIXED1:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004010 *pdata = msrs->cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004011 break;
4012 case MSR_IA32_VMX_VMCS_ENUM:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004013 *pdata = msrs->vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004014 break;
4015 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08004016 *pdata = vmx_control_msr(
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004017 msrs->secondary_ctls_low,
4018 msrs->secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004019 break;
4020 case MSR_IA32_VMX_EPT_VPID_CAP:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004021 *pdata = msrs->ept_caps |
4022 ((u64)msrs->vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004023 break;
Bandan Das27c42a12017-08-03 15:54:42 -04004024 case MSR_IA32_VMX_VMFUNC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004025 *pdata = msrs->vmfunc_controls;
Bandan Das27c42a12017-08-03 15:54:42 -04004026 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004027 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004028 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08004029 }
4030
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004031 return 0;
4032}
4033
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004034static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
4035 uint64_t val)
4036{
4037 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
4038
4039 return !(val & ~valid_bits);
4040}
4041
Tom Lendacky801e4592018-02-21 13:39:51 -06004042static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
4043{
Paolo Bonzini13893092018-02-26 13:40:09 +01004044 switch (msr->index) {
4045 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4046 if (!nested)
4047 return 1;
4048 return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
4049 default:
4050 return 1;
4051 }
4052
4053 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06004054}
4055
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03004056/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004057 * Reads an msr value (of 'msr_index') into 'pdata'.
4058 * Returns 0 on success, non-0 otherwise.
4059 * Assumes vcpu_load() was already called.
4060 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004061static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062{
Borislav Petkova6cb0992017-12-20 12:50:28 +01004063 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004064 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004065
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004066 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004067#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004068 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004069 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004070 break;
4071 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004072 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004074 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004075 msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004076 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03004077#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004079 return kvm_get_msr_common(vcpu, msr_info);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004080 case MSR_IA32_SPEC_CTRL:
4081 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004082 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4083 return 1;
4084
4085 msr_info->data = to_vmx(vcpu)->spec_ctrl;
4086 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004087 case MSR_IA32_ARCH_CAPABILITIES:
4088 if (!msr_info->host_initiated &&
4089 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
4090 return 1;
4091 msr_info->data = to_vmx(vcpu)->arch_capabilities;
4092 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004093 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004094 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004095 break;
4096 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004097 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 break;
4099 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004100 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004101 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004102 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004103 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004104 (!msr_info->host_initiated &&
4105 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004106 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004107 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004108 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004109 case MSR_IA32_MCG_EXT_CTL:
4110 if (!msr_info->host_initiated &&
Borislav Petkova6cb0992017-12-20 12:50:28 +01004111 !(vmx->msr_ia32_feature_control &
Ashok Rajc45dcc72016-06-22 14:59:56 +08004112 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01004113 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004114 msr_info->data = vcpu->arch.mcg_ext_ctl;
4115 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004116 case MSR_IA32_FEATURE_CONTROL:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004117 msr_info->data = vmx->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01004118 break;
4119 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
4120 if (!nested_vmx_allowed(vcpu))
4121 return 1;
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01004122 return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
4123 &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08004124 case MSR_IA32_XSS:
4125 if (!vmx_xsaves_supported())
4126 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004127 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08004128 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004129 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004130 if (!msr_info->host_initiated &&
4131 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004132 return 1;
4133 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004134 default:
Borislav Petkova6cb0992017-12-20 12:50:28 +01004135 msr = find_msr_entry(vmx, msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004136 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004137 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004138 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004139 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02004140 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004141 }
4142
Avi Kivity6aa8b732006-12-10 02:21:36 -08004143 return 0;
4144}
4145
Jan Kiszkacae50132014-01-04 18:47:22 +01004146static void vmx_leave_nested(struct kvm_vcpu *vcpu);
4147
Avi Kivity6aa8b732006-12-10 02:21:36 -08004148/*
4149 * Writes msr value into into the appropriate "register".
4150 * Returns 0 on success, non-0 otherwise.
4151 * Assumes vcpu_load() was already called.
4152 */
Will Auld8fe8ab42012-11-29 12:42:12 -08004153static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004154{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004155 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03004156 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03004157 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08004158 u32 msr_index = msr_info->index;
4159 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03004160
Avi Kivity6aa8b732006-12-10 02:21:36 -08004161 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08004162 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08004163 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03004164 break;
Avi Kivity16175a72009-03-23 22:13:44 +02004165#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004167 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004168 vmcs_writel(GUEST_FS_BASE, data);
4169 break;
4170 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03004171 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 vmcs_writel(GUEST_GS_BASE, data);
4173 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03004174 case MSR_KERNEL_GS_BASE:
Sean Christopherson678e3152018-07-23 12:32:43 -07004175 vmx_write_guest_kernel_gs_base(vmx, data);
Avi Kivity44ea2b12009-09-06 15:55:37 +03004176 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004177#endif
4178 case MSR_IA32_SYSENTER_CS:
4179 vmcs_write32(GUEST_SYSENTER_CS, data);
4180 break;
4181 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004182 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 break;
4184 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02004185 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00004187 case MSR_IA32_BNDCFGS:
Haozhong Zhang691bd432017-07-04 10:27:41 +08004188 if (!kvm_mpx_supported() ||
Radim Krčmářd6321d42017-08-05 00:12:49 +02004189 (!msr_info->host_initiated &&
4190 !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01004191 return 1;
Yu Zhangfd8cb432017-08-24 20:27:56 +08004192 if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
Jim Mattson45316622017-05-23 11:52:54 -07004193 (data & MSR_IA32_BNDCFGS_RSVD))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004195 vmcs_write64(GUEST_BNDCFGS, data);
4196 break;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004197 case MSR_IA32_SPEC_CTRL:
4198 if (!msr_info->host_initiated &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004199 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4200 return 1;
4201
4202 /* The STIBP bit doesn't fault even if it's not advertised */
Konrad Rzeszutek Wilk9f65fb22018-05-09 21:41:38 +02004203 if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01004204 return 1;
4205
4206 vmx->spec_ctrl = data;
4207
4208 if (!data)
4209 break;
4210
4211 /*
4212 * For non-nested:
4213 * When it's written (to non-zero) for the first time, pass
4214 * it through.
4215 *
4216 * For nested:
4217 * The handling of the MSR bitmap for L2 guests is done in
4218 * nested_vmx_merge_msr_bitmap. We should not touch the
4219 * vmcs02.msr_bitmap here since it gets completely overwritten
4220 * in the merging. We update the vmcs01 here for L1 as well
4221 * since it will end up touching the MSR anyway now.
4222 */
4223 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
4224 MSR_IA32_SPEC_CTRL,
4225 MSR_TYPE_RW);
4226 break;
Ashok Raj15d45072018-02-01 22:59:43 +01004227 case MSR_IA32_PRED_CMD:
4228 if (!msr_info->host_initiated &&
Ashok Raj15d45072018-02-01 22:59:43 +01004229 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
4230 return 1;
4231
4232 if (data & ~PRED_CMD_IBPB)
4233 return 1;
4234
4235 if (!data)
4236 break;
4237
4238 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
4239
4240 /*
4241 * For non-nested:
4242 * When it's written (to non-zero) for the first time, pass
4243 * it through.
4244 *
4245 * For nested:
4246 * The handling of the MSR bitmap for L2 guests is done in
4247 * nested_vmx_merge_msr_bitmap. We should not touch the
4248 * vmcs02.msr_bitmap here since it gets completely overwritten
4249 * in the merging.
4250 */
4251 vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
4252 MSR_TYPE_W);
4253 break;
KarimAllah Ahmed28c1c9f2018-02-01 22:59:44 +01004254 case MSR_IA32_ARCH_CAPABILITIES:
4255 if (!msr_info->host_initiated)
4256 return 1;
4257 vmx->arch_capabilities = data;
4258 break;
Sheng Yang468d4722008-10-09 16:01:55 +08004259 case MSR_IA32_CR_PAT:
4260 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03004261 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
4262 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08004263 vmcs_write64(GUEST_IA32_PAT, data);
4264 vcpu->arch.pat = data;
4265 break;
4266 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004267 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004268 break;
Will Auldba904632012-11-29 12:42:50 -08004269 case MSR_IA32_TSC_ADJUST:
4270 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004271 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08004272 case MSR_IA32_MCG_EXT_CTL:
4273 if ((!msr_info->host_initiated &&
4274 !(to_vmx(vcpu)->msr_ia32_feature_control &
4275 FEATURE_CONTROL_LMCE)) ||
4276 (data & ~MCG_EXT_CTL_LMCE_EN))
4277 return 1;
4278 vcpu->arch.mcg_ext_ctl = data;
4279 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01004280 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08004281 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08004282 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01004283 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
4284 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08004285 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01004286 if (msr_info->host_initiated && data == 0)
4287 vmx_leave_nested(vcpu);
4288 break;
4289 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08004290 if (!msr_info->host_initiated)
4291 return 1; /* they are read-only */
4292 if (!nested_vmx_allowed(vcpu))
4293 return 1;
4294 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08004295 case MSR_IA32_XSS:
4296 if (!vmx_xsaves_supported())
4297 return 1;
4298 /*
4299 * The only supported bit as of Skylake is bit 8, but
4300 * it is not supported on KVM.
4301 */
4302 if (data != 0)
4303 return 1;
4304 vcpu->arch.ia32_xss = data;
4305 if (vcpu->arch.ia32_xss != host_xss)
4306 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -04004307 vcpu->arch.ia32_xss, host_xss, false);
Wanpeng Li20300092014-12-02 19:14:59 +08004308 else
4309 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
4310 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004311 case MSR_TSC_AUX:
Radim Krčmářd6321d42017-08-05 00:12:49 +02004312 if (!msr_info->host_initiated &&
4313 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004314 return 1;
4315 /* Check reserved bit, higher 32 bits should be zero */
4316 if ((data >> 32) != 0)
4317 return 1;
4318 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10004320 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08004321 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07004322 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08004323 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004324 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
4325 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004326 ret = kvm_set_shared_msr(msr->index, msr->data,
4327 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03004328 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07004329 if (ret)
4330 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03004331 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08004332 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 }
Will Auld8fe8ab42012-11-29 12:42:12 -08004334 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335 }
4336
Eddie Dong2cc51562007-05-21 07:28:09 +03004337 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004338}
4339
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004340static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004342 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
4343 switch (reg) {
4344 case VCPU_REGS_RSP:
4345 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
4346 break;
4347 case VCPU_REGS_RIP:
4348 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
4349 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004350 case VCPU_EXREG_PDPTR:
4351 if (enable_ept)
4352 ept_save_pdptrs(vcpu);
4353 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004354 default:
4355 break;
4356 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357}
4358
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359static __init int cpu_has_kvm_support(void)
4360{
Eduardo Habkost6210e372008-11-17 19:03:16 -02004361 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362}
4363
4364static __init int vmx_disabled_by_bios(void)
4365{
4366 u64 msr;
4367
4368 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04004369 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08004370 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04004371 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
4372 && tboot_enabled())
4373 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08004374 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04004375 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08004376 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08004377 && !tboot_enabled()) {
4378 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08004379 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04004380 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08004381 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08004382 /* launched w/o TXT and VMX disabled */
4383 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
4384 && !tboot_enabled())
4385 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04004386 }
4387
4388 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389}
4390
Dongxiao Xu7725b892010-05-11 18:29:38 +08004391static void kvm_cpu_vmxon(u64 addr)
4392{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004393 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004394 intel_pt_handle_vmx(1);
4395
Dongxiao Xu7725b892010-05-11 18:29:38 +08004396 asm volatile (ASM_VMX_VMXON_RAX
4397 : : "a"(&addr), "m"(addr)
4398 : "memory", "cc");
4399}
4400
Radim Krčmář13a34e02014-08-28 15:13:03 +02004401static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402{
4403 int cpu = raw_smp_processor_id();
4404 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04004405 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004407 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02004408 return -EBUSY;
4409
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004410 /*
4411 * This can happen if we hot-added a CPU but failed to allocate
4412 * VP assist page for it.
4413 */
4414 if (static_branch_unlikely(&enable_evmcs) &&
4415 !hv_get_vp_assist_page(cpu))
4416 return -EFAULT;
4417
Nadav Har'Eld462b812011-05-24 15:26:10 +03004418 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08004419 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
4420 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08004421
4422 /*
4423 * Now we can enable the vmclear operation in kdump
4424 * since the loaded_vmcss_on_cpu list on this cpu
4425 * has been initialized.
4426 *
4427 * Though the cpu is not in VMX operation now, there
4428 * is no problem to enable the vmclear operation
4429 * for the loaded_vmcss_on_cpu list is empty!
4430 */
4431 crash_enable_local_vmclear(cpu);
4432
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04004434
4435 test_bits = FEATURE_CONTROL_LOCKED;
4436 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
4437 if (tboot_enabled())
4438 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
4439
4440 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004441 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04004442 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
4443 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004444 kvm_cpu_vmxon(phys_addr);
David Hildenbrandfdf288b2017-08-24 20:51:29 +02004445 if (enable_ept)
4446 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02004447
4448 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004449}
4450
Nadav Har'Eld462b812011-05-24 15:26:10 +03004451static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03004452{
4453 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03004454 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03004455
Nadav Har'Eld462b812011-05-24 15:26:10 +03004456 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
4457 loaded_vmcss_on_cpu_link)
4458 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03004459}
4460
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004461
4462/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
4463 * tricks.
4464 */
4465static void kvm_cpu_vmxoff(void)
4466{
4467 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03004468
4469 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004470 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02004471}
4472
Radim Krčmář13a34e02014-08-28 15:13:03 +02004473static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004474{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01004475 vmclear_local_loaded_vmcss();
4476 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477}
4478
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004479static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04004480 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004481{
4482 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004483 u32 ctl = ctl_min | ctl_opt;
4484
4485 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4486
4487 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
4488 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
4489
4490 /* Ensure minimum (required) set of control bits are supported. */
4491 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004492 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004493
4494 *result = ctl;
4495 return 0;
4496}
4497
Avi Kivity110312c2010-12-21 12:54:20 +02004498static __init bool allow_1_setting(u32 msr, u32 ctl)
4499{
4500 u32 vmx_msr_low, vmx_msr_high;
4501
4502 rdmsr(msr, vmx_msr_low, vmx_msr_high);
4503 return vmx_msr_high & ctl;
4504}
4505
Yang, Sheng002c7f72007-07-31 14:23:01 +03004506static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004507{
4508 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08004509 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004510 u32 _pin_based_exec_control = 0;
4511 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004512 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004513 u32 _vmexit_control = 0;
4514 u32 _vmentry_control = 0;
4515
Paolo Bonzini13893092018-02-26 13:40:09 +01004516 memset(vmcs_conf, 0, sizeof(*vmcs_conf));
Raghavendra K T10166742012-02-07 23:19:20 +05304517 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004518#ifdef CONFIG_X86_64
4519 CPU_BASED_CR8_LOAD_EXITING |
4520 CPU_BASED_CR8_STORE_EXITING |
4521#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08004522 CPU_BASED_CR3_LOAD_EXITING |
4523 CPU_BASED_CR3_STORE_EXITING |
Quan Xu8eb73e2d2017-12-12 16:44:21 +08004524 CPU_BASED_UNCOND_IO_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004525 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03004526 CPU_BASED_USE_TSC_OFFSETING |
Wanpeng Li4d5422c2018-03-12 04:53:02 -07004527 CPU_BASED_MWAIT_EXITING |
4528 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02004529 CPU_BASED_INVLPG_EXITING |
4530 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06004531
Sheng Yangf78e0e22007-10-29 09:40:42 +08004532 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08004533 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08004534 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004535 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
4536 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004537 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004538#ifdef CONFIG_X86_64
4539 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4540 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
4541 ~CPU_BASED_CR8_STORE_EXITING;
4542#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08004543 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08004544 min2 = 0;
4545 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08004546 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08004547 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08004548 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004549 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004550 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004551 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Paolo Bonzini0367f202016-07-12 10:44:55 +02004552 SECONDARY_EXEC_DESC |
Mao, Junjiead756a12012-07-02 01:18:48 +00004553 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08004554 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004555 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03004556 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08004557 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08004558 SECONDARY_EXEC_XSAVES |
David Hildenbrand736fdf72017-08-24 20:51:37 +02004559 SECONDARY_EXEC_RDSEED_EXITING |
4560 SECONDARY_EXEC_RDRAND_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004561 SECONDARY_EXEC_ENABLE_PML |
Bandan Das2a499e42017-08-03 15:54:41 -04004562 SECONDARY_EXEC_TSC_SCALING |
Sean Christopherson0b665d32018-08-14 09:33:34 -07004563 SECONDARY_EXEC_ENABLE_VMFUNC |
4564 SECONDARY_EXEC_ENCLS_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08004565 if (adjust_vmx_controls(min2, opt2,
4566 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08004567 &_cpu_based_2nd_exec_control) < 0)
4568 return -EIO;
4569 }
4570#ifndef CONFIG_X86_64
4571 if (!(_cpu_based_2nd_exec_control &
4572 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
4573 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
4574#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08004575
4576 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
4577 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08004578 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08004579 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
4580 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08004581
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004582 rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
4583 &vmx_capability.ept, &vmx_capability.vpid);
4584
Sheng Yangd56f5462008-04-25 10:13:16 +08004585 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03004586 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
4587 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03004588 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
4589 CPU_BASED_CR3_STORE_EXITING |
4590 CPU_BASED_INVLPG_EXITING);
Wanpeng Li61f1dd92017-10-18 16:02:19 -07004591 } else if (vmx_capability.ept) {
4592 vmx_capability.ept = 0;
4593 pr_warn_once("EPT CAP should not exist if not support "
4594 "1-setting enable EPT VM-execution control\n");
4595 }
4596 if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
4597 vmx_capability.vpid) {
4598 vmx_capability.vpid = 0;
4599 pr_warn_once("VPID CAP should not exist if not support "
4600 "1-setting enable VPID VM-execution control\n");
Sheng Yangd56f5462008-04-25 10:13:16 +08004601 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004602
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004603 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004604#ifdef CONFIG_X86_64
4605 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
4606#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08004607 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004608 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004609 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
4610 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004611 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004612
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01004613 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
4614 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
4615 PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004616 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
4617 &_pin_based_exec_control) < 0)
4618 return -EIO;
4619
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02004620 if (cpu_has_broken_vmx_preemption_timer())
4621 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08004622 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02004623 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08004624 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
4625
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01004626 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00004627 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004628 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
4629 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004630 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004631
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004632 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004633
4634 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
4635 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004636 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004637
4638#ifdef CONFIG_X86_64
4639 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
4640 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03004641 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004642#endif
4643
4644 /* Require Write-Back (WB) memory type for VMCS accesses. */
4645 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004646 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004647
Yang, Sheng002c7f72007-07-31 14:23:01 +03004648 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02004649 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03004650 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004651
Liran Alon2307af12018-06-29 22:59:04 +03004652 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004653
Yang, Sheng002c7f72007-07-31 14:23:01 +03004654 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
4655 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004656 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004657 vmcs_conf->vmexit_ctrl = _vmexit_control;
4658 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004659
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01004660 if (static_branch_unlikely(&enable_evmcs))
4661 evmcs_sanitize_exec_ctrls(vmcs_conf);
4662
Avi Kivity110312c2010-12-21 12:54:20 +02004663 cpu_has_load_ia32_efer =
4664 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4665 VM_ENTRY_LOAD_IA32_EFER)
4666 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4667 VM_EXIT_LOAD_IA32_EFER);
4668
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004669 cpu_has_load_perf_global_ctrl =
4670 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
4671 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
4672 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
4673 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
4674
4675 /*
4676 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02004677 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02004678 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
4679 *
4680 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
4681 *
4682 * AAK155 (model 26)
4683 * AAP115 (model 30)
4684 * AAT100 (model 37)
4685 * BC86,AAY89,BD102 (model 44)
4686 * BA97 (model 46)
4687 *
4688 */
4689 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
4690 switch (boot_cpu_data.x86_model) {
4691 case 26:
4692 case 30:
4693 case 37:
4694 case 44:
4695 case 46:
4696 cpu_has_load_perf_global_ctrl = false;
4697 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
4698 "does not work properly. Using workaround\n");
4699 break;
4700 default:
4701 break;
4702 }
4703 }
4704
Borislav Petkov782511b2016-04-04 22:25:03 +02004705 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08004706 rdmsrl(MSR_IA32_XSS, host_xss);
4707
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004708 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08004709}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004710
Liran Alon491a6032018-06-23 02:35:12 +03004711static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004712{
4713 int node = cpu_to_node(cpu);
4714 struct page *pages;
4715 struct vmcs *vmcs;
4716
Vlastimil Babka96db8002015-09-08 15:03:50 -07004717 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718 if (!pages)
4719 return NULL;
4720 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004721 memset(vmcs, 0, vmcs_config.size);
Liran Alon2307af12018-06-29 22:59:04 +03004722
4723 /* KVM supports Enlightened VMCS v1 only */
4724 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004725 vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
Liran Alon2307af12018-06-29 22:59:04 +03004726 else
Liran Alon392b2f22018-06-23 02:35:01 +03004727 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004728
Liran Alon491a6032018-06-23 02:35:12 +03004729 if (shadow)
4730 vmcs->hdr.shadow_vmcs = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731 return vmcs;
4732}
4733
Avi Kivity6aa8b732006-12-10 02:21:36 -08004734static void free_vmcs(struct vmcs *vmcs)
4735{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004736 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004737}
4738
Nadav Har'Eld462b812011-05-24 15:26:10 +03004739/*
4740 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
4741 */
4742static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4743{
4744 if (!loaded_vmcs->vmcs)
4745 return;
4746 loaded_vmcs_clear(loaded_vmcs);
4747 free_vmcs(loaded_vmcs->vmcs);
4748 loaded_vmcs->vmcs = NULL;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004749 if (loaded_vmcs->msr_bitmap)
4750 free_page((unsigned long)loaded_vmcs->msr_bitmap);
Jim Mattson355f4fb2016-10-28 08:29:39 -07004751 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004752}
4753
Liran Alon491a6032018-06-23 02:35:12 +03004754static struct vmcs *alloc_vmcs(bool shadow)
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004755{
Liran Alon491a6032018-06-23 02:35:12 +03004756 return alloc_vmcs_cpu(shadow, raw_smp_processor_id());
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004757}
4758
4759static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
4760{
Liran Alon491a6032018-06-23 02:35:12 +03004761 loaded_vmcs->vmcs = alloc_vmcs(false);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004762 if (!loaded_vmcs->vmcs)
4763 return -ENOMEM;
4764
4765 loaded_vmcs->shadow_vmcs = NULL;
4766 loaded_vmcs_init(loaded_vmcs);
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004767
4768 if (cpu_has_vmx_msr_bitmap()) {
4769 loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
4770 if (!loaded_vmcs->msr_bitmap)
4771 goto out_vmcs;
4772 memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004773
Arnd Bergmann1f008e12018-05-25 17:36:17 +02004774 if (IS_ENABLED(CONFIG_HYPERV) &&
4775 static_branch_unlikely(&enable_evmcs) &&
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02004776 (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
4777 struct hv_enlightened_vmcs *evmcs =
4778 (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
4779
4780 evmcs->hv_enlightenments_control.msr_bitmap = 1;
4781 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004782 }
Sean Christophersond7ee0392018-07-23 12:32:47 -07004783
4784 memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
4785
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004786 return 0;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01004787
4788out_vmcs:
4789 free_loaded_vmcs(loaded_vmcs);
4790 return -ENOMEM;
Paolo Bonzinif21f1652018-01-11 12:16:15 +01004791}
4792
Sam Ravnborg39959582007-06-01 00:47:13 -07004793static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004794{
4795 int cpu;
4796
Zachary Amsden3230bb42009-09-29 11:38:37 -10004797 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10004799 per_cpu(vmxarea, cpu) = NULL;
4800 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004801}
4802
Jim Mattsond37f4262017-12-22 12:12:16 -08004803enum vmcs_field_width {
4804 VMCS_FIELD_WIDTH_U16 = 0,
4805 VMCS_FIELD_WIDTH_U64 = 1,
4806 VMCS_FIELD_WIDTH_U32 = 2,
4807 VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3
Jim Mattson85fd5142017-07-07 12:51:41 -07004808};
4809
Jim Mattsond37f4262017-12-22 12:12:16 -08004810static inline int vmcs_field_width(unsigned long field)
Jim Mattson85fd5142017-07-07 12:51:41 -07004811{
4812 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
Jim Mattsond37f4262017-12-22 12:12:16 -08004813 return VMCS_FIELD_WIDTH_U32;
Jim Mattson85fd5142017-07-07 12:51:41 -07004814 return (field >> 13) & 0x3 ;
4815}
4816
4817static inline int vmcs_field_readonly(unsigned long field)
4818{
4819 return (((field >> 10) & 0x3) == 1);
4820}
4821
Bandan Dasfe2b2012014-04-21 15:20:14 -04004822static void init_vmcs_shadow_fields(void)
4823{
4824 int i, j;
4825
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004826 for (i = j = 0; i < max_shadow_read_only_fields; i++) {
4827 u16 field = shadow_read_only_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004828 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004829 (i + 1 == max_shadow_read_only_fields ||
4830 shadow_read_only_fields[i + 1] != field + 1))
4831 pr_err("Missing field from shadow_read_only_field %x\n",
4832 field + 1);
4833
4834 clear_bit(field, vmx_vmread_bitmap);
4835#ifdef CONFIG_X86_64
4836 if (field & 1)
4837 continue;
4838#endif
4839 if (j < i)
4840 shadow_read_only_fields[j] = field;
4841 j++;
4842 }
4843 max_shadow_read_only_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004844
4845 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004846 u16 field = shadow_read_write_fields[i];
Jim Mattsond37f4262017-12-22 12:12:16 -08004847 if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 &&
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004848 (i + 1 == max_shadow_read_write_fields ||
4849 shadow_read_write_fields[i + 1] != field + 1))
4850 pr_err("Missing field from shadow_read_write_field %x\n",
4851 field + 1);
4852
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004853 /*
4854 * PML and the preemption timer can be emulated, but the
4855 * processor cannot vmwrite to fields that don't exist
4856 * on bare metal.
4857 */
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004858 switch (field) {
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01004859 case GUEST_PML_INDEX:
4860 if (!cpu_has_vmx_pml())
4861 continue;
4862 break;
4863 case VMX_PREEMPTION_TIMER_VALUE:
4864 if (!cpu_has_vmx_preemption_timer())
4865 continue;
4866 break;
4867 case GUEST_INTR_STATUS:
4868 if (!cpu_has_vmx_apicv())
Bandan Dasfe2b2012014-04-21 15:20:14 -04004869 continue;
4870 break;
4871 default:
4872 break;
4873 }
4874
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004875 clear_bit(field, vmx_vmwrite_bitmap);
4876 clear_bit(field, vmx_vmread_bitmap);
4877#ifdef CONFIG_X86_64
4878 if (field & 1)
4879 continue;
4880#endif
Bandan Dasfe2b2012014-04-21 15:20:14 -04004881 if (j < i)
Paolo Bonzini44900ba2017-12-13 12:58:02 +01004882 shadow_read_write_fields[j] = field;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004883 j++;
4884 }
4885 max_shadow_read_write_fields = j;
Bandan Dasfe2b2012014-04-21 15:20:14 -04004886}
4887
Avi Kivity6aa8b732006-12-10 02:21:36 -08004888static __init int alloc_kvm_area(void)
4889{
4890 int cpu;
4891
Zachary Amsden3230bb42009-09-29 11:38:37 -10004892 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004893 struct vmcs *vmcs;
4894
Liran Alon491a6032018-06-23 02:35:12 +03004895 vmcs = alloc_vmcs_cpu(false, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004896 if (!vmcs) {
4897 free_kvm_area();
4898 return -ENOMEM;
4899 }
4900
Liran Alon2307af12018-06-29 22:59:04 +03004901 /*
4902 * When eVMCS is enabled, alloc_vmcs_cpu() sets
4903 * vmcs->revision_id to KVM_EVMCS_VERSION instead of
4904 * revision_id reported by MSR_IA32_VMX_BASIC.
4905 *
4906 * However, even though not explictly documented by
4907 * TLFS, VMXArea passed as VMXON argument should
4908 * still be marked with revision_id reported by
4909 * physical CPU.
4910 */
4911 if (static_branch_unlikely(&enable_evmcs))
Liran Alon392b2f22018-06-23 02:35:01 +03004912 vmcs->hdr.revision_id = vmcs_config.revision_id;
Liran Alon2307af12018-06-29 22:59:04 +03004913
Avi Kivity6aa8b732006-12-10 02:21:36 -08004914 per_cpu(vmxarea, cpu) = vmcs;
4915 }
4916 return 0;
4917}
4918
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004919static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02004920 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921{
Gleb Natapovd99e4152012-12-20 16:57:45 +02004922 if (!emulate_invalid_guest_state) {
4923 /*
4924 * CS and SS RPL should be equal during guest entry according
4925 * to VMX spec, but in reality it is not always so. Since vcpu
4926 * is in the middle of the transition from real mode to
4927 * protected mode it is safe to assume that RPL 0 is a good
4928 * default value.
4929 */
4930 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03004931 save->selector &= ~SEGMENT_RPL_MASK;
4932 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02004933 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02004935 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936}
4937
4938static void enter_pmode(struct kvm_vcpu *vcpu)
4939{
4940 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03004941 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942
Gleb Natapovd99e4152012-12-20 16:57:45 +02004943 /*
4944 * Update real mode segment cache. It may be not up-to-date if sement
4945 * register was written while vcpu was in a guest mode.
4946 */
4947 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
4948 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
4949 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
4950 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
4951 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
4952 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
4953
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004954 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004955
Avi Kivity2fb92db2011-04-27 19:42:18 +03004956 vmx_segment_cache_clear(vmx);
4957
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004958 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004959
4960 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03004961 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
4962 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963 vmcs_writel(GUEST_RFLAGS, flags);
4964
Rusty Russell66aee912007-07-17 23:34:16 +10004965 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
4966 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004967
4968 update_exception_bitmap(vcpu);
4969
Gleb Natapov91b0aa22013-01-21 15:36:47 +02004970 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
4971 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
4972 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
4973 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
4974 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
4975 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004976}
4977
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004978static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004979{
Mathias Krause772e0312012-08-30 01:30:19 +02004980 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02004981 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004982
Gleb Natapovd99e4152012-12-20 16:57:45 +02004983 var.dpl = 0x3;
4984 if (seg == VCPU_SREG_CS)
4985 var.type = 0x3;
4986
4987 if (!emulate_invalid_guest_state) {
4988 var.selector = var.base >> 4;
4989 var.base = var.base & 0xffff0;
4990 var.limit = 0xffff;
4991 var.g = 0;
4992 var.db = 0;
4993 var.present = 1;
4994 var.s = 1;
4995 var.l = 0;
4996 var.unusable = 0;
4997 var.type = 0x3;
4998 var.avl = 0;
4999 if (save->base & 0xf)
5000 printk_once(KERN_WARNING "kvm: segment base is not "
5001 "paragraph aligned when entering "
5002 "protected mode (seg=%d)", seg);
5003 }
5004
5005 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05005006 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005007 vmcs_write32(sf->limit, var.limit);
5008 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005009}
5010
5011static void enter_rmode(struct kvm_vcpu *vcpu)
5012{
5013 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005014 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005015 struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005017 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
5018 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
5019 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
5020 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
5021 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005022 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
5023 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005024
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005025 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005026
Gleb Natapov776e58e2011-03-13 12:34:27 +02005027 /*
5028 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01005029 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02005030 */
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005031 if (!kvm_vmx->tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02005032 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
5033 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02005034
Avi Kivity2fb92db2011-04-27 19:42:18 +03005035 vmx_segment_cache_clear(vmx);
5036
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005037 vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005038 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005039 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5040
5041 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03005042 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005043
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01005044 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005045
5046 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10005047 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048 update_exception_bitmap(vcpu);
5049
Gleb Natapovd99e4152012-12-20 16:57:45 +02005050 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
5051 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
5052 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
5053 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
5054 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
5055 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03005056
Eddie Dong8668a3c2007-10-10 14:26:45 +08005057 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005058}
5059
Amit Shah401d10d2009-02-20 22:53:37 +05305060static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
5061{
5062 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03005063 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
5064
5065 if (!msr)
5066 return;
Amit Shah401d10d2009-02-20 22:53:37 +05305067
Avi Kivityf6801df2010-01-21 15:31:50 +02005068 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05305069 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005070 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305071 msr->data = efer;
5072 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02005073 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05305074
5075 msr->data = efer & ~EFER_LME;
5076 }
5077 setup_msrs(vmx);
5078}
5079
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005080#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005081
5082static void enter_lmode(struct kvm_vcpu *vcpu)
5083{
5084 u32 guest_tr_ar;
5085
Avi Kivity2fb92db2011-04-27 19:42:18 +03005086 vmx_segment_cache_clear(to_vmx(vcpu));
5087
Avi Kivity6aa8b732006-12-10 02:21:36 -08005088 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005089 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005090 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
5091 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005092 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005093 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
5094 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 }
Avi Kivityda38f432010-07-06 11:30:49 +03005096 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005097}
5098
5099static void exit_lmode(struct kvm_vcpu *vcpu)
5100{
Gleb Natapov2961e8762013-11-25 15:37:13 +02005101 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03005102 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005103}
5104
5105#endif
5106
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005107static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid,
5108 bool invalidate_gpa)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005109{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005110 if (enable_ept && (invalidate_gpa || !enable_vpid)) {
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005111 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
5112 return;
Peter Feiner995f00a2017-06-30 17:26:32 -07005113 ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07005114 } else {
5115 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08005116 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08005117}
5118
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005119static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005120{
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08005121 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa);
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005122}
5123
Junaid Shahidfaff8752018-06-29 13:10:05 -07005124static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
5125{
5126 int vpid = to_vmx(vcpu)->vpid;
5127
5128 if (!vpid_sync_vcpu_addr(vpid, addr))
5129 vpid_sync_context(vpid);
5130
5131 /*
5132 * If VPIDs are not supported or enabled, then the above is a no-op.
5133 * But we don't really need a TLB flush in that case anyway, because
5134 * each VM entry/exit includes an implicit flush when VPID is 0.
5135 */
5136}
5137
Avi Kivitye8467fd2009-12-29 18:43:06 +02005138static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
5139{
5140 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
5141
5142 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
5143 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
5144}
5145
Avi Kivityaff48ba2010-12-05 18:56:11 +02005146static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
5147{
Sean Christophersonb4d18512018-03-05 12:04:40 -08005148 if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
Avi Kivityaff48ba2010-12-05 18:56:11 +02005149 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
5150 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
5151}
5152
Anthony Liguori25c4c272007-04-27 09:29:21 +03005153static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08005154{
Avi Kivityfc78f512009-12-07 12:16:48 +02005155 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
5156
5157 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
5158 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08005159}
5160
Sheng Yang14394422008-04-28 12:24:45 +08005161static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
5162{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005163 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5164
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005165 if (!test_bit(VCPU_EXREG_PDPTR,
5166 (unsigned long *)&vcpu->arch.regs_dirty))
5167 return;
5168
Sheng Yang14394422008-04-28 12:24:45 +08005169 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005170 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
5171 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
5172 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
5173 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08005174 }
5175}
5176
Avi Kivity8f5d5492009-05-31 18:41:29 +03005177static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
5178{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005179 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
5180
Avi Kivity8f5d5492009-05-31 18:41:29 +03005181 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03005182 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
5183 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
5184 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
5185 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005186 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005187
5188 __set_bit(VCPU_EXREG_PDPTR,
5189 (unsigned long *)&vcpu->arch.regs_avail);
5190 __set_bit(VCPU_EXREG_PDPTR,
5191 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03005192}
5193
David Matlack38991522016-11-29 18:14:08 -08005194static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5195{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005196 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5197 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005198 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5199
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005200 if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high &
David Matlack38991522016-11-29 18:14:08 -08005201 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5202 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5203 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
5204
5205 return fixed_bits_valid(val, fixed0, fixed1);
5206}
5207
5208static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
5209{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005210 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0;
5211 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005212
5213 return fixed_bits_valid(val, fixed0, fixed1);
5214}
5215
5216static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
5217{
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01005218 u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0;
5219 u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1;
David Matlack38991522016-11-29 18:14:08 -08005220
5221 return fixed_bits_valid(val, fixed0, fixed1);
5222}
5223
5224/* No difference in the restrictions on guest and host CR4 in VMX operation. */
5225#define nested_guest_cr4_valid nested_cr4_valid
5226#define nested_host_cr4_valid nested_cr4_valid
5227
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005228static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08005229
5230static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
5231 unsigned long cr0,
5232 struct kvm_vcpu *vcpu)
5233{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03005234 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
5235 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005236 if (!(cr0 & X86_CR0_PG)) {
5237 /* From paging/starting to nonpaging */
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 } else if (!is_paging(vcpu)) {
5245 /* From nonpaging to paging */
5246 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08005247 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08005248 ~(CPU_BASED_CR3_LOAD_EXITING |
5249 CPU_BASED_CR3_STORE_EXITING));
5250 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02005251 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08005252 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08005253
5254 if (!(cr0 & X86_CR0_WP))
5255 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08005256}
5257
Avi Kivity6aa8b732006-12-10 02:21:36 -08005258static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
5259{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005260 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005261 unsigned long hw_cr0;
5262
Sean Christopherson3de63472018-07-13 08:42:30 -07005263 hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005264 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02005265 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02005266 else {
Gleb Natapov50378782013-02-04 16:00:28 +02005267 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005268
Gleb Natapov218e7632013-01-21 15:36:45 +02005269 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
5270 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005271
Gleb Natapov218e7632013-01-21 15:36:45 +02005272 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
5273 enter_rmode(vcpu);
5274 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005276#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02005277 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10005278 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005279 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10005280 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08005281 exit_lmode(vcpu);
5282 }
5283#endif
5284
Sean Christophersonb4d18512018-03-05 12:04:40 -08005285 if (enable_ept && !enable_unrestricted_guest)
Sheng Yang14394422008-04-28 12:24:45 +08005286 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
5287
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08005289 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005290 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02005291
5292 /* depends on vcpu->arch.cr0 to be set to a new value */
5293 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005294}
5295
Yu Zhang855feb62017-08-24 20:27:55 +08005296static int get_ept_level(struct kvm_vcpu *vcpu)
5297{
5298 if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
5299 return 5;
5300 return 4;
5301}
5302
Peter Feiner995f00a2017-06-30 17:26:32 -07005303static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
Sheng Yang14394422008-04-28 12:24:45 +08005304{
Yu Zhang855feb62017-08-24 20:27:55 +08005305 u64 eptp = VMX_EPTP_MT_WB;
Sheng Yang14394422008-04-28 12:24:45 +08005306
Yu Zhang855feb62017-08-24 20:27:55 +08005307 eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
Sheng Yang14394422008-04-28 12:24:45 +08005308
Peter Feiner995f00a2017-06-30 17:26:32 -07005309 if (enable_ept_ad_bits &&
5310 (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
David Hildenbrandbb97a012017-08-10 23:15:28 +02005311 eptp |= VMX_EPTP_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08005312 eptp |= (root_hpa & PAGE_MASK);
5313
5314 return eptp;
5315}
5316
Avi Kivity6aa8b732006-12-10 02:21:36 -08005317static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
5318{
Tianyu Lan877ad952018-07-19 08:40:23 +00005319 struct kvm *kvm = vcpu->kvm;
Sheng Yang14394422008-04-28 12:24:45 +08005320 unsigned long guest_cr3;
5321 u64 eptp;
5322
5323 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02005324 if (enable_ept) {
Peter Feiner995f00a2017-06-30 17:26:32 -07005325 eptp = construct_eptp(vcpu, cr3);
Sheng Yang14394422008-04-28 12:24:45 +08005326 vmcs_write64(EPT_POINTER, eptp);
Tianyu Lan877ad952018-07-19 08:40:23 +00005327
5328 if (kvm_x86_ops->tlb_remote_flush) {
5329 spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5330 to_vmx(vcpu)->ept_pointer = eptp;
5331 to_kvm_vmx(kvm)->ept_pointers_match
5332 = EPT_POINTERS_CHECK;
5333 spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
5334 }
5335
Sean Christophersone90008d2018-03-05 12:04:37 -08005336 if (enable_unrestricted_guest || is_paging(vcpu) ||
5337 is_guest_mode(vcpu))
Jan Kiszka59ab5a82013-08-08 16:26:29 +02005338 guest_cr3 = kvm_read_cr3(vcpu);
5339 else
Tianyu Lan877ad952018-07-19 08:40:23 +00005340 guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02005341 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08005342 }
5343
Sheng Yang14394422008-04-28 12:24:45 +08005344 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005345}
5346
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005347static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005348{
Ben Serebrin085e68e2015-04-16 11:58:05 -07005349 /*
5350 * Pass through host's Machine Check Enable value to hw_cr4, which
5351 * is in force while we are in guest mode. Do not let guests control
5352 * this bit, even if host CR4.MCE == 0.
5353 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005354 unsigned long hw_cr4;
5355
5356 hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
5357 if (enable_unrestricted_guest)
5358 hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
5359 else if (to_vmx(vcpu)->rmode.vm86_active)
5360 hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
5361 else
5362 hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08005363
Sean Christopherson64f7a112018-04-30 10:01:06 -07005364 if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
5365 if (cr4 & X86_CR4_UMIP) {
5366 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini0367f202016-07-12 10:44:55 +02005367 SECONDARY_EXEC_DESC);
Sean Christopherson64f7a112018-04-30 10:01:06 -07005368 hw_cr4 &= ~X86_CR4_UMIP;
5369 } else if (!is_guest_mode(vcpu) ||
5370 !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC))
5371 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5372 SECONDARY_EXEC_DESC);
5373 }
Paolo Bonzini0367f202016-07-12 10:44:55 +02005374
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005375 if (cr4 & X86_CR4_VMXE) {
5376 /*
5377 * To use VMXON (and later other VMX instructions), a guest
5378 * must first be able to turn on cr4.VMXE (see handle_vmon()).
5379 * So basically the check on whether to allow nested VMX
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005380 * is here. We operate under the default treatment of SMM,
5381 * so VMX cannot be enabled under SMM.
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005382 */
Paolo Bonzini5bea5122018-09-18 15:19:17 +02005383 if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005384 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005385 }
David Matlack38991522016-11-29 18:14:08 -08005386
5387 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005388 return 1;
5389
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005390 vcpu->arch.cr4 = cr4;
Sheng Yang14394422008-04-28 12:24:45 +08005391
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005392 if (!enable_unrestricted_guest) {
5393 if (enable_ept) {
5394 if (!is_paging(vcpu)) {
5395 hw_cr4 &= ~X86_CR4_PAE;
5396 hw_cr4 |= X86_CR4_PSE;
5397 } else if (!(cr4 & X86_CR4_PAE)) {
5398 hw_cr4 &= ~X86_CR4_PAE;
5399 }
5400 }
5401
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005402 /*
Huaitong Handdba2622016-03-22 16:51:15 +08005403 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
5404 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
5405 * to be manually disabled when guest switches to non-paging
5406 * mode.
5407 *
5408 * If !enable_unrestricted_guest, the CPU is always running
5409 * with CR0.PG=1 and CR4 needs to be modified.
5410 * If enable_unrestricted_guest, the CPU automatically
5411 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005412 */
Sean Christopherson5dc1f042018-03-05 12:04:39 -08005413 if (!is_paging(vcpu))
5414 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
5415 }
Radim Krčmář656ec4a2015-11-02 22:20:00 +01005416
Sheng Yang14394422008-04-28 12:24:45 +08005417 vmcs_writel(CR4_READ_SHADOW, cr4);
5418 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03005419 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005420}
5421
Avi Kivity6aa8b732006-12-10 02:21:36 -08005422static void vmx_get_segment(struct kvm_vcpu *vcpu,
5423 struct kvm_segment *var, int seg)
5424{
Avi Kivitya9179492011-01-03 14:28:52 +02005425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005426 u32 ar;
5427
Gleb Natapovc6ad11532012-12-12 19:10:51 +02005428 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005429 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02005430 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03005431 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03005432 return;
Avi Kivity1390a282012-08-21 17:07:08 +03005433 var->base = vmx_read_guest_seg_base(vmx, seg);
5434 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5435 return;
Avi Kivitya9179492011-01-03 14:28:52 +02005436 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005437 var->base = vmx_read_guest_seg_base(vmx, seg);
5438 var->limit = vmx_read_guest_seg_limit(vmx, seg);
5439 var->selector = vmx_read_guest_seg_selector(vmx, seg);
5440 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03005441 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442 var->type = ar & 15;
5443 var->s = (ar >> 4) & 1;
5444 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03005445 /*
5446 * Some userspaces do not preserve unusable property. Since usable
5447 * segment has to be present according to VMX spec we can use present
5448 * property to amend userspace bug by making unusable segment always
5449 * nonpresent. vmx_segment_access_rights() already marks nonpresent
5450 * segment as unusable.
5451 */
5452 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005453 var->avl = (ar >> 12) & 1;
5454 var->l = (ar >> 13) & 1;
5455 var->db = (ar >> 14) & 1;
5456 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005457}
5458
Avi Kivitya9179492011-01-03 14:28:52 +02005459static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
5460{
Avi Kivitya9179492011-01-03 14:28:52 +02005461 struct kvm_segment s;
5462
5463 if (to_vmx(vcpu)->rmode.vm86_active) {
5464 vmx_get_segment(vcpu, &s, seg);
5465 return s.base;
5466 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03005467 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02005468}
5469
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005470static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02005471{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02005472 struct vcpu_vmx *vmx = to_vmx(vcpu);
5473
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005474 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02005475 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02005476 else {
5477 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005478 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02005479 }
Avi Kivity69c73022011-03-07 15:26:44 +02005480}
5481
Avi Kivity653e3102007-05-07 10:55:37 +03005482static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483{
Avi Kivity6aa8b732006-12-10 02:21:36 -08005484 u32 ar;
5485
Avi Kivityf0495f92012-06-07 17:06:10 +03005486 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005487 ar = 1 << 16;
5488 else {
5489 ar = var->type & 15;
5490 ar |= (var->s & 1) << 4;
5491 ar |= (var->dpl & 3) << 5;
5492 ar |= (var->present & 1) << 7;
5493 ar |= (var->avl & 1) << 12;
5494 ar |= (var->l & 1) << 13;
5495 ar |= (var->db & 1) << 14;
5496 ar |= (var->g & 1) << 15;
5497 }
Avi Kivity653e3102007-05-07 10:55:37 +03005498
5499 return ar;
5500}
5501
5502static void vmx_set_segment(struct kvm_vcpu *vcpu,
5503 struct kvm_segment *var, int seg)
5504{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005505 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02005506 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03005507
Avi Kivity2fb92db2011-04-27 19:42:18 +03005508 vmx_segment_cache_clear(vmx);
5509
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005510 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
5511 vmx->rmode.segs[seg] = *var;
5512 if (seg == VCPU_SREG_TR)
5513 vmcs_write16(sf->selector, var->selector);
5514 else if (var->s)
5515 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02005516 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03005517 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02005518
Avi Kivity653e3102007-05-07 10:55:37 +03005519 vmcs_writel(sf->base, var->base);
5520 vmcs_write32(sf->limit, var->limit);
5521 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005522
5523 /*
5524 * Fix the "Accessed" bit in AR field of segment registers for older
5525 * qemu binaries.
5526 * IA32 arch specifies that at the time of processor reset the
5527 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08005528 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005529 * state vmexit when "unrestricted guest" mode is turned on.
5530 * Fix for this setup issue in cpu_reset is being pushed in the qemu
5531 * tree. Newer qemu binaries with that qemu fix would not need this
5532 * kvm hack.
5533 */
5534 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02005535 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005536
Gleb Natapovf924d662012-12-12 19:10:55 +02005537 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02005538
5539out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005540 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005541}
5542
Avi Kivity6aa8b732006-12-10 02:21:36 -08005543static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
5544{
Avi Kivity2fb92db2011-04-27 19:42:18 +03005545 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546
5547 *db = (ar >> 14) & 1;
5548 *l = (ar >> 13) & 1;
5549}
5550
Gleb Natapov89a27f42010-02-16 10:51:48 +02005551static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005553 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
5554 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005555}
5556
Gleb Natapov89a27f42010-02-16 10:51:48 +02005557static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005559 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
5560 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005561}
5562
Gleb Natapov89a27f42010-02-16 10:51:48 +02005563static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005564{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005565 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
5566 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567}
5568
Gleb Natapov89a27f42010-02-16 10:51:48 +02005569static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570{
Gleb Natapov89a27f42010-02-16 10:51:48 +02005571 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
5572 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005573}
5574
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005575static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
5576{
5577 struct kvm_segment var;
5578 u32 ar;
5579
5580 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02005581 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02005582 if (seg == VCPU_SREG_CS)
5583 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005584 ar = vmx_segment_access_rights(&var);
5585
5586 if (var.base != (var.selector << 4))
5587 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02005588 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005589 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02005590 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005591 return false;
5592
5593 return true;
5594}
5595
5596static bool code_segment_valid(struct kvm_vcpu *vcpu)
5597{
5598 struct kvm_segment cs;
5599 unsigned int cs_rpl;
5600
5601 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005602 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005603
Avi Kivity1872a3f2009-01-04 23:26:52 +02005604 if (cs.unusable)
5605 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005606 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005607 return false;
5608 if (!cs.s)
5609 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005610 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005611 if (cs.dpl > cs_rpl)
5612 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005613 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005614 if (cs.dpl != cs_rpl)
5615 return false;
5616 }
5617 if (!cs.present)
5618 return false;
5619
5620 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
5621 return true;
5622}
5623
5624static bool stack_segment_valid(struct kvm_vcpu *vcpu)
5625{
5626 struct kvm_segment ss;
5627 unsigned int ss_rpl;
5628
5629 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03005630 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005631
Avi Kivity1872a3f2009-01-04 23:26:52 +02005632 if (ss.unusable)
5633 return true;
5634 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005635 return false;
5636 if (!ss.s)
5637 return false;
5638 if (ss.dpl != ss_rpl) /* DPL != RPL */
5639 return false;
5640 if (!ss.present)
5641 return false;
5642
5643 return true;
5644}
5645
5646static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
5647{
5648 struct kvm_segment var;
5649 unsigned int rpl;
5650
5651 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03005652 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005653
Avi Kivity1872a3f2009-01-04 23:26:52 +02005654 if (var.unusable)
5655 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005656 if (!var.s)
5657 return false;
5658 if (!var.present)
5659 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07005660 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005661 if (var.dpl < rpl) /* DPL < RPL */
5662 return false;
5663 }
5664
5665 /* TODO: Add other members to kvm_segment_field to allow checking for other access
5666 * rights flags
5667 */
5668 return true;
5669}
5670
5671static bool tr_valid(struct kvm_vcpu *vcpu)
5672{
5673 struct kvm_segment tr;
5674
5675 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
5676
Avi Kivity1872a3f2009-01-04 23:26:52 +02005677 if (tr.unusable)
5678 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03005679 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005680 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02005681 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005682 return false;
5683 if (!tr.present)
5684 return false;
5685
5686 return true;
5687}
5688
5689static bool ldtr_valid(struct kvm_vcpu *vcpu)
5690{
5691 struct kvm_segment ldtr;
5692
5693 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
5694
Avi Kivity1872a3f2009-01-04 23:26:52 +02005695 if (ldtr.unusable)
5696 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03005697 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005698 return false;
5699 if (ldtr.type != 2)
5700 return false;
5701 if (!ldtr.present)
5702 return false;
5703
5704 return true;
5705}
5706
5707static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
5708{
5709 struct kvm_segment cs, ss;
5710
5711 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5712 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
5713
Nadav Amitb32a9912015-03-29 16:33:04 +03005714 return ((cs.selector & SEGMENT_RPL_MASK) ==
5715 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005716}
5717
5718/*
5719 * Check if guest state is valid. Returns true if valid, false if
5720 * not.
5721 * We assume that registers are always usable
5722 */
5723static bool guest_state_valid(struct kvm_vcpu *vcpu)
5724{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02005725 if (enable_unrestricted_guest)
5726 return true;
5727
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005728 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03005729 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03005730 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
5731 return false;
5732 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
5733 return false;
5734 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
5735 return false;
5736 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
5737 return false;
5738 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
5739 return false;
5740 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
5741 return false;
5742 } else {
5743 /* protected mode guest state checks */
5744 if (!cs_ss_rpl_check(vcpu))
5745 return false;
5746 if (!code_segment_valid(vcpu))
5747 return false;
5748 if (!stack_segment_valid(vcpu))
5749 return false;
5750 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
5751 return false;
5752 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
5753 return false;
5754 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
5755 return false;
5756 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
5757 return false;
5758 if (!tr_valid(vcpu))
5759 return false;
5760 if (!ldtr_valid(vcpu))
5761 return false;
5762 }
5763 /* TODO:
5764 * - Add checks on RIP
5765 * - Add checks on RFLAGS
5766 */
5767
5768 return true;
5769}
5770
Jim Mattson5fa99cb2017-07-06 16:33:07 -07005771static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa)
5772{
5773 return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu));
5774}
5775
Mike Dayd77c26f2007-10-08 09:02:08 -04005776static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005777{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005778 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02005779 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005780 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005781
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005782 idx = srcu_read_lock(&kvm->srcu);
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005783 fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
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 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08005788 r = kvm_write_guest_page(kvm, fn++, &data,
5789 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02005790 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005791 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005792 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
5793 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005794 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005795 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
5796 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005797 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02005798 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005799 r = kvm_write_guest_page(kvm, fn, &data,
5800 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
5801 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05005802out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005803 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005804 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005805}
5806
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005807static int init_rmode_identity_map(struct kvm *kvm)
5808{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005809 struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08005810 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08005811 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005812 u32 tmp;
5813
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005814 /* Protect kvm_vmx->ept_identity_pagetable_done. */
Tang Chena255d472014-09-16 18:41:58 +08005815 mutex_lock(&kvm->slots_lock);
5816
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005817 if (likely(kvm_vmx->ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08005818 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08005819
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005820 if (!kvm_vmx->ept_identity_map_addr)
5821 kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
5822 identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08005823
David Hildenbrandd8a6e362017-08-24 20:51:34 +02005824 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005825 kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
Tang Chenf51770e2014-09-16 18:41:59 +08005826 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08005827 goto out2;
5828
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005829 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005830 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
5831 if (r < 0)
5832 goto out;
5833 /* Set up identity-mapping pagetable for EPT in real mode */
5834 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
5835 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
5836 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
5837 r = kvm_write_guest_page(kvm, identity_map_pfn,
5838 &tmp, i * sizeof(tmp), sizeof(tmp));
5839 if (r < 0)
5840 goto out;
5841 }
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07005842 kvm_vmx->ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08005843
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005844out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08005845 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08005846
5847out2:
5848 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08005849 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08005850}
5851
Avi Kivity6aa8b732006-12-10 02:21:36 -08005852static void seg_setup(int seg)
5853{
Mathias Krause772e0312012-08-30 01:30:19 +02005854 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005855 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005856
5857 vmcs_write16(sf->selector, 0);
5858 vmcs_writel(sf->base, 0);
5859 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02005860 ar = 0x93;
5861 if (seg == VCPU_SREG_CS)
5862 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07005863
5864 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005865}
5866
Sheng Yangf78e0e22007-10-29 09:40:42 +08005867static int alloc_apic_access_page(struct kvm *kvm)
5868{
Xiao Guangrong44841412012-09-07 14:14:20 +08005869 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005870 int r = 0;
5871
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005872 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08005873 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005874 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005875 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
5876 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005877 if (r)
5878 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02005879
Tang Chen73a6d942014-09-11 13:38:00 +08005880 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08005881 if (is_error_page(page)) {
5882 r = -EFAULT;
5883 goto out;
5884 }
5885
Tang Chenc24ae0d2014-09-24 15:57:58 +08005886 /*
5887 * Do not pin the page in memory, so that memory hot-unplug
5888 * is able to migrate it.
5889 */
5890 put_page(page);
5891 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005892out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02005893 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08005894 return r;
5895}
5896
Wanpeng Li991e7a02015-09-16 17:30:05 +08005897static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005898{
5899 int vpid;
5900
Avi Kivity919818a2009-03-23 18:01:29 +02005901 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08005902 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005903 spin_lock(&vmx_vpid_lock);
5904 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005905 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08005906 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005907 else
5908 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005909 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005910 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08005911}
5912
Wanpeng Li991e7a02015-09-16 17:30:05 +08005913static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005914{
Wanpeng Li991e7a02015-09-16 17:30:05 +08005915 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005916 return;
5917 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08005918 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08005919 spin_unlock(&vmx_vpid_lock);
5920}
5921
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005922static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
5923 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08005924{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005925 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08005926
5927 if (!cpu_has_vmx_msr_bitmap())
5928 return;
5929
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005930 if (static_branch_unlikely(&enable_evmcs))
5931 evmcs_touch_msr_bitmap();
5932
Sheng Yang25c5f222008-03-28 13:18:56 +08005933 /*
5934 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5935 * have the write-low and read-high bitmap offsets the wrong way round.
5936 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5937 */
Sheng Yang25c5f222008-03-28 13:18:56 +08005938 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08005939 if (type & MSR_TYPE_R)
5940 /* read-low */
5941 __clear_bit(msr, msr_bitmap + 0x000 / f);
5942
5943 if (type & MSR_TYPE_W)
5944 /* write-low */
5945 __clear_bit(msr, msr_bitmap + 0x800 / f);
5946
Sheng Yang25c5f222008-03-28 13:18:56 +08005947 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5948 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08005949 if (type & MSR_TYPE_R)
5950 /* read-high */
5951 __clear_bit(msr, msr_bitmap + 0x400 / f);
5952
5953 if (type & MSR_TYPE_W)
5954 /* write-high */
5955 __clear_bit(msr, msr_bitmap + 0xc00 / f);
5956
5957 }
5958}
5959
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005960static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
5961 u32 msr, int type)
5962{
5963 int f = sizeof(unsigned long);
5964
5965 if (!cpu_has_vmx_msr_bitmap())
5966 return;
5967
Vitaly Kuznetsovceef7d12018-04-16 12:50:33 +02005968 if (static_branch_unlikely(&enable_evmcs))
5969 evmcs_touch_msr_bitmap();
5970
Paolo Bonzini904e14f2018-01-16 16:51:18 +01005971 /*
5972 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
5973 * have the write-low and read-high bitmap offsets the wrong way round.
5974 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
5975 */
5976 if (msr <= 0x1fff) {
5977 if (type & MSR_TYPE_R)
5978 /* read-low */
5979 __set_bit(msr, msr_bitmap + 0x000 / f);
5980
5981 if (type & MSR_TYPE_W)
5982 /* write-low */
5983 __set_bit(msr, msr_bitmap + 0x800 / f);
5984
5985 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
5986 msr &= 0x1fff;
5987 if (type & MSR_TYPE_R)
5988 /* read-high */
5989 __set_bit(msr, msr_bitmap + 0x400 / f);
5990
5991 if (type & MSR_TYPE_W)
5992 /* write-high */
5993 __set_bit(msr, msr_bitmap + 0xc00 / f);
5994
5995 }
5996}
5997
5998static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
5999 u32 msr, int type, bool value)
6000{
6001 if (value)
6002 vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
6003 else
6004 vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
6005}
6006
Wincy Vanf2b93282015-02-03 23:56:03 +08006007/*
6008 * If a msr is allowed by L0, we should check whether it is allowed by L1.
6009 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
6010 */
6011static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
6012 unsigned long *msr_bitmap_nested,
6013 u32 msr, int type)
6014{
6015 int f = sizeof(unsigned long);
6016
Wincy Vanf2b93282015-02-03 23:56:03 +08006017 /*
6018 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
6019 * have the write-low and read-high bitmap offsets the wrong way round.
6020 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
6021 */
6022 if (msr <= 0x1fff) {
6023 if (type & MSR_TYPE_R &&
6024 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
6025 /* read-low */
6026 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
6027
6028 if (type & MSR_TYPE_W &&
6029 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
6030 /* write-low */
6031 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
6032
6033 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
6034 msr &= 0x1fff;
6035 if (type & MSR_TYPE_R &&
6036 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
6037 /* read-high */
6038 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
6039
6040 if (type & MSR_TYPE_W &&
6041 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
6042 /* write-high */
6043 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
6044
6045 }
6046}
6047
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006048static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
Avi Kivity58972972009-02-24 22:26:47 +02006049{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006050 u8 mode = 0;
6051
6052 if (cpu_has_secondary_exec_ctrls() &&
6053 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
6054 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
6055 mode |= MSR_BITMAP_MODE_X2APIC;
6056 if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
6057 mode |= MSR_BITMAP_MODE_X2APIC_APICV;
6058 }
6059
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006060 return mode;
Yang Zhang8d146952013-01-25 10:18:50 +08006061}
6062
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006063#define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
6064
6065static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
6066 u8 mode)
Yang Zhang8d146952013-01-25 10:18:50 +08006067{
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006068 int msr;
6069
6070 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
6071 unsigned word = msr / BITS_PER_LONG;
6072 msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
6073 msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006074 }
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006075
6076 if (mode & MSR_BITMAP_MODE_X2APIC) {
6077 /*
6078 * TPR reads and writes can be virtualized even if virtual interrupt
6079 * delivery is not in use.
6080 */
6081 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
6082 if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
6083 vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
6084 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
6085 vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
6086 }
6087 }
6088}
6089
6090static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
6091{
6092 struct vcpu_vmx *vmx = to_vmx(vcpu);
6093 unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
6094 u8 mode = vmx_msr_bitmap_mode(vcpu);
6095 u8 changed = mode ^ vmx->msr_bitmap_mode;
6096
6097 if (!changed)
6098 return;
6099
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006100 if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
6101 vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
6102
6103 vmx->msr_bitmap_mode = mode;
Avi Kivity58972972009-02-24 22:26:47 +02006104}
6105
Suravee Suthikulpanitb2a05fe2017-09-12 10:42:41 -05006106static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006107{
Andrey Smetanind62caab2015-11-10 15:36:33 +03006108 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02006109}
6110
David Matlackc9f04402017-08-01 14:00:40 -07006111static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu)
6112{
6113 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6114 gfn_t gfn;
6115
6116 /*
6117 * Don't need to mark the APIC access page dirty; it is never
6118 * written to by the CPU during APIC virtualization.
6119 */
6120
6121 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
6122 gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT;
6123 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6124 }
6125
6126 if (nested_cpu_has_posted_intr(vmcs12)) {
6127 gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT;
6128 kvm_vcpu_mark_page_dirty(vcpu, gfn);
6129 }
6130}
6131
6132
David Hildenbrand6342c502017-01-25 11:58:58 +01006133static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08006134{
6135 struct vcpu_vmx *vmx = to_vmx(vcpu);
6136 int max_irr;
6137 void *vapic_page;
6138 u16 status;
6139
David Matlackc9f04402017-08-01 14:00:40 -07006140 if (!vmx->nested.pi_desc || !vmx->nested.pi_pending)
6141 return;
Wincy Van705699a2015-02-03 23:58:17 +08006142
David Matlackc9f04402017-08-01 14:00:40 -07006143 vmx->nested.pi_pending = false;
6144 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
6145 return;
Wincy Van705699a2015-02-03 23:58:17 +08006146
David Matlackc9f04402017-08-01 14:00:40 -07006147 max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256);
6148 if (max_irr != 256) {
Wincy Van705699a2015-02-03 23:58:17 +08006149 vapic_page = kmap(vmx->nested.virtual_apic_page);
Liran Alone7387b02017-12-24 18:12:54 +02006150 __kvm_apic_update_irr(vmx->nested.pi_desc->pir,
6151 vapic_page, &max_irr);
Wincy Van705699a2015-02-03 23:58:17 +08006152 kunmap(vmx->nested.virtual_apic_page);
6153
6154 status = vmcs_read16(GUEST_INTR_STATUS);
6155 if ((u8)max_irr > ((u8)status & 0xff)) {
6156 status &= ~0xff;
6157 status |= (u8)max_irr;
6158 vmcs_write16(GUEST_INTR_STATUS, status);
6159 }
6160 }
David Matlackc9f04402017-08-01 14:00:40 -07006161
6162 nested_mark_vmcs12_pages_dirty(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006163}
6164
Paolo Bonzini7e712682018-10-03 13:44:26 +02006165static u8 vmx_get_rvi(void)
6166{
6167 return vmcs_read16(GUEST_INTR_STATUS) & 0xff;
6168}
6169
Liran Alone6c67d82018-09-04 10:56:52 +03006170static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
6171{
6172 struct vcpu_vmx *vmx = to_vmx(vcpu);
6173 void *vapic_page;
6174 u32 vppr;
6175 int rvi;
6176
6177 if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
6178 !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
6179 WARN_ON_ONCE(!vmx->nested.virtual_apic_page))
6180 return false;
6181
Paolo Bonzini7e712682018-10-03 13:44:26 +02006182 rvi = vmx_get_rvi();
Liran Alone6c67d82018-09-04 10:56:52 +03006183
6184 vapic_page = kmap(vmx->nested.virtual_apic_page);
6185 vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
6186 kunmap(vmx->nested.virtual_apic_page);
6187
6188 return ((rvi & 0xf0) > (vppr & 0xf0));
6189}
6190
Wincy Van06a55242017-04-28 13:13:59 +08006191static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
6192 bool nested)
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006193{
6194#ifdef CONFIG_SMP
Wincy Van06a55242017-04-28 13:13:59 +08006195 int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
6196
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006197 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08006198 /*
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006199 * The vector of interrupt to be delivered to vcpu had
6200 * been set in PIR before this function.
Feng Wu28b835d2015-09-18 22:29:54 +08006201 *
Haozhong Zhang5753743f2017-09-18 09:56:50 +08006202 * Following cases will be reached in this block, and
6203 * we always send a notification event in all cases as
6204 * explained below.
6205 *
6206 * Case 1: vcpu keeps in non-root mode. Sending a
6207 * notification event posts the interrupt to vcpu.
6208 *
6209 * Case 2: vcpu exits to root mode and is still
6210 * runnable. PIR will be synced to vIRR before the
6211 * next vcpu entry. Sending a notification event in
6212 * this case has no effect, as vcpu is not in root
6213 * mode.
6214 *
6215 * Case 3: vcpu exits to root mode and is blocked.
6216 * vcpu_block() has already synced PIR to vIRR and
6217 * never blocks vcpu if vIRR is not cleared. Therefore,
6218 * a blocked vcpu here does not wait for any requested
6219 * interrupts in PIR, and sending a notification event
6220 * which has no effect is safe here.
Feng Wu28b835d2015-09-18 22:29:54 +08006221 */
Feng Wu28b835d2015-09-18 22:29:54 +08006222
Wincy Van06a55242017-04-28 13:13:59 +08006223 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01006224 return true;
6225 }
6226#endif
6227 return false;
6228}
6229
Wincy Van705699a2015-02-03 23:58:17 +08006230static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
6231 int vector)
6232{
6233 struct vcpu_vmx *vmx = to_vmx(vcpu);
6234
6235 if (is_guest_mode(vcpu) &&
6236 vector == vmx->nested.posted_intr_nv) {
Wincy Van705699a2015-02-03 23:58:17 +08006237 /*
6238 * If a posted intr is not recognized by hardware,
6239 * we will accomplish it in the next vmentry.
6240 */
6241 vmx->nested.pi_pending = true;
6242 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alon6b697712017-11-09 20:27:20 +02006243 /* the PIR and ON have been set by L1. */
6244 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
6245 kvm_vcpu_kick(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08006246 return 0;
6247 }
6248 return -1;
6249}
Avi Kivity6aa8b732006-12-10 02:21:36 -08006250/*
Yang Zhanga20ed542013-04-11 19:25:15 +08006251 * Send interrupt to vcpu via posted interrupt way.
6252 * 1. If target vcpu is running(non-root mode), send posted interrupt
6253 * notification to vcpu and hardware will sync PIR to vIRR atomically.
6254 * 2. If target vcpu isn't running(root mode), kick it to pick up the
6255 * interrupt from PIR in next vmentry.
6256 */
6257static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
6258{
6259 struct vcpu_vmx *vmx = to_vmx(vcpu);
6260 int r;
6261
Wincy Van705699a2015-02-03 23:58:17 +08006262 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
6263 if (!r)
6264 return;
6265
Yang Zhanga20ed542013-04-11 19:25:15 +08006266 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
6267 return;
6268
Paolo Bonzinib95234c2016-12-19 13:57:33 +01006269 /* If a previous notification has sent the IPI, nothing to do. */
6270 if (pi_test_and_set_on(&vmx->pi_desc))
6271 return;
6272
Wincy Van06a55242017-04-28 13:13:59 +08006273 if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
Yang Zhanga20ed542013-04-11 19:25:15 +08006274 kvm_vcpu_kick(vcpu);
6275}
6276
Avi Kivity6aa8b732006-12-10 02:21:36 -08006277/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006278 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
6279 * will not change in the lifetime of the guest.
6280 * Note that host-state that does change is set elsewhere. E.g., host-state
6281 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
6282 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006283static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006284{
6285 u32 low32, high32;
6286 unsigned long tmpl;
6287 struct desc_ptr dt;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006288 unsigned long cr0, cr3, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006289
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07006290 cr0 = read_cr0();
6291 WARN_ON(cr0 & X86_CR0_TS);
6292 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006293
6294 /*
6295 * Save the most likely value for this task's CR3 in the VMCS.
6296 * We can't use __get_current_cr3_fast() because we're not atomic.
6297 */
Andy Lutomirski6c690ee2017-06-12 10:26:14 -07006298 cr3 = __read_cr3();
Andy Lutomirskid6e41f12017-05-28 10:00:17 -07006299 vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006300 vmx->loaded_vmcs->host_state.cr3 = cr3;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006301
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006302 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07006303 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006304 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
Sean Christophersond7ee0392018-07-23 12:32:47 -07006305 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07006306
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006307 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006308#ifdef CONFIG_X86_64
6309 /*
6310 * Load null selectors, so we can avoid reloading them in
Sean Christopherson6d6095b2018-07-23 12:32:44 -07006311 * vmx_prepare_switch_to_host(), in case userspace uses
6312 * the null selectors too (the expected case).
Avi Kivityb2da15a2012-05-13 19:53:24 +03006313 */
6314 vmcs_write16(HOST_DS_SELECTOR, 0);
6315 vmcs_write16(HOST_ES_SELECTOR, 0);
6316#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006317 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6318 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03006319#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006320 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
6321 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
6322
Juergen Gross87930012017-09-04 12:25:27 +02006323 store_idt(&dt);
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006324 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006325 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006326
Avi Kivity83287ea422012-09-16 15:10:57 +03006327 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006328
6329 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
6330 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
6331 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
6332 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
6333
6334 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
6335 rdmsr(MSR_IA32_CR_PAT, low32, high32);
6336 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
6337 }
6338}
6339
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006340static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
6341{
6342 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
6343 if (enable_ept)
6344 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006345 if (is_guest_mode(&vmx->vcpu))
6346 vmx->vcpu.arch.cr4_guest_owned_bits &=
6347 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006348 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
6349}
6350
Yang Zhang01e439b2013-04-11 19:25:12 +08006351static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
6352{
6353 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
6354
Andrey Smetanind62caab2015-11-10 15:36:33 +03006355 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08006356 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006357
6358 if (!enable_vnmi)
6359 pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
6360
Yunhong Jiang64672c92016-06-13 14:19:59 -07006361 /* Enable the preemption timer dynamically */
6362 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08006363 return pin_based_exec_ctrl;
6364}
6365
Andrey Smetanind62caab2015-11-10 15:36:33 +03006366static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
6367{
6368 struct vcpu_vmx *vmx = to_vmx(vcpu);
6369
6370 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03006371 if (cpu_has_secondary_exec_ctrls()) {
6372 if (kvm_vcpu_apicv_active(vcpu))
6373 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
6374 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6375 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6376 else
6377 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6378 SECONDARY_EXEC_APIC_REGISTER_VIRT |
6379 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
6380 }
6381
6382 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006383 vmx_update_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03006384}
6385
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006386static u32 vmx_exec_control(struct vcpu_vmx *vmx)
6387{
6388 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01006389
6390 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
6391 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
6392
Paolo Bonzini35754c92015-07-29 12:05:37 +02006393 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006394 exec_control &= ~CPU_BASED_TPR_SHADOW;
6395#ifdef CONFIG_X86_64
6396 exec_control |= CPU_BASED_CR8_STORE_EXITING |
6397 CPU_BASED_CR8_LOAD_EXITING;
6398#endif
6399 }
6400 if (!enable_ept)
6401 exec_control |= CPU_BASED_CR3_STORE_EXITING |
6402 CPU_BASED_CR3_LOAD_EXITING |
6403 CPU_BASED_INVLPG_EXITING;
Wanpeng Li4d5422c2018-03-12 04:53:02 -07006404 if (kvm_mwait_in_guest(vmx->vcpu.kvm))
6405 exec_control &= ~(CPU_BASED_MWAIT_EXITING |
6406 CPU_BASED_MONITOR_EXITING);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006407 if (kvm_hlt_in_guest(vmx->vcpu.kvm))
6408 exec_control &= ~CPU_BASED_HLT_EXITING;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006409 return exec_control;
6410}
6411
Jim Mattson45ec3682017-08-23 16:32:04 -07006412static bool vmx_rdrand_supported(void)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006413{
Jim Mattson45ec3682017-08-23 16:32:04 -07006414 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006415 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006416}
6417
Jim Mattson75f4fc82017-08-23 16:32:03 -07006418static bool vmx_rdseed_supported(void)
6419{
6420 return vmcs_config.cpu_based_2nd_exec_ctrl &
David Hildenbrand736fdf72017-08-24 20:51:37 +02006421 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006422}
6423
Paolo Bonzini80154d72017-08-24 13:55:35 +02006424static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006425{
Paolo Bonzini80154d72017-08-24 13:55:35 +02006426 struct kvm_vcpu *vcpu = &vmx->vcpu;
6427
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006428 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006429
Paolo Bonzini80154d72017-08-24 13:55:35 +02006430 if (!cpu_need_virtualize_apic_accesses(vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006431 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6432 if (vmx->vpid == 0)
6433 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
6434 if (!enable_ept) {
6435 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
6436 enable_unrestricted_guest = 0;
6437 }
6438 if (!enable_unrestricted_guest)
6439 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Wanpeng Lib31c1142018-03-12 04:53:04 -07006440 if (kvm_pause_in_guest(vmx->vcpu.kvm))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006441 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini80154d72017-08-24 13:55:35 +02006442 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08006443 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
6444 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08006445 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02006446
6447 /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
6448 * in vmx_set_cr4. */
6449 exec_control &= ~SECONDARY_EXEC_DESC;
6450
Abel Gordonabc4fc52013-04-18 14:35:25 +03006451 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
6452 (handle_vmptrld).
6453 We can NOT enable shadow_vmcs here because we don't have yet
6454 a current VMCS12
6455 */
6456 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08006457
6458 if (!enable_pml)
6459 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08006460
Paolo Bonzini3db13482017-08-24 14:48:03 +02006461 if (vmx_xsaves_supported()) {
6462 /* Exposing XSAVES only when XSAVE is exposed */
6463 bool xsaves_enabled =
6464 guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
6465 guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
6466
6467 if (!xsaves_enabled)
6468 exec_control &= ~SECONDARY_EXEC_XSAVES;
6469
6470 if (nested) {
6471 if (xsaves_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006472 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006473 SECONDARY_EXEC_XSAVES;
6474 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006475 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini3db13482017-08-24 14:48:03 +02006476 ~SECONDARY_EXEC_XSAVES;
6477 }
6478 }
6479
Paolo Bonzini80154d72017-08-24 13:55:35 +02006480 if (vmx_rdtscp_supported()) {
6481 bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
6482 if (!rdtscp_enabled)
6483 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6484
6485 if (nested) {
6486 if (rdtscp_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006487 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006488 SECONDARY_EXEC_RDTSCP;
6489 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006490 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006491 ~SECONDARY_EXEC_RDTSCP;
6492 }
6493 }
6494
6495 if (vmx_invpcid_supported()) {
6496 /* Exposing INVPCID only when PCID is exposed */
6497 bool invpcid_enabled =
6498 guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
6499 guest_cpuid_has(vcpu, X86_FEATURE_PCID);
6500
6501 if (!invpcid_enabled) {
6502 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6503 guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
6504 }
6505
6506 if (nested) {
6507 if (invpcid_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006508 vmx->nested.msrs.secondary_ctls_high |=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006509 SECONDARY_EXEC_ENABLE_INVPCID;
6510 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006511 vmx->nested.msrs.secondary_ctls_high &=
Paolo Bonzini80154d72017-08-24 13:55:35 +02006512 ~SECONDARY_EXEC_ENABLE_INVPCID;
6513 }
6514 }
6515
Jim Mattson45ec3682017-08-23 16:32:04 -07006516 if (vmx_rdrand_supported()) {
6517 bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
6518 if (rdrand_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006519 exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006520
6521 if (nested) {
6522 if (rdrand_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006523 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006524 SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006525 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006526 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006527 ~SECONDARY_EXEC_RDRAND_EXITING;
Jim Mattson45ec3682017-08-23 16:32:04 -07006528 }
6529 }
6530
Jim Mattson75f4fc82017-08-23 16:32:03 -07006531 if (vmx_rdseed_supported()) {
6532 bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
6533 if (rdseed_enabled)
David Hildenbrand736fdf72017-08-24 20:51:37 +02006534 exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006535
6536 if (nested) {
6537 if (rdseed_enabled)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006538 vmx->nested.msrs.secondary_ctls_high |=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006539 SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006540 else
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01006541 vmx->nested.msrs.secondary_ctls_high &=
David Hildenbrand736fdf72017-08-24 20:51:37 +02006542 ~SECONDARY_EXEC_RDSEED_EXITING;
Jim Mattson75f4fc82017-08-23 16:32:03 -07006543 }
6544 }
6545
Paolo Bonzini80154d72017-08-24 13:55:35 +02006546 vmx->secondary_exec_control = exec_control;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006547}
6548
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006549static void ept_set_mmio_spte_mask(void)
6550{
6551 /*
6552 * EPT Misconfigurations can be generated if the value of bits 2:0
6553 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006554 */
Peter Feinerdcdca5f2017-06-30 17:26:30 -07006555 kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
6556 VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006557}
6558
Wanpeng Lif53cd632014-12-02 19:14:58 +08006559#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03006560/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006561 * Sets up the vmcs for emulated real mode.
6562 */
David Hildenbrand12d79912017-08-24 20:51:26 +02006563static void vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006564{
Avi Kivity6aa8b732006-12-10 02:21:36 -08006565 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006566
Abel Gordon4607c2d2013-04-18 14:35:55 +03006567 if (enable_shadow_vmcs) {
Jim Mattsonf4160e42018-05-29 09:11:33 -07006568 /*
6569 * At vCPU creation, "VMWRITE to any supported field
6570 * in the VMCS" is supported, so use the more
6571 * permissive vmx_vmread_bitmap to specify both read
6572 * and write permissions for the shadow VMCS.
6573 */
Abel Gordon4607c2d2013-04-18 14:35:55 +03006574 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
Jim Mattsonf4160e42018-05-29 09:11:33 -07006575 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap));
Abel Gordon4607c2d2013-04-18 14:35:55 +03006576 }
Sheng Yang25c5f222008-03-28 13:18:56 +08006577 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +01006578 vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
Sheng Yang25c5f222008-03-28 13:18:56 +08006579
Avi Kivity6aa8b732006-12-10 02:21:36 -08006580 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
6581
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08006583 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07006584 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006585
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006586 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006587
Dan Williamsdfa169b2016-06-02 11:17:24 -07006588 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +02006589 vmx_compute_secondary_exec_control(vmx);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006590 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
Paolo Bonzini80154d72017-08-24 13:55:35 +02006591 vmx->secondary_exec_control);
Dan Williamsdfa169b2016-06-02 11:17:24 -07006592 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08006593
Andrey Smetanind62caab2015-11-10 15:36:33 +03006594 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08006595 vmcs_write64(EOI_EXIT_BITMAP0, 0);
6596 vmcs_write64(EOI_EXIT_BITMAP1, 0);
6597 vmcs_write64(EOI_EXIT_BITMAP2, 0);
6598 vmcs_write64(EOI_EXIT_BITMAP3, 0);
6599
6600 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08006601
Li RongQing0bcf2612015-12-03 13:29:34 +08006602 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08006603 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08006604 }
6605
Wanpeng Lib31c1142018-03-12 04:53:04 -07006606 if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006607 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02006608 vmx->ple_window = ple_window;
6609 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006610 }
6611
Xiao Guangrongc3707952011-07-12 03:28:04 +08006612 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
6613 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006614 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
6615
Avi Kivity9581d442010-10-19 16:46:55 +02006616 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
6617 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08006618 vmx_set_constant_host_state(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006619 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
6620 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08006621
Bandan Das2a499e42017-08-03 15:54:41 -04006622 if (cpu_has_vmx_vmfunc())
6623 vmcs_write64(VM_FUNCTION_CONTROL, 0);
6624
Eddie Dong2cc51562007-05-21 07:28:09 +03006625 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
6626 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006627 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
Eddie Dong2cc51562007-05-21 07:28:09 +03006628 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -04006629 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Avi Kivity6aa8b732006-12-10 02:21:36 -08006630
Radim Krčmář74545702015-04-27 15:11:25 +02006631 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6632 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08006633
Paolo Bonzini03916db2014-07-24 14:21:57 +02006634 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006635 u32 index = vmx_msr_index[i];
6636 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006637 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006638
6639 if (rdmsr_safe(index, &data_low, &data_high) < 0)
6640 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08006641 if (wrmsr_safe(index, data_low, data_high) < 0)
6642 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03006643 vmx->guest_msrs[j].index = i;
6644 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02006645 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006646 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006647 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006648
Paolo Bonzini5b76a3c2018-08-05 16:07:47 +02006649 vmx->arch_capabilities = kvm_get_arch_capabilities();
Gleb Natapov2961e8762013-11-25 15:37:13 +02006650
6651 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006652
6653 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02006654 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03006655
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006656 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
6657 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
6658
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03006659 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006660
Wanpeng Lif53cd632014-12-02 19:14:58 +08006661 if (vmx_xsaves_supported())
6662 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
6663
Peter Feiner4e595162016-07-07 14:49:58 -07006664 if (enable_pml) {
6665 ASSERT(vmx->pml_pg);
6666 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
6667 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6668 }
Sean Christopherson0b665d32018-08-14 09:33:34 -07006669
6670 if (cpu_has_vmx_encls_vmexit())
6671 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006672}
6673
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006674static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006675{
6676 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01006677 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006678 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006679
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006680 vmx->rmode.vm86_active = 0;
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +01006681 vmx->spec_ctrl = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006682
Wanpeng Li518e7b92018-02-28 14:03:31 +08006683 vcpu->arch.microcode_version = 0x100000000ULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006684 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006685 kvm_set_cr8(vcpu, 0);
6686
6687 if (!init_event) {
6688 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
6689 MSR_IA32_APICBASE_ENABLE;
6690 if (kvm_vcpu_is_reset_bsp(vcpu))
6691 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
6692 apic_base_msr.host_initiated = true;
6693 kvm_set_apic_base(vcpu, &apic_base_msr);
6694 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006695
Avi Kivity2fb92db2011-04-27 19:42:18 +03006696 vmx_segment_cache_clear(vmx);
6697
Avi Kivity5706be02008-08-20 15:07:31 +03006698 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01006699 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006700 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006701
6702 seg_setup(VCPU_SREG_DS);
6703 seg_setup(VCPU_SREG_ES);
6704 seg_setup(VCPU_SREG_FS);
6705 seg_setup(VCPU_SREG_GS);
6706 seg_setup(VCPU_SREG_SS);
6707
6708 vmcs_write16(GUEST_TR_SELECTOR, 0);
6709 vmcs_writel(GUEST_TR_BASE, 0);
6710 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
6711 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
6712
6713 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
6714 vmcs_writel(GUEST_LDTR_BASE, 0);
6715 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
6716 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
6717
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006718 if (!init_event) {
6719 vmcs_write32(GUEST_SYSENTER_CS, 0);
6720 vmcs_writel(GUEST_SYSENTER_ESP, 0);
6721 vmcs_writel(GUEST_SYSENTER_EIP, 0);
6722 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
6723 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006724
Wanpeng Lic37c2872017-11-20 14:52:21 -08006725 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Jan Kiszka66450a22013-03-13 12:42:34 +01006726 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006727
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006728 vmcs_writel(GUEST_GDTR_BASE, 0);
6729 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
6730
6731 vmcs_writel(GUEST_IDTR_BASE, 0);
6732 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
6733
Anthony Liguori443381a2010-12-06 10:53:38 -06006734 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006735 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01006736 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Wanpeng Lia554d202017-10-11 05:10:19 -07006737 if (kvm_mpx_supported())
6738 vmcs_write64(GUEST_BNDCFGS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006739
Avi Kivitye00c8cf2007-10-21 11:00:39 +02006740 setup_msrs(vmx);
6741
Avi Kivity6aa8b732006-12-10 02:21:36 -08006742 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
6743
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006744 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08006745 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02006746 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08006747 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006748 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08006749 vmcs_write32(TPR_THRESHOLD, 0);
6750 }
6751
Paolo Bonzinia73896c2014-11-02 07:54:30 +01006752 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006753
Sheng Yang2384d2b2008-01-17 15:14:33 +08006754 if (vmx->vpid != 0)
6755 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6756
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006757 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006758 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06006759 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006760 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02006761 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08006762
Nadav Amitd28bc9d2015-04-13 14:34:08 +03006763 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006764
Wanpeng Lidd5f5342015-09-23 18:26:57 +08006765 vpid_sync_context(vmx->vpid);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006766 if (init_event)
6767 vmx_clear_hlt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006768}
6769
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006770/*
6771 * In nested virtualization, check if L1 asked to exit on external interrupts.
6772 * For most existing hypervisors, this will always return true.
6773 */
6774static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
6775{
6776 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
6777 PIN_BASED_EXT_INTR_MASK;
6778}
6779
Bandan Das77b0f5d2014-04-19 18:17:45 -04006780/*
6781 * In nested virtualization, check if L1 has set
6782 * VM_EXIT_ACK_INTR_ON_EXIT
6783 */
6784static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
6785{
6786 return get_vmcs12(vcpu)->vm_exit_controls &
6787 VM_EXIT_ACK_INTR_ON_EXIT;
6788}
6789
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006790static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
6791{
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -05006792 return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu));
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006793}
6794
Jan Kiszkac9a79532014-03-07 20:03:15 +01006795static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006796{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006797 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6798 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006799}
6800
Jan Kiszkac9a79532014-03-07 20:03:15 +01006801static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006802{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006803 if (!enable_vnmi ||
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006804 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01006805 enable_irq_window(vcpu);
6806 return;
6807 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02006808
Paolo Bonzini47c01522016-12-19 11:44:07 +01006809 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
6810 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006811}
6812
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006813static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03006814{
Avi Kivity9c8cba32007-11-22 11:42:59 +02006815 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006816 uint32_t intr;
6817 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02006818
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006819 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006820
Avi Kivityfa89a812008-09-01 15:57:51 +03006821 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006822 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006823 int inc_eip = 0;
6824 if (vcpu->arch.interrupt.soft)
6825 inc_eip = vcpu->arch.event_exit_inst_len;
6826 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006827 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006828 return;
6829 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006830 intr = irq | INTR_INFO_VALID_MASK;
6831 if (vcpu->arch.interrupt.soft) {
6832 intr |= INTR_TYPE_SOFT_INTR;
6833 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6834 vmx->vcpu.arch.event_exit_inst_len);
6835 } else
6836 intr |= INTR_TYPE_EXT_INTR;
6837 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006838
6839 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03006840}
6841
Sheng Yangf08864b2008-05-15 18:23:25 +08006842static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
6843{
Jan Kiszka66a5a342008-09-26 09:30:51 +02006844 struct vcpu_vmx *vmx = to_vmx(vcpu);
6845
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006846 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006847 /*
6848 * Tracking the NMI-blocked state in software is built upon
6849 * finding the next open IRQ window. This, in turn, depends on
6850 * well-behaving guests: They have to keep IRQs disabled at
6851 * least as long as the NMI handler runs. Otherwise we may
6852 * cause NMI nesting, maybe breaking the guest. But as this is
6853 * highly unlikely, we can live with the residual risk.
6854 */
6855 vmx->loaded_vmcs->soft_vnmi_blocked = 1;
6856 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6857 }
6858
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006859 ++vcpu->stat.nmi_injections;
6860 vmx->loaded_vmcs->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006861
Avi Kivity7ffd92c2009-06-09 14:10:45 +03006862 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05006863 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02006864 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02006865 return;
6866 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08006867
Sheng Yangf08864b2008-05-15 18:23:25 +08006868 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6869 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Wanpeng Licaa057a2018-03-12 04:53:03 -07006870
6871 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006872}
6873
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006874static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
6875{
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006876 struct vcpu_vmx *vmx = to_vmx(vcpu);
6877 bool masked;
6878
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006879 if (!enable_vnmi)
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006880 return vmx->loaded_vmcs->soft_vnmi_blocked;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006881 if (vmx->loaded_vmcs->nmi_known_unmasked)
Avi Kivity9d58b932011-03-07 16:52:07 +02006882 return false;
Paolo Bonzini4c4a6f72017-07-14 13:36:11 +02006883 masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
6884 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6885 return masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006886}
6887
6888static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
6889{
6890 struct vcpu_vmx *vmx = to_vmx(vcpu);
6891
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006892 if (!enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006893 if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
6894 vmx->loaded_vmcs->soft_vnmi_blocked = masked;
6895 vmx->loaded_vmcs->vnmi_blocked_time = 0;
6896 }
6897 } else {
6898 vmx->loaded_vmcs->nmi_known_unmasked = !masked;
6899 if (masked)
6900 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6901 GUEST_INTR_STATE_NMI);
6902 else
6903 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
6904 GUEST_INTR_STATE_NMI);
6905 }
Jan Kiszka3cfc3092009-11-12 01:04:25 +01006906}
6907
Jan Kiszka2505dc92013-04-14 12:12:47 +02006908static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
6909{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006910 if (to_vmx(vcpu)->nested.nested_run_pending)
6911 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02006912
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01006913 if (!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +01006914 to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
6915 return 0;
6916
Jan Kiszka2505dc92013-04-14 12:12:47 +02006917 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6918 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
6919 | GUEST_INTR_STATE_NMI));
6920}
6921
Gleb Natapov78646122009-03-23 12:12:11 +02006922static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
6923{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01006924 return (!to_vmx(vcpu)->nested.nested_run_pending &&
6925 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03006926 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
6927 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02006928}
6929
Izik Eiduscbc94022007-10-25 00:29:55 +02006930static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
6931{
6932 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02006933
Sean Christophersonf7eaeb02018-03-05 12:04:36 -08006934 if (enable_unrestricted_guest)
6935 return 0;
6936
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02006937 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
6938 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02006939 if (ret)
6940 return ret;
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006941 to_kvm_vmx(kvm)->tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02006942 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02006943}
6944
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006945static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
6946{
Sean Christopherson40bbb9d2018-03-20 12:17:20 -07006947 to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07006948 return 0;
6949}
6950
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006951static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006952{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006953 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006954 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01006955 /*
6956 * Update instruction length as we may reinject the exception
6957 * from user space while in guest debugging mode.
6958 */
6959 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
6960 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006961 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006962 return false;
6963 /* fall through */
6964 case DB_VECTOR:
6965 if (vcpu->guest_debug &
6966 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
6967 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01006968 /* fall through */
6969 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006970 case OF_VECTOR:
6971 case BR_VECTOR:
6972 case UD_VECTOR:
6973 case DF_VECTOR:
6974 case SS_VECTOR:
6975 case GP_VECTOR:
6976 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006977 return true;
6978 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02006979 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006980 return false;
6981}
6982
6983static int handle_rmode_exception(struct kvm_vcpu *vcpu,
6984 int vec, u32 err_code)
6985{
6986 /*
6987 * Instruction with address size override prefix opcode 0x67
6988 * Cause the #SS fault with 0 error code in VM86 mode.
6989 */
6990 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
Sean Christopherson0ce97a22018-08-23 13:56:52 -07006991 if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) {
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006992 if (vcpu->arch.halt_request) {
6993 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006994 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02006995 }
6996 return 1;
6997 }
6998 return 0;
6999 }
7000
7001 /*
7002 * Forward all other exceptions that are valid in real mode.
7003 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
7004 * the required debugging infrastructure rework.
7005 */
7006 kvm_queue_exception(vcpu, vec);
7007 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007008}
7009
Andi Kleena0861c02009-06-08 17:37:09 +08007010/*
7011 * Trigger machine check on the host. We assume all the MSRs are already set up
7012 * by the CPU and that we still run on the same CPU as the MCE occurred on.
7013 * We pass a fake environment to the machine check handler because we want
7014 * the guest to be always treated like user space, no matter what context
7015 * it used internally.
7016 */
7017static void kvm_machine_check(void)
7018{
7019#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
7020 struct pt_regs regs = {
7021 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
7022 .flags = X86_EFLAGS_IF,
7023 };
7024
7025 do_machine_check(&regs, 0);
7026#endif
7027}
7028
Avi Kivity851ba692009-08-24 11:10:17 +03007029static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08007030{
7031 /* already handled by vcpu_run */
7032 return 1;
7033}
7034
Avi Kivity851ba692009-08-24 11:10:17 +03007035static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007036{
Avi Kivity1155f762007-11-22 11:30:47 +02007037 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007038 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007039 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007040 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007041 u32 vect_info;
7042 enum emulation_result er;
7043
Avi Kivity1155f762007-11-22 11:30:47 +02007044 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02007045 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007046
Andi Kleena0861c02009-06-08 17:37:09 +08007047 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03007048 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007049
Jim Mattsonef85b672016-12-12 11:01:37 -08007050 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02007051 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03007052
Wanpeng Li082d06e2018-04-03 16:28:48 -07007053 if (is_invalid_opcode(intr_info))
7054 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05007055
Avi Kivity6aa8b732006-12-10 02:21:36 -08007056 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06007057 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007058 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007059
Liran Alon9e869482018-03-12 13:12:51 +02007060 if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
7061 WARN_ON_ONCE(!enable_vmware_backdoor);
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007062 er = kvm_emulate_instruction(vcpu,
Liran Alon9e869482018-03-12 13:12:51 +02007063 EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL);
7064 if (er == EMULATE_USER_EXIT)
7065 return 0;
7066 else if (er != EMULATE_DONE)
7067 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
7068 return 1;
7069 }
7070
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007071 /*
7072 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
7073 * MMIO, it is better to report an internal error.
7074 * See the comments in vmx_handle_exit.
7075 */
7076 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
7077 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
7078 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7079 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007080 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007081 vcpu->run->internal.data[0] = vect_info;
7082 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02007083 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08007084 return 0;
7085 }
7086
Avi Kivity6aa8b732006-12-10 02:21:36 -08007087 if (is_page_fault(intr_info)) {
7088 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07007089 /* EPT won't cause page fault directly */
7090 WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
Paolo Bonzinid0006532017-08-11 18:36:43 +02007091 return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007092 }
7093
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007094 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02007095
7096 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
7097 return handle_rmode_exception(vcpu, ex_no, error_code);
7098
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007099 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01007100 case AC_VECTOR:
7101 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
7102 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007103 case DB_VECTOR:
7104 dr6 = vmcs_readl(EXIT_QUALIFICATION);
7105 if (!(vcpu->guest_debug &
7106 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01007107 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007108 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Linus Torvalds32d43cd2018-03-20 12:16:59 -07007109 if (is_icebp(intr_info))
Huw Daviesfd2a4452014-04-16 10:02:51 +01007110 skip_emulated_instruction(vcpu);
7111
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007112 kvm_queue_exception(vcpu, DB_VECTOR);
7113 return 1;
7114 }
7115 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
7116 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
7117 /* fall through */
7118 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01007119 /*
7120 * Update instruction length as we may reinject #BP from
7121 * user space while in guest debugging mode. Reading it for
7122 * #DB as well causes no harm, it is not used in that case.
7123 */
7124 vmx->vcpu.arch.event_exit_inst_len =
7125 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007126 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03007127 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007128 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
7129 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007130 break;
7131 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01007132 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
7133 kvm_run->ex.exception = ex_no;
7134 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007136 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007137 return 0;
7138}
7139
Avi Kivity851ba692009-08-24 11:10:17 +03007140static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007141{
Avi Kivity1165f5f2007-04-19 17:27:43 +03007142 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007143 return 1;
7144}
7145
Avi Kivity851ba692009-08-24 11:10:17 +03007146static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08007147{
Avi Kivity851ba692009-08-24 11:10:17 +03007148 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Wanpeng Libbeac282017-08-09 22:33:12 -07007149 vcpu->mmio_needed = 0;
Avi Kivity988ad742007-02-12 00:54:36 -08007150 return 0;
7151}
Avi Kivity6aa8b732006-12-10 02:21:36 -08007152
Avi Kivity851ba692009-08-24 11:10:17 +03007153static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007154{
He, Qingbfdaab02007-09-12 14:18:28 +08007155 unsigned long exit_qualification;
Sean Christophersondca7f122018-03-08 08:57:27 -08007156 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02007157 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007158
He, Qingbfdaab02007-09-12 14:18:28 +08007159 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02007160 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03007161
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007162 ++vcpu->stat.io_exits;
7163
Sean Christopherson432baf62018-03-08 08:57:26 -08007164 if (string)
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007165 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007166
7167 port = exit_qualification >> 16;
7168 size = (exit_qualification & 7) + 1;
Sean Christopherson432baf62018-03-08 08:57:26 -08007169 in = (exit_qualification & 8) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02007170
Sean Christophersondca7f122018-03-08 08:57:27 -08007171 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007172}
7173
Ingo Molnar102d8322007-02-19 14:37:47 +02007174static void
7175vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
7176{
7177 /*
7178 * Patch in the VMCALL instruction:
7179 */
7180 hypercall[0] = 0x0f;
7181 hypercall[1] = 0x01;
7182 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02007183}
7184
Guo Chao0fa06072012-06-28 15:16:19 +08007185/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007186static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
7187{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007188 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007189 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7190 unsigned long orig_val = val;
7191
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007192 /*
7193 * We get here when L2 changed cr0 in a way that did not change
7194 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007195 * but did change L0 shadowed bits. So we first calculate the
7196 * effective cr0 value that L1 would like to write into the
7197 * hardware. It consists of the L2-owned bits from the new
7198 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007199 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007200 val = (val & ~vmcs12->cr0_guest_host_mask) |
7201 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
7202
David Matlack38991522016-11-29 18:14:08 -08007203 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007204 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007205
7206 if (kvm_set_cr0(vcpu, val))
7207 return 1;
7208 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007209 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007210 } else {
7211 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08007212 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007213 return 1;
David Matlack38991522016-11-29 18:14:08 -08007214
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007215 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007216 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007217}
7218
7219static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
7220{
7221 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007222 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7223 unsigned long orig_val = val;
7224
7225 /* analogously to handle_set_cr0 */
7226 val = (val & ~vmcs12->cr4_guest_host_mask) |
7227 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
7228 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007229 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01007230 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007231 return 0;
7232 } else
7233 return kvm_set_cr4(vcpu, val);
7234}
7235
Paolo Bonzini0367f202016-07-12 10:44:55 +02007236static int handle_desc(struct kvm_vcpu *vcpu)
7237{
7238 WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007239 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Paolo Bonzini0367f202016-07-12 10:44:55 +02007240}
7241
Avi Kivity851ba692009-08-24 11:10:17 +03007242static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007243{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007244 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007245 int cr;
7246 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03007247 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007248 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007249
He, Qingbfdaab02007-09-12 14:18:28 +08007250 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007251 cr = exit_qualification & 15;
7252 reg = (exit_qualification >> 8) & 15;
7253 switch ((exit_qualification >> 4) & 3) {
7254 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03007255 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007256 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007257 switch (cr) {
7258 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007259 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007260 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007261 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007262 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity23902182010-06-10 17:02:16 +03007263 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007264 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007265 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03007266 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007267 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007268 case 8: {
7269 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03007270 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01007271 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007272 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02007273 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08007274 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007275 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007276 return ret;
7277 /*
7278 * TODO: we might be squashing a
7279 * KVM_GUESTDBG_SINGLESTEP-triggered
7280 * KVM_EXIT_DEBUG here.
7281 */
Avi Kivity851ba692009-08-24 11:10:17 +03007282 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03007283 return 0;
7284 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02007285 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007286 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03007287 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08007288 WARN_ONCE(1, "Guest should always own CR0.TS");
7289 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02007290 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08007291 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007292 case 1: /*mov from cr*/
7293 switch (cr) {
7294 case 3:
Sean Christophersone1de91c2018-03-05 12:04:41 -08007295 WARN_ON_ONCE(enable_unrestricted_guest);
Avi Kivity9f8fe502010-12-05 17:30:00 +02007296 val = kvm_read_cr3(vcpu);
7297 kvm_register_write(vcpu, reg, val);
7298 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007299 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007300 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007301 val = kvm_get_cr8(vcpu);
7302 kvm_register_write(vcpu, reg, val);
7303 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007304 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007305 }
7306 break;
7307 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02007308 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02007309 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02007310 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007311
Kyle Huey6affcbe2016-11-29 12:40:40 -08007312 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007313 default:
7314 break;
7315 }
Avi Kivity851ba692009-08-24 11:10:17 +03007316 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03007317 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08007318 (int)(exit_qualification >> 4) & 3, cr);
7319 return 0;
7320}
7321
Avi Kivity851ba692009-08-24 11:10:17 +03007322static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007323{
He, Qingbfdaab02007-09-12 14:18:28 +08007324 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007325 int dr, dr7, reg;
7326
7327 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7328 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
7329
7330 /* First, if DR does not exist, trigger UD */
7331 if (!kvm_require_dr(vcpu, dr))
7332 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007333
Jan Kiszkaf2483412010-01-20 18:20:20 +01007334 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03007335 if (!kvm_require_cpl(vcpu, 0))
7336 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007337 dr7 = vmcs_readl(GUEST_DR7);
7338 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007339 /*
7340 * As the vm-exit takes precedence over the debug trap, we
7341 * need to emulate the latter, either for the host or the
7342 * guest debugging itself.
7343 */
7344 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03007345 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03007346 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02007347 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03007348 vcpu->run->debug.arch.exception = DB_VECTOR;
7349 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007350 return 0;
7351 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02007352 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03007353 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007354 kvm_queue_exception(vcpu, DB_VECTOR);
7355 return 1;
7356 }
7357 }
7358
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007359 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01007360 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7361 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007362
7363 /*
7364 * No more DR vmexits; force a reload of the debug registers
7365 * and reenter on this instruction. The next vmexit will
7366 * retrieve the full state of the debug registers.
7367 */
7368 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
7369 return 1;
7370 }
7371
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007372 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
7373 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03007374 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007375
7376 if (kvm_get_dr(vcpu, dr, &val))
7377 return 1;
7378 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03007379 } else
Nadav Amit57773922014-06-18 17:19:23 +03007380 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01007381 return 1;
7382
Kyle Huey6affcbe2016-11-29 12:40:40 -08007383 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007384}
7385
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01007386static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
7387{
7388 return vcpu->arch.dr6;
7389}
7390
7391static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
7392{
7393}
7394
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007395static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
7396{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007397 get_debugreg(vcpu->arch.db[0], 0);
7398 get_debugreg(vcpu->arch.db[1], 1);
7399 get_debugreg(vcpu->arch.db[2], 2);
7400 get_debugreg(vcpu->arch.db[3], 3);
7401 get_debugreg(vcpu->arch.dr6, 6);
7402 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
7403
7404 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01007405 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01007406}
7407
Gleb Natapov020df072010-04-13 10:05:23 +03007408static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
7409{
7410 vmcs_writel(GUEST_DR7, val);
7411}
7412
Avi Kivity851ba692009-08-24 11:10:17 +03007413static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007414{
Kyle Huey6a908b62016-11-29 12:40:37 -08007415 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007416}
7417
Avi Kivity851ba692009-08-24 11:10:17 +03007418static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007419{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007420 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007421 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007422
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007423 msr_info.index = ecx;
7424 msr_info.host_initiated = false;
7425 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02007426 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007427 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007428 return 1;
7429 }
7430
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007431 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007432
Avi Kivity6aa8b732006-12-10 02:21:36 -08007433 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02007434 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
7435 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08007436 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007437}
7438
Avi Kivity851ba692009-08-24 11:10:17 +03007439static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007440{
Will Auld8fe8ab42012-11-29 12:42:12 -08007441 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007442 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
7443 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
7444 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007445
Will Auld8fe8ab42012-11-29 12:42:12 -08007446 msr.data = data;
7447 msr.index = ecx;
7448 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03007449 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02007450 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02007451 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007452 return 1;
7453 }
7454
Avi Kivity59200272010-01-25 19:47:02 +02007455 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007456 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007457}
7458
Avi Kivity851ba692009-08-24 11:10:17 +03007459static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007460{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01007461 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007462 return 1;
7463}
7464
Avi Kivity851ba692009-08-24 11:10:17 +03007465static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007466{
Paolo Bonzini47c01522016-12-19 11:44:07 +01007467 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7468 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04007469
Avi Kivity3842d132010-07-27 12:30:24 +03007470 kvm_make_request(KVM_REQ_EVENT, vcpu);
7471
Jan Kiszkaa26bf122008-09-26 09:30:45 +02007472 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007473 return 1;
7474}
7475
Avi Kivity851ba692009-08-24 11:10:17 +03007476static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007477{
Avi Kivityd3bef152007-06-05 15:53:05 +03007478 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007479}
7480
Avi Kivity851ba692009-08-24 11:10:17 +03007481static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02007482{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03007483 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02007484}
7485
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007486static int handle_invd(struct kvm_vcpu *vcpu)
7487{
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007488 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007489}
7490
Avi Kivity851ba692009-08-24 11:10:17 +03007491static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03007492{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007493 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007494
7495 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007496 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03007497}
7498
Avi Kivityfee84b02011-11-10 14:57:25 +02007499static int handle_rdpmc(struct kvm_vcpu *vcpu)
7500{
7501 int err;
7502
7503 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007504 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02007505}
7506
Avi Kivity851ba692009-08-24 11:10:17 +03007507static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02007508{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007509 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02007510}
7511
Dexuan Cui2acf9232010-06-10 11:27:12 +08007512static int handle_xsetbv(struct kvm_vcpu *vcpu)
7513{
7514 u64 new_bv = kvm_read_edx_eax(vcpu);
7515 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
7516
7517 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08007518 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08007519 return 1;
7520}
7521
Wanpeng Lif53cd632014-12-02 19:14:58 +08007522static int handle_xsaves(struct kvm_vcpu *vcpu)
7523{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007524 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007525 WARN(1, "this should never happen\n");
7526 return 1;
7527}
7528
7529static int handle_xrstors(struct kvm_vcpu *vcpu)
7530{
Kyle Huey6affcbe2016-11-29 12:40:40 -08007531 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08007532 WARN(1, "this should never happen\n");
7533 return 1;
7534}
7535
Avi Kivity851ba692009-08-24 11:10:17 +03007536static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08007537{
Kevin Tian58fbbf22011-08-30 13:56:17 +03007538 if (likely(fasteoi)) {
7539 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7540 int access_type, offset;
7541
7542 access_type = exit_qualification & APIC_ACCESS_TYPE;
7543 offset = exit_qualification & APIC_ACCESS_OFFSET;
7544 /*
7545 * Sane guest uses MOV to write EOI, with written value
7546 * not cared. So make a short-circuit here by avoiding
7547 * heavy instruction emulation.
7548 */
7549 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
7550 (offset == APIC_EOI)) {
7551 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007552 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03007553 }
7554 }
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007555 return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08007556}
7557
Yang Zhangc7c9c562013-01-25 10:18:51 +08007558static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
7559{
7560 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7561 int vector = exit_qualification & 0xff;
7562
7563 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
7564 kvm_apic_set_eoi_accelerated(vcpu, vector);
7565 return 1;
7566}
7567
Yang Zhang83d4c282013-01-25 10:18:49 +08007568static int handle_apic_write(struct kvm_vcpu *vcpu)
7569{
7570 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7571 u32 offset = exit_qualification & 0xfff;
7572
7573 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
7574 kvm_apic_write_nodecode(vcpu, offset);
7575 return 1;
7576}
7577
Avi Kivity851ba692009-08-24 11:10:17 +03007578static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02007579{
Jan Kiszka60637aa2008-09-26 09:30:47 +02007580 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02007581 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02007582 bool has_error_code = false;
7583 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02007584 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007585 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007586
7587 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007588 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007589 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02007590
7591 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7592
7593 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007594 if (reason == TASK_SWITCH_GATE && idt_v) {
7595 switch (type) {
7596 case INTR_TYPE_NMI_INTR:
7597 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02007598 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007599 break;
7600 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007601 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007602 kvm_clear_interrupt_queue(vcpu);
7603 break;
7604 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02007605 if (vmx->idt_vectoring_info &
7606 VECTORING_INFO_DELIVER_CODE_MASK) {
7607 has_error_code = true;
7608 error_code =
7609 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7610 }
7611 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007612 case INTR_TYPE_SOFT_EXCEPTION:
7613 kvm_clear_exception_queue(vcpu);
7614 break;
7615 default:
7616 break;
7617 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02007618 }
Izik Eidus37817f22008-03-24 23:14:53 +02007619 tss_selector = exit_qualification;
7620
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007621 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
7622 type != INTR_TYPE_EXT_INTR &&
7623 type != INTR_TYPE_NMI_INTR))
7624 skip_emulated_instruction(vcpu);
7625
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01007626 if (kvm_task_switch(vcpu, tss_selector,
7627 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
7628 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03007629 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7630 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7631 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007632 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03007633 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007634
Jan Kiszka42dbaa52008-12-15 13:52:10 +01007635 /*
7636 * TODO: What about debug traps on tss switch?
7637 * Are we supposed to inject them and update dr6?
7638 */
7639
7640 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02007641}
7642
Avi Kivity851ba692009-08-24 11:10:17 +03007643static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08007644{
Sheng Yangf9c617f2009-03-25 10:08:52 +08007645 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08007646 gpa_t gpa;
Paolo Bonzinieebed242016-11-28 14:39:58 +01007647 u64 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08007648
Sheng Yangf9c617f2009-03-25 10:08:52 +08007649 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08007650
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007651 /*
7652 * EPT violation happened while executing iret from NMI,
7653 * "blocked by NMI" bit has to be set before next VM entry.
7654 * There are errata that may cause this bit to not be set:
7655 * AAK134, BY25.
7656 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007657 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007658 enable_vnmi &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03007659 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03007660 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
7661
Sheng Yang14394422008-04-28 12:24:45 +08007662 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007663 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007664
Junaid Shahid27959a42016-12-06 16:46:10 -08007665 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007666 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08007667 ? PFERR_USER_MASK : 0;
7668 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007669 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08007670 ? PFERR_WRITE_MASK : 0;
7671 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08007672 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08007673 ? PFERR_FETCH_MASK : 0;
7674 /* ept page table entry is present? */
7675 error_code |= (exit_qualification &
7676 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
7677 EPT_VIOLATION_EXECUTABLE))
7678 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007679
Paolo Bonzinieebed242016-11-28 14:39:58 +01007680 error_code |= (exit_qualification & 0x100) != 0 ?
7681 PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03007682
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007683 vcpu->arch.exit_qualification = exit_qualification;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08007684 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08007685}
7686
Avi Kivity851ba692009-08-24 11:10:17 +03007687static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007688{
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007689 gpa_t gpa;
7690
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007691 /*
7692 * A nested guest cannot optimize MMIO vmexits, because we have an
7693 * nGPA here instead of the required GPA.
7694 */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007695 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Paolo Bonzini9034e6e2017-08-17 18:36:58 +02007696 if (!is_guest_mode(vcpu) &&
7697 !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08007698 trace_kvm_fast_mmio(gpa);
Vitaly Kuznetsovd391f122018-01-25 16:37:07 +01007699 /*
7700 * Doing kvm_skip_emulated_instruction() depends on undefined
7701 * behavior: Intel's manual doesn't mandate
7702 * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG
7703 * occurs and while on real hardware it was observed to be set,
7704 * other hypervisors (namely Hyper-V) don't set it, we end up
7705 * advancing IP with some random value. Disable fast mmio when
7706 * running nested and keep it for real hardware in hope that
7707 * VM_EXIT_INSTRUCTION_LEN will always be set correctly.
7708 */
7709 if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
7710 return kvm_skip_emulated_instruction(vcpu);
7711 else
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007712 return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) ==
Sean Christophersonc4409902018-08-23 13:56:46 -07007713 EMULATE_DONE;
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03007714 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007715
Sean Christophersonc75d0edc2018-03-29 14:48:31 -07007716 return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007717}
7718
Avi Kivity851ba692009-08-24 11:10:17 +03007719static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08007720{
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007721 WARN_ON_ONCE(!enable_vnmi);
Paolo Bonzini47c01522016-12-19 11:44:07 +01007722 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
7723 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08007724 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03007725 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08007726
7727 return 1;
7728}
7729
Mohammed Gamal80ced182009-09-01 12:48:18 +02007730static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007731{
Avi Kivity8b3079a2009-01-05 12:10:54 +02007732 struct vcpu_vmx *vmx = to_vmx(vcpu);
7733 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007734 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02007735 u32 cpu_exec_ctrl;
7736 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03007737 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02007738
Sean Christopherson2bb8caf2018-03-12 10:56:13 -07007739 /*
7740 * We should never reach the point where we are emulating L2
7741 * due to invalid guest state as that means we incorrectly
7742 * allowed a nested VMEntry with an invalid vmcs12.
7743 */
7744 WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
7745
Avi Kivity49e9d552010-09-19 14:34:08 +02007746 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7747 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007748
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01007749 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03007750 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02007751 return handle_interrupt_window(&vmx->vcpu);
7752
Radim Krčmář72875d82017-04-26 22:32:19 +02007753 if (kvm_test_request(KVM_REQ_EVENT, vcpu))
Avi Kivityde87dcdd2012-06-12 20:21:38 +03007754 return 1;
7755
Sean Christopherson0ce97a22018-08-23 13:56:52 -07007756 err = kvm_emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007757
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02007758 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02007759 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02007760 ret = 0;
7761 goto out;
7762 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007763
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007764 if (err != EMULATE_DONE)
7765 goto emulation_error;
7766
7767 if (vmx->emulation_required && !vmx->rmode.vm86_active &&
7768 vcpu->arch.exception.pending)
7769 goto emulation_error;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007770
Gleb Natapov8d76c492013-05-08 18:38:44 +03007771 if (vcpu->arch.halt_request) {
7772 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06007773 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03007774 goto out;
7775 }
7776
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007777 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02007778 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007779 if (need_resched())
7780 schedule();
7781 }
7782
Mohammed Gamal80ced182009-09-01 12:48:18 +02007783out:
7784 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03007785
Sean Christophersonadd5ff72018-03-23 09:34:00 -07007786emulation_error:
7787 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7788 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
7789 vcpu->run->internal.ndata = 0;
7790 return 0;
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007791}
7792
7793static void grow_ple_window(struct kvm_vcpu *vcpu)
7794{
7795 struct vcpu_vmx *vmx = to_vmx(vcpu);
7796 int old = vmx->ple_window;
7797
Babu Mogerc8e88712018-03-16 16:37:24 -04007798 vmx->ple_window = __grow_ple_window(old, ple_window,
7799 ple_window_grow,
7800 ple_window_max);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007801
7802 if (vmx->ple_window != old)
7803 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007804
7805 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007806}
7807
7808static void shrink_ple_window(struct kvm_vcpu *vcpu)
7809{
7810 struct vcpu_vmx *vmx = to_vmx(vcpu);
7811 int old = vmx->ple_window;
7812
Babu Mogerc8e88712018-03-16 16:37:24 -04007813 vmx->ple_window = __shrink_ple_window(old, ple_window,
7814 ple_window_shrink,
7815 ple_window);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007816
7817 if (vmx->ple_window != old)
7818 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02007819
7820 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02007821}
7822
7823/*
Feng Wubf9f6ac2015-09-18 22:29:55 +08007824 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
7825 */
7826static void wakeup_handler(void)
7827{
7828 struct kvm_vcpu *vcpu;
7829 int cpu = smp_processor_id();
7830
7831 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7832 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
7833 blocked_vcpu_list) {
7834 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7835
7836 if (pi_test_on(pi_desc) == 1)
7837 kvm_vcpu_kick(vcpu);
7838 }
7839 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
7840}
7841
Peng Haoe01bca22018-04-07 05:47:32 +08007842static void vmx_enable_tdp(void)
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007843{
7844 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
7845 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
7846 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
7847 0ull, VMX_EPT_EXECUTABLE_MASK,
7848 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05007849 VMX_EPT_RWX_MASK, 0ull);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007850
7851 ept_set_mmio_spte_mask();
7852 kvm_enable_tdp();
7853}
7854
Tiejun Chenf2c76482014-10-28 10:14:47 +08007855static __init int hardware_setup(void)
7856{
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007857 unsigned long host_bndcfgs;
Paolo Bonzini904e14f2018-01-16 16:51:18 +01007858 int r = -ENOMEM, i;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007859
7860 rdmsrl_safe(MSR_EFER, &host_efer);
7861
7862 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
7863 kvm_define_shared_msr(i, vmx_msr_index[i]);
7864
Radim Krčmář23611332016-09-29 22:41:33 +02007865 for (i = 0; i < VMX_BITMAP_NR; i++) {
7866 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
7867 if (!vmx_bitmap[i])
7868 goto out;
7869 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007870
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007871 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
7872 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
7873
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007874 if (setup_vmcs_config(&vmcs_config) < 0) {
7875 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02007876 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08007877 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007878
7879 if (boot_cpu_has(X86_FEATURE_NX))
7880 kvm_enable_efer_bits(EFER_NX);
7881
Sean Christophersoncf81a7e2018-07-11 09:54:30 -07007882 if (boot_cpu_has(X86_FEATURE_MPX)) {
7883 rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
7884 WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
7885 }
7886
Wanpeng Li08d839c2017-03-23 05:30:08 -07007887 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
7888 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08007889 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07007890
Tiejun Chenf2c76482014-10-28 10:14:47 +08007891 if (!cpu_has_vmx_ept() ||
David Hildenbrand42aa53b2017-08-10 23:15:29 +02007892 !cpu_has_vmx_ept_4levels() ||
David Hildenbrandf5f51582017-08-24 20:51:30 +02007893 !cpu_has_vmx_ept_mt_wb() ||
Wanpeng Li8ad81822017-10-09 15:51:53 -07007894 !cpu_has_vmx_invept_global())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007895 enable_ept = 0;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007896
Wanpeng Lifce6ac42017-05-11 02:58:56 -07007897 if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007898 enable_ept_ad_bits = 0;
7899
Wanpeng Li8ad81822017-10-09 15:51:53 -07007900 if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007901 enable_unrestricted_guest = 0;
7902
Paolo Bonziniad15a292015-01-30 16:18:49 +01007903 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08007904 flexpriority_enabled = 0;
7905
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01007906 if (!cpu_has_virtual_nmis())
7907 enable_vnmi = 0;
7908
Paolo Bonziniad15a292015-01-30 16:18:49 +01007909 /*
7910 * set_apic_access_page_addr() is used to reload apic access
7911 * page upon invalidation. No need to do anything if not
7912 * using the APIC_ACCESS_ADDR VMCS field.
7913 */
7914 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08007915 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08007916
7917 if (!cpu_has_vmx_tpr_shadow())
7918 kvm_x86_ops->update_cr8_intercept = NULL;
7919
7920 if (enable_ept && !cpu_has_vmx_ept_2m_page())
7921 kvm_disable_largepages();
7922
Tianyu Lan877ad952018-07-19 08:40:23 +00007923#if IS_ENABLED(CONFIG_HYPERV)
7924 if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
7925 && enable_ept)
7926 kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb;
7927#endif
7928
Wanpeng Li0f107682017-09-28 18:06:24 -07007929 if (!cpu_has_vmx_ple()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007930 ple_gap = 0;
Wanpeng Li0f107682017-09-28 18:06:24 -07007931 ple_window = 0;
7932 ple_window_grow = 0;
7933 ple_window_max = 0;
7934 ple_window_shrink = 0;
7935 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007936
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007937 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08007938 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01007939 kvm_x86_ops->sync_pir_to_irr = NULL;
7940 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08007941
Haozhong Zhang64903d62015-10-20 15:39:09 +08007942 if (cpu_has_vmx_tsc_scaling()) {
7943 kvm_has_tsc_control = true;
7944 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
7945 kvm_tsc_scaling_ratio_frac_bits = 48;
7946 }
7947
Wanpeng Li04bb92e2015-09-16 19:31:11 +08007948 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7949
Junaid Shahidf160c7b2016-12-06 16:46:16 -08007950 if (enable_ept)
7951 vmx_enable_tdp();
7952 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08007953 kvm_disable_tdp();
7954
Jim Mattson8fcc4b52018-07-10 11:27:20 +02007955 if (!nested) {
7956 kvm_x86_ops->get_nested_state = NULL;
7957 kvm_x86_ops->set_nested_state = NULL;
7958 }
7959
Kai Huang843e4332015-01-28 10:54:28 +08007960 /*
7961 * Only enable PML when hardware supports PML feature, and both EPT
7962 * and EPT A/D bit features are enabled -- PML depends on them to work.
7963 */
7964 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
7965 enable_pml = 0;
7966
7967 if (!enable_pml) {
7968 kvm_x86_ops->slot_enable_log_dirty = NULL;
7969 kvm_x86_ops->slot_disable_log_dirty = NULL;
7970 kvm_x86_ops->flush_log_dirty = NULL;
7971 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
7972 }
7973
Sean Christophersond264ee02018-08-27 15:21:12 -07007974 if (!cpu_has_vmx_preemption_timer())
7975 kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
7976
Yunhong Jiang64672c92016-06-13 14:19:59 -07007977 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
7978 u64 vmx_msr;
7979
7980 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
7981 cpu_preemption_timer_multi =
7982 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
7983 } else {
7984 kvm_x86_ops->set_hv_timer = NULL;
7985 kvm_x86_ops->cancel_hv_timer = NULL;
7986 }
7987
Paolo Bonzinic5d167b2017-12-13 11:05:19 +01007988 if (!cpu_has_vmx_shadow_vmcs())
7989 enable_shadow_vmcs = 0;
7990 if (enable_shadow_vmcs)
7991 init_vmcs_shadow_fields();
7992
Feng Wubf9f6ac2015-09-18 22:29:55 +08007993 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
Paolo Bonzini13893092018-02-26 13:40:09 +01007994 nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv);
Feng Wubf9f6ac2015-09-18 22:29:55 +08007995
Ashok Rajc45dcc72016-06-22 14:59:56 +08007996 kvm_mce_cap_supported |= MCG_LMCE_P;
7997
Tiejun Chenf2c76482014-10-28 10:14:47 +08007998 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08007999
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008000out:
Radim Krčmář23611332016-09-29 22:41:33 +02008001 for (i = 0; i < VMX_BITMAP_NR; i++)
8002 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008003
8004 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08008005}
8006
8007static __exit void hardware_unsetup(void)
8008{
Radim Krčmář23611332016-09-29 22:41:33 +02008009 int i;
8010
8011 for (i = 0; i < VMX_BITMAP_NR; i++)
8012 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08008013
Tiejun Chenf2c76482014-10-28 10:14:47 +08008014 free_kvm_area();
8015}
8016
Avi Kivity6aa8b732006-12-10 02:21:36 -08008017/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008018 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
8019 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
8020 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03008021static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008022{
Wanpeng Lib31c1142018-03-12 04:53:04 -07008023 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +02008024 grow_ple_window(vcpu);
8025
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08008026 /*
8027 * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
8028 * VM-execution control is ignored if CPL > 0. OTOH, KVM
8029 * never set PAUSE_EXITING and just set PLE if supported,
8030 * so the vcpu must be CPL=0 if it gets a PAUSE exit.
8031 */
8032 kvm_vcpu_on_spin(vcpu, true);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008033 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008034}
8035
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008036static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08008037{
Kyle Huey6affcbe2016-11-29 12:40:40 -08008038 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08008039}
8040
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008041static int handle_mwait(struct kvm_vcpu *vcpu)
8042{
8043 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
8044 return handle_nop(vcpu);
8045}
8046
Jim Mattson45ec3682017-08-23 16:32:04 -07008047static int handle_invalid_op(struct kvm_vcpu *vcpu)
8048{
8049 kvm_queue_exception(vcpu, UD_VECTOR);
8050 return 1;
8051}
8052
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008053static int handle_monitor_trap(struct kvm_vcpu *vcpu)
8054{
8055 return 1;
8056}
8057
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04008058static int handle_monitor(struct kvm_vcpu *vcpu)
8059{
8060 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
8061 return handle_nop(vcpu);
8062}
8063
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08008064/*
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008065 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
8066 * set the success or error code of an emulated VMX instruction, as specified
8067 * by Vol 2B, VMX Instruction Reference, "Conventions".
8068 */
8069static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
8070{
8071 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
8072 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8073 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
8074}
8075
8076static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
8077{
8078 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8079 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
8080 X86_EFLAGS_SF | X86_EFLAGS_OF))
8081 | X86_EFLAGS_CF);
8082}
8083
Abel Gordon145c28d2013-04-18 14:36:55 +03008084static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08008085 u32 vm_instruction_error)
8086{
8087 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
8088 /*
8089 * failValid writes the error number to the current VMCS, which
8090 * can't be done there isn't a current VMCS.
8091 */
8092 nested_vmx_failInvalid(vcpu);
8093 return;
8094 }
8095 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
8096 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
8097 X86_EFLAGS_SF | X86_EFLAGS_OF))
8098 | X86_EFLAGS_ZF);
8099 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
8100 /*
8101 * We don't need to force a shadow sync because
8102 * VM_INSTRUCTION_ERROR is not shadowed
8103 */
8104}
Abel Gordon145c28d2013-04-18 14:36:55 +03008105
Wincy Vanff651cb2014-12-11 08:52:58 +03008106static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
8107{
8108 /* TODO: not to reset guest simply here. */
8109 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02008110 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03008111}
8112
Jan Kiszkaf41245002014-03-07 20:03:13 +01008113static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
8114{
8115 struct vcpu_vmx *vmx =
8116 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
8117
8118 vmx->nested.preemption_timer_expired = true;
8119 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
8120 kvm_vcpu_kick(&vmx->vcpu);
8121
8122 return HRTIMER_NORESTART;
8123}
8124
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008125/*
Bandan Das19677e32014-05-06 02:19:15 -04008126 * Decode the memory-address operand of a vmx instruction, as recorded on an
8127 * exit caused by such an instruction (run by a guest hypervisor).
8128 * On success, returns 0. When the operand is invalid, returns 1 and throws
8129 * #UD or #GP.
8130 */
8131static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
8132 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008133 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04008134{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008135 gva_t off;
8136 bool exn;
8137 struct kvm_segment s;
8138
Bandan Das19677e32014-05-06 02:19:15 -04008139 /*
8140 * According to Vol. 3B, "Information for VM Exits Due to Instruction
8141 * Execution", on an exit, vmx_instruction_info holds most of the
8142 * addressing components of the operand. Only the displacement part
8143 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
8144 * For how an actual address is calculated from all these components,
8145 * refer to Vol. 1, "Operand Addressing".
8146 */
8147 int scaling = vmx_instruction_info & 3;
8148 int addr_size = (vmx_instruction_info >> 7) & 7;
8149 bool is_reg = vmx_instruction_info & (1u << 10);
8150 int seg_reg = (vmx_instruction_info >> 15) & 7;
8151 int index_reg = (vmx_instruction_info >> 18) & 0xf;
8152 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
8153 int base_reg = (vmx_instruction_info >> 23) & 0xf;
8154 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
8155
8156 if (is_reg) {
8157 kvm_queue_exception(vcpu, UD_VECTOR);
8158 return 1;
8159 }
8160
8161 /* Addr = segment_base + offset */
8162 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008163 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04008164 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008165 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04008166 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008167 off += kvm_register_read(vcpu, index_reg)<<scaling;
8168 vmx_get_segment(vcpu, &s, seg_reg);
8169 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04008170
8171 if (addr_size == 1) /* 32 bit */
8172 *ret &= 0xffffffff;
8173
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008174 /* Checks for #GP/#SS exceptions. */
8175 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008176 if (is_long_mode(vcpu)) {
8177 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
8178 * non-canonical form. This is the only check on the memory
8179 * destination for long mode!
8180 */
Yu Zhangfd8cb432017-08-24 20:27:56 +08008181 exn = is_noncanonical_address(*ret, vcpu);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008182 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008183 /* Protected mode: apply checks for segment validity in the
8184 * following order:
8185 * - segment type check (#GP(0) may be thrown)
8186 * - usability check (#GP(0)/#SS(0))
8187 * - limit check (#GP(0)/#SS(0))
8188 */
8189 if (wr)
8190 /* #GP(0) if the destination operand is located in a
8191 * read-only data segment or any code segment.
8192 */
8193 exn = ((s.type & 0xa) == 0 || (s.type & 8));
8194 else
8195 /* #GP(0) if the source operand is located in an
8196 * execute-only code segment
8197 */
8198 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02008199 if (exn) {
8200 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
8201 return 1;
8202 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008203 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
8204 */
8205 exn = (s.unusable != 0);
8206 /* Protected mode: #GP(0)/#SS(0) if the memory
8207 * operand is outside the segment limit.
8208 */
8209 exn = exn || (off + sizeof(u64) > s.limit);
8210 }
8211 if (exn) {
8212 kvm_queue_exception_e(vcpu,
8213 seg_reg == VCPU_SREG_SS ?
8214 SS_VECTOR : GP_VECTOR,
8215 0);
8216 return 1;
8217 }
8218
Bandan Das19677e32014-05-06 02:19:15 -04008219 return 0;
8220}
8221
Radim Krčmářcbf71272017-05-19 15:48:51 +02008222static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04008223{
8224 gva_t gva;
Bandan Das3573e222014-05-06 02:19:16 -04008225 struct x86_exception e;
Bandan Das3573e222014-05-06 02:19:16 -04008226
8227 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008228 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04008229 return 1;
8230
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008231 if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) {
Bandan Das3573e222014-05-06 02:19:16 -04008232 kvm_inject_page_fault(vcpu, &e);
8233 return 1;
8234 }
8235
Bandan Das3573e222014-05-06 02:19:16 -04008236 return 0;
8237}
8238
Liran Alonabfc52c2018-06-23 02:35:13 +03008239/*
8240 * Allocate a shadow VMCS and associate it with the currently loaded
8241 * VMCS, unless such a shadow VMCS already exists. The newly allocated
8242 * VMCS is also VMCLEARed, so that it is ready for use.
8243 */
8244static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu)
8245{
8246 struct vcpu_vmx *vmx = to_vmx(vcpu);
8247 struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs;
8248
8249 /*
8250 * We should allocate a shadow vmcs for vmcs01 only when L1
8251 * executes VMXON and free it when L1 executes VMXOFF.
8252 * As it is invalid to execute VMXON twice, we shouldn't reach
8253 * here when vmcs01 already have an allocated shadow vmcs.
8254 */
8255 WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs);
8256
8257 if (!loaded_vmcs->shadow_vmcs) {
8258 loaded_vmcs->shadow_vmcs = alloc_vmcs(true);
8259 if (loaded_vmcs->shadow_vmcs)
8260 vmcs_clear(loaded_vmcs->shadow_vmcs);
8261 }
8262 return loaded_vmcs->shadow_vmcs;
8263}
8264
Jim Mattsone29acc52016-11-30 12:03:43 -08008265static int enter_vmx_operation(struct kvm_vcpu *vcpu)
8266{
8267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008268 int r;
Jim Mattsone29acc52016-11-30 12:03:43 -08008269
Paolo Bonzinif21f1652018-01-11 12:16:15 +01008270 r = alloc_loaded_vmcs(&vmx->nested.vmcs02);
8271 if (r < 0)
Jim Mattsonde3a0022017-11-27 17:22:25 -06008272 goto out_vmcs02;
Jim Mattsone29acc52016-11-30 12:03:43 -08008273
8274 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8275 if (!vmx->nested.cached_vmcs12)
8276 goto out_cached_vmcs12;
8277
Liran Alon61ada742018-06-23 02:35:08 +03008278 vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
8279 if (!vmx->nested.cached_shadow_vmcs12)
8280 goto out_cached_shadow_vmcs12;
8281
Liran Alonabfc52c2018-06-23 02:35:13 +03008282 if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu))
8283 goto out_shadow_vmcs;
Jim Mattsone29acc52016-11-30 12:03:43 -08008284
Jim Mattsone29acc52016-11-30 12:03:43 -08008285 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
8286 HRTIMER_MODE_REL_PINNED);
8287 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
8288
Roman Kagan63aff652018-07-19 21:59:07 +03008289 vmx->nested.vpid02 = allocate_vpid();
8290
Jim Mattsone29acc52016-11-30 12:03:43 -08008291 vmx->nested.vmxon = true;
8292 return 0;
8293
8294out_shadow_vmcs:
Liran Alon61ada742018-06-23 02:35:08 +03008295 kfree(vmx->nested.cached_shadow_vmcs12);
8296
8297out_cached_shadow_vmcs12:
Jim Mattsone29acc52016-11-30 12:03:43 -08008298 kfree(vmx->nested.cached_vmcs12);
8299
8300out_cached_vmcs12:
Jim Mattsonde3a0022017-11-27 17:22:25 -06008301 free_loaded_vmcs(&vmx->nested.vmcs02);
Jim Mattsone29acc52016-11-30 12:03:43 -08008302
Jim Mattsonde3a0022017-11-27 17:22:25 -06008303out_vmcs02:
Jim Mattsone29acc52016-11-30 12:03:43 -08008304 return -ENOMEM;
8305}
8306
Bandan Das3573e222014-05-06 02:19:16 -04008307/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008308 * Emulate the VMXON instruction.
8309 * Currently, we just remember that VMX is active, and do not save or even
8310 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
8311 * do not currently need to store anything in that guest-allocated memory
8312 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
8313 * argument is different from the VMXON pointer (which the spec says they do).
8314 */
8315static int handle_vmon(struct kvm_vcpu *vcpu)
8316{
Jim Mattsone29acc52016-11-30 12:03:43 -08008317 int ret;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008318 gpa_t vmptr;
8319 struct page *page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008320 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008321 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
8322 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008323
Jim Mattson70f3aac2017-04-26 08:53:46 -07008324 /*
8325 * The Intel VMX Instruction Reference lists a bunch of bits that are
8326 * prerequisite to running VMXON, most notably cr4.VMXE must be set to
8327 * 1 (see vmx_set_cr4() for when we allow the guest to set this).
8328 * Otherwise, we should fail with #UD. But most faulting conditions
8329 * have already been checked by hardware, prior to the VM-exit for
8330 * VMXON. We do test guest cr4.VMXE because processor CR4 always has
8331 * that bit set to 1 in non-root mode.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008332 */
Jim Mattson70f3aac2017-04-26 08:53:46 -07008333 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008334 kvm_queue_exception(vcpu, UD_VECTOR);
8335 return 1;
8336 }
8337
Felix Wilhelm727ba742018-06-11 09:43:44 +02008338 /* CPL=0 must be checked manually. */
8339 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008340 kvm_inject_gp(vcpu, 0);
Felix Wilhelm727ba742018-06-11 09:43:44 +02008341 return 1;
8342 }
8343
Abel Gordon145c28d2013-04-18 14:36:55 +03008344 if (vmx->nested.vmxon) {
8345 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008346 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03008347 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008348
Haozhong Zhang3b840802016-06-22 14:59:54 +08008349 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08008350 != VMXON_NEEDED_FEATURES) {
8351 kvm_inject_gp(vcpu, 0);
8352 return 1;
8353 }
8354
Radim Krčmářcbf71272017-05-19 15:48:51 +02008355 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Jim Mattson21e7fbe2016-12-22 15:49:55 -08008356 return 1;
Radim Krčmářcbf71272017-05-19 15:48:51 +02008357
8358 /*
8359 * SDM 3: 24.11.5
8360 * The first 4 bytes of VMXON region contain the supported
8361 * VMCS revision identifier
8362 *
8363 * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case;
8364 * which replaces physical address width with 32
8365 */
8366 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8367 nested_vmx_failInvalid(vcpu);
8368 return kvm_skip_emulated_instruction(vcpu);
8369 }
8370
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008371 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8372 if (is_error_page(page)) {
Radim Krčmářcbf71272017-05-19 15:48:51 +02008373 nested_vmx_failInvalid(vcpu);
8374 return kvm_skip_emulated_instruction(vcpu);
8375 }
8376 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
8377 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008378 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008379 nested_vmx_failInvalid(vcpu);
8380 return kvm_skip_emulated_instruction(vcpu);
8381 }
8382 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008383 kvm_release_page_clean(page);
Radim Krčmářcbf71272017-05-19 15:48:51 +02008384
8385 vmx->nested.vmxon_ptr = vmptr;
Jim Mattsone29acc52016-11-30 12:03:43 -08008386 ret = enter_vmx_operation(vcpu);
8387 if (ret)
8388 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008389
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008390 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008391 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008392}
8393
8394/*
8395 * Intel's VMX Instruction Reference specifies a common set of prerequisites
8396 * for running VMX instructions (except VMXON, whose prerequisites are
8397 * slightly different). It also specifies what exception to inject otherwise.
Jim Mattson70f3aac2017-04-26 08:53:46 -07008398 * Note that many of these exceptions have priority over VM exits, so they
8399 * don't have to be checked again here.
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008400 */
8401static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
8402{
Jim Mattson70f3aac2017-04-26 08:53:46 -07008403 if (!to_vmx(vcpu)->nested.vmxon) {
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008404 kvm_queue_exception(vcpu, UD_VECTOR);
8405 return 0;
8406 }
Jim Mattsone49fcb82018-07-27 13:44:45 -07008407
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008408 if (vmx_get_cpl(vcpu)) {
Jim Mattson36090bf2018-07-27 09:18:50 -07008409 kvm_inject_gp(vcpu, 0);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008410 return 0;
8411 }
8412
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008413 return 1;
8414}
8415
David Matlack8ca44e82017-08-01 14:00:39 -07008416static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx)
8417{
8418 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
8419 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8420}
8421
Abel Gordone7953d72013-04-18 14:37:55 +03008422static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
8423{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008424 if (vmx->nested.current_vmptr == -1ull)
8425 return;
8426
Abel Gordon012f83c2013-04-18 14:39:25 +03008427 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008428 /* copy to memory all shadowed fields in case
8429 they were modified */
8430 copy_shadow_to_vmcs12(vmx);
8431 vmx->nested.sync_shadow_vmcs = false;
David Matlack8ca44e82017-08-01 14:00:39 -07008432 vmx_disable_shadow_vmcs(vmx);
Abel Gordon012f83c2013-04-18 14:39:25 +03008433 }
Wincy Van705699a2015-02-03 23:58:17 +08008434 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07008435
8436 /* Flush VMCS12 to guest memory */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008437 kvm_vcpu_write_guest_page(&vmx->vcpu,
8438 vmx->nested.current_vmptr >> PAGE_SHIFT,
8439 vmx->nested.cached_vmcs12, 0, VMCS12_SIZE);
David Matlack4f2777b2016-07-13 17:16:37 -07008440
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008441 vmx->nested.current_vmptr = -1ull;
Abel Gordone7953d72013-04-18 14:37:55 +03008442}
8443
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008444/*
8445 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
8446 * just stops using VMX.
8447 */
8448static void free_nested(struct vcpu_vmx *vmx)
8449{
Wanpeng Lib7455822017-11-22 14:04:00 -08008450 if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008451 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008452
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008453 vmx->nested.vmxon = false;
Wanpeng Lib7455822017-11-22 14:04:00 -08008454 vmx->nested.smm.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07008455 free_vpid(vmx->nested.vpid02);
David Matlack8ca44e82017-08-01 14:00:39 -07008456 vmx->nested.posted_intr_nv = -1;
8457 vmx->nested.current_vmptr = -1ull;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008458 if (enable_shadow_vmcs) {
David Matlack8ca44e82017-08-01 14:00:39 -07008459 vmx_disable_shadow_vmcs(vmx);
Jim Mattson355f4fb2016-10-28 08:29:39 -07008460 vmcs_clear(vmx->vmcs01.shadow_vmcs);
8461 free_vmcs(vmx->vmcs01.shadow_vmcs);
8462 vmx->vmcs01.shadow_vmcs = NULL;
8463 }
David Matlack4f2777b2016-07-13 17:16:37 -07008464 kfree(vmx->nested.cached_vmcs12);
Liran Alon61ada742018-06-23 02:35:08 +03008465 kfree(vmx->nested.cached_shadow_vmcs12);
Jim Mattsonde3a0022017-11-27 17:22:25 -06008466 /* Unpin physical memory we referred to in the vmcs02 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008467 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008468 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008469 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008470 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008471 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +02008472 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02008473 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008474 }
Wincy Van705699a2015-02-03 23:58:17 +08008475 if (vmx->nested.pi_desc_page) {
8476 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008477 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +08008478 vmx->nested.pi_desc_page = NULL;
8479 vmx->nested.pi_desc = NULL;
8480 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03008481
Jim Mattsonde3a0022017-11-27 17:22:25 -06008482 free_loaded_vmcs(&vmx->nested.vmcs02);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008483}
8484
8485/* Emulate the VMXOFF instruction */
8486static int handle_vmoff(struct kvm_vcpu *vcpu)
8487{
8488 if (!nested_vmx_check_permission(vcpu))
8489 return 1;
8490 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08008491 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008492 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03008493}
8494
Nadav Har'El27d6c862011-05-25 23:06:59 +03008495/* Emulate the VMCLEAR instruction */
8496static int handle_vmclear(struct kvm_vcpu *vcpu)
8497{
8498 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08008499 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008500 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03008501
8502 if (!nested_vmx_check_permission(vcpu))
8503 return 1;
8504
Radim Krčmářcbf71272017-05-19 15:48:51 +02008505 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03008506 return 1;
8507
Radim Krčmářcbf71272017-05-19 15:48:51 +02008508 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8509 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
8510 return kvm_skip_emulated_instruction(vcpu);
8511 }
8512
8513 if (vmptr == vmx->nested.vmxon_ptr) {
8514 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER);
8515 return kvm_skip_emulated_instruction(vcpu);
8516 }
8517
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008518 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03008519 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008520
Jim Mattson587d7e722017-03-02 12:41:48 -08008521 kvm_vcpu_write_guest(vcpu,
8522 vmptr + offsetof(struct vmcs12, launch_state),
8523 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03008524
Nadav Har'El27d6c862011-05-25 23:06:59 +03008525 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008526 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03008527}
8528
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008529static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
8530
8531/* Emulate the VMLAUNCH instruction */
8532static int handle_vmlaunch(struct kvm_vcpu *vcpu)
8533{
8534 return nested_vmx_run(vcpu, true);
8535}
8536
8537/* Emulate the VMRESUME instruction */
8538static int handle_vmresume(struct kvm_vcpu *vcpu)
8539{
8540
8541 return nested_vmx_run(vcpu, false);
8542}
8543
Nadav Har'El49f705c2011-05-25 23:08:30 +03008544/*
8545 * Read a vmcs12 field. Since these can have varying lengths and we return
8546 * one type, we chose the biggest type (u64) and zero-extend the return value
8547 * to that size. Note that the caller, handle_vmread, might need to use only
8548 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
8549 * 64-bit fields are to be returned).
8550 */
Liran Alone2536742018-06-23 02:35:02 +03008551static inline int vmcs12_read_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008552 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03008553{
8554 short offset = vmcs_field_to_offset(field);
8555 char *p;
8556
8557 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008558 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008559
Liran Alone2536742018-06-23 02:35:02 +03008560 p = (char *)vmcs12 + offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008561
Jim Mattsond37f4262017-12-22 12:12:16 -08008562 switch (vmcs_field_width(field)) {
8563 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008564 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008565 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008566 case VMCS_FIELD_WIDTH_U16:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008567 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008568 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008569 case VMCS_FIELD_WIDTH_U32:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008570 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008571 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008572 case VMCS_FIELD_WIDTH_U64:
Nadav Har'El49f705c2011-05-25 23:08:30 +03008573 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008574 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008575 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008576 WARN_ON(1);
8577 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008578 }
8579}
8580
Abel Gordon20b97fe2013-04-18 14:36:25 +03008581
Liran Alone2536742018-06-23 02:35:02 +03008582static inline int vmcs12_write_any(struct vmcs12 *vmcs12,
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008583 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03008584 short offset = vmcs_field_to_offset(field);
Liran Alone2536742018-06-23 02:35:02 +03008585 char *p = (char *)vmcs12 + offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008586 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008587 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008588
Jim Mattsond37f4262017-12-22 12:12:16 -08008589 switch (vmcs_field_width(field)) {
8590 case VMCS_FIELD_WIDTH_U16:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008591 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008592 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008593 case VMCS_FIELD_WIDTH_U32:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008594 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008595 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008596 case VMCS_FIELD_WIDTH_U64:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008597 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008598 return 0;
Jim Mattsond37f4262017-12-22 12:12:16 -08008599 case VMCS_FIELD_WIDTH_NATURAL_WIDTH:
Abel Gordon20b97fe2013-04-18 14:36:25 +03008600 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008601 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008602 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01008603 WARN_ON(1);
8604 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03008605 }
8606
8607}
8608
Jim Mattsonf4160e42018-05-29 09:11:33 -07008609/*
8610 * Copy the writable VMCS shadow fields back to the VMCS12, in case
8611 * they have been modified by the L1 guest. Note that the "read-only"
8612 * VM-exit information fields are actually writable if the vCPU is
8613 * configured to support "VMWRITE to any supported field in the VMCS."
8614 */
Abel Gordon16f5b902013-04-18 14:38:25 +03008615static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
8616{
Jim Mattsonf4160e42018-05-29 09:11:33 -07008617 const u16 *fields[] = {
8618 shadow_read_write_fields,
8619 shadow_read_only_fields
8620 };
8621 const int max_fields[] = {
8622 max_shadow_read_write_fields,
8623 max_shadow_read_only_fields
8624 };
8625 int i, q;
Abel Gordon16f5b902013-04-18 14:38:25 +03008626 unsigned long field;
8627 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008628 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordon16f5b902013-04-18 14:38:25 +03008629
Jan Kiszka282da872014-10-08 18:05:39 +02008630 preempt_disable();
8631
Abel Gordon16f5b902013-04-18 14:38:25 +03008632 vmcs_load(shadow_vmcs);
8633
Jim Mattsonf4160e42018-05-29 09:11:33 -07008634 for (q = 0; q < ARRAY_SIZE(fields); q++) {
8635 for (i = 0; i < max_fields[q]; i++) {
8636 field = fields[q][i];
8637 field_value = __vmcs_readl(field);
Liran Alone2536742018-06-23 02:35:02 +03008638 vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value);
Jim Mattsonf4160e42018-05-29 09:11:33 -07008639 }
8640 /*
8641 * Skip the VM-exit information fields if they are read-only.
8642 */
8643 if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu))
8644 break;
Abel Gordon16f5b902013-04-18 14:38:25 +03008645 }
8646
8647 vmcs_clear(shadow_vmcs);
8648 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02008649
8650 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03008651}
8652
Abel Gordonc3114422013-04-18 14:38:55 +03008653static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
8654{
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008655 const u16 *fields[] = {
Mathias Krausec2bae892013-06-26 20:36:21 +02008656 shadow_read_write_fields,
8657 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03008658 };
Mathias Krausec2bae892013-06-26 20:36:21 +02008659 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03008660 max_shadow_read_write_fields,
8661 max_shadow_read_only_fields
8662 };
8663 int i, q;
8664 unsigned long field;
8665 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07008666 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03008667
8668 vmcs_load(shadow_vmcs);
8669
Mathias Krausec2bae892013-06-26 20:36:21 +02008670 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03008671 for (i = 0; i < max_fields[q]; i++) {
8672 field = fields[q][i];
Liran Alone2536742018-06-23 02:35:02 +03008673 vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value);
Paolo Bonzini44900ba2017-12-13 12:58:02 +01008674 __vmcs_writel(field, field_value);
Abel Gordonc3114422013-04-18 14:38:55 +03008675 }
8676 }
8677
8678 vmcs_clear(shadow_vmcs);
8679 vmcs_load(vmx->loaded_vmcs->vmcs);
8680}
8681
Nadav Har'El49f705c2011-05-25 23:08:30 +03008682/*
8683 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
8684 * used before) all generate the same failure when it is missing.
8685 */
8686static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
8687{
8688 struct vcpu_vmx *vmx = to_vmx(vcpu);
8689 if (vmx->nested.current_vmptr == -1ull) {
8690 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008691 return 0;
8692 }
8693 return 1;
8694}
8695
8696static int handle_vmread(struct kvm_vcpu *vcpu)
8697{
8698 unsigned long field;
8699 u64 field_value;
8700 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8701 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8702 gva_t gva = 0;
Liran Alon6d894f42018-06-23 02:35:09 +03008703 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008704
Kyle Hueyeb277562016-11-29 12:40:39 -08008705 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008706 return 1;
8707
Kyle Huey6affcbe2016-11-29 12:40:40 -08008708 if (!nested_vmx_check_vmcs12(vcpu))
8709 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008710
Liran Alon6d894f42018-06-23 02:35:09 +03008711 if (!is_guest_mode(vcpu))
8712 vmcs12 = get_vmcs12(vcpu);
8713 else {
8714 /*
8715 * When vmcs->vmcs_link_pointer is -1ull, any VMREAD
8716 * to shadowed-field sets the ALU flags for VMfailInvalid.
8717 */
8718 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8719 nested_vmx_failInvalid(vcpu);
8720 return kvm_skip_emulated_instruction(vcpu);
8721 }
8722 vmcs12 = get_shadow_vmcs12(vcpu);
8723 }
8724
Nadav Har'El49f705c2011-05-25 23:08:30 +03008725 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03008726 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03008727 /* Read the field, zero-extended to a u64 field_value */
Liran Alon6d894f42018-06-23 02:35:09 +03008728 if (vmcs12_read_any(vmcs12, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008729 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008730 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008731 }
8732 /*
8733 * Now copy part of this value to register or memory, as requested.
8734 * Note that the number of bits actually copied is 32 or 64 depending
8735 * on the guest's mode (32 or 64 bit), not on the given field's length.
8736 */
8737 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03008738 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03008739 field_value);
8740 } else {
8741 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008742 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008743 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008744 /* _system ok, nested_vmx_check_permission has verified cpl=0 */
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008745 kvm_write_guest_virt_system(vcpu, gva, &field_value,
8746 (is_long_mode(vcpu) ? 8 : 4), NULL);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008747 }
8748
8749 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008750 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008751}
8752
8753
8754static int handle_vmwrite(struct kvm_vcpu *vcpu)
8755{
8756 unsigned long field;
8757 gva_t gva;
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008758 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008759 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
8760 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008761
Nadav Har'El49f705c2011-05-25 23:08:30 +03008762 /* The value to write might be 32 or 64 bits, depending on L1's long
8763 * mode, and eventually we need to write that into a field of several
8764 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08008765 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03008766 * bits into the vmcs12 field.
8767 */
8768 u64 field_value = 0;
8769 struct x86_exception e;
Liran Alon6d894f42018-06-23 02:35:09 +03008770 struct vmcs12 *vmcs12;
Nadav Har'El49f705c2011-05-25 23:08:30 +03008771
Kyle Hueyeb277562016-11-29 12:40:39 -08008772 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008773 return 1;
8774
Kyle Huey6affcbe2016-11-29 12:40:40 -08008775 if (!nested_vmx_check_vmcs12(vcpu))
8776 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08008777
Nadav Har'El49f705c2011-05-25 23:08:30 +03008778 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03008779 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03008780 (((vmx_instruction_info) >> 3) & 0xf));
8781 else {
8782 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008783 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03008784 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008785 if (kvm_read_guest_virt(vcpu, gva, &field_value,
8786 (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008787 kvm_inject_page_fault(vcpu, &e);
8788 return 1;
8789 }
8790 }
8791
8792
Nadav Amit27e6fb52014-06-18 17:19:26 +03008793 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Jim Mattsonf4160e42018-05-29 09:11:33 -07008794 /*
8795 * If the vCPU supports "VMWRITE to any supported field in the
8796 * VMCS," then the "read-only" fields are actually read/write.
8797 */
8798 if (vmcs_field_readonly(field) &&
8799 !nested_cpu_has_vmwrite_any_field(vcpu)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008800 nested_vmx_failValid(vcpu,
8801 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008802 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008803 }
8804
Liran Alon6d894f42018-06-23 02:35:09 +03008805 if (!is_guest_mode(vcpu))
8806 vmcs12 = get_vmcs12(vcpu);
8807 else {
8808 /*
8809 * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE
8810 * to shadowed-field sets the ALU flags for VMfailInvalid.
8811 */
8812 if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) {
8813 nested_vmx_failInvalid(vcpu);
8814 return kvm_skip_emulated_instruction(vcpu);
8815 }
8816 vmcs12 = get_shadow_vmcs12(vcpu);
8817
8818 }
8819
8820 if (vmcs12_write_any(vmcs12, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03008821 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008822 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008823 }
8824
Liran Alon6d894f42018-06-23 02:35:09 +03008825 /*
8826 * Do not track vmcs12 dirty-state if in guest-mode
8827 * as we actually dirty shadow vmcs12 instead of vmcs12.
8828 */
8829 if (!is_guest_mode(vcpu)) {
8830 switch (field) {
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008831#define SHADOW_FIELD_RW(x) case x:
8832#include "vmx_shadow_fields.h"
Liran Alon6d894f42018-06-23 02:35:09 +03008833 /*
8834 * The fields that can be updated by L1 without a vmexit are
8835 * always updated in the vmcs02, the others go down the slow
8836 * path of prepare_vmcs02.
8837 */
8838 break;
8839 default:
8840 vmx->nested.dirty_vmcs12 = true;
8841 break;
8842 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008843 }
8844
Nadav Har'El49f705c2011-05-25 23:08:30 +03008845 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008846 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03008847}
8848
Jim Mattsona8bc2842016-11-30 12:03:44 -08008849static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
8850{
8851 vmx->nested.current_vmptr = vmptr;
8852 if (enable_shadow_vmcs) {
8853 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
8854 SECONDARY_EXEC_SHADOW_VMCS);
8855 vmcs_write64(VMCS_LINK_POINTER,
8856 __pa(vmx->vmcs01.shadow_vmcs));
8857 vmx->nested.sync_shadow_vmcs = true;
8858 }
Paolo Bonzini74a497f2017-12-20 13:55:39 +01008859 vmx->nested.dirty_vmcs12 = true;
Jim Mattsona8bc2842016-11-30 12:03:44 -08008860}
8861
Nadav Har'El63846662011-05-25 23:07:29 +03008862/* Emulate the VMPTRLD instruction */
8863static int handle_vmptrld(struct kvm_vcpu *vcpu)
8864{
8865 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008866 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03008867
8868 if (!nested_vmx_check_permission(vcpu))
8869 return 1;
8870
Radim Krčmářcbf71272017-05-19 15:48:51 +02008871 if (nested_vmx_get_vmptr(vcpu, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03008872 return 1;
8873
Radim Krčmářcbf71272017-05-19 15:48:51 +02008874 if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) {
8875 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
8876 return kvm_skip_emulated_instruction(vcpu);
8877 }
8878
8879 if (vmptr == vmx->nested.vmxon_ptr) {
8880 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER);
8881 return kvm_skip_emulated_instruction(vcpu);
8882 }
8883
Nadav Har'El63846662011-05-25 23:07:29 +03008884 if (vmx->nested.current_vmptr != vmptr) {
8885 struct vmcs12 *new_vmcs12;
8886 struct page *page;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +02008887 page = kvm_vcpu_gpa_to_page(vcpu, vmptr);
8888 if (is_error_page(page)) {
Nadav Har'El63846662011-05-25 23:07:29 +03008889 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008890 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008891 }
8892 new_vmcs12 = kmap(page);
Liran Alon392b2f22018-06-23 02:35:01 +03008893 if (new_vmcs12->hdr.revision_id != VMCS12_REVISION ||
Liran Alonfa97d7d2018-07-18 14:07:59 +02008894 (new_vmcs12->hdr.shadow_vmcs &&
8895 !nested_cpu_has_vmx_shadow_vmcs(vcpu))) {
Nadav Har'El63846662011-05-25 23:07:29 +03008896 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008897 kvm_release_page_clean(page);
Nadav Har'El63846662011-05-25 23:07:29 +03008898 nested_vmx_failValid(vcpu,
8899 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008900 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008901 }
Nadav Har'El63846662011-05-25 23:07:29 +03008902
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02008903 nested_release_vmcs12(vmx);
David Matlack4f2777b2016-07-13 17:16:37 -07008904 /*
8905 * Load VMCS12 from guest memory since it is not already
8906 * cached.
8907 */
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008908 memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE);
8909 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +02008910 kvm_release_page_clean(page);
Paolo Bonzini9f744c52017-07-27 15:54:46 +02008911
Jim Mattsona8bc2842016-11-30 12:03:44 -08008912 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03008913 }
8914
8915 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008916 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03008917}
8918
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008919/* Emulate the VMPTRST instruction */
8920static int handle_vmptrst(struct kvm_vcpu *vcpu)
8921{
Sean Christopherson0a06d422018-07-19 10:31:00 -07008922 unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION);
8923 u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8924 gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008925 struct x86_exception e;
Sean Christopherson0a06d422018-07-19 10:31:00 -07008926 gva_t gva;
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008927
8928 if (!nested_vmx_check_permission(vcpu))
8929 return 1;
8930
Sean Christopherson0a06d422018-07-19 10:31:00 -07008931 if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008932 return 1;
Felix Wilhelm727ba742018-06-11 09:43:44 +02008933 /* *_system ok, nested_vmx_check_permission has verified cpl=0 */
Sean Christopherson0a06d422018-07-19 10:31:00 -07008934 if (kvm_write_guest_virt_system(vcpu, gva, (void *)&current_vmptr,
8935 sizeof(gpa_t), &e)) {
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008936 kvm_inject_page_fault(vcpu, &e);
8937 return 1;
8938 }
8939 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008940 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03008941}
8942
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008943/* Emulate the INVEPT instruction */
8944static int handle_invept(struct kvm_vcpu *vcpu)
8945{
Wincy Vanb9c237b2015-02-03 23:56:30 +08008946 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008947 u32 vmx_instruction_info, types;
8948 unsigned long type;
8949 gva_t gva;
8950 struct x86_exception e;
8951 struct {
8952 u64 eptp, gpa;
8953 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008954
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008955 if (!(vmx->nested.msrs.secondary_ctls_high &
Wincy Vanb9c237b2015-02-03 23:56:30 +08008956 SECONDARY_EXEC_ENABLE_EPT) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008957 !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008958 kvm_queue_exception(vcpu, UD_VECTOR);
8959 return 1;
8960 }
8961
8962 if (!nested_vmx_check_permission(vcpu))
8963 return 1;
8964
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008965 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03008966 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008967
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01008968 types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008969
Jim Mattson85c856b2016-10-26 08:38:38 -07008970 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008971 nested_vmx_failValid(vcpu,
8972 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08008973 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008974 }
8975
8976 /* According to the Intel VMX instruction reference, the memory
8977 * operand is read even if it isn't needed (e.g., for type==global)
8978 */
8979 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00008980 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008981 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02008982 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008983 kvm_inject_page_fault(vcpu, &e);
8984 return 1;
8985 }
8986
8987 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008988 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04008989 /*
8990 * TODO: track mappings and invalidate
8991 * single context requests appropriately
8992 */
8993 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008994 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04008995 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03008996 nested_vmx_succeed(vcpu);
8997 break;
8998 default:
8999 BUG_ON(1);
9000 break;
9001 }
9002
Kyle Huey6affcbe2016-11-29 12:40:40 -08009003 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009004}
9005
Liran Alon3d5bdae2018-10-08 23:42:18 +03009006static u16 nested_get_vpid02(struct kvm_vcpu *vcpu)
9007{
9008 struct vcpu_vmx *vmx = to_vmx(vcpu);
9009
9010 return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid;
9011}
9012
Petr Matouseka642fc32014-09-23 20:22:30 +02009013static int handle_invvpid(struct kvm_vcpu *vcpu)
9014{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009015 struct vcpu_vmx *vmx = to_vmx(vcpu);
9016 u32 vmx_instruction_info;
9017 unsigned long type, types;
9018 gva_t gva;
9019 struct x86_exception e;
Jim Mattson40352602017-06-28 09:37:37 -07009020 struct {
9021 u64 vpid;
9022 u64 gla;
9023 } operand;
Liran Alon3d5bdae2018-10-08 23:42:18 +03009024 u16 vpid02;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009025
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009026 if (!(vmx->nested.msrs.secondary_ctls_high &
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009027 SECONDARY_EXEC_ENABLE_VPID) ||
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009028 !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009029 kvm_queue_exception(vcpu, UD_VECTOR);
9030 return 1;
9031 }
9032
9033 if (!nested_vmx_check_permission(vcpu))
9034 return 1;
9035
9036 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9037 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9038
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009039 types = (vmx->nested.msrs.vpid_caps &
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009040 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009041
Jim Mattson85c856b2016-10-26 08:38:38 -07009042 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009043 nested_vmx_failValid(vcpu,
9044 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009045 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009046 }
9047
9048 /* according to the intel vmx instruction reference, the memory
9049 * operand is read even if it isn't needed (e.g., for type==global)
9050 */
9051 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9052 vmx_instruction_info, false, &gva))
9053 return 1;
Paolo Bonzinice14e868a2018-06-06 17:37:49 +02009054 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009055 kvm_inject_page_fault(vcpu, &e);
9056 return 1;
9057 }
Jim Mattson40352602017-06-28 09:37:37 -07009058 if (operand.vpid >> 16) {
9059 nested_vmx_failValid(vcpu,
9060 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9061 return kvm_skip_emulated_instruction(vcpu);
9062 }
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009063
Liran Alon3d5bdae2018-10-08 23:42:18 +03009064 vpid02 = nested_get_vpid02(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009065 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009066 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Liran Aloncd9a4912018-05-22 17:16:15 +03009067 if (!operand.vpid ||
9068 is_noncanonical_address(operand.gla, vcpu)) {
Jim Mattson40352602017-06-28 09:37:37 -07009069 nested_vmx_failValid(vcpu,
9070 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
9071 return kvm_skip_emulated_instruction(vcpu);
9072 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009073 if (cpu_has_vmx_invvpid_individual_addr()) {
Liran Aloncd9a4912018-05-22 17:16:15 +03009074 __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR,
Liran Alon3d5bdae2018-10-08 23:42:18 +03009075 vpid02, operand.gla);
Liran Aloncd9a4912018-05-22 17:16:15 +03009076 } else
Liran Alon3d5bdae2018-10-08 23:42:18 +03009077 __vmx_flush_tlb(vcpu, vpid02, true);
Liran Aloncd9a4912018-05-22 17:16:15 +03009078 break;
Paolo Bonzinief697a72016-03-18 16:58:38 +01009079 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009080 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
Jim Mattson40352602017-06-28 09:37:37 -07009081 if (!operand.vpid) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009082 nested_vmx_failValid(vcpu,
9083 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009084 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009085 }
Liran Alon3d5bdae2018-10-08 23:42:18 +03009086 __vmx_flush_tlb(vcpu, vpid02, true);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009087 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009088 case VMX_VPID_EXTENT_ALL_CONTEXT:
Liran Alon3d5bdae2018-10-08 23:42:18 +03009089 __vmx_flush_tlb(vcpu, vpid02, true);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009090 break;
9091 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009092 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08009093 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07009094 }
9095
Jan Dakinevichbcdde302016-10-28 07:00:30 +03009096 nested_vmx_succeed(vcpu);
9097
Kyle Huey6affcbe2016-11-29 12:40:40 -08009098 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02009099}
9100
Junaid Shahideb4b2482018-06-27 14:59:14 -07009101static int handle_invpcid(struct kvm_vcpu *vcpu)
9102{
9103 u32 vmx_instruction_info;
9104 unsigned long type;
9105 bool pcid_enabled;
9106 gva_t gva;
9107 struct x86_exception e;
Junaid Shahidb94742c2018-06-27 14:59:20 -07009108 unsigned i;
9109 unsigned long roots_to_free = 0;
Junaid Shahideb4b2482018-06-27 14:59:14 -07009110 struct {
9111 u64 pcid;
9112 u64 gla;
9113 } operand;
9114
9115 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
9116 kvm_queue_exception(vcpu, UD_VECTOR);
9117 return 1;
9118 }
9119
9120 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9121 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
9122
9123 if (type > 3) {
9124 kvm_inject_gp(vcpu, 0);
9125 return 1;
9126 }
9127
9128 /* According to the Intel instruction reference, the memory operand
9129 * is read even if it isn't needed (e.g., for type==all)
9130 */
9131 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
9132 vmx_instruction_info, false, &gva))
9133 return 1;
9134
9135 if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
9136 kvm_inject_page_fault(vcpu, &e);
9137 return 1;
9138 }
9139
9140 if (operand.pcid >> 12 != 0) {
9141 kvm_inject_gp(vcpu, 0);
9142 return 1;
9143 }
9144
9145 pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
9146
9147 switch (type) {
9148 case INVPCID_TYPE_INDIV_ADDR:
9149 if ((!pcid_enabled && (operand.pcid != 0)) ||
9150 is_noncanonical_address(operand.gla, vcpu)) {
9151 kvm_inject_gp(vcpu, 0);
9152 return 1;
9153 }
9154 kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
9155 return kvm_skip_emulated_instruction(vcpu);
9156
9157 case INVPCID_TYPE_SINGLE_CTXT:
9158 if (!pcid_enabled && (operand.pcid != 0)) {
9159 kvm_inject_gp(vcpu, 0);
9160 return 1;
9161 }
9162
9163 if (kvm_get_active_pcid(vcpu) == operand.pcid) {
9164 kvm_mmu_sync_roots(vcpu);
9165 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
9166 }
9167
Junaid Shahidb94742c2018-06-27 14:59:20 -07009168 for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
9169 if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3)
9170 == operand.pcid)
9171 roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
Junaid Shahidade61e22018-06-27 14:59:15 -07009172
Junaid Shahidb94742c2018-06-27 14:59:20 -07009173 kvm_mmu_free_roots(vcpu, roots_to_free);
Junaid Shahideb4b2482018-06-27 14:59:14 -07009174 /*
Junaid Shahidb94742c2018-06-27 14:59:20 -07009175 * If neither the current cr3 nor any of the prev_roots use the
Junaid Shahidade61e22018-06-27 14:59:15 -07009176 * given PCID, then nothing needs to be done here because a
9177 * resync will happen anyway before switching to any other CR3.
Junaid Shahideb4b2482018-06-27 14:59:14 -07009178 */
9179
9180 return kvm_skip_emulated_instruction(vcpu);
9181
9182 case INVPCID_TYPE_ALL_NON_GLOBAL:
9183 /*
9184 * Currently, KVM doesn't mark global entries in the shadow
9185 * page tables, so a non-global flush just degenerates to a
9186 * global flush. If needed, we could optimize this later by
9187 * keeping track of global entries in shadow page tables.
9188 */
9189
9190 /* fall-through */
9191 case INVPCID_TYPE_ALL_INCL_GLOBAL:
9192 kvm_mmu_unload(vcpu);
9193 return kvm_skip_emulated_instruction(vcpu);
9194
9195 default:
9196 BUG(); /* We have already checked above that type <= 3 */
9197 }
9198}
9199
Kai Huang843e4332015-01-28 10:54:28 +08009200static int handle_pml_full(struct kvm_vcpu *vcpu)
9201{
9202 unsigned long exit_qualification;
9203
9204 trace_kvm_pml_full(vcpu->vcpu_id);
9205
9206 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9207
9208 /*
9209 * PML buffer FULL happened while executing iret from NMI,
9210 * "blocked by NMI" bit has to be set before next VM entry.
9211 */
9212 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Paolo Bonzinid02fcf52017-11-06 13:31:13 +01009213 enable_vnmi &&
Kai Huang843e4332015-01-28 10:54:28 +08009214 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
9215 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
9216 GUEST_INTR_STATE_NMI);
9217
9218 /*
9219 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
9220 * here.., and there's no userspace involvement needed for PML.
9221 */
9222 return 1;
9223}
9224
Yunhong Jiang64672c92016-06-13 14:19:59 -07009225static int handle_preemption_timer(struct kvm_vcpu *vcpu)
9226{
Sean Christophersond264ee02018-08-27 15:21:12 -07009227 if (!to_vmx(vcpu)->req_immediate_exit)
9228 kvm_lapic_expired_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -07009229 return 1;
9230}
9231
Bandan Das41ab9372017-08-03 15:54:43 -04009232static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address)
9233{
9234 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das41ab9372017-08-03 15:54:43 -04009235 int maxphyaddr = cpuid_maxphyaddr(vcpu);
9236
9237 /* Check for memory type validity */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009238 switch (address & VMX_EPTP_MT_MASK) {
9239 case VMX_EPTP_MT_UC:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009240 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009241 return false;
9242 break;
David Hildenbrandbb97a012017-08-10 23:15:28 +02009243 case VMX_EPTP_MT_WB:
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009244 if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009245 return false;
9246 break;
9247 default:
9248 return false;
9249 }
9250
David Hildenbrandbb97a012017-08-10 23:15:28 +02009251 /* only 4 levels page-walk length are valid */
9252 if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4)
Bandan Das41ab9372017-08-03 15:54:43 -04009253 return false;
9254
9255 /* Reserved bits should not be set */
9256 if (address >> maxphyaddr || ((address >> 7) & 0x1f))
9257 return false;
9258
9259 /* AD, if set, should be supported */
David Hildenbrandbb97a012017-08-10 23:15:28 +02009260 if (address & VMX_EPTP_AD_ENABLE_BIT) {
Paolo Bonzini6677f3d2018-02-26 13:40:08 +01009261 if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT))
Bandan Das41ab9372017-08-03 15:54:43 -04009262 return false;
9263 }
9264
9265 return true;
9266}
9267
9268static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,
9269 struct vmcs12 *vmcs12)
9270{
9271 u32 index = vcpu->arch.regs[VCPU_REGS_RCX];
9272 u64 address;
9273 bool accessed_dirty;
9274 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
9275
9276 if (!nested_cpu_has_eptp_switching(vmcs12) ||
9277 !nested_cpu_has_ept(vmcs12))
9278 return 1;
9279
9280 if (index >= VMFUNC_EPTP_ENTRIES)
9281 return 1;
9282
9283
9284 if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT,
9285 &address, index * 8, 8))
9286 return 1;
9287
David Hildenbrandbb97a012017-08-10 23:15:28 +02009288 accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT);
Bandan Das41ab9372017-08-03 15:54:43 -04009289
9290 /*
9291 * If the (L2) guest does a vmfunc to the currently
9292 * active ept pointer, we don't have to do anything else
9293 */
9294 if (vmcs12->ept_pointer != address) {
9295 if (!valid_ept_address(vcpu, address))
9296 return 1;
9297
9298 kvm_mmu_unload(vcpu);
9299 mmu->ept_ad = accessed_dirty;
9300 mmu->base_role.ad_disabled = !accessed_dirty;
9301 vmcs12->ept_pointer = address;
9302 /*
9303 * TODO: Check what's the correct approach in case
9304 * mmu reload fails. Currently, we just let the next
9305 * reload potentially fail
9306 */
9307 kvm_mmu_reload(vcpu);
9308 }
9309
9310 return 0;
9311}
9312
Bandan Das2a499e42017-08-03 15:54:41 -04009313static int handle_vmfunc(struct kvm_vcpu *vcpu)
9314{
Bandan Das27c42a12017-08-03 15:54:42 -04009315 struct vcpu_vmx *vmx = to_vmx(vcpu);
9316 struct vmcs12 *vmcs12;
9317 u32 function = vcpu->arch.regs[VCPU_REGS_RAX];
9318
9319 /*
9320 * VMFUNC is only supported for nested guests, but we always enable the
9321 * secondary control for simplicity; for non-nested mode, fake that we
9322 * didn't by injecting #UD.
9323 */
9324 if (!is_guest_mode(vcpu)) {
9325 kvm_queue_exception(vcpu, UD_VECTOR);
9326 return 1;
9327 }
9328
9329 vmcs12 = get_vmcs12(vcpu);
9330 if ((vmcs12->vm_function_control & (1 << function)) == 0)
9331 goto fail;
Bandan Das41ab9372017-08-03 15:54:43 -04009332
9333 switch (function) {
9334 case 0:
9335 if (nested_vmx_eptp_switching(vcpu, vmcs12))
9336 goto fail;
9337 break;
9338 default:
9339 goto fail;
9340 }
9341 return kvm_skip_emulated_instruction(vcpu);
Bandan Das27c42a12017-08-03 15:54:42 -04009342
9343fail:
9344 nested_vmx_vmexit(vcpu, vmx->exit_reason,
9345 vmcs_read32(VM_EXIT_INTR_INFO),
9346 vmcs_readl(EXIT_QUALIFICATION));
Bandan Das2a499e42017-08-03 15:54:41 -04009347 return 1;
9348}
9349
Sean Christopherson0b665d32018-08-14 09:33:34 -07009350static int handle_encls(struct kvm_vcpu *vcpu)
9351{
9352 /*
9353 * SGX virtualization is not yet supported. There is no software
9354 * enable bit for SGX, so we have to trap ENCLS and inject a #UD
9355 * to prevent the guest from executing ENCLS.
9356 */
9357 kvm_queue_exception(vcpu, UD_VECTOR);
9358 return 1;
9359}
9360
Nadav Har'El0140cae2011-05-25 23:06:28 +03009361/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08009362 * The exit handlers return 1 if the exit was handled fully and guest execution
9363 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
9364 * to be done to userspace and return 0.
9365 */
Mathias Krause772e0312012-08-30 01:30:19 +02009366static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009367 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
9368 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08009369 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08009370 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009371 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009372 [EXIT_REASON_CR_ACCESS] = handle_cr,
9373 [EXIT_REASON_DR_ACCESS] = handle_dr,
9374 [EXIT_REASON_CPUID] = handle_cpuid,
9375 [EXIT_REASON_MSR_READ] = handle_rdmsr,
9376 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
9377 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
9378 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02009379 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03009380 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02009381 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02009382 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03009383 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009384 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03009385 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03009386 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009387 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009388 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03009389 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03009390 [EXIT_REASON_VMOFF] = handle_vmoff,
9391 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08009392 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
9393 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08009394 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009395 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02009396 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08009397 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02009398 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08009399 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Paolo Bonzini0367f202016-07-12 10:44:55 +02009400 [EXIT_REASON_GDTR_IDTR] = handle_desc,
9401 [EXIT_REASON_LDTR_TR] = handle_desc,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03009402 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
9403 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08009404 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009405 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009406 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04009407 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03009408 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02009409 [EXIT_REASON_INVVPID] = handle_invvpid,
Jim Mattson45ec3682017-08-23 16:32:04 -07009410 [EXIT_REASON_RDRAND] = handle_invalid_op,
Jim Mattson75f4fc82017-08-23 16:32:03 -07009411 [EXIT_REASON_RDSEED] = handle_invalid_op,
Wanpeng Lif53cd632014-12-02 19:14:58 +08009412 [EXIT_REASON_XSAVES] = handle_xsaves,
9413 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08009414 [EXIT_REASON_PML_FULL] = handle_pml_full,
Junaid Shahideb4b2482018-06-27 14:59:14 -07009415 [EXIT_REASON_INVPCID] = handle_invpcid,
Bandan Das2a499e42017-08-03 15:54:41 -04009416 [EXIT_REASON_VMFUNC] = handle_vmfunc,
Yunhong Jiang64672c92016-06-13 14:19:59 -07009417 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Sean Christopherson0b665d32018-08-14 09:33:34 -07009418 [EXIT_REASON_ENCLS] = handle_encls,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009419};
9420
9421static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04009422 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009423
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009424static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
9425 struct vmcs12 *vmcs12)
9426{
9427 unsigned long exit_qualification;
9428 gpa_t bitmap, last_bitmap;
9429 unsigned int port;
9430 int size;
9431 u8 b;
9432
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009433 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05009434 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009435
9436 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9437
9438 port = exit_qualification >> 16;
9439 size = (exit_qualification & 7) + 1;
9440
9441 last_bitmap = (gpa_t)-1;
9442 b = -1;
9443
9444 while (size > 0) {
9445 if (port < 0x8000)
9446 bitmap = vmcs12->io_bitmap_a;
9447 else if (port < 0x10000)
9448 bitmap = vmcs12->io_bitmap_b;
9449 else
Joe Perches1d804d02015-03-30 16:46:09 -07009450 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009451 bitmap += (port & 0x7fff) / 8;
9452
9453 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009454 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009455 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009456 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07009457 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009458
9459 port++;
9460 size--;
9461 last_bitmap = bitmap;
9462 }
9463
Joe Perches1d804d02015-03-30 16:46:09 -07009464 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009465}
9466
Nadav Har'El644d7112011-05-25 23:12:35 +03009467/*
9468 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
9469 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
9470 * disinterest in the current event (read or write a specific MSR) by using an
9471 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
9472 */
9473static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
9474 struct vmcs12 *vmcs12, u32 exit_reason)
9475{
9476 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
9477 gpa_t bitmap;
9478
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01009479 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07009480 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009481
9482 /*
9483 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
9484 * for the four combinations of read/write and low/high MSR numbers.
9485 * First we need to figure out which of the four to use:
9486 */
9487 bitmap = vmcs12->msr_bitmap;
9488 if (exit_reason == EXIT_REASON_MSR_WRITE)
9489 bitmap += 2048;
9490 if (msr_index >= 0xc0000000) {
9491 msr_index -= 0xc0000000;
9492 bitmap += 1024;
9493 }
9494
9495 /* Then read the msr_index'th bit from this bitmap: */
9496 if (msr_index < 1024*8) {
9497 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009498 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07009499 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009500 return 1 & (b >> (msr_index & 7));
9501 } else
Joe Perches1d804d02015-03-30 16:46:09 -07009502 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03009503}
9504
9505/*
9506 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
9507 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
9508 * intercept (via guest_host_mask etc.) the current event.
9509 */
9510static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
9511 struct vmcs12 *vmcs12)
9512{
9513 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
9514 int cr = exit_qualification & 15;
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009515 int reg;
9516 unsigned long val;
Nadav Har'El644d7112011-05-25 23:12:35 +03009517
9518 switch ((exit_qualification >> 4) & 3) {
9519 case 0: /* mov to cr */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009520 reg = (exit_qualification >> 8) & 15;
9521 val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03009522 switch (cr) {
9523 case 0:
9524 if (vmcs12->cr0_guest_host_mask &
9525 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009526 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009527 break;
9528 case 3:
9529 if ((vmcs12->cr3_target_count >= 1 &&
9530 vmcs12->cr3_target_value0 == val) ||
9531 (vmcs12->cr3_target_count >= 2 &&
9532 vmcs12->cr3_target_value1 == val) ||
9533 (vmcs12->cr3_target_count >= 3 &&
9534 vmcs12->cr3_target_value2 == val) ||
9535 (vmcs12->cr3_target_count >= 4 &&
9536 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07009537 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009538 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009539 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009540 break;
9541 case 4:
9542 if (vmcs12->cr4_guest_host_mask &
9543 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07009544 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009545 break;
9546 case 8:
9547 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07009548 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009549 break;
9550 }
9551 break;
9552 case 2: /* clts */
9553 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
9554 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009555 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009556 break;
9557 case 1: /* mov from cr */
9558 switch (cr) {
9559 case 3:
9560 if (vmcs12->cpu_based_vm_exec_control &
9561 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009562 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009563 break;
9564 case 8:
9565 if (vmcs12->cpu_based_vm_exec_control &
9566 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07009567 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009568 break;
9569 }
9570 break;
9571 case 3: /* lmsw */
9572 /*
9573 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
9574 * cr0. Other attempted changes are ignored, with no exit.
9575 */
Jan H. Schönherre1d39b12017-05-20 13:22:56 +02009576 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Nadav Har'El644d7112011-05-25 23:12:35 +03009577 if (vmcs12->cr0_guest_host_mask & 0xe &
9578 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07009579 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009580 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
9581 !(vmcs12->cr0_read_shadow & 0x1) &&
9582 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07009583 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009584 break;
9585 }
Joe Perches1d804d02015-03-30 16:46:09 -07009586 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009587}
9588
Liran Alona7cde482018-06-23 02:35:10 +03009589static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu,
9590 struct vmcs12 *vmcs12, gpa_t bitmap)
9591{
9592 u32 vmx_instruction_info;
9593 unsigned long field;
9594 u8 b;
9595
9596 if (!nested_cpu_has_shadow_vmcs(vmcs12))
9597 return true;
9598
9599 /* Decode instruction info and find the field to access */
9600 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9601 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
9602
9603 /* Out-of-range fields always cause a VM exit from L2 to L1 */
9604 if (field >> 15)
9605 return true;
9606
9607 if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1))
9608 return true;
9609
9610 return 1 & (b >> (field & 7));
9611}
9612
Nadav Har'El644d7112011-05-25 23:12:35 +03009613/*
9614 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
9615 * should handle it ourselves in L0 (and then continue L2). Only call this
9616 * when in is_guest_mode (L2).
9617 */
Paolo Bonzini7313c692017-07-27 10:31:25 +02009618static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason)
Nadav Har'El644d7112011-05-25 23:12:35 +03009619{
Nadav Har'El644d7112011-05-25 23:12:35 +03009620 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9621 struct vcpu_vmx *vmx = to_vmx(vcpu);
9622 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9623
Jim Mattson4f350c62017-09-14 16:31:44 -07009624 if (vmx->nested.nested_run_pending)
9625 return false;
9626
9627 if (unlikely(vmx->fail)) {
9628 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
9629 vmcs_read32(VM_INSTRUCTION_ERROR));
9630 return true;
9631 }
Jan Kiszka542060e2014-01-04 18:47:21 +01009632
David Matlackc9f04402017-08-01 14:00:40 -07009633 /*
9634 * The host physical addresses of some pages of guest memory
Jim Mattsonde3a0022017-11-27 17:22:25 -06009635 * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC
9636 * Page). The CPU may write to these pages via their host
9637 * physical address while L2 is running, bypassing any
9638 * address-translation-based dirty tracking (e.g. EPT write
9639 * protection).
David Matlackc9f04402017-08-01 14:00:40 -07009640 *
9641 * Mark them dirty on every exit from L2 to prevent them from
9642 * getting out of sync with dirty tracking.
9643 */
9644 nested_mark_vmcs12_pages_dirty(vcpu);
9645
Jim Mattson4f350c62017-09-14 16:31:44 -07009646 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
9647 vmcs_readl(EXIT_QUALIFICATION),
9648 vmx->idt_vectoring_info,
9649 intr_info,
9650 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
9651 KVM_ISA_VMX);
Nadav Har'El644d7112011-05-25 23:12:35 +03009652
9653 switch (exit_reason) {
9654 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08009655 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07009656 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009657 else if (is_page_fault(intr_info))
Wanpeng Li52a5c152017-07-13 18:30:42 -07009658 return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01009659 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01009660 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07009661 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01009662 else if (is_debug(intr_info) &&
9663 vcpu->guest_debug &
9664 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
9665 return false;
9666 else if (is_breakpoint(intr_info) &&
9667 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
9668 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009669 return vmcs12->exception_bitmap &
9670 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
9671 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07009672 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009673 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07009674 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009675 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009676 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009677 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02009678 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009679 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07009680 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009681 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07009682 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009683 case EXIT_REASON_HLT:
9684 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
9685 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07009686 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009687 case EXIT_REASON_INVLPG:
9688 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
9689 case EXIT_REASON_RDPMC:
9690 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009691 case EXIT_REASON_RDRAND:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009692 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02009693 case EXIT_REASON_RDSEED:
David Hildenbrand736fdf72017-08-24 20:51:37 +02009694 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009695 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03009696 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
Liran Alona7cde482018-06-23 02:35:10 +03009697 case EXIT_REASON_VMREAD:
9698 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9699 vmcs12->vmread_bitmap);
9700 case EXIT_REASON_VMWRITE:
9701 return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12,
9702 vmcs12->vmwrite_bitmap);
Nadav Har'El644d7112011-05-25 23:12:35 +03009703 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
9704 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
Liran Alona7cde482018-06-23 02:35:10 +03009705 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME:
Nadav Har'El644d7112011-05-25 23:12:35 +03009706 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02009707 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03009708 /*
9709 * VMX instructions trap unconditionally. This allows L1 to
9710 * emulate them for its L2 guest, i.e., allows 3-level nesting!
9711 */
Joe Perches1d804d02015-03-30 16:46:09 -07009712 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009713 case EXIT_REASON_CR_ACCESS:
9714 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
9715 case EXIT_REASON_DR_ACCESS:
9716 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
9717 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01009718 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02009719 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
9720 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03009721 case EXIT_REASON_MSR_READ:
9722 case EXIT_REASON_MSR_WRITE:
9723 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
9724 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07009725 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009726 case EXIT_REASON_MWAIT_INSTRUCTION:
9727 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03009728 case EXIT_REASON_MONITOR_TRAP_FLAG:
9729 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03009730 case EXIT_REASON_MONITOR_INSTRUCTION:
9731 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
9732 case EXIT_REASON_PAUSE_INSTRUCTION:
9733 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
9734 nested_cpu_has2(vmcs12,
9735 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
9736 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07009737 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009738 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009739 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03009740 case EXIT_REASON_APIC_ACCESS:
Wincy Van82f0dd42015-02-03 23:57:18 +08009741 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08009742 case EXIT_REASON_EOI_INDUCED:
Jim Mattsonab5df312018-05-09 17:02:03 -04009743 /*
9744 * The controls for "virtualize APIC accesses," "APIC-
9745 * register virtualization," and "virtual-interrupt
9746 * delivery" only come from vmcs12.
9747 */
Joe Perches1d804d02015-03-30 16:46:09 -07009748 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009749 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009750 /*
9751 * L0 always deals with the EPT violation. If nested EPT is
9752 * used, and the nested mmu code discovers that the address is
9753 * missing in the guest EPT table (EPT12), the EPT violation
9754 * will be injected with nested_ept_inject_page_fault()
9755 */
Joe Perches1d804d02015-03-30 16:46:09 -07009756 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009757 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03009758 /*
9759 * L2 never uses directly L1's EPT, but rather L0's own EPT
9760 * table (shadow on EPT) or a merged EPT table that L0 built
9761 * (EPT on EPT). So any problems with the structure of the
9762 * table is L0's fault.
9763 */
Joe Perches1d804d02015-03-30 16:46:09 -07009764 return false;
Paolo Bonzini90a2db62017-07-27 13:22:13 +02009765 case EXIT_REASON_INVPCID:
9766 return
9767 nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) &&
9768 nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
Nadav Har'El644d7112011-05-25 23:12:35 +03009769 case EXIT_REASON_WBINVD:
9770 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
9771 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07009772 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009773 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
9774 /*
9775 * This should never happen, since it is not possible to
9776 * set XSS to a non-zero value---neither in L1 nor in L2.
9777 * If if it were, XSS would have to be checked against
9778 * the XSS exit bitmap in vmcs12.
9779 */
9780 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08009781 case EXIT_REASON_PREEMPTION_TIMER:
9782 return false;
Ladi Prosekab007cc2017-03-31 10:19:26 +02009783 case EXIT_REASON_PML_FULL:
Bandan Das03efce62017-05-05 15:25:15 -04009784 /* We emulate PML support to L1. */
Ladi Prosekab007cc2017-03-31 10:19:26 +02009785 return false;
Bandan Das2a499e42017-08-03 15:54:41 -04009786 case EXIT_REASON_VMFUNC:
9787 /* VM functions are emulated through L2->L0 vmexits. */
9788 return false;
Sean Christopherson0b665d32018-08-14 09:33:34 -07009789 case EXIT_REASON_ENCLS:
9790 /* SGX is never exposed to L1 */
9791 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03009792 default:
Joe Perches1d804d02015-03-30 16:46:09 -07009793 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03009794 }
9795}
9796
Paolo Bonzini7313c692017-07-27 10:31:25 +02009797static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason)
9798{
9799 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
9800
9801 /*
9802 * At this point, the exit interruption info in exit_intr_info
9803 * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT
9804 * we need to query the in-kernel LAPIC.
9805 */
9806 WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT);
9807 if ((exit_intr_info &
9808 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9809 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) {
9810 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9811 vmcs12->vm_exit_intr_error_code =
9812 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
9813 }
9814
9815 nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info,
9816 vmcs_readl(EXIT_QUALIFICATION));
9817 return 1;
9818}
9819
Avi Kivity586f9602010-11-18 13:09:54 +02009820static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
9821{
9822 *info1 = vmcs_readl(EXIT_QUALIFICATION);
9823 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
9824}
9825
Kai Huanga3eaa862015-11-04 13:46:05 +08009826static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08009827{
Kai Huanga3eaa862015-11-04 13:46:05 +08009828 if (vmx->pml_pg) {
9829 __free_page(vmx->pml_pg);
9830 vmx->pml_pg = NULL;
9831 }
Kai Huang843e4332015-01-28 10:54:28 +08009832}
9833
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009834static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08009835{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08009837 u64 *pml_buf;
9838 u16 pml_idx;
9839
9840 pml_idx = vmcs_read16(GUEST_PML_INDEX);
9841
9842 /* Do nothing if PML buffer is empty */
9843 if (pml_idx == (PML_ENTITY_NUM - 1))
9844 return;
9845
9846 /* PML index always points to next available PML buffer entity */
9847 if (pml_idx >= PML_ENTITY_NUM)
9848 pml_idx = 0;
9849 else
9850 pml_idx++;
9851
9852 pml_buf = page_address(vmx->pml_pg);
9853 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
9854 u64 gpa;
9855
9856 gpa = pml_buf[pml_idx];
9857 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009858 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08009859 }
9860
9861 /* reset PML index */
9862 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
9863}
9864
9865/*
9866 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
9867 * Called before reporting dirty_bitmap to userspace.
9868 */
9869static void kvm_flush_pml_buffers(struct kvm *kvm)
9870{
9871 int i;
9872 struct kvm_vcpu *vcpu;
9873 /*
9874 * We only need to kick vcpu out of guest mode here, as PML buffer
9875 * is flushed at beginning of all VMEXITs, and it's obvious that only
9876 * vcpus running in guest are possible to have unflushed GPAs in PML
9877 * buffer.
9878 */
9879 kvm_for_each_vcpu(i, vcpu, kvm)
9880 kvm_vcpu_kick(vcpu);
9881}
9882
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009883static void vmx_dump_sel(char *name, uint32_t sel)
9884{
9885 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05009886 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009887 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
9888 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
9889 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
9890}
9891
9892static void vmx_dump_dtsel(char *name, uint32_t limit)
9893{
9894 pr_err("%s limit=0x%08x, base=0x%016lx\n",
9895 name, vmcs_read32(limit),
9896 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
9897}
9898
9899static void dump_vmcs(void)
9900{
9901 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
9902 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
9903 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
9904 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
9905 u32 secondary_exec_control = 0;
9906 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01009907 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009908 int i, n;
9909
9910 if (cpu_has_secondary_exec_ctrls())
9911 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9912
9913 pr_err("*** Guest State ***\n");
9914 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9915 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
9916 vmcs_readl(CR0_GUEST_HOST_MASK));
9917 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
9918 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
9919 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
9920 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
9921 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
9922 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009923 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
9924 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
9925 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
9926 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009927 }
9928 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
9929 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
9930 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
9931 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
9932 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9933 vmcs_readl(GUEST_SYSENTER_ESP),
9934 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
9935 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
9936 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
9937 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
9938 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
9939 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
9940 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
9941 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
9942 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
9943 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
9944 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
9945 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
9946 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009947 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9948 efer, vmcs_read64(GUEST_IA32_PAT));
9949 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
9950 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009951 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009952 if (cpu_has_load_perf_global_ctrl &&
9953 vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009954 pr_err("PerfGlobCtl = 0x%016llx\n",
9955 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009956 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009957 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009958 pr_err("Interruptibility = %08x ActivityState = %08x\n",
9959 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
9960 vmcs_read32(GUEST_ACTIVITY_STATE));
9961 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
9962 pr_err("InterruptStatus = %04x\n",
9963 vmcs_read16(GUEST_INTR_STATUS));
9964
9965 pr_err("*** Host State ***\n");
9966 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
9967 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
9968 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
9969 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
9970 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
9971 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
9972 vmcs_read16(HOST_TR_SELECTOR));
9973 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
9974 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
9975 vmcs_readl(HOST_TR_BASE));
9976 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
9977 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
9978 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
9979 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
9980 vmcs_readl(HOST_CR4));
9981 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
9982 vmcs_readl(HOST_IA32_SYSENTER_ESP),
9983 vmcs_read32(HOST_IA32_SYSENTER_CS),
9984 vmcs_readl(HOST_IA32_SYSENTER_EIP));
9985 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009986 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
9987 vmcs_read64(HOST_IA32_EFER),
9988 vmcs_read64(HOST_IA32_PAT));
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +01009989 if (cpu_has_load_perf_global_ctrl &&
9990 vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01009991 pr_err("PerfGlobCtl = 0x%016llx\n",
9992 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02009993
9994 pr_err("*** Control State ***\n");
9995 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
9996 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
9997 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
9998 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
9999 vmcs_read32(EXCEPTION_BITMAP),
10000 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
10001 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
10002 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
10003 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10004 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
10005 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
10006 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
10007 vmcs_read32(VM_EXIT_INTR_INFO),
10008 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
10009 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
10010 pr_err(" reason=%08x qualification=%016lx\n",
10011 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
10012 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
10013 vmcs_read32(IDT_VECTORING_INFO_FIELD),
10014 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010015 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +080010016 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010017 pr_err("TSC Multiplier = 0x%016llx\n",
10018 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010019 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
10020 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
10021 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
10022 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
10023 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +010010024 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010025 n = vmcs_read32(CR3_TARGET_COUNT);
10026 for (i = 0; i + 1 < n; i += 4)
10027 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
10028 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
10029 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
10030 if (i < n)
10031 pr_err("CR3 target%u=%016lx\n",
10032 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
10033 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
10034 pr_err("PLE Gap=%08x Window=%08x\n",
10035 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
10036 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
10037 pr_err("Virtual processor ID = 0x%04x\n",
10038 vmcs_read16(VIRTUAL_PROCESSOR_ID));
10039}
10040
Avi Kivity6aa8b732006-12-10 02:21:36 -080010041/*
10042 * The guest has exited. See if we can fix it or if we need userspace
10043 * assistance.
10044 */
Avi Kivity851ba692009-08-24 11:10:17 +030010045static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010046{
Avi Kivity29bd8a72007-09-10 17:27:03 +030010047 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +080010048 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +020010049 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +030010050
Paolo Bonzini8b89fe12015-12-10 18:37:32 +010010051 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
10052
Kai Huang843e4332015-01-28 10:54:28 +080010053 /*
10054 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
10055 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
10056 * querying dirty_bitmap, we only need to kick all vcpus out of guest
10057 * mode as if vcpus is in root mode, the PML buffer must has been
10058 * flushed already.
10059 */
10060 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020010061 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +080010062
Mohammed Gamal80ced182009-09-01 12:48:18 +020010063 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +020010064 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +020010065 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +010010066
Paolo Bonzini7313c692017-07-27 10:31:25 +020010067 if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
10068 return nested_vmx_reflect_vmexit(vcpu, exit_reason);
Nadav Har'El644d7112011-05-25 23:12:35 +030010069
Mohammed Gamal51207022010-05-31 22:40:54 +030010070 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +020010071 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +030010072 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10073 vcpu->run->fail_entry.hardware_entry_failure_reason
10074 = exit_reason;
10075 return 0;
10076 }
10077
Avi Kivity29bd8a72007-09-10 17:27:03 +030010078 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +030010079 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
10080 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +030010081 = vmcs_read32(VM_INSTRUCTION_ERROR);
10082 return 0;
10083 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010084
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010085 /*
10086 * Note:
10087 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
10088 * delivery event since it indicates guest is accessing MMIO.
10089 * The vm-exit can be triggered again after return to guest that
10090 * will cause infinite loop.
10091 */
Mike Dayd77c26f2007-10-08 09:02:08 -040010092 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +080010093 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +020010094 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +000010095 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010096 exit_reason != EXIT_REASON_TASK_SWITCH)) {
10097 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
10098 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010099 vcpu->run->internal.ndata = 3;
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010100 vcpu->run->internal.data[0] = vectoring_info;
10101 vcpu->run->internal.data[1] = exit_reason;
Paolo Bonzini70bcd702017-07-05 12:38:06 +020010102 vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
10103 if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
10104 vcpu->run->internal.ndata++;
10105 vcpu->run->internal.data[3] =
10106 vmcs_read64(GUEST_PHYSICAL_ADDRESS);
10107 }
Xiao Guangrongb9bf6882012-10-17 13:46:52 +080010108 return 0;
10109 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +020010110
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010111 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010112 vmx->loaded_vmcs->soft_vnmi_blocked)) {
10113 if (vmx_interrupt_allowed(vcpu)) {
10114 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10115 } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
10116 vcpu->arch.nmi_pending) {
10117 /*
10118 * This CPU don't support us in finding the end of an
10119 * NMI-blocked window if the guest runs with IRQs
10120 * disabled. So we pull the trigger after 1 s of
10121 * futile waiting, but inform the user about this.
10122 */
10123 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
10124 "state on VCPU %d after 1 s timeout\n",
10125 __func__, vcpu->vcpu_id);
10126 vmx->loaded_vmcs->soft_vnmi_blocked = 0;
10127 }
10128 }
10129
Avi Kivity6aa8b732006-12-10 02:21:36 -080010130 if (exit_reason < kvm_vmx_max_exit_handlers
10131 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +030010132 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010133 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +010010134 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
10135 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +030010136 kvm_queue_exception(vcpu, UD_VECTOR);
10137 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010138 }
Avi Kivity6aa8b732006-12-10 02:21:36 -080010139}
10140
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010141/*
10142 * Software based L1D cache flush which is used when microcode providing
10143 * the cache control MSR is not loaded.
10144 *
10145 * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
10146 * flush it is required to read in 64 KiB because the replacement algorithm
10147 * is not exactly LRU. This could be sized at runtime via topology
10148 * information but as all relevant affected CPUs have 32KiB L1D cache size
10149 * there is no point in doing so.
10150 */
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010151static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010152{
10153 int size = PAGE_SIZE << L1D_CACHE_ORDER;
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010154
10155 /*
Thomas Gleixner2f055942018-07-13 16:23:17 +020010156 * This code is only executed when the the flush mode is 'cond' or
10157 * 'always'
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010158 */
Nicolai Stange427362a2018-07-21 22:25:00 +020010159 if (static_branch_likely(&vmx_l1d_flush_cond)) {
Nicolai Stange45b575c2018-07-27 13:22:16 +020010160 bool flush_l1d;
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010161
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010162 /*
Nicolai Stange45b575c2018-07-27 13:22:16 +020010163 * Clear the per-vcpu flush bit, it gets set again
10164 * either from vcpu_run() or from one of the unsafe
10165 * VMEXIT handlers.
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010166 */
Nicolai Stange45b575c2018-07-27 13:22:16 +020010167 flush_l1d = vcpu->arch.l1tf_flush_l1d;
Thomas Gleixner4c6523e2018-07-13 16:23:20 +020010168 vcpu->arch.l1tf_flush_l1d = false;
Nicolai Stange45b575c2018-07-27 13:22:16 +020010169
10170 /*
10171 * Clear the per-cpu flush bit, it gets set again from
10172 * the interrupt handlers.
10173 */
10174 flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
10175 kvm_clear_cpu_l1tf_flush_l1d();
10176
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010177 if (!flush_l1d)
10178 return;
Nicolai Stange379fd0c2018-07-21 22:16:56 +020010179 }
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010180
10181 vcpu->stat.l1d_flush++;
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010182
Paolo Bonzini3fa045b2018-07-02 13:03:48 +020010183 if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
10184 wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
10185 return;
10186 }
10187
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010188 asm volatile(
10189 /* First ensure the pages are in the TLB */
10190 "xorl %%eax, %%eax\n"
10191 ".Lpopulate_tlb:\n\t"
Nicolai Stange288d1522018-07-18 19:07:38 +020010192 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010193 "addl $4096, %%eax\n\t"
10194 "cmpl %%eax, %[size]\n\t"
10195 "jne .Lpopulate_tlb\n\t"
10196 "xorl %%eax, %%eax\n\t"
10197 "cpuid\n\t"
10198 /* Now fill the cache */
10199 "xorl %%eax, %%eax\n"
10200 ".Lfill_cache:\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010201 "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010202 "addl $64, %%eax\n\t"
10203 "cmpl %%eax, %[size]\n\t"
10204 "jne .Lfill_cache\n\t"
10205 "lfence\n"
Nicolai Stange288d1522018-07-18 19:07:38 +020010206 :: [flush_pages] "r" (vmx_l1d_flush_pages),
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020010207 [size] "r" (size)
10208 : "eax", "ebx", "ecx", "edx");
10209}
10210
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010211static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010212{
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010213 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10214
10215 if (is_guest_mode(vcpu) &&
10216 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
10217 return;
10218
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010219 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010220 vmcs_write32(TPR_THRESHOLD, 0);
10221 return;
10222 }
10223
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010224 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +080010225}
10226
Jim Mattson8d860bb2018-05-09 16:56:05 -040010227static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
Yang Zhang8d146952013-01-25 10:18:50 +080010228{
10229 u32 sec_exec_control;
10230
Jim Mattson8d860bb2018-05-09 16:56:05 -040010231 if (!lapic_in_kernel(vcpu))
10232 return;
10233
Sean Christophersonfd6b6d92018-10-01 14:25:34 -070010234 if (!flexpriority_enabled &&
10235 !cpu_has_vmx_virtualize_x2apic_mode())
10236 return;
10237
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010238 /* Postpone execution until vmcs01 is the current VMCS. */
10239 if (is_guest_mode(vcpu)) {
Jim Mattson8d860bb2018-05-09 16:56:05 -040010240 to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010241 return;
10242 }
10243
Yang Zhang8d146952013-01-25 10:18:50 +080010244 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Jim Mattson8d860bb2018-05-09 16:56:05 -040010245 sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
10246 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
Yang Zhang8d146952013-01-25 10:18:50 +080010247
Jim Mattson8d860bb2018-05-09 16:56:05 -040010248 switch (kvm_get_apic_mode(vcpu)) {
10249 case LAPIC_MODE_INVALID:
10250 WARN_ONCE(true, "Invalid local APIC state");
10251 case LAPIC_MODE_DISABLED:
10252 break;
10253 case LAPIC_MODE_XAPIC:
10254 if (flexpriority_enabled) {
10255 sec_exec_control |=
10256 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
10257 vmx_flush_tlb(vcpu, true);
10258 }
10259 break;
10260 case LAPIC_MODE_X2APIC:
10261 if (cpu_has_vmx_virtualize_x2apic_mode())
10262 sec_exec_control |=
10263 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
10264 break;
Yang Zhang8d146952013-01-25 10:18:50 +080010265 }
10266 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
10267
Paolo Bonzini904e14f2018-01-16 16:51:18 +010010268 vmx_update_msr_bitmap(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +080010269}
10270
Tang Chen38b99172014-09-24 15:57:54 +080010271static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
10272{
Jim Mattsonab5df312018-05-09 17:02:03 -040010273 if (!is_guest_mode(vcpu)) {
Tang Chen38b99172014-09-24 15:57:54 +080010274 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Junaid Shahida468f2d2018-04-26 13:09:50 -070010275 vmx_flush_tlb(vcpu, true);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010276 }
Tang Chen38b99172014-09-24 15:57:54 +080010277}
10278
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010279static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010280{
10281 u16 status;
10282 u8 old;
10283
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010284 if (max_isr == -1)
10285 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010286
10287 status = vmcs_read16(GUEST_INTR_STATUS);
10288 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010289 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +080010290 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +020010291 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +080010292 vmcs_write16(GUEST_INTR_STATUS, status);
10293 }
10294}
10295
10296static void vmx_set_rvi(int vector)
10297{
10298 u16 status;
10299 u8 old;
10300
Wei Wang4114c272014-11-05 10:53:43 +080010301 if (vector == -1)
10302 vector = 0;
10303
Yang Zhangc7c9c562013-01-25 10:18:51 +080010304 status = vmcs_read16(GUEST_INTR_STATUS);
10305 old = (u8)status & 0xff;
10306 if ((u8)vector != old) {
10307 status &= ~0xff;
10308 status |= (u8)vector;
10309 vmcs_write16(GUEST_INTR_STATUS, status);
10310 }
10311}
10312
10313static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
10314{
Liran Alon851c1a182017-12-24 18:12:56 +020010315 /*
10316 * When running L2, updating RVI is only relevant when
10317 * vmcs12 virtual-interrupt-delivery enabled.
10318 * However, it can be enabled only when L1 also
10319 * intercepts external-interrupts and in that case
10320 * we should not update vmcs02 RVI but instead intercept
10321 * interrupt. Therefore, do nothing when running L2.
10322 */
10323 if (!is_guest_mode(vcpu))
Wanpeng Li963fee12014-07-17 19:03:00 +080010324 vmx_set_rvi(max_irr);
Yang Zhangc7c9c562013-01-25 10:18:51 +080010325}
10326
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010327static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010328{
10329 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010330 int max_irr;
Liran Alonf27a85c2017-12-24 18:12:55 +020010331 bool max_irr_updated;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010332
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010333 WARN_ON(!vcpu->arch.apicv_active);
10334 if (pi_test_on(&vmx->pi_desc)) {
10335 pi_clear_on(&vmx->pi_desc);
10336 /*
10337 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
10338 * But on x86 this is just a compiler barrier anyway.
10339 */
10340 smp_mb__after_atomic();
Liran Alonf27a85c2017-12-24 18:12:55 +020010341 max_irr_updated =
10342 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
10343
10344 /*
10345 * If we are running L2 and L1 has a new pending interrupt
10346 * which can be injected, we should re-evaluate
10347 * what should be done with this new L1 interrupt.
Liran Alon851c1a182017-12-24 18:12:56 +020010348 * If L1 intercepts external-interrupts, we should
10349 * exit from L2 to L1. Otherwise, interrupt should be
10350 * delivered directly to L2.
Liran Alonf27a85c2017-12-24 18:12:55 +020010351 */
Liran Alon851c1a182017-12-24 18:12:56 +020010352 if (is_guest_mode(vcpu) && max_irr_updated) {
10353 if (nested_exit_on_intr(vcpu))
10354 kvm_vcpu_exiting_guest_mode(vcpu);
10355 else
10356 kvm_make_request(KVM_REQ_EVENT, vcpu);
10357 }
Paolo Bonzini76dfafd52016-12-19 17:17:11 +010010358 } else {
10359 max_irr = kvm_lapic_find_highest_irr(vcpu);
10360 }
10361 vmx_hwapic_irr_update(vcpu, max_irr);
10362 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +010010363}
10364
Paolo Bonzini7e712682018-10-03 13:44:26 +020010365static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu)
10366{
10367 u8 rvi = vmx_get_rvi();
10368 u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI);
10369
10370 return ((rvi & 0xf0) > (vppr & 0xf0));
10371}
10372
Andrey Smetanin63086302015-11-10 15:36:32 +030010373static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +080010374{
Andrey Smetanind62caab2015-11-10 15:36:33 +030010375 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +080010376 return;
10377
Yang Zhangc7c9c562013-01-25 10:18:51 +080010378 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
10379 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
10380 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
10381 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
10382}
10383
Paolo Bonzini967235d2016-12-19 14:03:45 +010010384static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
10385{
10386 struct vcpu_vmx *vmx = to_vmx(vcpu);
10387
10388 pi_clear_on(&vmx->pi_desc);
10389 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
10390}
10391
Avi Kivity51aa01d2010-07-20 14:31:20 +030010392static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +030010393{
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010394 u32 exit_intr_info = 0;
10395 u16 basic_exit_reason = (u16)vmx->exit_reason;
Avi Kivity00eba012011-03-07 17:24:54 +020010396
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010397 if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
10398 || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI))
Avi Kivity00eba012011-03-07 17:24:54 +020010399 return;
10400
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010401 if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10402 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10403 vmx->exit_intr_info = exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +080010404
Wanpeng Li1261bfa2017-07-13 18:30:40 -070010405 /* if exit due to PF check for async PF */
10406 if (is_page_fault(exit_intr_info))
10407 vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
10408
Andi Kleena0861c02009-06-08 17:37:09 +080010409 /* Handle machine checks before interrupts are enabled */
Jim Mattson48ae0fb2017-05-22 09:48:33 -070010410 if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY ||
10411 is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +080010412 kvm_machine_check();
10413
Gleb Natapov20f65982009-05-11 13:35:55 +030010414 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -080010415 if (is_nmi(exit_intr_info)) {
Andi Kleendd60d212017-07-25 17:20:32 -070010416 kvm_before_interrupt(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +030010417 asm("int $2");
Andi Kleendd60d212017-07-25 17:20:32 -070010418 kvm_after_interrupt(&vmx->vcpu);
Zhang, Yanminff9d07a2010-04-19 13:32:45 +080010419 }
Avi Kivity51aa01d2010-07-20 14:31:20 +030010420}
Gleb Natapov20f65982009-05-11 13:35:55 +030010421
Yang Zhanga547c6d2013-04-11 19:25:10 +080010422static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
10423{
10424 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10425
Yang Zhanga547c6d2013-04-11 19:25:10 +080010426 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
10427 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
10428 unsigned int vector;
10429 unsigned long entry;
10430 gate_desc *desc;
10431 struct vcpu_vmx *vmx = to_vmx(vcpu);
10432#ifdef CONFIG_X86_64
10433 unsigned long tmp;
10434#endif
10435
10436 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10437 desc = (gate_desc *)vmx->host_idt_base + vector;
Thomas Gleixner64b163f2017-08-28 08:47:37 +020010438 entry = gate_offset(desc);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010439 asm volatile(
10440#ifdef CONFIG_X86_64
10441 "mov %%" _ASM_SP ", %[sp]\n\t"
10442 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
10443 "push $%c[ss]\n\t"
10444 "push %[sp]\n\t"
10445#endif
10446 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +080010447 __ASM_SIZE(push) " $%c[cs]\n\t"
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010448 CALL_NOSPEC
Yang Zhanga547c6d2013-04-11 19:25:10 +080010449 :
10450#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -060010451 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010452#endif
Josh Poimboeuff5caf622017-09-20 16:24:33 -050010453 ASM_CALL_CONSTRAINT
Yang Zhanga547c6d2013-04-11 19:25:10 +080010454 :
Peter Zijlstrac940a3f2018-01-25 10:58:14 +010010455 THUNK_TARGET(entry),
Yang Zhanga547c6d2013-04-11 19:25:10 +080010456 [ss]"i"(__KERNEL_DS),
10457 [cs]"i"(__KERNEL_CS)
10458 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +020010459 }
Yang Zhanga547c6d2013-04-11 19:25:10 +080010460}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010461STACK_FRAME_NON_STANDARD(vmx_handle_external_intr);
Yang Zhanga547c6d2013-04-11 19:25:10 +080010462
Tom Lendackybc226f02018-05-10 22:06:39 +020010463static bool vmx_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010464{
Tom Lendackybc226f02018-05-10 22:06:39 +020010465 switch (index) {
10466 case MSR_IA32_SMBASE:
10467 /*
10468 * We cannot do SMM unless we can run the guest in big
10469 * real mode.
10470 */
10471 return enable_unrestricted_guest || emulate_invalid_guest_state;
10472 case MSR_AMD64_VIRT_SPEC_CTRL:
10473 /* This is AMD only. */
10474 return false;
10475 default:
10476 return true;
10477 }
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010478}
10479
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010480static bool vmx_mpx_supported(void)
10481{
10482 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
10483 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
10484}
10485
Wanpeng Li55412b22014-12-02 19:21:30 +080010486static bool vmx_xsaves_supported(void)
10487{
10488 return vmcs_config.cpu_based_2nd_exec_ctrl &
10489 SECONDARY_EXEC_XSAVES;
10490}
10491
Avi Kivity51aa01d2010-07-20 14:31:20 +030010492static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
10493{
Avi Kivityc5ca8e52011-03-07 17:37:37 +020010494 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +030010495 bool unblock_nmi;
10496 u8 vector;
10497 bool idtv_info_valid;
10498
10499 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +030010500
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010501 if (enable_vnmi) {
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010502 if (vmx->loaded_vmcs->nmi_known_unmasked)
10503 return;
10504 /*
10505 * Can't use vmx->exit_intr_info since we're not sure what
10506 * the exit reason is.
10507 */
10508 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
10509 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
10510 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
10511 /*
10512 * SDM 3: 27.7.1.2 (September 2008)
10513 * Re-set bit "block by NMI" before VM entry if vmexit caused by
10514 * a guest IRET fault.
10515 * SDM 3: 23.2.2 (September 2008)
10516 * Bit 12 is undefined in any of the following cases:
10517 * If the VM exit sets the valid bit in the IDT-vectoring
10518 * information field.
10519 * If the VM exit is due to a double fault.
10520 */
10521 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
10522 vector != DF_VECTOR && !idtv_info_valid)
10523 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
10524 GUEST_INTR_STATE_NMI);
10525 else
10526 vmx->loaded_vmcs->nmi_known_unmasked =
10527 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
10528 & GUEST_INTR_STATE_NMI);
10529 } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
10530 vmx->loaded_vmcs->vnmi_blocked_time +=
10531 ktime_to_ns(ktime_sub(ktime_get(),
10532 vmx->loaded_vmcs->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +030010533}
10534
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010535static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +030010536 u32 idt_vectoring_info,
10537 int instr_len_field,
10538 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +030010539{
Avi Kivity51aa01d2010-07-20 14:31:20 +030010540 u8 vector;
10541 int type;
10542 bool idtv_info_valid;
10543
10544 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +030010545
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010546 vcpu->arch.nmi_injected = false;
10547 kvm_clear_exception_queue(vcpu);
10548 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010549
10550 if (!idtv_info_valid)
10551 return;
10552
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010553 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +030010554
Avi Kivity668f6122008-07-02 09:28:55 +030010555 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
10556 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010557
Gleb Natapov64a7ec02009-03-30 16:03:29 +030010558 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +030010559 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010560 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +030010561 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +030010562 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +030010563 * Clear bit "block by NMI" before VM entry if a NMI
10564 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +030010565 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010566 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010567 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +030010568 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010569 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010570 /* fall through */
10571 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +030010572 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +030010573 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +030010574 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +030010575 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +030010576 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010577 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010578 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010579 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +030010580 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +030010581 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010582 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +030010583 break;
10584 default:
10585 break;
Avi Kivityf7d92382008-07-03 16:14:28 +030010586 }
Avi Kivitycf393f72008-07-01 16:20:21 +030010587}
10588
Avi Kivity83422e12010-07-20 14:43:23 +030010589static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
10590{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010591 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +030010592 VM_EXIT_INSTRUCTION_LEN,
10593 IDT_VECTORING_ERROR_CODE);
10594}
10595
Avi Kivityb463a6f2010-07-20 15:06:17 +030010596static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
10597{
Jan Kiszka3ab66e82013-02-20 14:03:24 +010010598 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010599 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
10600 VM_ENTRY_INSTRUCTION_LEN,
10601 VM_ENTRY_EXCEPTION_ERROR_CODE);
10602
10603 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
10604}
10605
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010606static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
10607{
10608 int i, nr_msrs;
10609 struct perf_guest_switch_msr *msrs;
10610
10611 msrs = perf_guest_get_msrs(&nr_msrs);
10612
10613 if (!msrs)
10614 return;
10615
10616 for (i = 0; i < nr_msrs; i++)
10617 if (msrs[i].host == msrs[i].guest)
10618 clear_atomic_switch_msr(vmx, msrs[i].msr);
10619 else
10620 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
Konrad Rzeszutek Wilk989e3992018-06-20 22:01:22 -040010621 msrs[i].host, false);
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010622}
10623
Sean Christophersonf459a702018-08-27 15:21:11 -070010624static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val)
10625{
10626 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val);
10627 if (!vmx->loaded_vmcs->hv_timer_armed)
10628 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10629 PIN_BASED_VMX_PREEMPTION_TIMER);
10630 vmx->loaded_vmcs->hv_timer_armed = true;
10631}
10632
10633static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -070010634{
10635 struct vcpu_vmx *vmx = to_vmx(vcpu);
10636 u64 tscl;
10637 u32 delta_tsc;
10638
Sean Christophersond264ee02018-08-27 15:21:12 -070010639 if (vmx->req_immediate_exit) {
10640 vmx_arm_hv_timer(vmx, 0);
10641 return;
10642 }
10643
Sean Christophersonf459a702018-08-27 15:21:11 -070010644 if (vmx->hv_deadline_tsc != -1) {
10645 tscl = rdtsc();
10646 if (vmx->hv_deadline_tsc > tscl)
10647 /* set_hv_timer ensures the delta fits in 32-bits */
10648 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
10649 cpu_preemption_timer_multi);
10650 else
10651 delta_tsc = 0;
10652
10653 vmx_arm_hv_timer(vmx, delta_tsc);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010654 return;
Sean Christophersonf459a702018-08-27 15:21:11 -070010655 }
Yunhong Jiang64672c92016-06-13 14:19:59 -070010656
Sean Christophersonf459a702018-08-27 15:21:11 -070010657 if (vmx->loaded_vmcs->hv_timer_armed)
10658 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10659 PIN_BASED_VMX_PREEMPTION_TIMER);
10660 vmx->loaded_vmcs->hv_timer_armed = false;
Yunhong Jiang64672c92016-06-13 14:19:59 -070010661}
10662
Lai Jiangshana3b5ba42011-02-11 14:29:40 +080010663static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010664{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040010665 struct vcpu_vmx *vmx = to_vmx(vcpu);
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010666 unsigned long cr3, cr4, evmcs_rsp;
Avi Kivity104f2262010-11-18 13:12:52 +020010667
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010668 /* Record the guest's net vcpu time for enforced NMI injections. */
Paolo Bonzinid02fcf52017-11-06 13:31:13 +010010669 if (unlikely(!enable_vnmi &&
Paolo Bonzini8a1b4392017-11-06 13:31:12 +010010670 vmx->loaded_vmcs->soft_vnmi_blocked))
10671 vmx->loaded_vmcs->entry_time = ktime_get();
10672
Avi Kivity104f2262010-11-18 13:12:52 +020010673 /* Don't enter VMX if guest state is invalid, let the exit handler
10674 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +020010675 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +020010676 return;
10677
Radim Krčmářa7653ec2014-08-21 18:08:07 +020010678 if (vmx->ple_window_dirty) {
10679 vmx->ple_window_dirty = false;
10680 vmcs_write32(PLE_WINDOW, vmx->ple_window);
10681 }
10682
Abel Gordon012f83c2013-04-18 14:39:25 +030010683 if (vmx->nested.sync_shadow_vmcs) {
10684 copy_vmcs12_to_shadow(vmx);
10685 vmx->nested.sync_shadow_vmcs = false;
10686 }
10687
Avi Kivity104f2262010-11-18 13:12:52 +020010688 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
10689 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
10690 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
10691 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
10692
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010693 cr3 = __get_current_cr3_fast();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010694 if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010695 vmcs_writel(HOST_CR3, cr3);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010696 vmx->loaded_vmcs->host_state.cr3 = cr3;
Andy Lutomirskid6e41f12017-05-28 10:00:17 -070010697 }
10698
Andy Lutomirski1e02ce42014-10-24 15:58:08 -070010699 cr4 = cr4_read_shadow();
Sean Christophersond7ee0392018-07-23 12:32:47 -070010700 if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010701 vmcs_writel(HOST_CR4, cr4);
Sean Christophersond7ee0392018-07-23 12:32:47 -070010702 vmx->loaded_vmcs->host_state.cr4 = cr4;
Andy Lutomirskid974baa2014-10-08 09:02:13 -070010703 }
10704
Avi Kivity104f2262010-11-18 13:12:52 +020010705 /* When single-stepping over STI and MOV SS, we must clear the
10706 * corresponding interruptibility bits in the guest state. Otherwise
10707 * vmentry fails as it then expects bit 14 (BS) in pending debug
10708 * exceptions being set, but that's not correct for the guest debugging
10709 * case. */
10710 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
10711 vmx_set_interrupt_shadow(vcpu, 0);
10712
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010713 if (static_cpu_has(X86_FEATURE_PKU) &&
10714 kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
10715 vcpu->arch.pkru != vmx->host_pkru)
10716 __write_pkru(vcpu->arch.pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010717
Gleb Natapovd7cd9792011-10-05 14:01:23 +020010718 atomic_switch_perf_msrs(vmx);
10719
Sean Christophersonf459a702018-08-27 15:21:11 -070010720 vmx_update_hv_timer(vcpu);
Yunhong Jiang64672c92016-06-13 14:19:59 -070010721
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010722 /*
10723 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
10724 * it's non-zero. Since vmentry is serialising on affected CPUs, there
10725 * is no need to worry about the conditional branch over the wrmsr
10726 * being speculatively taken.
10727 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010728 x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010729
Nadav Har'Eld462b812011-05-24 15:26:10 +030010730 vmx->__launched = vmx->loaded_vmcs->launched;
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010731
10732 evmcs_rsp = static_branch_unlikely(&enable_evmcs) ?
10733 (unsigned long)&current_evmcs->host_rsp : 0;
10734
Nicolai Stange5b6ccc62018-07-21 22:35:28 +020010735 if (static_branch_unlikely(&vmx_l1d_should_flush))
10736 vmx_l1d_flush(vcpu);
Paolo Bonzinic595cee2018-07-02 13:07:14 +020010737
Avi Kivity104f2262010-11-18 13:12:52 +020010738 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -080010739 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010740 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
10741 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
10742 "push %%" _ASM_CX " \n\t"
10743 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010744 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010745 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010746 /* Avoid VMWRITE when Enlightened VMCS is in use */
10747 "test %%" _ASM_SI ", %%" _ASM_SI " \n\t"
10748 "jz 2f \n\t"
10749 "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t"
10750 "jmp 1f \n\t"
10751 "2: \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010752 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +030010753 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +030010754 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010755 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
10756 "mov %%cr2, %%" _ASM_DX " \n\t"
10757 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010758 "je 3f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010759 "mov %%" _ASM_AX", %%cr2 \n\t"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010760 "3: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010761 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +020010762 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010763 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010764 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
10765 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
10766 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
10767 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
10768 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
10769 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010770#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010771 "mov %c[r8](%0), %%r8 \n\t"
10772 "mov %c[r9](%0), %%r9 \n\t"
10773 "mov %c[r10](%0), %%r10 \n\t"
10774 "mov %c[r11](%0), %%r11 \n\t"
10775 "mov %c[r12](%0), %%r12 \n\t"
10776 "mov %c[r13](%0), %%r13 \n\t"
10777 "mov %c[r14](%0), %%r14 \n\t"
10778 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010779#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010780 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +030010781
Avi Kivity6aa8b732006-12-10 02:21:36 -080010782 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +030010783 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +030010784 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010785 "jmp 2f \n\t"
10786 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
10787 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -080010788 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +030010789 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +020010790 "pop %0 \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010791 "setbe %c[fail](%0)\n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010792 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
10793 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
10794 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
10795 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
10796 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
10797 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
10798 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010799#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +020010800 "mov %%r8, %c[r8](%0) \n\t"
10801 "mov %%r9, %c[r9](%0) \n\t"
10802 "mov %%r10, %c[r10](%0) \n\t"
10803 "mov %%r11, %c[r11](%0) \n\t"
10804 "mov %%r12, %c[r12](%0) \n\t"
10805 "mov %%r13, %c[r13](%0) \n\t"
10806 "mov %%r14, %c[r14](%0) \n\t"
10807 "mov %%r15, %c[r15](%0) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -080010808 "xor %%r8d, %%r8d \n\t"
10809 "xor %%r9d, %%r9d \n\t"
10810 "xor %%r10d, %%r10d \n\t"
10811 "xor %%r11d, %%r11d \n\t"
10812 "xor %%r12d, %%r12d \n\t"
10813 "xor %%r13d, %%r13d \n\t"
10814 "xor %%r14d, %%r14d \n\t"
10815 "xor %%r15d, %%r15d \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -080010816#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +030010817 "mov %%cr2, %%" _ASM_AX " \n\t"
10818 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +030010819
Jim Mattson0cb5b302018-01-03 14:31:38 -080010820 "xor %%eax, %%eax \n\t"
10821 "xor %%ebx, %%ebx \n\t"
10822 "xor %%esi, %%esi \n\t"
10823 "xor %%edi, %%edi \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010824 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +030010825 ".pushsection .rodata \n\t"
10826 ".global vmx_return \n\t"
10827 "vmx_return: " _ASM_PTR " 2b \n\t"
10828 ".popsection"
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010829 : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp),
Nadav Har'Eld462b812011-05-24 15:26:10 +030010830 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +020010831 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +030010832 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010833 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
10834 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
10835 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
10836 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
10837 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
10838 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
10839 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -080010840#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +080010841 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
10842 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
10843 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
10844 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
10845 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
10846 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
10847 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
10848 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -080010849#endif
Avi Kivity40712fa2011-01-06 18:09:12 +020010850 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
10851 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +020010852 : "cc", "memory"
10853#ifdef CONFIG_X86_64
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010854 , "rax", "rbx", "rdi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010855 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +030010856#else
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010857 , "eax", "ebx", "edi"
Laurent Vivierc2036302007-10-25 14:18:52 +020010858#endif
10859 );
Avi Kivity6aa8b732006-12-10 02:21:36 -080010860
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010861 /*
10862 * We do not use IBRS in the kernel. If this vCPU has used the
10863 * SPEC_CTRL MSR it may have left it on; save the value and
10864 * turn it off. This is much more efficient than blindly adding
10865 * it to the atomic save/restore list. Especially as the former
10866 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
10867 *
10868 * For non-nested case:
10869 * If the L01 MSR bitmap does not intercept the MSR, then we need to
10870 * save it.
10871 *
10872 * For nested case:
10873 * If the L02 MSR bitmap does not intercept the MSR, then we need to
10874 * save it.
10875 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +010010876 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +010010877 vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010878
Thomas Gleixnerccbcd262018-05-09 23:01:01 +020010879 x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010010880
David Woodhouse117cc7a2018-01-12 11:11:27 +000010881 /* Eliminate branch target predictions from guest mode */
10882 vmexit_fill_RSB();
10883
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010010884 /* All fields are clean at this point */
10885 if (static_branch_unlikely(&enable_evmcs))
10886 current_evmcs->hv_clean_fields |=
10887 HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
10888
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010889 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
Wanpeng Li74c55932017-11-29 01:31:20 -080010890 if (vmx->host_debugctlmsr)
10891 update_debugctlmsr(vmx->host_debugctlmsr);
Gleb Natapov2a7921b2012-08-12 16:12:29 +030010892
Avi Kivityaa67f602012-08-01 16:48:03 +030010893#ifndef CONFIG_X86_64
10894 /*
10895 * The sysexit path does not restore ds/es, so we must set them to
10896 * a reasonable value ourselves.
10897 *
Sean Christopherson6d6095b2018-07-23 12:32:44 -070010898 * We can't defer this to vmx_prepare_switch_to_host() since that
10899 * function may be executed in interrupt context, which saves and
10900 * restore segments around it, nullifying its effect.
Avi Kivityaa67f602012-08-01 16:48:03 +030010901 */
10902 loadsegment(ds, __USER_DS);
10903 loadsegment(es, __USER_DS);
10904#endif
10905
Avi Kivity6de4f3a2009-05-31 22:58:47 +030010906 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +020010907 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010908 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +030010909 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +020010910 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010911 vcpu->arch.regs_dirty = 0;
10912
Gleb Natapove0b890d2013-09-25 12:51:33 +030010913 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010914 * eager fpu is enabled if PKEY is supported and CR4 is switched
10915 * back on host, so it is safe to read guest PKRU from current
10916 * XSAVE.
10917 */
Paolo Bonzinib9dd21e2017-08-23 23:14:38 +020010918 if (static_cpu_has(X86_FEATURE_PKU) &&
10919 kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
10920 vcpu->arch.pkru = __read_pkru();
10921 if (vcpu->arch.pkru != vmx->host_pkru)
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010922 __write_pkru(vmx->host_pkru);
Xiao Guangrong1be0e612016-03-22 16:51:18 +080010923 }
10924
Gleb Natapove0b890d2013-09-25 12:51:33 +030010925 vmx->nested.nested_run_pending = 0;
Jim Mattsonb060ca32017-09-14 16:31:42 -070010926 vmx->idt_vectoring_info = 0;
10927
10928 vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
10929 if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
10930 return;
10931
10932 vmx->loaded_vmcs->launched = 1;
10933 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Gleb Natapove0b890d2013-09-25 12:51:33 +030010934
Avi Kivity51aa01d2010-07-20 14:31:20 +030010935 vmx_complete_atomic_exit(vmx);
10936 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +030010937 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010938}
Josh Poimboeufc207aee2017-06-28 10:11:06 -050010939STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010940
Sean Christopherson434a1e92018-03-20 12:17:18 -070010941static struct kvm *vmx_vm_alloc(void)
10942{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010943 struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
Sean Christopherson40bbb9d2018-03-20 12:17:20 -070010944 return &kvm_vmx->kvm;
Sean Christopherson434a1e92018-03-20 12:17:18 -070010945}
10946
10947static void vmx_vm_free(struct kvm *kvm)
10948{
Marc Orrd1e5b0e2018-05-15 04:37:37 -070010949 vfree(to_kvm_vmx(kvm));
Sean Christopherson434a1e92018-03-20 12:17:18 -070010950}
10951
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010952static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010953{
10954 struct vcpu_vmx *vmx = to_vmx(vcpu);
10955 int cpu;
10956
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010957 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010958 return;
10959
10960 cpu = get_cpu();
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010961 vmx_vcpu_put(vcpu);
Sean Christophersonbd9966d2018-07-23 12:32:42 -070010962 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010963 vmx_vcpu_load(vcpu, cpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010964 put_cpu();
10965}
10966
Jim Mattson2f1fe812016-07-08 15:36:06 -070010967/*
10968 * Ensure that the current vmcs of the logical processor is the
10969 * vmcs01 of the vcpu before calling free_nested().
10970 */
10971static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
10972{
10973 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010974
Christoffer Dallec7660c2017-12-04 21:35:23 +010010975 vcpu_load(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010976 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010977 free_nested(vmx);
10978 vcpu_put(vcpu);
10979}
10980
Avi Kivity6aa8b732006-12-10 02:21:36 -080010981static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
10982{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010983 struct vcpu_vmx *vmx = to_vmx(vcpu);
10984
Kai Huang843e4332015-01-28 10:54:28 +080010985 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +080010986 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +080010987 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010988 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -070010989 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +020010990 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010991 kfree(vmx->guest_msrs);
10992 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +100010993 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -080010994}
10995
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010996static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -080010997{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100010998 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +100010999 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011000 unsigned long *msr_bitmap;
Avi Kivity15ad7142007-07-11 18:17:21 +030011001 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011002
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011003 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011004 return ERR_PTR(-ENOMEM);
11005
Wanpeng Li991e7a02015-09-16 17:30:05 +080011006 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +080011007
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011008 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
11009 if (err)
11010 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011011
Peter Feiner4e595162016-07-07 14:49:58 -070011012 err = -ENOMEM;
11013
11014 /*
11015 * If PML is turned on, failure on enabling PML just results in failure
11016 * of creating the vcpu, therefore we can simplify PML logic (by
11017 * avoiding dealing with cases, such as enabling PML partially on vcpus
11018 * for the guest, etc.
11019 */
11020 if (enable_pml) {
11021 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
11022 if (!vmx->pml_pg)
11023 goto uninit_vcpu;
11024 }
11025
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011026 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +020011027 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
11028 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +030011029
Peter Feiner4e595162016-07-07 14:49:58 -070011030 if (!vmx->guest_msrs)
11031 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011032
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011033 err = alloc_loaded_vmcs(&vmx->vmcs01);
11034 if (err < 0)
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011035 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040011036
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011037 msr_bitmap = vmx->vmcs01.msr_bitmap;
11038 vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
11039 vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
11040 vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
11041 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
11042 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
11043 vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
11044 vmx->msr_bitmap_mode = 0;
11045
Paolo Bonzinif21f1652018-01-11 12:16:15 +010011046 vmx->loaded_vmcs = &vmx->vmcs01;
Avi Kivity15ad7142007-07-11 18:17:21 +030011047 cpu = get_cpu();
11048 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -100011049 vmx->vcpu.cpu = cpu;
David Hildenbrand12d79912017-08-24 20:51:26 +020011050 vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011051 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +030011052 put_cpu();
Paolo Bonzini35754c92015-07-29 12:05:37 +020011053 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +020011054 err = alloc_apic_access_page(kvm);
11055 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -020011056 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +020011057 }
Ingo Molnar965b58a2007-01-05 16:36:23 -080011058
Sean Christophersone90008d2018-03-05 12:04:37 -080011059 if (enable_ept && !enable_unrestricted_guest) {
Tang Chenf51770e2014-09-16 18:41:59 +080011060 err = init_rmode_identity_map(kvm);
11061 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +020011062 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +080011063 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +080011064
Roman Kagan63aff652018-07-19 21:59:07 +030011065 if (nested)
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011066 nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
11067 kvm_vcpu_apicv_active(&vmx->vcpu));
Wincy Vanb9c237b2015-02-03 23:56:30 +080011068
Wincy Van705699a2015-02-03 23:58:17 +080011069 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011070 vmx->nested.current_vmptr = -1ull;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +030011071
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011072 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
11073
Paolo Bonzini31afb2e2017-06-06 12:57:06 +020011074 /*
11075 * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
11076 * or POSTED_INTR_WAKEUP_VECTOR.
11077 */
11078 vmx->pi_desc.nv = POSTED_INTR_VECTOR;
11079 vmx->pi_desc.sn = 1;
11080
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011081 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -080011082
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011083free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +080011084 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011085free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011086 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -070011087free_pml:
11088 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011089uninit_vcpu:
11090 kvm_vcpu_uninit(&vmx->vcpu);
11091free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +080011092 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +100011093 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +100011094 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -080011095}
11096
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011097#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"
11098#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 -040011099
Wanpeng Lib31c1142018-03-12 04:53:04 -070011100static int vmx_vm_init(struct kvm *kvm)
11101{
Tianyu Lan877ad952018-07-19 08:40:23 +000011102 spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
11103
Wanpeng Lib31c1142018-03-12 04:53:04 -070011104 if (!ple_gap)
11105 kvm->arch.pause_in_guest = true;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011106
Jiri Kosinad90a7a02018-07-13 16:23:25 +020011107 if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
11108 switch (l1tf_mitigation) {
11109 case L1TF_MITIGATION_OFF:
11110 case L1TF_MITIGATION_FLUSH_NOWARN:
11111 /* 'I explicitly don't care' is set */
11112 break;
11113 case L1TF_MITIGATION_FLUSH:
11114 case L1TF_MITIGATION_FLUSH_NOSMT:
11115 case L1TF_MITIGATION_FULL:
11116 /*
11117 * Warn upon starting the first VM in a potentially
11118 * insecure environment.
11119 */
11120 if (cpu_smt_control == CPU_SMT_ENABLED)
11121 pr_warn_once(L1TF_MSG_SMT);
11122 if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
11123 pr_warn_once(L1TF_MSG_L1D);
11124 break;
11125 case L1TF_MITIGATION_FULL_FORCE:
11126 /* Flush is enforced */
11127 break;
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011128 }
Konrad Rzeszutek Wilk26acfb62018-06-20 11:29:53 -040011129 }
Wanpeng Lib31c1142018-03-12 04:53:04 -070011130 return 0;
11131}
11132
Yang, Sheng002c7f72007-07-31 14:23:01 +030011133static void __init vmx_check_processor_compat(void *rtn)
11134{
11135 struct vmcs_config vmcs_conf;
11136
11137 *(int *)rtn = 0;
11138 if (setup_vmcs_config(&vmcs_conf) < 0)
11139 *(int *)rtn = -EIO;
Paolo Bonzini13893092018-02-26 13:40:09 +010011140 nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv);
Yang, Sheng002c7f72007-07-31 14:23:01 +030011141 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
11142 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
11143 smp_processor_id());
11144 *(int *)rtn = -EIO;
11145 }
11146}
11147
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011148static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +080011149{
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011150 u8 cache;
11151 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011152
Sheng Yang522c68c2009-04-27 20:35:43 +080011153 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +020011154 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +080011155 * 2. EPT with VT-d:
11156 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +020011157 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +080011158 * b. VT-d with snooping control feature: snooping control feature of
11159 * VT-d engine can guarantee the cache correctness. Just set it
11160 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +080011161 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +080011162 * consistent with host MTRR
11163 */
Paolo Bonzini606decd2015-10-01 13:12:47 +020011164 if (is_mmio) {
11165 cache = MTRR_TYPE_UNCACHABLE;
11166 goto exit;
11167 }
11168
11169 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011170 ipat = VMX_EPT_IPAT_BIT;
11171 cache = MTRR_TYPE_WRBACK;
11172 goto exit;
11173 }
11174
11175 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
11176 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +020011177 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +080011178 cache = MTRR_TYPE_WRBACK;
11179 else
11180 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011181 goto exit;
11182 }
11183
Xiao Guangrongff536042015-06-15 16:55:22 +080011184 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +080011185
11186exit:
11187 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +080011188}
11189
Sheng Yang17cc3932010-01-05 19:02:27 +080011190static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +020011191{
Sheng Yang878403b2010-01-05 19:02:29 +080011192 if (enable_ept && !cpu_has_vmx_ept_1g_page())
11193 return PT_DIRECTORY_LEVEL;
11194 else
11195 /* For shadow and EPT supported 1GB page */
11196 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +020011197}
11198
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011199static void vmcs_set_secondary_exec_control(u32 new_ctl)
11200{
11201 /*
11202 * These bits in the secondary execution controls field
11203 * are dynamic, the others are mostly based on the hypervisor
11204 * architecture and the guest's CPUID. Do not touch the
11205 * dynamic bits.
11206 */
11207 u32 mask =
11208 SECONDARY_EXEC_SHADOW_VMCS |
11209 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Paolo Bonzini0367f202016-07-12 10:44:55 +020011210 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
11211 SECONDARY_EXEC_DESC;
Xiao Guangrongfeda8052015-09-09 14:05:55 +080011212
11213 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
11214
11215 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
11216 (new_ctl & ~mask) | (cur_ctl & mask));
11217}
11218
David Matlack8322ebb2016-11-29 18:14:09 -080011219/*
11220 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
11221 * (indicating "allowed-1") if they are supported in the guest's CPUID.
11222 */
11223static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
11224{
11225 struct vcpu_vmx *vmx = to_vmx(vcpu);
11226 struct kvm_cpuid_entry2 *entry;
11227
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011228 vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
11229 vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
David Matlack8322ebb2016-11-29 18:14:09 -080011230
11231#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
11232 if (entry && (entry->_reg & (_cpuid_mask))) \
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011233 vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
David Matlack8322ebb2016-11-29 18:14:09 -080011234} while (0)
11235
11236 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
11237 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
11238 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
11239 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
11240 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
11241 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
11242 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
11243 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
11244 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
11245 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
11246 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
11247 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
11248 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
11249 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
11250 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
11251
11252 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
11253 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
11254 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
11255 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
11256 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
Paolo Bonzinic4ad77e2017-11-13 14:23:59 +010011257 cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
David Matlack8322ebb2016-11-29 18:14:09 -080011258
11259#undef cr4_fixed1_update
11260}
11261
Liran Alon5f76f6f2018-09-14 03:25:52 +030011262static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
11263{
11264 struct vcpu_vmx *vmx = to_vmx(vcpu);
11265
11266 if (kvm_mpx_supported()) {
11267 bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
11268
11269 if (mpx_enabled) {
11270 vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
11271 vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
11272 } else {
11273 vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
11274 vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
11275 }
11276 }
11277}
11278
Sheng Yang0e851882009-12-18 16:48:46 +080011279static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
11280{
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011281 struct vcpu_vmx *vmx = to_vmx(vcpu);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011282
Paolo Bonzini80154d72017-08-24 13:55:35 +020011283 if (cpu_has_secondary_exec_ctrls()) {
11284 vmx_compute_secondary_exec_control(vmx);
11285 vmcs_set_secondary_exec_control(vmx->secondary_exec_control);
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011286 }
Mao, Junjiead756a12012-07-02 01:18:48 +000011287
Haozhong Zhang37e4c992016-06-22 14:59:55 +080011288 if (nested_vmx_allowed(vcpu))
11289 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11290 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
11291 else
11292 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11293 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -080011294
Liran Alon5f76f6f2018-09-14 03:25:52 +030011295 if (nested_vmx_allowed(vcpu)) {
David Matlack8322ebb2016-11-29 18:14:09 -080011296 nested_vmx_cr_fixed1_bits_update(vcpu);
Liran Alon5f76f6f2018-09-14 03:25:52 +030011297 nested_vmx_entry_exit_ctls_update(vcpu);
11298 }
Sheng Yang0e851882009-12-18 16:48:46 +080011299}
11300
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011301static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
11302{
Nadav Har'El7b8050f2011-05-25 23:16:10 +030011303 if (func == 1 && nested)
11304 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011305}
11306
Yang Zhang25d92082013-08-06 12:00:32 +030011307static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
11308 struct x86_exception *fault)
11309{
Jan Kiszka533558b2014-01-04 18:47:20 +010011310 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Bandan Dasc5f983f2017-05-05 15:25:14 -040011311 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010011312 u32 exit_reason;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011313 unsigned long exit_qualification = vcpu->arch.exit_qualification;
Yang Zhang25d92082013-08-06 12:00:32 +030011314
Bandan Dasc5f983f2017-05-05 15:25:14 -040011315 if (vmx->nested.pml_full) {
11316 exit_reason = EXIT_REASON_PML_FULL;
11317 vmx->nested.pml_full = false;
11318 exit_qualification &= INTR_INFO_UNBLOCK_NMI;
11319 } else if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +010011320 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +030011321 else
Jan Kiszka533558b2014-01-04 18:47:20 +010011322 exit_reason = EXIT_REASON_EPT_VIOLATION;
Bandan Dasc5f983f2017-05-05 15:25:14 -040011323
11324 nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +030011325 vmcs12->guest_physical_address = fault->address;
11326}
11327
Peter Feiner995f00a2017-06-30 17:26:32 -070011328static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu)
11329{
David Hildenbrandbb97a012017-08-10 23:15:28 +020011330 return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT;
Peter Feiner995f00a2017-06-30 17:26:32 -070011331}
11332
Nadav Har'El155a97a2013-08-05 11:07:16 +030011333/* Callbacks for nested_ept_init_mmu_context: */
11334
11335static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
11336{
11337 /* return the page table to be shadowed - in our case, EPT12 */
11338 return get_vmcs12(vcpu)->ept_pointer;
11339}
11340
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011341static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +030011342{
Paolo Bonziniad896af2013-10-02 16:56:14 +020011343 WARN_ON(mmu_is_nested(vcpu));
David Hildenbranda057e0e2017-08-10 23:36:54 +020011344 if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011345 return 1;
11346
Paolo Bonziniad896af2013-10-02 16:56:14 +020011347 kvm_init_shadow_ept_mmu(vcpu,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010011348 to_vmx(vcpu)->nested.msrs.ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011349 VMX_EPT_EXECUTE_ONLY_BIT,
Junaid Shahid50c28f22018-06-27 14:59:11 -070011350 nested_ept_ad_enabled(vcpu),
11351 nested_ept_get_cr3(vcpu));
Nadav Har'El155a97a2013-08-05 11:07:16 +030011352 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
11353 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
11354 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
11355
11356 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020011357 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +030011358}
11359
11360static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
11361{
11362 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
11363}
11364
Eugene Korenevsky19d5f102014-12-16 22:35:53 +030011365static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
11366 u16 error_code)
11367{
11368 bool inequality, bit;
11369
11370 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
11371 inequality =
11372 (error_code & vmcs12->page_fault_error_code_mask) !=
11373 vmcs12->page_fault_error_code_match;
11374 return inequality ^ bit;
11375}
11376
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011377static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
11378 struct x86_exception *fault)
11379{
11380 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
11381
11382 WARN_ON(!is_guest_mode(vcpu));
11383
Wanpeng Li305d0ab2017-09-28 18:16:44 -070011384 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) &&
11385 !to_vmx(vcpu)->nested.nested_run_pending) {
Paolo Bonzinib96fb432017-07-27 12:29:32 +020011386 vmcs12->vm_exit_intr_error_code = fault->error_code;
11387 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
11388 PF_VECTOR | INTR_TYPE_HARD_EXCEPTION |
11389 INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK,
11390 fault->address);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011391 } else {
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011392 kvm_inject_page_fault(vcpu, fault);
Paolo Bonzini7313c692017-07-27 10:31:25 +020011393 }
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030011394}
11395
Paolo Bonzinic9923842017-12-13 14:16:30 +010011396static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11397 struct vmcs12 *vmcs12);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011398
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011399static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011400{
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020011401 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011402 struct vcpu_vmx *vmx = to_vmx(vcpu);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011403 struct page *page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011404 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011405
11406 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011407 /*
11408 * Translate L1 physical address to host physical
11409 * address for vmcs02. Keep the page pinned, so this
11410 * physical address remains valid. We keep a reference
11411 * to it so we can release it later.
11412 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011413 if (vmx->nested.apic_access_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011414 kvm_release_page_dirty(vmx->nested.apic_access_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011415 vmx->nested.apic_access_page = NULL;
11416 }
11417 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011418 /*
11419 * If translation failed, no matter: This feature asks
11420 * to exit when accessing the given address, and if it
11421 * can never be accessed, this feature won't do
11422 * anything anyway.
11423 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011424 if (!is_error_page(page)) {
11425 vmx->nested.apic_access_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011426 hpa = page_to_phys(vmx->nested.apic_access_page);
11427 vmcs_write64(APIC_ACCESS_ADDR, hpa);
11428 } else {
11429 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
11430 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
11431 }
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011432 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011433
11434 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011435 if (vmx->nested.virtual_apic_page) { /* shouldn't happen */
David Hildenbrand53a70da2017-08-03 18:11:05 +020011436 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011437 vmx->nested.virtual_apic_page = NULL;
11438 }
11439 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011440
11441 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011442 * If translation failed, VM entry will fail because
11443 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
11444 * Failing the vm entry is _not_ what the processor
11445 * does but it's basically the only possibility we
11446 * have. We could still enter the guest if CR8 load
11447 * exits are enabled, CR8 store exits are enabled, and
11448 * virtualize APIC access is disabled; in this case
11449 * the processor would never use the TPR shadow and we
11450 * could simply clear the bit from the execution
11451 * control. But such a configuration is useless, so
11452 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011453 */
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011454 if (!is_error_page(page)) {
11455 vmx->nested.virtual_apic_page = page;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011456 hpa = page_to_phys(vmx->nested.virtual_apic_page);
11457 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
11458 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011459 }
11460
Wincy Van705699a2015-02-03 23:58:17 +080011461 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080011462 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
11463 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011464 kvm_release_page_dirty(vmx->nested.pi_desc_page);
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011465 vmx->nested.pi_desc_page = NULL;
Wincy Van705699a2015-02-03 23:58:17 +080011466 }
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011467 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr);
11468 if (is_error_page(page))
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011469 return;
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011470 vmx->nested.pi_desc_page = page;
11471 vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080011472 vmx->nested.pi_desc =
11473 (struct pi_desc *)((void *)vmx->nested.pi_desc +
11474 (unsigned long)(vmcs12->posted_intr_desc_addr &
11475 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011476 vmcs_write64(POSTED_INTR_DESC_ADDR,
11477 page_to_phys(vmx->nested.pi_desc_page) +
11478 (unsigned long)(vmcs12->posted_intr_desc_addr &
11479 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +080011480 }
Linus Torvaldsd4667ca2018-02-14 17:02:15 -080011481 if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12))
KarimAllah Ahmed3712caeb2018-02-10 23:39:26 +000011482 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
11483 CPU_BASED_USE_MSR_BITMAPS);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080011484 else
11485 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
11486 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +080011487}
11488
Jan Kiszkaf41245002014-03-07 20:03:13 +010011489static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
11490{
11491 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
11492 struct vcpu_vmx *vmx = to_vmx(vcpu);
11493
Sean Christopherson4c008122018-08-27 15:21:10 -070011494 /*
11495 * A timer value of zero is architecturally guaranteed to cause
11496 * a VMExit prior to executing any instructions in the guest.
11497 */
11498 if (preemption_timeout == 0) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010011499 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
11500 return;
11501 }
11502
Sean Christopherson4c008122018-08-27 15:21:10 -070011503 if (vcpu->arch.virtual_tsc_khz == 0)
11504 return;
11505
Jan Kiszkaf41245002014-03-07 20:03:13 +010011506 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
11507 preemption_timeout *= 1000000;
11508 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
11509 hrtimer_start(&vmx->nested.preemption_timer,
11510 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
11511}
11512
Jim Mattson56a20512017-07-06 16:33:06 -070011513static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu,
11514 struct vmcs12 *vmcs12)
11515{
11516 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
11517 return 0;
11518
11519 if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) ||
11520 !page_address_valid(vcpu, vmcs12->io_bitmap_b))
11521 return -EINVAL;
11522
11523 return 0;
11524}
11525
Wincy Van3af18d92015-02-03 23:49:31 +080011526static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
11527 struct vmcs12 *vmcs12)
11528{
Wincy Van3af18d92015-02-03 23:49:31 +080011529 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11530 return 0;
11531
Jim Mattson5fa99cb2017-07-06 16:33:07 -070011532 if (!page_address_valid(vcpu, vmcs12->msr_bitmap))
Wincy Van3af18d92015-02-03 23:49:31 +080011533 return -EINVAL;
11534
11535 return 0;
11536}
11537
Jim Mattson712b12d2017-08-24 13:24:47 -070011538static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu,
11539 struct vmcs12 *vmcs12)
11540{
11541 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11542 return 0;
11543
11544 if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr))
11545 return -EINVAL;
11546
11547 return 0;
11548}
11549
Wincy Van3af18d92015-02-03 23:49:31 +080011550/*
11551 * Merge L0's and L1's MSR bitmap, return false to indicate that
11552 * we do not use the hardware.
11553 */
Paolo Bonzinic9923842017-12-13 14:16:30 +010011554static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu,
11555 struct vmcs12 *vmcs12)
Wincy Van3af18d92015-02-03 23:49:31 +080011556{
Wincy Van82f0dd42015-02-03 23:57:18 +080011557 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +080011558 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +020011559 unsigned long *msr_bitmap_l1;
Paolo Bonzini904e14f2018-01-16 16:51:18 +010011560 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap;
Ashok Raj15d45072018-02-01 22:59:43 +010011561 /*
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011562 * pred_cmd & spec_ctrl are trying to verify two things:
Ashok Raj15d45072018-02-01 22:59:43 +010011563 *
11564 * 1. L0 gave a permission to L1 to actually passthrough the MSR. This
11565 * ensures that we do not accidentally generate an L02 MSR bitmap
11566 * from the L12 MSR bitmap that is too permissive.
11567 * 2. That L1 or L2s have actually used the MSR. This avoids
11568 * unnecessarily merging of the bitmap if the MSR is unused. This
11569 * works properly because we only update the L01 MSR bitmap lazily.
11570 * So even if L0 should pass L1 these MSRs, the L01 bitmap is only
11571 * updated to reflect this when L1 (or its L2s) actually write to
11572 * the MSR.
11573 */
KarimAllah Ahmed206587a2018-02-10 23:39:25 +000011574 bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD);
11575 bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL);
Wincy Vanf2b93282015-02-03 23:56:03 +080011576
Paolo Bonzinic9923842017-12-13 14:16:30 +010011577 /* Nothing to do if the MSR bitmap is not in use. */
11578 if (!cpu_has_vmx_msr_bitmap() ||
11579 !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
11580 return false;
11581
Ashok Raj15d45072018-02-01 22:59:43 +010011582 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011583 !pred_cmd && !spec_ctrl)
Wincy Vanf2b93282015-02-03 23:56:03 +080011584 return false;
11585
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020011586 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap);
11587 if (is_error_page(page))
Wincy Vanf2b93282015-02-03 23:56:03 +080011588 return false;
Paolo Bonzinic9923842017-12-13 14:16:30 +010011589
Radim Krčmářd048c092016-08-08 20:16:22 +020011590 msr_bitmap_l1 = (unsigned long *)kmap(page);
Paolo Bonzinic9923842017-12-13 14:16:30 +010011591 if (nested_cpu_has_apic_reg_virt(vmcs12)) {
11592 /*
11593 * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it
11594 * just lets the processor take the value from the virtual-APIC page;
11595 * take those 256 bits directly from the L1 bitmap.
11596 */
11597 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11598 unsigned word = msr / BITS_PER_LONG;
11599 msr_bitmap_l0[word] = msr_bitmap_l1[word];
11600 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
Wincy Van608406e2015-02-03 23:57:51 +080011601 }
Paolo Bonzinic9923842017-12-13 14:16:30 +010011602 } else {
11603 for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
11604 unsigned word = msr / BITS_PER_LONG;
11605 msr_bitmap_l0[word] = ~0;
11606 msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0;
11607 }
11608 }
11609
11610 nested_vmx_disable_intercept_for_msr(
11611 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011612 X2APIC_MSR(APIC_TASKPRI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011613 MSR_TYPE_W);
11614
11615 if (nested_cpu_has_vid(vmcs12)) {
11616 nested_vmx_disable_intercept_for_msr(
11617 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011618 X2APIC_MSR(APIC_EOI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011619 MSR_TYPE_W);
11620 nested_vmx_disable_intercept_for_msr(
11621 msr_bitmap_l1, msr_bitmap_l0,
Paolo Bonzinid7231e72017-12-21 00:47:55 +010011622 X2APIC_MSR(APIC_SELF_IPI),
Paolo Bonzinic9923842017-12-13 14:16:30 +010011623 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +080011624 }
Ashok Raj15d45072018-02-01 22:59:43 +010011625
KarimAllah Ahmedd28b3872018-02-01 22:59:45 +010011626 if (spec_ctrl)
11627 nested_vmx_disable_intercept_for_msr(
11628 msr_bitmap_l1, msr_bitmap_l0,
11629 MSR_IA32_SPEC_CTRL,
11630 MSR_TYPE_R | MSR_TYPE_W);
11631
Ashok Raj15d45072018-02-01 22:59:43 +010011632 if (pred_cmd)
11633 nested_vmx_disable_intercept_for_msr(
11634 msr_bitmap_l1, msr_bitmap_l0,
11635 MSR_IA32_PRED_CMD,
11636 MSR_TYPE_W);
11637
Wincy Vanf2b93282015-02-03 23:56:03 +080011638 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020011639 kvm_release_page_clean(page);
Wincy Vanf2b93282015-02-03 23:56:03 +080011640
11641 return true;
11642}
11643
Liran Alon61ada742018-06-23 02:35:08 +030011644static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu,
11645 struct vmcs12 *vmcs12)
11646{
11647 struct vmcs12 *shadow;
11648 struct page *page;
11649
11650 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11651 vmcs12->vmcs_link_pointer == -1ull)
11652 return;
11653
11654 shadow = get_shadow_vmcs12(vcpu);
11655 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
11656
11657 memcpy(shadow, kmap(page), VMCS12_SIZE);
11658
11659 kunmap(page);
11660 kvm_release_page_clean(page);
11661}
11662
11663static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu,
11664 struct vmcs12 *vmcs12)
11665{
11666 struct vcpu_vmx *vmx = to_vmx(vcpu);
11667
11668 if (!nested_cpu_has_shadow_vmcs(vmcs12) ||
11669 vmcs12->vmcs_link_pointer == -1ull)
11670 return;
11671
11672 kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer,
11673 get_shadow_vmcs12(vcpu), VMCS12_SIZE);
11674}
11675
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040011676static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu,
11677 struct vmcs12 *vmcs12)
11678{
11679 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
11680 !page_address_valid(vcpu, vmcs12->apic_access_addr))
11681 return -EINVAL;
11682 else
11683 return 0;
11684}
11685
Wincy Vanf2b93282015-02-03 23:56:03 +080011686static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
11687 struct vmcs12 *vmcs12)
11688{
Wincy Van82f0dd42015-02-03 23:57:18 +080011689 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +080011690 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +080011691 !nested_cpu_has_vid(vmcs12) &&
11692 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +080011693 return 0;
11694
11695 /*
11696 * If virtualize x2apic mode is enabled,
11697 * virtualize apic access must be disabled.
11698 */
Wincy Van82f0dd42015-02-03 23:57:18 +080011699 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
11700 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +080011701 return -EINVAL;
11702
Wincy Van608406e2015-02-03 23:57:51 +080011703 /*
11704 * If virtual interrupt delivery is enabled,
11705 * we must exit on external interrupts.
11706 */
11707 if (nested_cpu_has_vid(vmcs12) &&
11708 !nested_exit_on_intr(vcpu))
11709 return -EINVAL;
11710
Wincy Van705699a2015-02-03 23:58:17 +080011711 /*
11712 * bits 15:8 should be zero in posted_intr_nv,
11713 * the descriptor address has been already checked
11714 * in nested_get_vmcs12_pages.
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011715 *
11716 * bits 5:0 of posted_intr_desc_addr should be zero.
Wincy Van705699a2015-02-03 23:58:17 +080011717 */
11718 if (nested_cpu_has_posted_intr(vmcs12) &&
11719 (!nested_cpu_has_vid(vmcs12) ||
11720 !nested_exit_intr_ack_set(vcpu) ||
Krish Sadhukhan6de84e52018-08-23 20:03:03 -040011721 (vmcs12->posted_intr_nv & 0xff00) ||
11722 (vmcs12->posted_intr_desc_addr & 0x3f) ||
11723 (!page_address_valid(vcpu, vmcs12->posted_intr_desc_addr))))
Wincy Van705699a2015-02-03 23:58:17 +080011724 return -EINVAL;
11725
Wincy Vanf2b93282015-02-03 23:56:03 +080011726 /* tpr shadow is needed by all apicv features. */
11727 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
11728 return -EINVAL;
11729
11730 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +080011731}
11732
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011733static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
11734 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011735 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +030011736{
Liran Alone2536742018-06-23 02:35:02 +030011737 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011738 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011739 u64 count, addr;
11740
Liran Alone2536742018-06-23 02:35:02 +030011741 if (vmcs12_read_any(vmcs12, count_field, &count) ||
11742 vmcs12_read_any(vmcs12, addr_field, &addr)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011743 WARN_ON(1);
11744 return -EINVAL;
11745 }
11746 if (count == 0)
11747 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011748 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011749 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
11750 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011751 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011752 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
11753 addr_field, maxphyaddr, count, addr);
11754 return -EINVAL;
11755 }
11756 return 0;
11757}
11758
11759static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
11760 struct vmcs12 *vmcs12)
11761{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011762 if (vmcs12->vm_exit_msr_load_count == 0 &&
11763 vmcs12->vm_exit_msr_store_count == 0 &&
11764 vmcs12->vm_entry_msr_load_count == 0)
11765 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011766 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011767 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011768 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011769 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011770 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +030011771 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +030011772 return -EINVAL;
11773 return 0;
11774}
11775
Bandan Dasc5f983f2017-05-05 15:25:14 -040011776static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu,
11777 struct vmcs12 *vmcs12)
11778{
11779 u64 address = vmcs12->pml_address;
11780 int maxphyaddr = cpuid_maxphyaddr(vcpu);
11781
11782 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) {
11783 if (!nested_cpu_has_ept(vmcs12) ||
11784 !IS_ALIGNED(address, 4096) ||
11785 address >> maxphyaddr)
11786 return -EINVAL;
11787 }
11788
11789 return 0;
11790}
11791
Liran Alona8a7c022018-06-23 02:35:06 +030011792static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu,
11793 struct vmcs12 *vmcs12)
11794{
11795 if (!nested_cpu_has_shadow_vmcs(vmcs12))
11796 return 0;
11797
11798 if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) ||
11799 !page_address_valid(vcpu, vmcs12->vmwrite_bitmap))
11800 return -EINVAL;
11801
11802 return 0;
11803}
11804
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011805static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
11806 struct vmx_msr_entry *e)
11807{
11808 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +020011809 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011810 return -EINVAL;
11811 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
11812 e->index == MSR_IA32_UCODE_REV)
11813 return -EINVAL;
11814 if (e->reserved != 0)
11815 return -EINVAL;
11816 return 0;
11817}
11818
11819static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
11820 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +030011821{
11822 if (e->index == MSR_FS_BASE ||
11823 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011824 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
11825 nested_vmx_msr_check_common(vcpu, e))
11826 return -EINVAL;
11827 return 0;
11828}
11829
11830static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
11831 struct vmx_msr_entry *e)
11832{
11833 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
11834 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +030011835 return -EINVAL;
11836 return 0;
11837}
11838
11839/*
11840 * Load guest's/host's msr at nested entry/exit.
11841 * return 0 for success, entry index for failure.
11842 */
11843static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11844{
11845 u32 i;
11846 struct vmx_msr_entry e;
11847 struct msr_data msr;
11848
11849 msr.host_initiated = false;
11850 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011851 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
11852 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011853 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011854 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11855 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011856 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011857 }
11858 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011859 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011860 "%s check failed (%u, 0x%x, 0x%x)\n",
11861 __func__, i, e.index, e.reserved);
11862 goto fail;
11863 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011864 msr.index = e.index;
11865 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011866 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011867 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011868 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
11869 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +030011870 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011871 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011872 }
11873 return 0;
11874fail:
11875 return i + 1;
11876}
11877
11878static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
11879{
11880 u32 i;
11881 struct vmx_msr_entry e;
11882
11883 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011884 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011885 if (kvm_vcpu_read_guest(vcpu,
11886 gpa + i * sizeof(e),
11887 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011888 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011889 "%s cannot read MSR entry (%u, 0x%08llx)\n",
11890 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +030011891 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011892 }
11893 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011894 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011895 "%s check failed (%u, 0x%x, 0x%x)\n",
11896 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +030011897 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011898 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011899 msr_info.host_initiated = false;
11900 msr_info.index = e.index;
11901 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011902 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011903 "%s cannot read MSR (%u, 0x%x)\n",
11904 __func__, i, e.index);
11905 return -EINVAL;
11906 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +020011907 if (kvm_vcpu_write_guest(vcpu,
11908 gpa + i * sizeof(e) +
11909 offsetof(struct vmx_msr_entry, value),
11910 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +020011911 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011912 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +020011913 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +030011914 return -EINVAL;
11915 }
Wincy Vanff651cb2014-12-11 08:52:58 +030011916 }
11917 return 0;
11918}
11919
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011920static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
11921{
11922 unsigned long invalid_mask;
11923
11924 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
11925 return (val & invalid_mask) == 0;
11926}
11927
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030011928/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011929 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
11930 * emulating VM entry into a guest with EPT enabled.
11931 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
11932 * is assigned to entry_failure_code on failure.
11933 */
11934static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -080011935 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011936{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011937 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +010011938 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011939 *entry_failure_code = ENTRY_FAIL_DEFAULT;
11940 return 1;
11941 }
11942
11943 /*
11944 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
11945 * must not be dereferenced.
11946 */
11947 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
11948 !nested_ept) {
11949 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
11950 *entry_failure_code = ENTRY_FAIL_PDPTE;
11951 return 1;
11952 }
11953 }
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011954 }
11955
Junaid Shahid50c28f22018-06-27 14:59:11 -070011956 if (!nested_ept)
Junaid Shahidade61e22018-06-27 14:59:15 -070011957 kvm_mmu_new_cr3(vcpu, cr3, false);
Junaid Shahid50c28f22018-06-27 14:59:11 -070011958
11959 vcpu->arch.cr3 = cr3;
11960 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
11961
11962 kvm_init_mmu(vcpu, false);
11963
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010011964 return 0;
11965}
11966
Jim Mattson6514dc32018-04-26 16:09:12 -070011967static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Paolo Bonzini74a497f2017-12-20 13:55:39 +010011968{
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010011969 struct vcpu_vmx *vmx = to_vmx(vcpu);
11970
11971 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
11972 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
11973 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
11974 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
11975 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
11976 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
11977 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
11978 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
11979 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
11980 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
11981 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
11982 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
11983 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
11984 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
11985 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
11986 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
11987 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
11988 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
11989 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
11990 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
11991 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
11992 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
11993 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
11994 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
11995 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
11996 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
11997 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
11998 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
11999 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
12000 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
12001 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012002
12003 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
12004 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
12005 vmcs12->guest_pending_dbg_exceptions);
12006 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
12007 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
12008
12009 if (nested_cpu_has_xsaves(vmcs12))
12010 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
12011 vmcs_write64(VMCS_LINK_POINTER, -1ull);
12012
12013 if (cpu_has_vmx_posted_intr())
12014 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR);
12015
12016 /*
12017 * Whether page-faults are trapped is determined by a combination of
12018 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
12019 * If enable_ept, L0 doesn't care about page faults and we should
12020 * set all of these to L1's desires. However, if !enable_ept, L0 does
12021 * care about (at least some) page faults, and because it is not easy
12022 * (if at all possible?) to merge L0 and L1's desires, we simply ask
12023 * to exit on each and every L2 page fault. This is done by setting
12024 * MASK=MATCH=0 and (see below) EB.PF=1.
12025 * Note that below we don't need special code to set EB.PF beyond the
12026 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
12027 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
12028 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
12029 */
12030 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
12031 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
12032 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
12033 enable_ept ? vmcs12->page_fault_error_code_match : 0);
12034
12035 /* All VMFUNCs are currently emulated through L0 vmexits. */
12036 if (cpu_has_vmx_vmfunc())
12037 vmcs_write64(VM_FUNCTION_CONTROL, 0);
12038
12039 if (cpu_has_vmx_apicv()) {
12040 vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0);
12041 vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1);
12042 vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2);
12043 vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3);
12044 }
12045
12046 /*
12047 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
12048 * Some constant fields are set here by vmx_set_constant_host_state().
12049 * Other fields are different per CPU, and will be set later when
Sean Christopherson6d6095b2018-07-23 12:32:44 -070012050 * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest()
12051 * is called.
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012052 */
12053 vmx_set_constant_host_state(vmx);
12054
12055 /*
12056 * Set the MSR load/store lists to match L0's settings.
12057 */
12058 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040012059 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
12060 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
12061 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
12062 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012063
12064 set_cr4_guest_host_mask(vmx);
12065
Liran Alon62cf9bd812018-09-14 03:25:54 +030012066 if (kvm_mpx_supported()) {
12067 if (vmx->nested.nested_run_pending &&
12068 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12069 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
12070 else
12071 vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs);
12072 }
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012073
12074 if (enable_vpid) {
12075 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02)
12076 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
12077 else
12078 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
12079 }
12080
12081 /*
12082 * L1 may access the L2's PDPTR, so save them to construct vmcs12
12083 */
12084 if (enable_ept) {
12085 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
12086 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
12087 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
12088 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
12089 }
Radim Krčmář80132f42018-02-02 18:26:58 +010012090
12091 if (cpu_has_vmx_msr_bitmap())
12092 vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap));
Paolo Bonzini74a497f2017-12-20 13:55:39 +010012093}
12094
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012095/*
12096 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
12097 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +080012098 * 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 +030012099 * guest in a way that will both be appropriate to L1's requests, and our
12100 * needs. In addition to modifying the active vmcs (which is vmcs02), this
12101 * function also has additional necessary side-effects, like setting various
12102 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +010012103 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
12104 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012105 */
Ladi Prosekee146c12016-11-30 16:03:09 +010012106static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattson6514dc32018-04-26 16:09:12 -070012107 u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012108{
12109 struct vcpu_vmx *vmx = to_vmx(vcpu);
Bandan Das03efce62017-05-05 15:25:15 -040012110 u32 exec_control, vmcs12_exec_ctrl;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012111
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012112 if (vmx->nested.dirty_vmcs12) {
Jim Mattson6514dc32018-04-26 16:09:12 -070012113 prepare_vmcs02_full(vcpu, vmcs12);
Sean Christopherson9d1887e2018-03-05 09:33:27 -080012114 vmx->nested.dirty_vmcs12 = false;
12115 }
12116
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012117 /*
12118 * First, the fields that are shadowed. This must be kept in sync
12119 * with vmx_shadow_fields.h.
12120 */
12121
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012122 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012123 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012124 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012125 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
12126 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
Paolo Bonzini8665c3f2017-12-20 13:56:53 +010012127
Jim Mattson6514dc32018-04-26 16:09:12 -070012128 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012129 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +020012130 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
12131 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
12132 } else {
12133 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
12134 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
12135 }
Jim Mattson6514dc32018-04-26 16:09:12 -070012136 if (vmx->nested.nested_run_pending) {
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012137 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
12138 vmcs12->vm_entry_intr_info_field);
12139 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
12140 vmcs12->vm_entry_exception_error_code);
12141 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
12142 vmcs12->vm_entry_instruction_len);
12143 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
12144 vmcs12->guest_interruptibility_info);
Wanpeng Li2d6144e2017-07-25 03:40:46 -070012145 vmx->loaded_vmcs->nmi_known_unmasked =
12146 !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012147 } else {
12148 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
12149 }
Gleb Natapov63fbf592013-07-28 18:31:06 +030012150 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012151
Jan Kiszkaf41245002014-03-07 20:03:13 +010012152 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +080012153
Sean Christophersonf459a702018-08-27 15:21:11 -070012154 /* Preemption timer setting is computed directly in vmx_vcpu_run. */
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012155 exec_control |= vmcs_config.pin_based_exec_ctrl;
Sean Christophersonf459a702018-08-27 15:21:11 -070012156 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
12157 vmx->loaded_vmcs->hv_timer_armed = false;
Paolo Bonzini9314006db2016-07-06 13:23:51 +020012158
12159 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +080012160 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +080012161 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
12162 vmx->nested.pi_pending = false;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012163 } else {
Wincy Van705699a2015-02-03 23:58:17 +080012164 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012165 }
Wincy Van705699a2015-02-03 23:58:17 +080012166
Jan Kiszkaf41245002014-03-07 20:03:13 +010012167 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012168
Jan Kiszkaf41245002014-03-07 20:03:13 +010012169 vmx->nested.preemption_timer_expired = false;
12170 if (nested_cpu_has_preemption_timer(vmcs12))
12171 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010012172
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012173 if (cpu_has_secondary_exec_ctrls()) {
Paolo Bonzini80154d72017-08-24 13:55:35 +020012174 exec_control = vmx->secondary_exec_control;
Xiao Guangronge2821622015-09-09 14:05:52 +080012175
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012176 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012177 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Paolo Bonzini90a2db62017-07-27 13:22:13 +020012178 SECONDARY_EXEC_ENABLE_INVPCID |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010012179 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini3db13482017-08-24 14:48:03 +020012180 SECONDARY_EXEC_XSAVES |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020012181 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Bandan Das27c42a12017-08-03 15:54:42 -040012182 SECONDARY_EXEC_APIC_REGISTER_VIRT |
12183 SECONDARY_EXEC_ENABLE_VMFUNC);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012184 if (nested_cpu_has(vmcs12,
Bandan Das03efce62017-05-05 15:25:15 -040012185 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) {
12186 vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control &
12187 ~SECONDARY_EXEC_ENABLE_PML;
12188 exec_control |= vmcs12_exec_ctrl;
12189 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012190
Liran Alon32c7acf2018-06-23 02:35:11 +030012191 /* VMCS shadowing for L2 is emulated for now */
12192 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
12193
Paolo Bonzini25a2e4f2017-12-20 14:05:21 +010012194 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
Wincy Van608406e2015-02-03 23:57:51 +080012195 vmcs_write16(GUEST_INTR_STATUS,
12196 vmcs12->guest_intr_status);
Wincy Van608406e2015-02-03 23:57:51 +080012197
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012198 /*
12199 * Write an illegal value to APIC_ACCESS_ADDR. Later,
12200 * nested_get_vmcs12_pages will either fix it up or
12201 * remove the VM execution control.
12202 */
12203 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
12204 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
12205
Sean Christopherson0b665d32018-08-14 09:33:34 -070012206 if (exec_control & SECONDARY_EXEC_ENCLS_EXITING)
12207 vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
12208
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012209 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
12210 }
12211
Jim Mattson83bafef2016-10-04 10:48:38 -070012212 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012213 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
12214 * entry, but only if the current (host) sp changed from the value
12215 * we wrote last (vmx->host_rsp). This cache is no longer relevant
12216 * if we switch vmcs, and rather than hold a separate cache per vmcs,
12217 * here we just force the write to happen on entry.
12218 */
12219 vmx->host_rsp = 0;
12220
12221 exec_control = vmx_exec_control(vmx); /* L0's desires */
12222 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
12223 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
12224 exec_control &= ~CPU_BASED_TPR_SHADOW;
12225 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012226
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012227 /*
12228 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
12229 * nested_get_vmcs12_pages can't fix it up, the illegal value
12230 * will result in a VM entry failure.
12231 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012232 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080012233 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012234 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
Jim Mattson51aa68e2017-09-12 13:02:54 -070012235 } else {
12236#ifdef CONFIG_X86_64
12237 exec_control |= CPU_BASED_CR8_LOAD_EXITING |
12238 CPU_BASED_CR8_STORE_EXITING;
12239#endif
Wanpeng Lia7c0b072014-08-21 19:46:50 +080012240 }
12241
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012242 /*
Quan Xu8eb73e2d2017-12-12 16:44:21 +080012243 * A vmexit (to either L1 hypervisor or L0 userspace) is always needed
12244 * for I/O port accesses.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012245 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012246 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
12247 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
12248
12249 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
12250
12251 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
12252 * bitwise-or of what L1 wants to trap for L2, and what we want to
12253 * trap. Note that CR0.TS also needs updating - we do this later.
12254 */
12255 update_exception_bitmap(vcpu);
12256 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
12257 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
12258
Nadav Har'El8049d652013-08-05 11:07:06 +030012259 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
12260 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
12261 * bits are further modified by vmx_set_efer() below.
12262 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010012263 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030012264
12265 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
12266 * emulated by vmx_set_efer(), below.
12267 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020012268 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030012269 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
12270 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012271 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
12272
Jim Mattson6514dc32018-04-26 16:09:12 -070012273 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012274 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012275 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020012276 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012277 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012278 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012279 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012280
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012281 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
12282
Peter Feinerc95ba922016-08-17 09:36:47 -070012283 if (kvm_has_tsc_control)
12284 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012285
12286 if (enable_vpid) {
12287 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070012288 * There is no direct mapping between vpid02 and vpid12, the
12289 * vpid02 is per-vCPU for L0 and reused while the value of
12290 * vpid12 is changed w/ one invvpid during nested vmentry.
12291 * The vpid12 is allocated by L1 for L2, so it will not
12292 * influence global bitmap(for vpid01 and vpid02 allocation)
12293 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012294 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070012295 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
Wanpeng Li5c614b32015-10-13 09:18:36 -070012296 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
12297 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
Liran Alon6bce30c2018-05-22 17:16:12 +030012298 __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012299 }
12300 } else {
Liran Alon14389212018-10-08 23:42:17 +030012301 /*
12302 * If L1 use EPT, then L0 needs to execute INVEPT on
12303 * EPTP02 instead of EPTP01. Therefore, delay TLB
12304 * flush until vmcs02->eptp is fully updated by
12305 * KVM_REQ_LOAD_CR3. Note that this assumes
12306 * KVM_REQ_TLB_FLUSH is evaluated after
12307 * KVM_REQ_LOAD_CR3 in vcpu_enter_guest().
12308 */
12309 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Wanpeng Li5c614b32015-10-13 09:18:36 -070012310 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012311 }
12312
Ladi Prosek1fb883b2017-04-04 14:18:53 +020012313 if (enable_pml) {
12314 /*
12315 * Conceptually we want to copy the PML address and index from
12316 * vmcs01 here, and then back to vmcs01 on nested vmexit. But,
12317 * since we always flush the log on each vmexit, this happens
12318 * to be equivalent to simply resetting the fields in vmcs02.
12319 */
12320 ASSERT(vmx->pml_pg);
12321 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
12322 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
12323 }
12324
Nadav Har'El155a97a2013-08-05 11:07:16 +030012325 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020012326 if (nested_ept_init_mmu_context(vcpu)) {
12327 *entry_failure_code = ENTRY_FAIL_DEFAULT;
12328 return 1;
12329 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070012330 } else if (nested_cpu_has2(vmcs12,
12331 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070012332 vmx_flush_tlb(vcpu, true);
Nadav Har'El155a97a2013-08-05 11:07:16 +030012333 }
12334
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012335 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080012336 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
12337 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012338 * The CR0_READ_SHADOW is what L2 should have expected to read given
12339 * the specifications by L1; It's not enough to take
12340 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
12341 * have more bits than L1 expected.
12342 */
12343 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
12344 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
12345
12346 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
12347 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
12348
Jim Mattson6514dc32018-04-26 16:09:12 -070012349 if (vmx->nested.nested_run_pending &&
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012350 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080012351 vcpu->arch.efer = vmcs12->guest_ia32_efer;
12352 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
12353 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
12354 else
12355 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
12356 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
12357 vmx_set_efer(vcpu, vcpu->arch.efer);
12358
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012359 /*
12360 * Guest state is invalid and unrestricted guest is disabled,
12361 * which means L1 attempted VMEntry to L2 with invalid state.
12362 * Fail the VMEntry.
12363 */
Paolo Bonzini3184a992018-03-21 14:20:18 +010012364 if (vmx->emulation_required) {
12365 *entry_failure_code = ENTRY_FAIL_DEFAULT;
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012366 return 1;
Paolo Bonzini3184a992018-03-21 14:20:18 +010012367 }
Sean Christopherson2bb8caf2018-03-12 10:56:13 -070012368
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012369 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010012370 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010012371 entry_failure_code))
12372 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010012373
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030012374 if (!enable_ept)
12375 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
12376
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012377 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
12378 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010012379 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030012380}
12381
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012382static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12)
12383{
12384 if (!nested_cpu_has_nmi_exiting(vmcs12) &&
12385 nested_cpu_has_virtual_nmis(vmcs12))
12386 return -EINVAL;
12387
12388 if (!nested_cpu_has_virtual_nmis(vmcs12) &&
12389 nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING))
12390 return -EINVAL;
12391
12392 return 0;
12393}
12394
Jim Mattsonca0bde22016-11-30 12:03:46 -080012395static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12396{
12397 struct vcpu_vmx *vmx = to_vmx(vcpu);
12398
12399 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
12400 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
12401 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12402
Krish Sadhukhanba8e23d2018-09-04 14:42:58 -040012403 if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id)
12404 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12405
Jim Mattson56a20512017-07-06 16:33:06 -070012406 if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12))
12407 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12408
Jim Mattsonca0bde22016-11-30 12:03:46 -080012409 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
12410 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12411
Krish Sadhukhanf0f4cf52018-04-11 01:10:16 -040012412 if (nested_vmx_check_apic_access_controls(vcpu, vmcs12))
12413 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12414
Jim Mattson712b12d2017-08-24 13:24:47 -070012415 if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12))
12416 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12417
Jim Mattsonca0bde22016-11-30 12:03:46 -080012418 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
12419 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12420
12421 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
12422 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12423
Bandan Dasc5f983f2017-05-05 15:25:14 -040012424 if (nested_vmx_check_pml_controls(vcpu, vmcs12))
12425 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12426
Liran Alona8a7c022018-06-23 02:35:06 +030012427 if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12))
12428 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12429
Jim Mattsonca0bde22016-11-30 12:03:46 -080012430 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012431 vmx->nested.msrs.procbased_ctls_low,
12432 vmx->nested.msrs.procbased_ctls_high) ||
Jim Mattson2e5b0bd2017-05-04 11:51:58 -070012433 (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
12434 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012435 vmx->nested.msrs.secondary_ctls_low,
12436 vmx->nested.msrs.secondary_ctls_high)) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012437 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012438 vmx->nested.msrs.pinbased_ctls_low,
12439 vmx->nested.msrs.pinbased_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012440 !vmx_control_verify(vmcs12->vm_exit_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012441 vmx->nested.msrs.exit_ctls_low,
12442 vmx->nested.msrs.exit_ctls_high) ||
Jim Mattsonca0bde22016-11-30 12:03:46 -080012443 !vmx_control_verify(vmcs12->vm_entry_controls,
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012444 vmx->nested.msrs.entry_ctls_low,
12445 vmx->nested.msrs.entry_ctls_high))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012446 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12447
Krish Sadhukhan0c7f6502018-02-20 21:24:39 -050012448 if (nested_vmx_check_nmi_controls(vmcs12))
Jim Mattsonca0bde22016-11-30 12:03:46 -080012449 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12450
Bandan Das41ab9372017-08-03 15:54:43 -040012451 if (nested_cpu_has_vmfunc(vmcs12)) {
12452 if (vmcs12->vm_function_control &
Paolo Bonzini6677f3d2018-02-26 13:40:08 +010012453 ~vmx->nested.msrs.vmfunc_controls)
Bandan Das41ab9372017-08-03 15:54:43 -040012454 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12455
12456 if (nested_cpu_has_eptp_switching(vmcs12)) {
12457 if (!nested_cpu_has_ept(vmcs12) ||
12458 !page_address_valid(vcpu, vmcs12->eptp_list_address))
12459 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12460 }
12461 }
Bandan Das27c42a12017-08-03 15:54:42 -040012462
Jim Mattsonc7c2c7092017-05-05 11:28:09 -070012463 if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu))
12464 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12465
Jim Mattsonca0bde22016-11-30 12:03:46 -080012466 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
12467 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
12468 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
12469 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
12470
Marc Orr04473782018-06-20 17:21:29 -070012471 /*
12472 * From the Intel SDM, volume 3:
12473 * Fields relevant to VM-entry event injection must be set properly.
12474 * These fields are the VM-entry interruption-information field, the
12475 * VM-entry exception error code, and the VM-entry instruction length.
12476 */
12477 if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) {
12478 u32 intr_info = vmcs12->vm_entry_intr_info_field;
12479 u8 vector = intr_info & INTR_INFO_VECTOR_MASK;
12480 u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK;
12481 bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK;
12482 bool should_have_error_code;
12483 bool urg = nested_cpu_has2(vmcs12,
12484 SECONDARY_EXEC_UNRESTRICTED_GUEST);
12485 bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE;
12486
12487 /* VM-entry interruption-info field: interruption type */
12488 if (intr_type == INTR_TYPE_RESERVED ||
12489 (intr_type == INTR_TYPE_OTHER_EVENT &&
12490 !nested_cpu_supports_monitor_trap_flag(vcpu)))
12491 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12492
12493 /* VM-entry interruption-info field: vector */
12494 if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) ||
12495 (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) ||
12496 (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0))
12497 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12498
12499 /* VM-entry interruption-info field: deliver error code */
12500 should_have_error_code =
12501 intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode &&
12502 x86_exception_has_error_code(vector);
12503 if (has_error_code != should_have_error_code)
12504 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12505
12506 /* VM-entry exception error code */
12507 if (has_error_code &&
12508 vmcs12->vm_entry_exception_error_code & GENMASK(31, 15))
12509 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12510
12511 /* VM-entry interruption-info field: reserved bits */
12512 if (intr_info & INTR_INFO_RESVD_BITS_MASK)
12513 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12514
12515 /* VM-entry instruction length */
12516 switch (intr_type) {
12517 case INTR_TYPE_SOFT_EXCEPTION:
12518 case INTR_TYPE_SOFT_INTR:
12519 case INTR_TYPE_PRIV_SW_EXCEPTION:
12520 if ((vmcs12->vm_entry_instruction_len > 15) ||
12521 (vmcs12->vm_entry_instruction_len == 0 &&
12522 !nested_cpu_has_zero_length_injection(vcpu)))
12523 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
12524 }
12525 }
12526
Jim Mattsonca0bde22016-11-30 12:03:46 -080012527 return 0;
12528}
12529
Liran Alonf145d902018-06-23 02:35:07 +030012530static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu,
12531 struct vmcs12 *vmcs12)
12532{
12533 int r;
12534 struct page *page;
12535 struct vmcs12 *shadow;
12536
12537 if (vmcs12->vmcs_link_pointer == -1ull)
12538 return 0;
12539
12540 if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer))
12541 return -EINVAL;
12542
12543 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer);
12544 if (is_error_page(page))
12545 return -EINVAL;
12546
12547 r = 0;
12548 shadow = kmap(page);
12549 if (shadow->hdr.revision_id != VMCS12_REVISION ||
12550 shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12))
12551 r = -EINVAL;
12552 kunmap(page);
12553 kvm_release_page_clean(page);
12554 return r;
12555}
12556
Jim Mattsonca0bde22016-11-30 12:03:46 -080012557static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
12558 u32 *exit_qual)
12559{
12560 bool ia32e;
12561
12562 *exit_qual = ENTRY_FAIL_DEFAULT;
12563
12564 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
12565 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
12566 return 1;
12567
Liran Alonf145d902018-06-23 02:35:07 +030012568 if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) {
Jim Mattsonca0bde22016-11-30 12:03:46 -080012569 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
12570 return 1;
12571 }
12572
12573 /*
12574 * If the load IA32_EFER VM-entry control is 1, the following checks
12575 * are performed on the field for the IA32_EFER MSR:
12576 * - Bits reserved in the IA32_EFER MSR must be 0.
12577 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
12578 * the IA-32e mode guest VM-exit control. It must also be identical
12579 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
12580 * CR0.PG) is 1.
12581 */
12582 if (to_vmx(vcpu)->nested.nested_run_pending &&
12583 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
12584 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
12585 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
12586 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
12587 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
12588 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
12589 return 1;
12590 }
12591
12592 /*
12593 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
12594 * IA32_EFER MSR must be 0 in the field for that register. In addition,
12595 * the values of the LMA and LME bits in the field must each be that of
12596 * the host address-space size VM-exit control.
12597 */
12598 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
12599 ia32e = (vmcs12->vm_exit_controls &
12600 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
12601 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
12602 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
12603 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
12604 return 1;
12605 }
12606
Wanpeng Lif1b026a2017-11-05 16:54:48 -080012607 if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) &&
12608 (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) ||
12609 (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD)))
12610 return 1;
12611
Jim Mattsonca0bde22016-11-30 12:03:46 -080012612 return 0;
12613}
12614
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012615/*
Jim Mattson8fcc4b52018-07-10 11:27:20 +020012616 * If exit_qual is NULL, this is being called from state restore (either RSM
12617 * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume.
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012618 */
12619static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual)
Jim Mattson858e25c2016-11-30 12:03:47 -080012620{
12621 struct vcpu_vmx *vmx = to_vmx(vcpu);
12622 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012623 bool from_vmentry = !!exit_qual;
12624 u32 dummy_exit_qual;
Paolo Bonzini7e712682018-10-03 13:44:26 +020012625 bool evaluate_pending_interrupts;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012626 int r = 0;
Jim Mattson858e25c2016-11-30 12:03:47 -080012627
Paolo Bonzini7e712682018-10-03 13:44:26 +020012628 evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
12629 (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING);
12630 if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu))
12631 evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012632
Jim Mattson858e25c2016-11-30 12:03:47 -080012633 enter_guest_mode(vcpu);
12634
12635 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
12636 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Liran Alon62cf9bd812018-09-14 03:25:54 +030012637 if (kvm_mpx_supported() &&
12638 !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS))
12639 vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattson858e25c2016-11-30 12:03:47 -080012640
Jim Mattsonde3a0022017-11-27 17:22:25 -060012641 vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080012642 vmx_segment_cache_clear(vmx);
12643
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012644 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12645 vcpu->arch.tsc_offset += vmcs12->tsc_offset;
12646
12647 r = EXIT_REASON_INVALID_STATE;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012648 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual))
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012649 goto fail;
Jim Mattson858e25c2016-11-30 12:03:47 -080012650
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012651 if (from_vmentry) {
12652 nested_get_vmcs12_pages(vcpu);
Jim Mattson858e25c2016-11-30 12:03:47 -080012653
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012654 r = EXIT_REASON_MSR_LOAD_FAIL;
12655 *exit_qual = nested_vmx_load_msr(vcpu,
12656 vmcs12->vm_entry_msr_load_addr,
12657 vmcs12->vm_entry_msr_load_count);
12658 if (*exit_qual)
12659 goto fail;
12660 } else {
12661 /*
12662 * The MMU is not initialized to point at the right entities yet and
12663 * "get pages" would need to read data from the guest (i.e. we will
12664 * need to perform gpa to hpa translation). Request a call
12665 * to nested_get_vmcs12_pages before the next VM-entry. The MSRs
12666 * have already been set at vmentry time and should not be reset.
12667 */
12668 kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu);
12669 }
Jim Mattson858e25c2016-11-30 12:03:47 -080012670
Jim Mattson858e25c2016-11-30 12:03:47 -080012671 /*
Liran Alonb5861e52018-09-03 15:20:22 +030012672 * If L1 had a pending IRQ/NMI until it executed
12673 * VMLAUNCH/VMRESUME which wasn't delivered because it was
12674 * disallowed (e.g. interrupts disabled), L0 needs to
12675 * evaluate if this pending event should cause an exit from L2
12676 * to L1 or delivered directly to L2 (e.g. In case L1 don't
12677 * intercept EXTERNAL_INTERRUPT).
12678 *
Paolo Bonzini7e712682018-10-03 13:44:26 +020012679 * Usually this would be handled by the processor noticing an
12680 * IRQ/NMI window request, or checking RVI during evaluation of
12681 * pending virtual interrupts. However, this setting was done
12682 * on VMCS01 and now VMCS02 is active instead. Thus, we force L0
12683 * to perform pending event evaluation by requesting a KVM_REQ_EVENT.
Liran Alonb5861e52018-09-03 15:20:22 +030012684 */
Paolo Bonzini7e712682018-10-03 13:44:26 +020012685 if (unlikely(evaluate_pending_interrupts))
Liran Alonb5861e52018-09-03 15:20:22 +030012686 kvm_make_request(KVM_REQ_EVENT, vcpu);
Liran Alonb5861e52018-09-03 15:20:22 +030012687
12688 /*
Jim Mattson858e25c2016-11-30 12:03:47 -080012689 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
12690 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
12691 * returned as far as L1 is concerned. It will only return (and set
12692 * the success flag) when L2 exits (see nested_vmx_vmexit()).
12693 */
12694 return 0;
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020012695
12696fail:
12697 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
12698 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
12699 leave_guest_mode(vcpu);
12700 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012701 return r;
Jim Mattson858e25c2016-11-30 12:03:47 -080012702}
12703
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012704/*
12705 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
12706 * for running an L2 nested guest.
12707 */
12708static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
12709{
12710 struct vmcs12 *vmcs12;
12711 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012712 u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080012713 u32 exit_qual;
12714 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012715
Kyle Hueyeb277562016-11-29 12:40:39 -080012716 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012717 return 1;
12718
Kyle Hueyeb277562016-11-29 12:40:39 -080012719 if (!nested_vmx_check_vmcs12(vcpu))
12720 goto out;
12721
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012722 vmcs12 = get_vmcs12(vcpu);
12723
Liran Alona6192d42018-06-23 02:35:04 +030012724 /*
12725 * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact
12726 * that there *is* a valid VMCS pointer, RFLAGS.CF is set
12727 * rather than RFLAGS.ZF, and no error number is stored to the
12728 * VM-instruction error field.
12729 */
12730 if (vmcs12->hdr.shadow_vmcs) {
12731 nested_vmx_failInvalid(vcpu);
12732 goto out;
12733 }
12734
Abel Gordon012f83c2013-04-18 14:39:25 +030012735 if (enable_shadow_vmcs)
12736 copy_shadow_to_vmcs12(vmx);
12737
Nadav Har'El7c177932011-05-25 23:12:04 +030012738 /*
12739 * The nested entry process starts with enforcing various prerequisites
12740 * on vmcs12 as required by the Intel SDM, and act appropriately when
12741 * they fail: As the SDM explains, some conditions should cause the
12742 * instruction to fail, while others will cause the instruction to seem
12743 * to succeed, but return an EXIT_REASON_INVALID_STATE.
12744 * To speed up the normal (success) code path, we should avoid checking
12745 * for misconfigurations which will anyway be caught by the processor
12746 * when using the merged vmcs02.
12747 */
Jim Mattsonb3f1dfb2017-07-17 12:00:34 -070012748 if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) {
12749 nested_vmx_failValid(vcpu,
12750 VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS);
12751 goto out;
12752 }
12753
Nadav Har'El7c177932011-05-25 23:12:04 +030012754 if (vmcs12->launch_state == launch) {
12755 nested_vmx_failValid(vcpu,
12756 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
12757 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080012758 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030012759 }
12760
Jim Mattsonca0bde22016-11-30 12:03:46 -080012761 ret = check_vmentry_prereqs(vcpu, vmcs12);
12762 if (ret) {
12763 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080012764 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020012765 }
12766
Nadav Har'El7c177932011-05-25 23:12:04 +030012767 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080012768 * After this point, the trap flag no longer triggers a singlestep trap
12769 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
12770 * This is not 100% correct; for performance reasons, we delegate most
12771 * of the checks on host state to the processor. If those fail,
12772 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020012773 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080012774 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020012775
Jim Mattsonca0bde22016-11-30 12:03:46 -080012776 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
12777 if (ret) {
12778 nested_vmx_entry_failure(vcpu, vmcs12,
12779 EXIT_REASON_INVALID_STATE, exit_qual);
12780 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020012781 }
12782
12783 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030012784 * We're finally done with prerequisite checking, and can start with
12785 * the nested entry.
12786 */
12787
Jim Mattson6514dc32018-04-26 16:09:12 -070012788 vmx->nested.nested_run_pending = 1;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012789 ret = enter_vmx_non_root_mode(vcpu, &exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012790 if (ret) {
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012791 nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual);
Jim Mattson6514dc32018-04-26 16:09:12 -070012792 vmx->nested.nested_run_pending = 0;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020012793 return 1;
Jim Mattson6514dc32018-04-26 16:09:12 -070012794 }
Wincy Vanff651cb2014-12-11 08:52:58 +030012795
Paolo Bonzinic595cee2018-07-02 13:07:14 +020012796 /* Hide L1D cache contents from the nested guest. */
12797 vmx->vcpu.arch.l1tf_flush_l1d = true;
12798
Chao Gao135a06c2018-02-11 10:06:30 +080012799 /*
Liran Alon61ada742018-06-23 02:35:08 +030012800 * Must happen outside of enter_vmx_non_root_mode() as it will
12801 * also be used as part of restoring nVMX state for
12802 * snapshot restore (migration).
12803 *
12804 * In this flow, it is assumed that vmcs12 cache was
12805 * trasferred as part of captured nVMX state and should
12806 * therefore not be read from guest memory (which may not
12807 * exist on destination host yet).
12808 */
12809 nested_cache_shadow_vmcs12(vcpu, vmcs12);
12810
12811 /*
Chao Gao135a06c2018-02-11 10:06:30 +080012812 * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken
12813 * by event injection, halt vcpu.
12814 */
12815 if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) &&
Jim Mattson6514dc32018-04-26 16:09:12 -070012816 !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) {
12817 vmx->nested.nested_run_pending = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -060012818 return kvm_vcpu_halt(vcpu);
Jim Mattson6514dc32018-04-26 16:09:12 -070012819 }
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012820 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080012821
12822out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080012823 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030012824}
12825
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012826/*
12827 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
12828 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
12829 * This function returns the new value we should put in vmcs12.guest_cr0.
12830 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
12831 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
12832 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
12833 * didn't trap the bit, because if L1 did, so would L0).
12834 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
12835 * been modified by L2, and L1 knows it. So just leave the old value of
12836 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
12837 * isn't relevant, because if L0 traps this bit it can set it to anything.
12838 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
12839 * changed these bits, and therefore they need to be updated, but L0
12840 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
12841 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
12842 */
12843static inline unsigned long
12844vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12845{
12846 return
12847 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
12848 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
12849 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
12850 vcpu->arch.cr0_guest_owned_bits));
12851}
12852
12853static inline unsigned long
12854vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
12855{
12856 return
12857 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
12858 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
12859 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
12860 vcpu->arch.cr4_guest_owned_bits));
12861}
12862
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012863static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
12864 struct vmcs12 *vmcs12)
12865{
12866 u32 idt_vectoring;
12867 unsigned int nr;
12868
Wanpeng Li664f8e22017-08-24 03:35:09 -070012869 if (vcpu->arch.exception.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012870 nr = vcpu->arch.exception.nr;
12871 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12872
12873 if (kvm_exception_is_soft(nr)) {
12874 vmcs12->vm_exit_instruction_len =
12875 vcpu->arch.event_exit_inst_len;
12876 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
12877 } else
12878 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
12879
12880 if (vcpu->arch.exception.has_error_code) {
12881 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
12882 vmcs12->idt_vectoring_error_code =
12883 vcpu->arch.exception.error_code;
12884 }
12885
12886 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010012887 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012888 vmcs12->idt_vectoring_info_field =
12889 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
Liran Alon04140b42018-03-23 03:01:31 +030012890 } else if (vcpu->arch.interrupt.injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020012891 nr = vcpu->arch.interrupt.nr;
12892 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
12893
12894 if (vcpu->arch.interrupt.soft) {
12895 idt_vectoring |= INTR_TYPE_SOFT_INTR;
12896 vmcs12->vm_entry_instruction_len =
12897 vcpu->arch.event_exit_inst_len;
12898 } else
12899 idt_vectoring |= INTR_TYPE_EXT_INTR;
12900
12901 vmcs12->idt_vectoring_info_field = idt_vectoring;
12902 }
12903}
12904
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012905static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
12906{
12907 struct vcpu_vmx *vmx = to_vmx(vcpu);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012908 unsigned long exit_qual;
Liran Alon917dc602017-11-05 16:07:43 +020012909 bool block_nested_events =
12910 vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu);
Wanpeng Liacc9ab62017-02-27 04:24:39 -080012911
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012912 if (vcpu->arch.exception.pending &&
12913 nested_vmx_check_exception(vcpu, &exit_qual)) {
Liran Alon917dc602017-11-05 16:07:43 +020012914 if (block_nested_events)
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012915 return -EBUSY;
12916 nested_vmx_inject_exception_vmexit(vcpu, exit_qual);
Wanpeng Libfcf83b2017-08-24 03:35:11 -070012917 return 0;
12918 }
12919
Jan Kiszkaf41245002014-03-07 20:03:13 +010012920 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
12921 vmx->nested.preemption_timer_expired) {
Liran Alon917dc602017-11-05 16:07:43 +020012922 if (block_nested_events)
Jan Kiszkaf41245002014-03-07 20:03:13 +010012923 return -EBUSY;
12924 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
12925 return 0;
12926 }
12927
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012928 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012929 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012930 return -EBUSY;
12931 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
12932 NMI_VECTOR | INTR_TYPE_NMI_INTR |
12933 INTR_INFO_VALID_MASK, 0);
12934 /*
12935 * The NMI-triggered VM exit counts as injection:
12936 * clear this one and block further NMIs.
12937 */
12938 vcpu->arch.nmi_pending = 0;
12939 vmx_set_nmi_mask(vcpu, true);
12940 return 0;
12941 }
12942
12943 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
12944 nested_exit_on_intr(vcpu)) {
Liran Alon917dc602017-11-05 16:07:43 +020012945 if (block_nested_events)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012946 return -EBUSY;
12947 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080012948 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012949 }
12950
David Hildenbrand6342c502017-01-25 11:58:58 +010012951 vmx_complete_nested_posted_interrupt(vcpu);
12952 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010012953}
12954
Sean Christophersond264ee02018-08-27 15:21:12 -070012955static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
12956{
12957 to_vmx(vcpu)->req_immediate_exit = true;
12958}
12959
Jan Kiszkaf41245002014-03-07 20:03:13 +010012960static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
12961{
12962 ktime_t remaining =
12963 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
12964 u64 value;
12965
12966 if (ktime_to_ns(remaining) <= 0)
12967 return 0;
12968
12969 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
12970 do_div(value, 1000000);
12971 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
12972}
12973
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012974/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012975 * Update the guest state fields of vmcs12 to reflect changes that
12976 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
12977 * VM-entry controls is also updated, since this is really a guest
12978 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012979 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080012980static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012981{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012982 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
12983 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
12984
Nadav Har'El4704d0b2011-05-25 23:11:34 +030012985 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
12986 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
12987 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
12988
12989 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
12990 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
12991 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
12992 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
12993 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
12994 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
12995 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
12996 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
12997 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
12998 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
12999 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
13000 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
13001 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
13002 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
13003 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
13004 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
13005 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
13006 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
13007 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
13008 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
13009 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
13010 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
13011 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
13012 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
13013 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
13014 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
13015 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
13016 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
13017 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
13018 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
13019 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
13020 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
13021 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
13022 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
13023 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
13024 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
13025
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013026 vmcs12->guest_interruptibility_info =
13027 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
13028 vmcs12->guest_pending_dbg_exceptions =
13029 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010013030 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
13031 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
13032 else
13033 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013034
Jan Kiszkaf41245002014-03-07 20:03:13 +010013035 if (nested_cpu_has_preemption_timer(vmcs12)) {
13036 if (vmcs12->vm_exit_controls &
13037 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
13038 vmcs12->vmx_preemption_timer_value =
13039 vmx_get_preemption_timer_value(vcpu);
13040 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
13041 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080013042
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013043 /*
13044 * In some cases (usually, nested EPT), L2 is allowed to change its
13045 * own CR3 without exiting. If it has changed it, we must keep it.
13046 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
13047 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
13048 *
13049 * Additionally, restore L2's PDPTR to vmcs12.
13050 */
13051 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010013052 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030013053 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
13054 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
13055 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
13056 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
13057 }
13058
Jim Mattsond281e132017-06-01 12:44:46 -070013059 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
Jan Dakinevich119a9c02016-09-04 21:22:47 +030013060
Wincy Van608406e2015-02-03 23:57:51 +080013061 if (nested_cpu_has_vid(vmcs12))
13062 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
13063
Jan Kiszkac18911a2013-03-13 16:06:41 +010013064 vmcs12->vm_entry_controls =
13065 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020013066 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010013067
Jan Kiszka2996fca2014-06-16 13:59:43 +020013068 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
13069 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
13070 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
13071 }
13072
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013073 /* TODO: These cannot have changed unless we have MSR bitmaps and
13074 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020013075 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013076 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020013077 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
13078 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013079 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
13080 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
13081 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010013082 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013083 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080013084}
13085
13086/*
13087 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
13088 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
13089 * and this function updates it to reflect the changes to the guest state while
13090 * L2 was running (and perhaps made some exits which were handled directly by L0
13091 * without going back to L1), and to reflect the exit reason.
13092 * Note that we do not have to copy here all VMCS fields, just those that
13093 * could have changed by the L2 guest or the exit - i.e., the guest-state and
13094 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
13095 * which already writes to vmcs12 directly.
13096 */
13097static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
13098 u32 exit_reason, u32 exit_intr_info,
13099 unsigned long exit_qualification)
13100{
13101 /* update guest state fields: */
13102 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013103
13104 /* update exit information fields: */
13105
Jan Kiszka533558b2014-01-04 18:47:20 +010013106 vmcs12->vm_exit_reason = exit_reason;
13107 vmcs12->exit_qualification = exit_qualification;
Jan Kiszka533558b2014-01-04 18:47:20 +010013108 vmcs12->vm_exit_intr_info = exit_intr_info;
Paolo Bonzini7313c692017-07-27 10:31:25 +020013109
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013110 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013111 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
13112 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
13113
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013114 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
Jim Mattson7cdc2d62017-07-06 16:33:05 -070013115 vmcs12->launch_state = 1;
13116
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013117 /* vm_entry_intr_info_field is cleared on exit. Emulate this
13118 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013119 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013120
13121 /*
13122 * Transfer the event that L0 or L1 may wanted to inject into
13123 * L2 to IDT_VECTORING_INFO_FIELD.
13124 */
13125 vmcs12_save_pending_event(vcpu, vmcs12);
13126 }
13127
13128 /*
13129 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
13130 * preserved above and would only end up incorrectly in L1.
13131 */
13132 vcpu->arch.nmi_injected = false;
13133 kvm_clear_exception_queue(vcpu);
13134 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013135}
13136
Wanpeng Li5af41572017-11-05 16:54:49 -080013137static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu,
13138 struct vmcs12 *vmcs12)
13139{
13140 u32 entry_failure_code;
13141
13142 nested_ept_uninit_mmu_context(vcpu);
13143
13144 /*
13145 * Only PDPTE load can fail as the value of cr3 was checked on entry and
13146 * couldn't have changed.
13147 */
13148 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
13149 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
13150
13151 if (!enable_ept)
13152 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
13153}
13154
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013155/*
13156 * A part of what we need to when the nested L2 guest exits and we want to
13157 * run its L1 parent, is to reset L1's guest state to the host state specified
13158 * in vmcs12.
13159 * This function is to be called not only on normal nested exit, but also on
13160 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
13161 * Failures During or After Loading Guest State").
13162 * This function should be called when the active VMCS is L1's (vmcs01).
13163 */
Jan Kiszka733568f2013-02-23 15:07:47 +010013164static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
13165 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013166{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013167 struct kvm_segment seg;
13168
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013169 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
13170 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020013171 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013172 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
13173 else
13174 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
13175 vmx_set_efer(vcpu, vcpu->arch.efer);
13176
13177 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
13178 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070013179 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013180 /*
13181 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013182 * actually changed, because vmx_set_cr0 refers to efer set above.
13183 *
13184 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
13185 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013186 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013187 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020013188 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013189
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080013190 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013191 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
Haozhong Zhang8eb3f872017-10-10 15:01:22 +080013192 vmx_set_cr4(vcpu, vmcs12->host_cr4);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013193
Wanpeng Li5af41572017-11-05 16:54:49 -080013194 load_vmcs12_mmu_host_state(vcpu, vmcs12);
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030013195
Liran Alon6f1e03b2018-05-22 17:16:14 +030013196 /*
13197 * If vmcs01 don't use VPID, CPU flushes TLB on every
13198 * VMEntry/VMExit. Thus, no need to flush TLB.
13199 *
13200 * If vmcs12 uses VPID, TLB entries populated by L2 are
13201 * tagged with vmx->nested.vpid02 while L1 entries are tagged
13202 * with vmx->vpid. Thus, no need to flush TLB.
13203 *
13204 * Therefore, flush TLB only in case vmcs01 uses VPID and
13205 * vmcs12 don't use VPID as in this case L1 & L2 TLB entries
13206 * are both tagged with vmx->vpid.
Liran Alon14389212018-10-08 23:42:17 +030013207 *
13208 * If vmcs12 uses EPT, we need to execute this flush on EPTP01
13209 * and therefore we request the TLB flush to happen only after VMCS EPTP
13210 * has been set by KVM_REQ_LOAD_CR3.
Liran Alon6f1e03b2018-05-22 17:16:14 +030013211 */
13212 if (enable_vpid &&
13213 !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) {
Liran Alon14389212018-10-08 23:42:17 +030013214 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013215 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013216
13217 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
13218 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
13219 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
13220 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
13221 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Ladi Prosek21f2d552017-10-11 16:54:42 +020013222 vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF);
13223 vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013224
Paolo Bonzini36be0b92014-02-24 12:30:04 +010013225 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
13226 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
13227 vmcs_write64(GUEST_BNDCFGS, 0);
13228
Jan Kiszka44811c02013-08-04 17:17:27 +020013229 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013230 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020013231 vcpu->arch.pat = vmcs12->host_ia32_pat;
13232 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013233 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
13234 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
13235 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013236
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013237 /* Set L1 segment info according to Intel SDM
13238 27.5.2 Loading Host Segment and Descriptor-Table Registers */
13239 seg = (struct kvm_segment) {
13240 .base = 0,
13241 .limit = 0xFFFFFFFF,
13242 .selector = vmcs12->host_cs_selector,
13243 .type = 11,
13244 .present = 1,
13245 .s = 1,
13246 .g = 1
13247 };
13248 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
13249 seg.l = 1;
13250 else
13251 seg.db = 1;
13252 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
13253 seg = (struct kvm_segment) {
13254 .base = 0,
13255 .limit = 0xFFFFFFFF,
13256 .type = 3,
13257 .present = 1,
13258 .s = 1,
13259 .db = 1,
13260 .g = 1
13261 };
13262 seg.selector = vmcs12->host_ds_selector;
13263 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
13264 seg.selector = vmcs12->host_es_selector;
13265 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
13266 seg.selector = vmcs12->host_ss_selector;
13267 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
13268 seg.selector = vmcs12->host_fs_selector;
13269 seg.base = vmcs12->host_fs_base;
13270 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
13271 seg.selector = vmcs12->host_gs_selector;
13272 seg.base = vmcs12->host_gs_base;
13273 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
13274 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030013275 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080013276 .limit = 0x67,
13277 .selector = vmcs12->host_tr_selector,
13278 .type = 11,
13279 .present = 1
13280 };
13281 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
13282
Jan Kiszka503cd0c2013-03-03 13:05:44 +010013283 kvm_set_dr(vcpu, 7, 0x400);
13284 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030013285
Wincy Van3af18d92015-02-03 23:49:31 +080013286 if (cpu_has_vmx_msr_bitmap())
Paolo Bonzini904e14f2018-01-16 16:51:18 +010013287 vmx_update_msr_bitmap(vcpu);
Wincy Van3af18d92015-02-03 23:49:31 +080013288
Wincy Vanff651cb2014-12-11 08:52:58 +030013289 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
13290 vmcs12->vm_exit_msr_load_count))
13291 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013292}
13293
13294/*
13295 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
13296 * and modify vmcs12 to make it see what it would expect to see there if
13297 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
13298 */
Jan Kiszka533558b2014-01-04 18:47:20 +010013299static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
13300 u32 exit_intr_info,
13301 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013302{
13303 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013304 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13305
Jan Kiszka5f3d5792013-04-14 12:12:46 +020013306 /* trying to cancel vmlaunch/vmresume is a bug */
13307 WARN_ON_ONCE(vmx->nested.nested_run_pending);
13308
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013309 /*
Jim Mattson4f350c62017-09-14 16:31:44 -070013310 * The only expected VM-instruction error is "VM entry with
13311 * invalid control field(s)." Anything else indicates a
13312 * problem with L0.
Wanpeng Li6550c4d2017-07-31 19:25:27 -070013313 */
Jim Mattson4f350c62017-09-14 16:31:44 -070013314 WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) !=
13315 VMXERR_ENTRY_INVALID_CONTROL_FIELD));
13316
13317 leave_guest_mode(vcpu);
13318
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020013319 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
13320 vcpu->arch.tsc_offset -= vmcs12->tsc_offset;
13321
Jim Mattson4f350c62017-09-14 16:31:44 -070013322 if (likely(!vmx->fail)) {
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013323 if (exit_reason == -1)
13324 sync_vmcs12(vcpu, vmcs12);
13325 else
13326 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
13327 exit_qualification);
Jim Mattson4f350c62017-09-14 16:31:44 -070013328
Liran Alon61ada742018-06-23 02:35:08 +030013329 /*
13330 * Must happen outside of sync_vmcs12() as it will
13331 * also be used to capture vmcs12 cache as part of
13332 * capturing nVMX state for snapshot (migration).
13333 *
13334 * Otherwise, this flush will dirty guest memory at a
13335 * point it is already assumed by user-space to be
13336 * immutable.
13337 */
13338 nested_flush_cached_shadow_vmcs12(vcpu, vmcs12);
13339
Jim Mattson4f350c62017-09-14 16:31:44 -070013340 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
13341 vmcs12->vm_exit_msr_store_count))
13342 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
Bandan Das77b0f5d2014-04-19 18:17:45 -040013343 }
13344
Jim Mattson4f350c62017-09-14 16:31:44 -070013345 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Paolo Bonzini8391ce42016-07-07 14:58:33 +020013346 vm_entry_controls_reset_shadow(vmx);
13347 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010013348 vmx_segment_cache_clear(vmx);
13349
Paolo Bonzini9314006db2016-07-06 13:23:51 +020013350 /* Update any VMCS fields that might have changed while L2 ran */
Konrad Rzeszutek Wilk33966dd62018-06-20 13:58:37 -040013351 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr);
13352 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010013353 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Sean Christophersonf459a702018-08-27 15:21:11 -070013354
Peter Feinerc95ba922016-08-17 09:36:47 -070013355 if (kvm_has_tsc_control)
13356 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013357
Jim Mattson8d860bb2018-05-09 16:56:05 -040013358 if (vmx->nested.change_vmcs01_virtual_apic_mode) {
13359 vmx->nested.change_vmcs01_virtual_apic_mode = false;
13360 vmx_set_virtual_apic_mode(vcpu);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070013361 } else if (!nested_cpu_has_ept(vmcs12) &&
13362 nested_cpu_has2(vmcs12,
13363 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Junaid Shahida468f2d2018-04-26 13:09:50 -070013364 vmx_flush_tlb(vcpu, true);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020013365 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013366
13367 /* This is needed for same reason as it was needed in prepare_vmcs02 */
13368 vmx->host_rsp = 0;
13369
13370 /* Unpin physical memory we referred to in vmcs02 */
13371 if (vmx->nested.apic_access_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013372 kvm_release_page_dirty(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013373 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013374 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013375 if (vmx->nested.virtual_apic_page) {
David Hildenbrand53a70da2017-08-03 18:11:05 +020013376 kvm_release_page_dirty(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013377 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080013378 }
Wincy Van705699a2015-02-03 23:58:17 +080013379 if (vmx->nested.pi_desc_page) {
13380 kunmap(vmx->nested.pi_desc_page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013381 kvm_release_page_dirty(vmx->nested.pi_desc_page);
Wincy Van705699a2015-02-03 23:58:17 +080013382 vmx->nested.pi_desc_page = NULL;
13383 vmx->nested.pi_desc = NULL;
13384 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013385
13386 /*
Tang Chen38b99172014-09-24 15:57:54 +080013387 * We are now running in L2, mmu_notifier will force to reload the
13388 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
13389 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080013390 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080013391
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013392 if (enable_shadow_vmcs && exit_reason != -1)
Abel Gordon012f83c2013-04-18 14:39:25 +030013393 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010013394
13395 /* in case we halted in L2 */
13396 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Jim Mattson4f350c62017-09-14 16:31:44 -070013397
13398 if (likely(!vmx->fail)) {
13399 /*
13400 * TODO: SDM says that with acknowledge interrupt on
13401 * exit, bit 31 of the VM-exit interrupt information
13402 * (valid interrupt) is always set to 1 on
13403 * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't
13404 * need kvm_cpu_has_interrupt(). See the commit
13405 * message for details.
13406 */
13407 if (nested_exit_intr_ack_set(vcpu) &&
13408 exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT &&
13409 kvm_cpu_has_interrupt(vcpu)) {
13410 int irq = kvm_cpu_get_interrupt(vcpu);
13411 WARN_ON(irq < 0);
13412 vmcs12->vm_exit_intr_info = irq |
13413 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
13414 }
13415
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013416 if (exit_reason != -1)
13417 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
13418 vmcs12->exit_qualification,
13419 vmcs12->idt_vectoring_info_field,
13420 vmcs12->vm_exit_intr_info,
13421 vmcs12->vm_exit_intr_error_code,
13422 KVM_ISA_VMX);
Jim Mattson4f350c62017-09-14 16:31:44 -070013423
13424 load_vmcs12_host_state(vcpu, vmcs12);
13425
13426 return;
13427 }
13428
13429 /*
13430 * After an early L2 VM-entry failure, we're now back
13431 * in L1 which thinks it just finished a VMLAUNCH or
13432 * VMRESUME instruction, so we need to set the failure
13433 * flag and the VM-instruction error field of the VMCS
13434 * accordingly.
13435 */
13436 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
Wanpeng Li5af41572017-11-05 16:54:49 -080013437
13438 load_vmcs12_mmu_host_state(vcpu, vmcs12);
13439
Jim Mattson4f350c62017-09-14 16:31:44 -070013440 /*
13441 * The emulated instruction was already skipped in
13442 * nested_vmx_run, but the updated RIP was never
13443 * written back to the vmcs01.
13444 */
13445 skip_emulated_instruction(vcpu);
13446 vmx->fail = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030013447}
13448
Nadav Har'El7c177932011-05-25 23:12:04 +030013449/*
Jan Kiszka42124922014-01-04 18:47:19 +010013450 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
13451 */
13452static void vmx_leave_nested(struct kvm_vcpu *vcpu)
13453{
Wanpeng Li2f707d92017-03-06 04:03:28 -080013454 if (is_guest_mode(vcpu)) {
13455 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010013456 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080013457 }
Jan Kiszka42124922014-01-04 18:47:19 +010013458 free_nested(to_vmx(vcpu));
13459}
13460
13461/*
Nadav Har'El7c177932011-05-25 23:12:04 +030013462 * L1's failure to enter L2 is a subset of a normal exit, as explained in
13463 * 23.7 "VM-entry failures during or after loading guest state" (this also
13464 * lists the acceptable exit-reason and exit-qualification parameters).
13465 * It should only be called before L2 actually succeeded to run, and when
13466 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
13467 */
13468static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
13469 struct vmcs12 *vmcs12,
13470 u32 reason, unsigned long qualification)
13471{
13472 load_vmcs12_host_state(vcpu, vmcs12);
13473 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
13474 vmcs12->exit_qualification = qualification;
13475 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030013476 if (enable_shadow_vmcs)
13477 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030013478}
13479
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013480static int vmx_check_intercept(struct kvm_vcpu *vcpu,
13481 struct x86_instruction_info *info,
13482 enum x86_intercept_stage stage)
13483{
Paolo Bonzinifb6d4d32016-07-12 11:04:26 +020013484 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
13485 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
13486
13487 /*
13488 * RDPID causes #UD if disabled through secondary execution controls.
13489 * Because it is marked as EmulateOnUD, we need to intercept it here.
13490 */
13491 if (info->intercept == x86_intercept_rdtscp &&
13492 !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
13493 ctxt->exception.vector = UD_VECTOR;
13494 ctxt->exception.error_code_valid = false;
13495 return X86EMUL_PROPAGATE_FAULT;
13496 }
13497
13498 /* TODO: check more intercepts... */
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020013499 return X86EMUL_CONTINUE;
13500}
13501
Yunhong Jiang64672c92016-06-13 14:19:59 -070013502#ifdef CONFIG_X86_64
13503/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
13504static inline int u64_shl_div_u64(u64 a, unsigned int shift,
13505 u64 divisor, u64 *result)
13506{
13507 u64 low = a << shift, high = a >> (64 - shift);
13508
13509 /* To avoid the overflow on divq */
13510 if (high >= divisor)
13511 return 1;
13512
13513 /* Low hold the result, high hold rem which is discarded */
13514 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
13515 "rm" (divisor), "0" (low), "1" (high));
13516 *result = low;
13517
13518 return 0;
13519}
13520
13521static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
13522{
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013523 struct vcpu_vmx *vmx;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013524 u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
KarimAllah Ahmed386c6dd2018-04-10 14:15:46 +020013525
13526 if (kvm_mwait_in_guest(vcpu->kvm))
13527 return -EOPNOTSUPP;
13528
13529 vmx = to_vmx(vcpu);
13530 tscl = rdtsc();
13531 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
13532 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Wanpeng Lic5ce8232018-05-29 14:53:17 +080013533 lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns);
13534
13535 if (delta_tsc > lapic_timer_advance_cycles)
13536 delta_tsc -= lapic_timer_advance_cycles;
13537 else
13538 delta_tsc = 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013539
13540 /* Convert to host delta tsc if tsc scaling is enabled */
13541 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
13542 u64_shl_div_u64(delta_tsc,
13543 kvm_tsc_scaling_ratio_frac_bits,
13544 vcpu->arch.tsc_scaling_ratio,
13545 &delta_tsc))
13546 return -ERANGE;
13547
13548 /*
13549 * If the delta tsc can't fit in the 32 bit after the multi shift,
13550 * we can't use the preemption timer.
13551 * It's possible that it fits on later vmentries, but checking
13552 * on every vmentry is costly so we just use an hrtimer.
13553 */
13554 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
13555 return -ERANGE;
13556
13557 vmx->hv_deadline_tsc = tscl + delta_tsc;
Wanpeng Lic8533542017-06-29 06:28:09 -070013558 return delta_tsc == 0;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013559}
13560
13561static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
13562{
Sean Christophersonf459a702018-08-27 15:21:11 -070013563 to_vmx(vcpu)->hv_deadline_tsc = -1;
Yunhong Jiang64672c92016-06-13 14:19:59 -070013564}
13565#endif
13566
Paolo Bonzini48d89b92014-08-26 13:27:46 +020013567static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013568{
Wanpeng Lib31c1142018-03-12 04:53:04 -070013569 if (!kvm_pause_in_guest(vcpu->kvm))
Radim Krčmářb4a2d312014-08-21 18:08:08 +020013570 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020013571}
13572
Kai Huang843e4332015-01-28 10:54:28 +080013573static void vmx_slot_enable_log_dirty(struct kvm *kvm,
13574 struct kvm_memory_slot *slot)
13575{
13576 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
13577 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
13578}
13579
13580static void vmx_slot_disable_log_dirty(struct kvm *kvm,
13581 struct kvm_memory_slot *slot)
13582{
13583 kvm_mmu_slot_set_dirty(kvm, slot);
13584}
13585
13586static void vmx_flush_log_dirty(struct kvm *kvm)
13587{
13588 kvm_flush_pml_buffers(kvm);
13589}
13590
Bandan Dasc5f983f2017-05-05 15:25:14 -040013591static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
13592{
13593 struct vmcs12 *vmcs12;
13594 struct vcpu_vmx *vmx = to_vmx(vcpu);
13595 gpa_t gpa;
13596 struct page *page = NULL;
13597 u64 *pml_address;
13598
13599 if (is_guest_mode(vcpu)) {
13600 WARN_ON_ONCE(vmx->nested.pml_full);
13601
13602 /*
13603 * Check if PML is enabled for the nested guest.
13604 * Whether eptp bit 6 is set is already checked
13605 * as part of A/D emulation.
13606 */
13607 vmcs12 = get_vmcs12(vcpu);
13608 if (!nested_cpu_has_pml(vmcs12))
13609 return 0;
13610
Dan Carpenter47698862017-05-10 22:43:17 +030013611 if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
Bandan Dasc5f983f2017-05-05 15:25:14 -040013612 vmx->nested.pml_full = true;
13613 return 1;
13614 }
13615
13616 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
13617
David Hildenbrand5e2f30b2017-08-03 18:11:04 +020013618 page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address);
13619 if (is_error_page(page))
Bandan Dasc5f983f2017-05-05 15:25:14 -040013620 return 0;
13621
13622 pml_address = kmap(page);
13623 pml_address[vmcs12->guest_pml_index--] = gpa;
13624 kunmap(page);
David Hildenbrand53a70da2017-08-03 18:11:05 +020013625 kvm_release_page_clean(page);
Bandan Dasc5f983f2017-05-05 15:25:14 -040013626 }
13627
13628 return 0;
13629}
13630
Kai Huang843e4332015-01-28 10:54:28 +080013631static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
13632 struct kvm_memory_slot *memslot,
13633 gfn_t offset, unsigned long mask)
13634{
13635 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
13636}
13637
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013638static void __pi_post_block(struct kvm_vcpu *vcpu)
13639{
13640 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13641 struct pi_desc old, new;
13642 unsigned int dest;
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013643
13644 do {
13645 old.control = new.control = pi_desc->control;
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013646 WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
13647 "Wakeup handler not enabled while the VCPU is blocked\n");
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013648
13649 dest = cpu_physical_id(vcpu->cpu);
13650
13651 if (x2apic_enabled())
13652 new.ndst = dest;
13653 else
13654 new.ndst = (dest << 8) & 0xFF00;
13655
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013656 /* set 'NV' to 'notification vector' */
13657 new.nv = POSTED_INTR_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013658 } while (cmpxchg64(&pi_desc->control, old.control,
13659 new.control) != old.control);
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013660
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013661 if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
13662 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013663 list_del(&vcpu->blocked_vcpu_list);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013664 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013665 vcpu->pre_pcpu = -1;
13666 }
13667}
13668
Feng Wuefc64402015-09-18 22:29:51 +080013669/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080013670 * This routine does the following things for vCPU which is going
13671 * to be blocked if VT-d PI is enabled.
13672 * - Store the vCPU to the wakeup list, so when interrupts happen
13673 * we can find the right vCPU to wake up.
13674 * - Change the Posted-interrupt descriptor as below:
13675 * 'NDST' <-- vcpu->pre_pcpu
13676 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
13677 * - If 'ON' is set during this process, which means at least one
13678 * interrupt is posted for this vCPU, we cannot block it, in
13679 * this case, return 1, otherwise, return 0.
13680 *
13681 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070013682static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013683{
Feng Wubf9f6ac2015-09-18 22:29:55 +080013684 unsigned int dest;
13685 struct pi_desc old, new;
13686 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
13687
13688 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013689 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13690 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080013691 return 0;
13692
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013693 WARN_ON(irqs_disabled());
13694 local_irq_disable();
13695 if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
13696 vcpu->pre_pcpu = vcpu->cpu;
13697 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13698 list_add_tail(&vcpu->blocked_vcpu_list,
13699 &per_cpu(blocked_vcpu_on_cpu,
13700 vcpu->pre_pcpu));
13701 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
13702 }
Feng Wubf9f6ac2015-09-18 22:29:55 +080013703
13704 do {
13705 old.control = new.control = pi_desc->control;
13706
Feng Wubf9f6ac2015-09-18 22:29:55 +080013707 WARN((pi_desc->sn == 1),
13708 "Warning: SN field of posted-interrupts "
13709 "is set before blocking\n");
13710
13711 /*
13712 * Since vCPU can be preempted during this process,
13713 * vcpu->cpu could be different with pre_pcpu, we
13714 * need to set pre_pcpu as the destination of wakeup
13715 * notification event, then we can find the right vCPU
13716 * to wakeup in wakeup handler if interrupts happen
13717 * when the vCPU is in blocked state.
13718 */
13719 dest = cpu_physical_id(vcpu->pre_pcpu);
13720
13721 if (x2apic_enabled())
13722 new.ndst = dest;
13723 else
13724 new.ndst = (dest << 8) & 0xFF00;
13725
13726 /* set 'NV' to 'wakeup vector' */
13727 new.nv = POSTED_INTR_WAKEUP_VECTOR;
Paolo Bonzinic0a16662017-09-28 17:58:41 +020013728 } while (cmpxchg64(&pi_desc->control, old.control,
13729 new.control) != old.control);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013730
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013731 /* We should not block the vCPU if an interrupt is posted for it. */
13732 if (pi_test_on(pi_desc) == 1)
13733 __pi_post_block(vcpu);
13734
13735 local_irq_enable();
13736 return (vcpu->pre_pcpu == -1);
Feng Wubf9f6ac2015-09-18 22:29:55 +080013737}
13738
Yunhong Jiangbc225122016-06-13 14:19:58 -070013739static int vmx_pre_block(struct kvm_vcpu *vcpu)
13740{
13741 if (pi_pre_block(vcpu))
13742 return 1;
13743
Yunhong Jiang64672c92016-06-13 14:19:59 -070013744 if (kvm_lapic_hv_timer_in_use(vcpu))
13745 kvm_lapic_switch_to_sw_timer(vcpu);
13746
Yunhong Jiangbc225122016-06-13 14:19:58 -070013747 return 0;
13748}
13749
13750static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013751{
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013752 if (vcpu->pre_pcpu == -1)
Feng Wubf9f6ac2015-09-18 22:29:55 +080013753 return;
13754
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013755 WARN_ON(irqs_disabled());
13756 local_irq_disable();
Paolo Bonzinicd39e112017-06-06 12:57:04 +020013757 __pi_post_block(vcpu);
Paolo Bonzini8b306e22017-06-06 12:57:05 +020013758 local_irq_enable();
Feng Wubf9f6ac2015-09-18 22:29:55 +080013759}
13760
Yunhong Jiangbc225122016-06-13 14:19:58 -070013761static void vmx_post_block(struct kvm_vcpu *vcpu)
13762{
Yunhong Jiang64672c92016-06-13 14:19:59 -070013763 if (kvm_x86_ops->set_hv_timer)
13764 kvm_lapic_switch_to_hv_timer(vcpu);
13765
Yunhong Jiangbc225122016-06-13 14:19:58 -070013766 pi_post_block(vcpu);
13767}
13768
Feng Wubf9f6ac2015-09-18 22:29:55 +080013769/*
Feng Wuefc64402015-09-18 22:29:51 +080013770 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
13771 *
13772 * @kvm: kvm
13773 * @host_irq: host irq of the interrupt
13774 * @guest_irq: gsi of the interrupt
13775 * @set: set or unset PI
13776 * returns 0 on success, < 0 on failure
13777 */
13778static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
13779 uint32_t guest_irq, bool set)
13780{
13781 struct kvm_kernel_irq_routing_entry *e;
13782 struct kvm_irq_routing_table *irq_rt;
13783 struct kvm_lapic_irq irq;
13784 struct kvm_vcpu *vcpu;
13785 struct vcpu_data vcpu_info;
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013786 int idx, ret = 0;
Feng Wuefc64402015-09-18 22:29:51 +080013787
13788 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080013789 !irq_remapping_cap(IRQ_POSTING_CAP) ||
13790 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080013791 return 0;
13792
13793 idx = srcu_read_lock(&kvm->irq_srcu);
13794 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
Jan H. Schönherr3a8b0672017-09-07 19:02:30 +010013795 if (guest_irq >= irq_rt->nr_rt_entries ||
13796 hlist_empty(&irq_rt->map[guest_irq])) {
13797 pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
13798 guest_irq, irq_rt->nr_rt_entries);
13799 goto out;
13800 }
Feng Wuefc64402015-09-18 22:29:51 +080013801
13802 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
13803 if (e->type != KVM_IRQ_ROUTING_MSI)
13804 continue;
13805 /*
13806 * VT-d PI cannot support posting multicast/broadcast
13807 * interrupts to a vCPU, we still use interrupt remapping
13808 * for these kind of interrupts.
13809 *
13810 * For lowest-priority interrupts, we only support
13811 * those with single CPU as the destination, e.g. user
13812 * configures the interrupts via /proc/irq or uses
13813 * irqbalance to make the interrupts single-CPU.
13814 *
13815 * We will support full lowest-priority interrupt later.
13816 */
13817
Radim Krčmář371313132016-07-12 22:09:27 +020013818 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080013819 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
13820 /*
13821 * Make sure the IRTE is in remapped mode if
13822 * we don't handle it in posted mode.
13823 */
13824 ret = irq_set_vcpu_affinity(host_irq, NULL);
13825 if (ret < 0) {
13826 printk(KERN_INFO
13827 "failed to back to remapped mode, irq: %u\n",
13828 host_irq);
13829 goto out;
13830 }
13831
Feng Wuefc64402015-09-18 22:29:51 +080013832 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080013833 }
Feng Wuefc64402015-09-18 22:29:51 +080013834
13835 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
13836 vcpu_info.vector = irq.vector;
13837
hu huajun2698d822018-04-11 15:16:40 +080013838 trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080013839 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
13840
13841 if (set)
13842 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
Haozhong Zhangdc91f2e2017-09-18 09:56:49 +080013843 else
Feng Wuefc64402015-09-18 22:29:51 +080013844 ret = irq_set_vcpu_affinity(host_irq, NULL);
Feng Wuefc64402015-09-18 22:29:51 +080013845
13846 if (ret < 0) {
13847 printk(KERN_INFO "%s: failed to update PI IRTE\n",
13848 __func__);
13849 goto out;
13850 }
13851 }
13852
13853 ret = 0;
13854out:
13855 srcu_read_unlock(&kvm->irq_srcu, idx);
13856 return ret;
13857}
13858
Ashok Rajc45dcc72016-06-22 14:59:56 +080013859static void vmx_setup_mce(struct kvm_vcpu *vcpu)
13860{
13861 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
13862 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
13863 FEATURE_CONTROL_LMCE;
13864 else
13865 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
13866 ~FEATURE_CONTROL_LMCE;
13867}
13868
Ladi Prosek72d7b372017-10-11 16:54:41 +020013869static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
13870{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013871 /* we need a nested vmexit to enter SMM, postpone if run is pending */
13872 if (to_vmx(vcpu)->nested.nested_run_pending)
13873 return 0;
Ladi Prosek72d7b372017-10-11 16:54:41 +020013874 return 1;
13875}
13876
Ladi Prosek0234bf82017-10-11 16:54:40 +020013877static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
13878{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013879 struct vcpu_vmx *vmx = to_vmx(vcpu);
13880
13881 vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
13882 if (vmx->nested.smm.guest_mode)
13883 nested_vmx_vmexit(vcpu, -1, 0, 0);
13884
13885 vmx->nested.smm.vmxon = vmx->nested.vmxon;
13886 vmx->nested.vmxon = false;
Wanpeng Licaa057a2018-03-12 04:53:03 -070013887 vmx_clear_hlt(vcpu);
Ladi Prosek0234bf82017-10-11 16:54:40 +020013888 return 0;
13889}
13890
13891static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase)
13892{
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013893 struct vcpu_vmx *vmx = to_vmx(vcpu);
13894 int ret;
13895
13896 if (vmx->nested.smm.vmxon) {
13897 vmx->nested.vmxon = true;
13898 vmx->nested.smm.vmxon = false;
13899 }
13900
13901 if (vmx->nested.smm.guest_mode) {
13902 vcpu->arch.hflags &= ~HF_SMM_MASK;
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020013903 ret = enter_vmx_non_root_mode(vcpu, NULL);
Ladi Prosek72e9cbd2017-10-11 16:54:43 +020013904 vcpu->arch.hflags |= HF_SMM_MASK;
13905 if (ret)
13906 return ret;
13907
13908 vmx->nested.smm.guest_mode = false;
13909 }
Ladi Prosek0234bf82017-10-11 16:54:40 +020013910 return 0;
13911}
13912
Ladi Prosekcc3d9672017-10-17 16:02:39 +020013913static int enable_smi_window(struct kvm_vcpu *vcpu)
13914{
13915 return 0;
13916}
13917
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013918static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
13919 struct kvm_nested_state __user *user_kvm_nested_state,
13920 u32 user_data_size)
13921{
13922 struct vcpu_vmx *vmx;
13923 struct vmcs12 *vmcs12;
13924 struct kvm_nested_state kvm_state = {
13925 .flags = 0,
13926 .format = 0,
13927 .size = sizeof(kvm_state),
13928 .vmx.vmxon_pa = -1ull,
13929 .vmx.vmcs_pa = -1ull,
13930 };
13931
13932 if (!vcpu)
13933 return kvm_state.size + 2 * VMCS12_SIZE;
13934
13935 vmx = to_vmx(vcpu);
13936 vmcs12 = get_vmcs12(vcpu);
13937 if (nested_vmx_allowed(vcpu) &&
13938 (vmx->nested.vmxon || vmx->nested.smm.vmxon)) {
13939 kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr;
13940 kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr;
13941
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013942 if (vmx->nested.current_vmptr != -1ull) {
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013943 kvm_state.size += VMCS12_SIZE;
13944
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013945 if (is_guest_mode(vcpu) &&
13946 nested_cpu_has_shadow_vmcs(vmcs12) &&
13947 vmcs12->vmcs_link_pointer != -1ull)
13948 kvm_state.size += VMCS12_SIZE;
13949 }
13950
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013951 if (vmx->nested.smm.vmxon)
13952 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON;
13953
13954 if (vmx->nested.smm.guest_mode)
13955 kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE;
13956
13957 if (is_guest_mode(vcpu)) {
13958 kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE;
13959
13960 if (vmx->nested.nested_run_pending)
13961 kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING;
13962 }
13963 }
13964
13965 if (user_data_size < kvm_state.size)
13966 goto out;
13967
13968 if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state)))
13969 return -EFAULT;
13970
13971 if (vmx->nested.current_vmptr == -1ull)
13972 goto out;
13973
13974 /*
13975 * When running L2, the authoritative vmcs12 state is in the
13976 * vmcs02. When running L1, the authoritative vmcs12 state is
13977 * in the shadow vmcs linked to vmcs01, unless
13978 * sync_shadow_vmcs is set, in which case, the authoritative
13979 * vmcs12 state is in the vmcs12 already.
13980 */
13981 if (is_guest_mode(vcpu))
13982 sync_vmcs12(vcpu, vmcs12);
13983 else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs)
13984 copy_shadow_to_vmcs12(vmx);
13985
13986 if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12)))
13987 return -EFAULT;
13988
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020013989 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
13990 vmcs12->vmcs_link_pointer != -1ull) {
13991 if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE,
13992 get_shadow_vmcs12(vcpu), sizeof(*vmcs12)))
13993 return -EFAULT;
13994 }
13995
Jim Mattson8fcc4b52018-07-10 11:27:20 +020013996out:
13997 return kvm_state.size;
13998}
13999
14000static int vmx_set_nested_state(struct kvm_vcpu *vcpu,
14001 struct kvm_nested_state __user *user_kvm_nested_state,
14002 struct kvm_nested_state *kvm_state)
14003{
14004 struct vcpu_vmx *vmx = to_vmx(vcpu);
14005 struct vmcs12 *vmcs12;
14006 u32 exit_qual;
14007 int ret;
14008
14009 if (kvm_state->format != 0)
14010 return -EINVAL;
14011
14012 if (!nested_vmx_allowed(vcpu))
14013 return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL;
14014
14015 if (kvm_state->vmx.vmxon_pa == -1ull) {
14016 if (kvm_state->vmx.smm.flags)
14017 return -EINVAL;
14018
14019 if (kvm_state->vmx.vmcs_pa != -1ull)
14020 return -EINVAL;
14021
14022 vmx_leave_nested(vcpu);
14023 return 0;
14024 }
14025
14026 if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa))
14027 return -EINVAL;
14028
14029 if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12))
14030 return -EINVAL;
14031
14032 if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa ||
14033 !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa))
14034 return -EINVAL;
14035
14036 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14037 (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14038 return -EINVAL;
14039
14040 if (kvm_state->vmx.smm.flags &
14041 ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON))
14042 return -EINVAL;
14043
Paolo Bonzini5bea5122018-09-18 15:19:17 +020014044 /*
14045 * SMM temporarily disables VMX, so we cannot be in guest mode,
14046 * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags
14047 * must be zero.
14048 */
14049 if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags)
14050 return -EINVAL;
14051
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014052 if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) &&
14053 !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON))
14054 return -EINVAL;
14055
14056 vmx_leave_nested(vcpu);
14057 if (kvm_state->vmx.vmxon_pa == -1ull)
14058 return 0;
14059
14060 vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa;
14061 ret = enter_vmx_operation(vcpu);
14062 if (ret)
14063 return ret;
14064
14065 set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa);
14066
14067 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) {
14068 vmx->nested.smm.vmxon = true;
14069 vmx->nested.vmxon = false;
14070
14071 if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE)
14072 vmx->nested.smm.guest_mode = true;
14073 }
14074
14075 vmcs12 = get_vmcs12(vcpu);
14076 if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12)))
14077 return -EFAULT;
14078
Liran Alon392b2f22018-06-23 02:35:01 +030014079 if (vmcs12->hdr.revision_id != VMCS12_REVISION)
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014080 return -EINVAL;
14081
14082 if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE))
14083 return 0;
14084
14085 vmx->nested.nested_run_pending =
14086 !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING);
14087
Paolo Bonzinifa58a9f2018-07-18 19:45:51 +020014088 if (nested_cpu_has_shadow_vmcs(vmcs12) &&
14089 vmcs12->vmcs_link_pointer != -1ull) {
14090 struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu);
14091 if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12))
14092 return -EINVAL;
14093
14094 if (copy_from_user(shadow_vmcs12,
14095 user_kvm_nested_state->data + VMCS12_SIZE,
14096 sizeof(*vmcs12)))
14097 return -EFAULT;
14098
14099 if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION ||
14100 !shadow_vmcs12->hdr.shadow_vmcs)
14101 return -EINVAL;
14102 }
14103
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014104 if (check_vmentry_prereqs(vcpu, vmcs12) ||
14105 check_vmentry_postreqs(vcpu, vmcs12, &exit_qual))
14106 return -EINVAL;
14107
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014108 vmx->nested.dirty_vmcs12 = true;
14109 ret = enter_vmx_non_root_mode(vcpu, NULL);
14110 if (ret)
14111 return -EINVAL;
14112
14113 return 0;
14114}
14115
Kees Cook404f6aa2016-08-08 16:29:06 -070014116static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080014117 .cpu_has_kvm_support = cpu_has_kvm_support,
14118 .disabled_by_bios = vmx_disabled_by_bios,
14119 .hardware_setup = hardware_setup,
14120 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030014121 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014122 .hardware_enable = hardware_enable,
14123 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080014124 .cpu_has_accelerated_tpr = report_flexpriority,
Tom Lendackybc226f02018-05-10 22:06:39 +020014125 .has_emulated_msr = vmx_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014126
Wanpeng Lib31c1142018-03-12 04:53:04 -070014127 .vm_init = vmx_vm_init,
Sean Christopherson434a1e92018-03-20 12:17:18 -070014128 .vm_alloc = vmx_vm_alloc,
14129 .vm_free = vmx_vm_free,
Wanpeng Lib31c1142018-03-12 04:53:04 -070014130
Avi Kivity6aa8b732006-12-10 02:21:36 -080014131 .vcpu_create = vmx_create_vcpu,
14132 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030014133 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014134
Sean Christopherson6d6095b2018-07-23 12:32:44 -070014135 .prepare_guest_switch = vmx_prepare_switch_to_guest,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014136 .vcpu_load = vmx_vcpu_load,
14137 .vcpu_put = vmx_vcpu_put,
14138
Paolo Bonzinia96036b2015-11-10 11:55:36 +010014139 .update_bp_intercept = update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -060014140 .get_msr_feature = vmx_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014141 .get_msr = vmx_get_msr,
14142 .set_msr = vmx_set_msr,
14143 .get_segment_base = vmx_get_segment_base,
14144 .get_segment = vmx_get_segment,
14145 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020014146 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014147 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020014148 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020014149 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030014150 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014151 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014152 .set_cr3 = vmx_set_cr3,
14153 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014154 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014155 .get_idt = vmx_get_idt,
14156 .set_idt = vmx_set_idt,
14157 .get_gdt = vmx_get_gdt,
14158 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010014159 .get_dr6 = vmx_get_dr6,
14160 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030014161 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010014162 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030014163 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014164 .get_rflags = vmx_get_rflags,
14165 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080014166
Avi Kivity6aa8b732006-12-10 02:21:36 -080014167 .tlb_flush = vmx_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -070014168 .tlb_flush_gva = vmx_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014169
Avi Kivity6aa8b732006-12-10 02:21:36 -080014170 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020014171 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014172 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040014173 .set_interrupt_shadow = vmx_set_interrupt_shadow,
14174 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020014175 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030014176 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014177 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020014178 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030014179 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020014180 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014181 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010014182 .get_nmi_mask = vmx_get_nmi_mask,
14183 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014184 .enable_nmi_window = enable_nmi_window,
14185 .enable_irq_window = enable_irq_window,
14186 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -040014187 .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080014188 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030014189 .get_enable_apicv = vmx_get_enable_apicv,
14190 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014191 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010014192 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080014193 .hwapic_irr_update = vmx_hwapic_irr_update,
14194 .hwapic_isr_update = vmx_hwapic_isr_update,
Liran Alone6c67d82018-09-04 10:56:52 +030014195 .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
Yang Zhanga20ed542013-04-11 19:25:15 +080014196 .sync_pir_to_irr = vmx_sync_pir_to_irr,
14197 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030014198
Izik Eiduscbc94022007-10-25 00:29:55 +020014199 .set_tss_addr = vmx_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -070014200 .set_identity_map_addr = vmx_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080014201 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080014202 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030014203
Avi Kivity586f9602010-11-18 13:09:54 +020014204 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020014205
Sheng Yang17cc3932010-01-05 19:02:27 +080014206 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080014207
14208 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080014209
14210 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000014211 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020014212
14213 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080014214
14215 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014216
KarimAllah Ahmede79f2452018-04-14 05:10:52 +020014217 .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100014218 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020014219
14220 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020014221
14222 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080014223 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000014224 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080014225 .xsaves_supported = vmx_xsaves_supported,
Paolo Bonzini66336ca2016-07-12 10:36:41 +020014226 .umip_emulated = vmx_umip_emulated,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010014227
14228 .check_nested_events = vmx_check_nested_events,
Sean Christophersond264ee02018-08-27 15:21:12 -070014229 .request_immediate_exit = vmx_request_immediate_exit,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020014230
14231 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080014232
14233 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
14234 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
14235 .flush_log_dirty = vmx_flush_log_dirty,
14236 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Bandan Dasc5f983f2017-05-05 15:25:14 -040014237 .write_log_dirty = vmx_write_pml_buffer,
Wei Huang25462f72015-06-19 15:45:05 +020014238
Feng Wubf9f6ac2015-09-18 22:29:55 +080014239 .pre_block = vmx_pre_block,
14240 .post_block = vmx_post_block,
14241
Wei Huang25462f72015-06-19 15:45:05 +020014242 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080014243
14244 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070014245
14246#ifdef CONFIG_X86_64
14247 .set_hv_timer = vmx_set_hv_timer,
14248 .cancel_hv_timer = vmx_cancel_hv_timer,
14249#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080014250
14251 .setup_mce = vmx_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014252
Jim Mattson8fcc4b52018-07-10 11:27:20 +020014253 .get_nested_state = vmx_get_nested_state,
14254 .set_nested_state = vmx_set_nested_state,
Paolo Bonzini7f7f1ba2018-07-18 18:49:01 +020014255 .get_vmcs12_pages = nested_get_vmcs12_pages,
14256
Ladi Prosek72d7b372017-10-11 16:54:41 +020014257 .smi_allowed = vmx_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +020014258 .pre_enter_smm = vmx_pre_enter_smm,
14259 .pre_leave_smm = vmx_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +020014260 .enable_smi_window = enable_smi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -080014261};
14262
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014263static void vmx_cleanup_l1d_flush(void)
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014264{
14265 if (vmx_l1d_flush_pages) {
14266 free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
14267 vmx_l1d_flush_pages = NULL;
14268 }
Thomas Gleixner72c6d2d2018-07-13 16:23:16 +020014269 /* Restore state so sysfs ignores VMX */
14270 l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
Konrad Rzeszutek Wilka3994772018-07-02 12:29:30 +020014271}
14272
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014273static void vmx_exit(void)
14274{
14275#ifdef CONFIG_KEXEC_CORE
14276 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
14277 synchronize_rcu();
14278#endif
14279
14280 kvm_exit();
14281
14282#if IS_ENABLED(CONFIG_HYPERV)
14283 if (static_branch_unlikely(&enable_evmcs)) {
14284 int cpu;
14285 struct hv_vp_assist_page *vp_ap;
14286 /*
14287 * Reset everything to support using non-enlightened VMCS
14288 * access later (e.g. when we reload the module with
14289 * enlightened_vmcs=0)
14290 */
14291 for_each_online_cpu(cpu) {
14292 vp_ap = hv_get_vp_assist_page(cpu);
14293
14294 if (!vp_ap)
14295 continue;
14296
14297 vp_ap->current_nested_vmcs = 0;
14298 vp_ap->enlighten_vmentry = 0;
14299 }
14300
14301 static_branch_disable(&enable_evmcs);
14302 }
14303#endif
14304 vmx_cleanup_l1d_flush();
14305}
14306module_exit(vmx_exit);
14307
Avi Kivity6aa8b732006-12-10 02:21:36 -080014308static int __init vmx_init(void)
14309{
Vitaly Kuznetsov773e8a02018-03-20 15:02:11 +010014310 int r;
14311
14312#if IS_ENABLED(CONFIG_HYPERV)
14313 /*
14314 * Enlightened VMCS usage should be recommended and the host needs
14315 * to support eVMCS v1 or above. We can also disable eVMCS support
14316 * with module parameter.
14317 */
14318 if (enlightened_vmcs &&
14319 ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
14320 (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
14321 KVM_EVMCS_VERSION) {
14322 int cpu;
14323
14324 /* Check that we have assist pages on all online CPUs */
14325 for_each_online_cpu(cpu) {
14326 if (!hv_get_vp_assist_page(cpu)) {
14327 enlightened_vmcs = false;
14328 break;
14329 }
14330 }
14331
14332 if (enlightened_vmcs) {
14333 pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
14334 static_branch_enable(&enable_evmcs);
14335 }
14336 } else {
14337 enlightened_vmcs = false;
14338 }
14339#endif
14340
14341 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014342 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030014343 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080014344 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080014345
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014346 /*
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014347 * Must be called after kvm_init() so enable_ept is properly set
14348 * up. Hand the parameter mitigation value in which was stored in
14349 * the pre module init parser. If no parameter was given, it will
14350 * contain 'auto' which will be turned into the default 'cond'
14351 * mitigation mode.
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014352 */
Thomas Gleixner7db92e12018-07-13 16:23:19 +020014353 if (boot_cpu_has(X86_BUG_L1TF)) {
14354 r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
14355 if (r) {
14356 vmx_exit();
14357 return r;
14358 }
Paolo Bonzinia47dd5f2018-07-02 12:47:38 +020014359 }
14360
Dave Young2965faa2015-09-09 15:38:55 -070014361#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014362 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
14363 crash_vmclear_local_loaded_vmcss);
14364#endif
Jim Mattson21ebf532018-05-01 15:40:28 -070014365 vmx_check_vmcs12_offsets();
Zhang Yanfei8f536b72012-12-06 23:43:34 +080014366
He, Qingfdef3ad2007-04-30 09:45:24 +030014367 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080014368}
Thomas Gleixnera7b90202018-07-13 16:23:18 +020014369module_init(vmx_init);